Advertisement
tylkas

Zabbix 2 latest data perfomance patch

Dec 4th, 2013
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 4.22 KB | None | 0 0
  1. diff --git a/frontends/php/api/classes/managers/CHistoryManager.php b/frontends/php/api/classes/managers/CHistoryManager.php
  2. index 84a8952..a5c5788 100644
  3. --- a/frontends/php/api/classes/managers/CHistoryManager.php
  4. +++ b/frontends/php/api/classes/managers/CHistoryManager.php
  5. @@ -35,14 +35,27 @@ class CHistoryManager {
  6.     public function getLast(array $items, $limit = 1) {
  7.         $queries = array();
  8.         foreach ($items as $item) {
  9. -           $table = self::getTableName($item['value_type']);
  10. -           $queries[$table][] = DBaddLimit(
  11. -               'SELECT *'.
  12. -               ' FROM '.$table.' h'.
  13. -               ' WHERE h.itemid='.zbx_dbstr($item['itemid']).
  14. -               ' ORDER BY h.clock DESC',
  15. -               $limit
  16. -           );
  17. +           $table = self::getTableNameForPartitioning($item['value_type']);
  18. +
  19. +           if (isset($item['delay'])) {
  20. +               $period = time() - ( $item['delay'] * LATEST_DATA_MULTIPLIER );
  21. +               $queries[$table][] = DBaddLimit(
  22. +                   'SELECT *'.
  23. +                   ' FROM '.$table.' h'.
  24. +                   ' WHERE h.itemid='.zbx_dbstr($item['itemid']).
  25. +                   ' AND h.clock > '.$period.
  26. +                   ' ORDER BY h.clock DESC',
  27. +                   $limit
  28. +               );
  29. +           } else {
  30. +                                $queries[$table][] = DBaddLimit(
  31. +                                        'SELECT *'.
  32. +                                        ' FROM '.$table.' h'.
  33. +                                        ' WHERE h.itemid='.zbx_dbstr($item['itemid']).
  34. +                                        ' ORDER BY h.clock DESC',
  35. +                                        $limit
  36. +               );
  37. +           }
  38.         }
  39.  
  40.         $rs = array();
  41. @@ -75,4 +88,33 @@ class CHistoryManager {
  42.         return $tables[$valueType];
  43.     }
  44.  
  45. +   /**
  46. +   * Return the name of the partitioned table where the data for the given value type is stored.
  47. +   *
  48. +   * @param int $valueType
  49. +   *
  50. +   * @return string
  51. +   */
  52. +   private static function getTableNameForPartitioning($valueType) {
  53. +       // Is this table define in partitioned tables?
  54. +       if (strripos( PARTITION_TABLES_DAILY, '"'.self::getTableName($valueType).'"' )) {
  55. +           // Is partitions schema and partition dateformat defined?
  56. +           if (PARTITION_DATEFORMAT > "" and PARTITION_SCHEMA > "") {
  57. +
  58. +               // If tables parted with unix timestamp (first second for each day)
  59. +               if (PARTITION_DATEFORMAT == 'U') {
  60. +                                   $nowdt= mktime( 0,0,0,date( "m",time() ),date( "d",time() ),date("y",time() ) );
  61. +                   return PARTITION_SCHEMA.'.'.self::getTableName($valueType).PARTITION_DATE_PREFIX.date(PARTITION_DATEFORMAT, $nowdt);
  62. +
  63. +                           // For partitioning by current date (current day, current month, etc.)
  64. +                           } else {
  65. +                   return $table_part = PARTITION_SCHEMA.'.'.self::getTableName($valueType).PARTITION_DATE_PREFIX.date(PARTITION_DATEFORMAT);
  66. +               }
  67. +           }
  68. +       }
  69. +
  70. +       // Default: return normal table name
  71. +       return self::getTableName($valueType);
  72. +   }
  73. +
  74.  }
  75. diff --git a/frontends/php/include/defines.inc.php b/frontends/php/include/defines.inc.php
  76. index 3d851a7..78569ef 100644
  77. --- a/frontends/php/include/defines.inc.php
  78. +++ b/frontends/php/include/defines.inc.php
  79. @@ -991,3 +991,14 @@ ini_set('precision', 14);
  80.  if (function_exists('bcscale')) {
  81.     bcscale(7);
  82.  }
  83. +
  84. +// partitions.history_uint_p2013_01_02
  85. +// |          |           | \ PARTITION_DATEFORMAT
  86. +// |          |           \ PARTITION_DATE_PREFIX
  87. +// |          \ normal table name
  88. +// \ PARTITION_SCHEMA
  89. +define('PARTITION_SCHEMA', 'partitions');
  90. +define('PARTITION_TABLES_DAILY', '"history" "history_log" "history_str" "history_text" "history_uint"');
  91. +define('PARTITION_DATE_PREFIX', '_p');
  92. +define('PARTITION_DATEFORMAT', 'Y_m_d');
  93. +define('LATEST_DATA_MULTIPLIER', 3);
  94. diff --git a/frontends/php/latest.php b/frontends/php/latest.php
  95. index ec2f68b..5f5565e 100644
  96. --- a/frontends/php/latest.php
  97. +++ b/frontends/php/latest.php
  98. @@ -258,7 +258,7 @@ CArrayHelper::sort($applications, $sortFields);
  99.  
  100.  // fetch items and data
  101.  $allItems = DBfetchArray(DBselect(
  102. -   'SELECT DISTINCT i.hostid,i.itemid,i.name,i.value_type,i.units,i.state,i.valuemapid,i.key_,ia.applicationid'.
  103. +   'SELECT DISTINCT i.hostid,i.itemid,i.name,i.value_type,i.units,i.state,i.valuemapid,i.key_,ia.applicationid,i.delay'.
  104.     ' FROM items i '.
  105.         ' LEFT JOIN items_applications ia ON ia.itemid=i.itemid'.
  106.     ' WHERE i.status='.ITEM_STATUS_ACTIVE.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement