Exemple #1
0
 public function GetItemValues($itemInfo)
 {
     $stime = $itemInfo['stime'];
     $period = $itemInfo['period'];
     $sizeX = $itemInfo['sizeX'];
     $now = time(null);
     if (!isset($stime)) {
         $stime = $now - $period;
     }
     $diffTZ = date('Z', $stime) - date('Z', $stime + $period);
     $from_time = $stime;
     // + timeZone offset
     $to_time = $stime + $period;
     // + timeZone offset
     $p = $to_time - $from_time;
     // graph size in time
     $z = $p - $from_time % $p;
     // graphsize - mod(from_time,p) for Oracle...
     $x = $sizeX;
     // graph size in px
     $config = select_config();
     $real_item = get_item_by_itemid($itemInfo['itemid']);
     $type = $itemInfo['calc_type'];
     $calc_field = 'round(' . $x . '*' . zbx_sql_mod(zbx_dbcast_2bigint('clock') . '+' . $z, $p) . '/(' . $p . '),0)';
     // required for 'group by' support of Oracle
     $sql_arr = array();
     // override item history setting with housekeeping settings
     if ($config['hk_history_global']) {
         $real_item['history'] = $config['hk_history'];
     }
     $trendsEnabled = $config['hk_trends_global'] ? $config['hk_trends'] > 0 : $real_item['trends'] > 0;
     if (!$trendsEnabled || $item['history'] * SEC_PER_DAY > time() - ($from_time + $period / 2) && $period / $sizeX <= ZBX_MAX_TREND_DIFF / ZBX_GRAPH_MAX_SKIP_CELL) {
         // is reasonable to take data from history?
         //$this->dataFrom = 'history';
         array_push($sql_arr, 'SELECT itemid,' . $calc_field . ' AS i,' . 'COUNT(*) AS count,AVG(value) AS avg,MIN(value) as min,' . 'MAX(value) AS max,MAX(clock) AS clock' . ' FROM history ' . ' WHERE itemid=' . $itemInfo['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field, 'SELECT itemid,' . $calc_field . ' AS i,' . 'COUNT(*) AS count,AVG(value) AS avg,MIN(value) AS min,' . 'MAX(value) AS max,MAX(clock) AS clock' . ' FROM history_uint ' . ' WHERE itemid=' . $itemInfo['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field);
     } else {
         //$this->dataFrom = 'trends';
         array_push($sql_arr, 'SELECT itemid,' . $calc_field . ' AS i,' . 'SUM(num) AS count,AVG(value_avg) AS avg,MIN(value_min) AS min,' . 'MAX(value_max) AS max,MAX(clock) AS clock' . ' FROM trends' . ' WHERE itemid=' . $itemInfo['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field, 'SELECT itemid,' . $calc_field . ' AS i,' . 'SUM(num) AS count,AVG(value_avg) AS avg,MIN(value_min) AS min,' . 'MAX(value_max) AS max,MAX(clock) AS clock' . ' FROM trends_uint ' . ' WHERE itemid=' . $itemInfo['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field);
         $itemInfo['delay'] = max($itemInfo['delay'], SEC_PER_HOUR);
     }
     $curr_data['count'] = null;
     $curr_data['min'] = null;
     $curr_data['max'] = null;
     $curr_data['avg'] = null;
     $curr_data['clock'] = null;
     foreach ($sql_arr as $sql) {
         $result = DBselect($sql);
         while ($row = DBfetch($result)) {
             $idx = $row['i'] - 1;
             if ($idx < 0) {
                 continue;
             }
             $curr_data['count'][$idx] = $row['count'];
             $curr_data['min'][$idx] = $row['min'];
             $curr_data['max'][$idx] = $row['max'];
             $curr_data['avg'][$idx] = $row['avg'];
             $curr_data['clock'][$idx] = $row['clock'];
             $curr_data['shift_min'][$idx] = 0;
             $curr_data['shift_max'][$idx] = 0;
             $curr_data['shift_avg'][$idx] = 0;
         }
         unset($row);
     }
     $curr_data['avg_orig'] = is_array($curr_data['avg']) ? zbx_avg($curr_data['avg']) : null;
     return $curr_data;
 }
 protected function selectData()
 {
     $this->data = array();
     $now = time(null);
     if (!isset($this->stime)) {
         $this->stime = $now - $this->period;
     }
     $this->diffTZ = date('Z', $this->stime) - date('Z', $this->stime + $this->period);
     $this->from_time = $this->stime;
     // + timeZone offset
     $this->to_time = $this->stime + $this->period;
     // + timeZone offset
     $p = $this->to_time - $this->from_time;
     // graph size in time
     $z = $p - $this->from_time % $p;
     // graphsize - mod(from_time,p) for Oracle...
     $x = $this->sizeX;
     // graph size in px
     $this->itemsHost = null;
     for ($i = 0; $i < $this->num; $i++) {
         $real_item = get_item_by_itemid($this->items[$i]['itemid']);
         if (is_null($this->itemsHost)) {
             $this->itemsHost = $real_item['hostid'];
         } elseif ($this->itemsHost != $real_item['hostid']) {
             $this->itemsHost = false;
         }
         if (!isset($this->axis_valuetype[$this->items[$i]['axisside']])) {
             $this->axis_valuetype[$this->items[$i]['axisside']] = $real_item['value_type'];
         } elseif ($this->axis_valuetype[$this->items[$i]['axisside']] != $real_item['value_type']) {
             $this->axis_valuetype[$this->items[$i]['axisside']] = ITEM_VALUE_TYPE_FLOAT;
         }
         $type = $this->items[$i]['calc_type'];
         $from_time = $this->from_time;
         $to_time = $this->to_time;
         $calc_field = 'round(' . $x . '*' . zbx_sql_mod(zbx_dbcast_2bigint('clock') . '+' . $z, $p) . '/(' . $p . '),0)';
         // required for 'group by' support of Oracle
         $sql_arr = array();
         if (ZBX_HISTORY_DATA_UPKEEP > -1) {
             $real_item['history'] = ZBX_HISTORY_DATA_UPKEEP;
         }
         if ($real_item['history'] * SEC_PER_DAY > time() - ($this->from_time + $this->period / 2) && $this->period / $this->sizeX <= ZBX_MAX_TREND_DIFF / ZBX_GRAPH_MAX_SKIP_CELL) {
             // is reasonable to take data from history?
             $this->dataFrom = 'history';
             array_push($sql_arr, 'SELECT itemid,' . $calc_field . ' AS i,' . 'COUNT(*) AS count,AVG(value) AS avg,MIN(value) as min,' . 'MAX(value) AS max,MAX(clock) AS clock' . ' FROM history ' . ' WHERE itemid=' . $this->items[$i]['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field, 'SELECT itemid,' . $calc_field . ' AS i,' . 'COUNT(*) AS count,AVG(value) AS avg,MIN(value) AS min,' . 'MAX(value) AS max,MAX(clock) AS clock' . ' FROM history_uint ' . ' WHERE itemid=' . $this->items[$i]['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field);
         } else {
             $this->dataFrom = 'trends';
             array_push($sql_arr, 'SELECT itemid,' . $calc_field . ' AS i,' . 'SUM(num) AS count,AVG(value_avg) AS avg,MIN(value_min) AS min,' . 'MAX(value_max) AS max,MAX(clock) AS clock' . ' FROM trends' . ' WHERE itemid=' . $this->items[$i]['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field, 'SELECT itemid,' . $calc_field . ' AS i,' . 'SUM(num) AS count,AVG(value_avg) AS avg,MIN(value_min) AS min,' . 'MAX(value_max) AS max,MAX(clock) AS clock' . ' FROM trends_uint ' . ' WHERE itemid=' . $this->items[$i]['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field);
             $this->items[$i]['delay'] = max($this->items[$i]['delay'], SEC_PER_HOUR);
         }
         if (!isset($this->data[$this->items[$i]['itemid']])) {
             $this->data[$this->items[$i]['itemid']] = array();
         }
         if (!isset($this->data[$this->items[$i]['itemid']][$type])) {
             $this->data[$this->items[$i]['itemid']][$type] = array();
         }
         $curr_data =& $this->data[$this->items[$i]['itemid']][$type];
         $curr_data['count'] = null;
         $curr_data['min'] = null;
         $curr_data['max'] = null;
         $curr_data['avg'] = null;
         $curr_data['clock'] = null;
         foreach ($sql_arr as $sql) {
             $result = DBselect($sql);
             while ($row = DBfetch($result)) {
                 $idx = $row['i'] - 1;
                 if ($idx < 0) {
                     continue;
                 }
                 /* --------------------------------------------------
                 				We are taking graph on 1px more than we need,
                 				and here we are skiping first px, because of MOD (in SELECT),
                 				it combines prelast point (it would be last point if not that 1px in begining)
                 				and first point, but we still losing prelast point :(
                 				but now we've got the first point.
                 			--------------------------------------------------*/
                 $curr_data['count'][$idx] = $row['count'];
                 $curr_data['min'][$idx] = $row['min'];
                 $curr_data['max'][$idx] = $row['max'];
                 $curr_data['avg'][$idx] = $row['avg'];
                 $curr_data['clock'][$idx] = $row['clock'];
                 $curr_data['shift_min'][$idx] = 0;
                 $curr_data['shift_max'][$idx] = 0;
                 $curr_data['shift_avg'][$idx] = 0;
             }
             $loc_min = is_array($curr_data['min']) ? min($curr_data['min']) : null;
             $this->setGraphOrientation($loc_min, $this->items[$i]['axisside']);
             unset($row);
         }
         $curr_data['avg_orig'] = is_array($curr_data['avg']) ? zbx_avg($curr_data['avg']) : null;
         // calculate missed points
         $first_idx = 0;
         /*
         	first_idx - last existing point
         	ci - current index
         	cj - count of missed in one go
         	dx - offset to first value (count to last existing point)
         */
         for ($ci = 0, $cj = 0; $ci < $this->sizeX; $ci++) {
             if (!isset($curr_data['count'][$ci]) || $curr_data['count'][$ci] == 0) {
                 $curr_data['count'][$ci] = 0;
                 $curr_data['shift_min'][$ci] = 0;
                 $curr_data['shift_max'][$ci] = 0;
                 $curr_data['shift_avg'][$ci] = 0;
                 $cj++;
                 continue;
             }
             if ($cj == 0) {
                 continue;
             }
             $dx = $cj + 1;
             $first_idx = $ci - $dx;
             if ($first_idx < 0) {
                 $first_idx = $ci;
                 // if no data from start of graph get current data as first data
             }
             for (; $cj > 0; $cj--) {
                 if ($dx < $this->sizeX / 20 && $this->type == GRAPH_TYPE_STACKED) {
                     $curr_data['count'][$ci - ($dx - $cj)] = 1;
                 }
                 foreach (array('clock', 'min', 'max', 'avg') as $var_name) {
                     $var =& $curr_data[$var_name];
                     if ($first_idx == $ci && $var_name == 'clock') {
                         $var[$ci - ($dx - $cj)] = $var[$first_idx] - $p / $this->sizeX * ($dx - $cj);
                         continue;
                     }
                     $dy = $var[$ci] - $var[$first_idx];
                     $var[$ci - ($dx - $cj)] = bcadd($var[$first_idx], bcdiv($cj * $dy, $dx));
                 }
             }
         }
         if ($cj > 0 && $ci > $cj) {
             $dx = $cj + 1;
             $first_idx = $ci - $dx;
             for (; $cj > 0; $cj--) {
                 foreach (array('clock', 'min', 'max', 'avg') as $var_name) {
                     $var =& $curr_data[$var_name];
                     if ($var_name == 'clock') {
                         $var[$first_idx + ($dx - $cj)] = $var[$first_idx] + $p / $this->sizeX * ($dx - $cj);
                         continue;
                     }
                     $var[$first_idx + ($dx - $cj)] = $var[$first_idx];
                 }
             }
         }
         // end of missed points calculation
     }
     // calculte shift for stacked graphs
     if ($this->type == GRAPH_TYPE_STACKED) {
         for ($i = 1; $i < $this->num; $i++) {
             $curr_data =& $this->data[$this->items[$i]['itemid']][$this->items[$i]['calc_type']];
             if (!isset($curr_data)) {
                 continue;
             }
             for ($j = $i - 1; $j >= 0; $j--) {
                 if ($this->items[$j]['axisside'] != $this->items[$i]['axisside']) {
                     continue;
                 }
                 $prev_data =& $this->data[$this->items[$j]['itemid']][$this->items[$j]['calc_type']];
                 if (!isset($prev_data)) {
                     continue;
                 }
                 for ($ci = 0; $ci < $this->sizeX; $ci++) {
                     foreach (array('min', 'max', 'avg') as $var_name) {
                         $shift_var_name = 'shift_' . $var_name;
                         $curr_shift =& $curr_data[$shift_var_name];
                         $curr_var =& $curr_data[$var_name];
                         $prev_shift =& $prev_data[$shift_var_name];
                         $prev_var =& $prev_data[$var_name];
                         $curr_shift[$ci] = $prev_var[$ci] + $prev_shift[$ci];
                     }
                 }
                 break;
             }
         }
     }
     $this->newData = $this->data;
     // end calculation of stacked graphs
 }
Exemple #3
0
         break;
     case TIMEPERIOD_TYPE_YEARLY:
         $start = $end;
         $str_start['year'] = date('Y', $start);
         $str = $str_start['year'] . '-01-01 00:00:00';
         $end = strtotime($str);
         $end = strtotime('+1 year', $end);
         break;
 }
 $p = $end - $start;
 // graph size in time
 $z = $p - $start % $p;
 // graphsize - mod(from_time,p) for Oracle...
 $x = floor($scaleperiod / $period);
 // graph size in px
 $calc_field = 'round(' . $x . '*' . zbx_sql_mod(zbx_dbcast_2bigint('clock') . '+' . $z, $p) . '/(' . $p . '),0)';
 // required for 'group by' support of Oracle
 $item_data = null;
 $sql_arr = array();
 array_push($sql_arr, 'SELECT itemid,' . $calc_field . ' as i,sum(num) as count,avg(value_avg) as avg ' . ' FROM trends ' . ' WHERE itemid=' . $itemid . ' AND clock>=' . $start . ' AND clock<=' . $end . ' GROUP BY itemid,' . $calc_field, 'SELECT itemid,' . $calc_field . ' as i,sum(num) as count,avg(value_avg) as avg ' . ' FROM trends_uint ' . ' WHERE itemid=' . $itemid . ' AND clock>=' . $start . ' AND clock<=' . $end . ' GROUP BY itemid,' . $calc_field);
 foreach ($sql_arr as $sql) {
     $result = DBselect($sql);
     while ($row = DBfetch($result)) {
         if ($row['i'] == $x) {
             continue;
         }
         if (!is_null($item_data)) {
             $item_data = ($item_data + $row['avg']) / 2;
         } else {
             $item_data = $row['avg'];
         }