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'];
         } else {
             if ($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'];
         } else {
             if ($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'];
         if ($type == GRAPH_ITEM_AGGREGATED) {
             // skip current period
             $from_time = $this->from_time - $this->period * $this->items[$i]['periods_cnt'];
             $to_time = $this->from_time;
         } else {
             $from_time = $this->from_time;
             $to_time = $this->to_time;
         }
         $calc_field = 'round(' . $x . '*(mod(' . zbx_dbcast_2bigint('clock') . '+' . $z . ',' . $p . '))/(' . $p . '),0)';
         /* required for 'group by' support of Oracle */
         $sql_arr = array();
         // [ZBX-3249] for partitioned DB installs!
         if (ZBX_HISTORY_DATA_UPKEEP > -1) {
             $real_item['history'] = ZBX_HISTORY_DATA_UPKEEP;
         }
         //---
         if ($real_item['history'] * 86400 > time() - ($this->from_time + $this->period / 2) && $this->period / $this->sizeX <= ZBX_MAX_TREND_DIFF / ZBX_GRAPH_MAX_SKIP_CELL) {
             $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'], 3600);
         }
         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'] = zbx_avg($curr_data['avg']);
         // 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;
             }
         }
     }
     /* end calculation of stacked graphs */
 }
Example #2
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'];
         }
Example #3
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;
 }
Example #4
0
 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;
     $config = select_config();
     for ($i = 0; $i < $this->num; $i++) {
         $item = get_item_by_itemid($this->items[$i]['itemid']);
         if ($this->itemsHost === null) {
             $this->itemsHost = $item['hostid'];
         } elseif ($this->itemsHost != $item['hostid']) {
             $this->itemsHost = false;
         }
         if (!isset($this->axis_valuetype[$this->items[$i]['axisside']])) {
             $this->axis_valuetype[$this->items[$i]['axisside']] = $item['value_type'];
         } elseif ($this->axis_valuetype[$this->items[$i]['axisside']] != $item['value_type']) {
             $this->axis_valuetype[$this->items[$i]['axisside']] = ITEM_VALUE_TYPE_FLOAT;
         }
         $calc_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();
         // override item history setting with housekeeping settings
         if ($config['hk_history_global']) {
             $item['history'] = $config['hk_history'];
         }
         $trendsEnabled = $config['hk_trends_global'] ? $config['hk_trends'] > 0 : $item['trends'] > 0;
         if (!$trendsEnabled || $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) {
             $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=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND clock>=' . zbx_dbstr($from_time) . ' AND clock<=' . zbx_dbstr($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=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND clock>=' . zbx_dbstr($from_time) . ' AND clock<=' . zbx_dbstr($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>=' . zbx_dbstr($from_time) . ' AND clock<=' . zbx_dbstr($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=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND clock>=' . zbx_dbstr($from_time) . ' AND clock<=' . zbx_dbstr($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();
         }
         $curr_data =& $this->data[$this->items[$i]['itemid']][$calc_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];
                 }
             }
         }
     }
     // calculate 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;
             }
         }
     }
 }
Example #5
0
 /**
  * Get actions data
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['actionids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  *
  * @return array|int item data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = array('select' => array('actions' => 'a.actionid'), 'from' => array('actions' => 'actions a'), 'where' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'actionids' => null, 'triggerids' => null, 'mediatypeids' => null, 'usrgrpids' => null, 'userids' => null, 'scriptids' => null, 'nopermissions' => null, 'editable' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectConditions' => null, 'selectOperations' => null, 'countOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         // conditions are checked here by sql, operations after, by api queries
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
         $userGroups = getUserGroupsByUserId($userid);
         // condition hostgroup
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc' . ' LEFT JOIN rights r' . ' ON r.id=' . zbx_dbcast_2bigint('cc.value') . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND cc.conditiontype=' . CONDITION_TYPE_HOST_GROUP . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)=' . PERM_DENY . ' OR MAX(r.permission)<' . $permission . ')';
         // condition host or template
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND ' . zbx_dbcast_2bigint('cc.value') . '=hgg.hostid' . ' AND cc.conditiontype IN (' . CONDITION_TYPE_HOST . ',' . CONDITION_TYPE_TEMPLATE . ')' . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)=' . PERM_DENY . ' OR MAX(r.permission)<' . $permission . ')';
         // condition trigger
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc,functions f,items i,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND ' . zbx_dbcast_2bigint('cc.value') . '=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' AND cc.conditiontype=' . CONDITION_TYPE_TRIGGER . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)=' . PERM_DENY . ' OR MAX(r.permission)<' . $permission . ')';
     }
     // actionids
     if (!is_null($options['actionids'])) {
         zbx_value2array($options['actionids']);
         $sqlParts['select']['actionid'] = 'a.actionid';
         $sqlParts['where'][] = dbConditionInt('a.actionid', $options['actionids']);
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         $sqlParts['select']['groupids'] = 'cg.value';
         $sqlParts['from']['conditions_groups'] = 'conditions cg';
         $sqlParts['where'][] = dbConditionString('cg.value', $options['groupids']);
         $sqlParts['where']['ctg'] = 'cg.conditiontype=' . CONDITION_TYPE_HOST_GROUP;
         $sqlParts['where']['acg'] = 'a.actionid=cg.actionid';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sqlParts['select']['hostids'] = 'ch.value';
         $sqlParts['from']['conditions_hosts'] = 'conditions ch';
         $sqlParts['where'][] = dbConditionString('ch.value', $options['hostids']);
         $sqlParts['where']['cth'] = 'ch.conditiontype=' . CONDITION_TYPE_HOST;
         $sqlParts['where']['ach'] = 'a.actionid=ch.actionid';
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sqlParts['select']['triggerids'] = 'ct.value';
         $sqlParts['from']['conditions_triggers'] = 'conditions ct';
         $sqlParts['where'][] = dbConditionString('ct.value', $options['triggerids']);
         $sqlParts['where']['ctt'] = 'ct.conditiontype=' . CONDITION_TYPE_TRIGGER;
         $sqlParts['where']['act'] = 'a.actionid=ct.actionid';
     }
     // mediatypeids
     if (!is_null($options['mediatypeids'])) {
         zbx_value2array($options['mediatypeids']);
         $sqlParts['select']['mediatypeid'] = 'om.mediatypeid';
         $sqlParts['from']['opmessage'] = 'opmessage om';
         $sqlParts['from']['operations_media'] = 'operations omed';
         $sqlParts['where'][] = dbConditionInt('om.mediatypeid', $options['mediatypeids']);
         $sqlParts['where']['aomed'] = 'a.actionid=omed.actionid';
         $sqlParts['where']['oom'] = 'omed.operationid=om.operationid';
     }
     // operation messages
     // usrgrpids
     if (!is_null($options['usrgrpids'])) {
         zbx_value2array($options['usrgrpids']);
         $sqlParts['select']['usrgrpid'] = 'omg.usrgrpid';
         $sqlParts['from']['opmessage_grp'] = 'opmessage_grp omg';
         $sqlParts['from']['operations_usergroups'] = 'operations oug';
         $sqlParts['where'][] = dbConditionInt('omg.usrgrpid', $options['usrgrpids']);
         $sqlParts['where']['aoug'] = 'a.actionid=oug.actionid';
         $sqlParts['where']['oomg'] = 'oug.operationid=omg.operationid';
     }
     // userids
     if (!is_null($options['userids'])) {
         zbx_value2array($options['userids']);
         $sqlParts['select']['userid'] = 'omu.userid';
         $sqlParts['from']['opmessage_usr'] = '******';
         $sqlParts['from']['operations_users'] = 'operations ou';
         $sqlParts['where'][] = dbConditionInt('omu.userid', $options['userids']);
         $sqlParts['where']['aou'] = 'a.actionid=ou.actionid';
         $sqlParts['where']['oomu'] = 'ou.operationid=omu.operationid';
     }
     // operation commands
     // scriptids
     if (!is_null($options['scriptids'])) {
         zbx_value2array($options['scriptids']);
         $sqlParts['select']['scriptid'] = 'oc.scriptid';
         $sqlParts['from']['opcommand'] = 'opcommand oc';
         $sqlParts['from']['operations_scripts'] = 'operations os';
         $sqlParts['where'][] = '(' . dbConditionInt('oc.scriptid', $options['scriptids']) . ' AND oc.type=' . ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT . ')';
         $sqlParts['where']['aos'] = 'a.actionid=os.actionid';
         $sqlParts['where']['ooc'] = 'os.operationid=oc.operationid';
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('actions a', $options, $sqlParts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('actions a', $options, $sqlParts);
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $actionids = array();
     $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($action = DBfetch($dbRes)) {
         if ($options['countOutput']) {
             $result = $action['rowscount'];
         } else {
             $actionids[$action['actionid']] = $action['actionid'];
             if (!isset($result[$action['actionid']])) {
                 $result[$action['actionid']] = array();
             }
             $result[$action['actionid']] += $action;
             // return mediatype as array
             if (!empty($action['mediatypeid'])) {
                 $result[$action['actionid']]['mediatypeids'][] = $action['mediatypeid'];
             }
             unset($result[$action['actionid']]['mediatypeid']);
         }
     }
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         // check hosts, templates
         $hosts = $hostids = array();
         $sql = 'SELECT o.actionid,och.hostid' . ' FROM operations o,opcommand_hst och' . ' WHERE o.operationid=och.operationid' . ' AND och.hostid<>0' . ' AND ' . dbConditionInt('o.actionid', $actionids);
         $dbHosts = DBselect($sql);
         while ($host = DBfetch($dbHosts)) {
             if (!isset($hosts[$host['hostid']])) {
                 $hosts[$host['hostid']] = array();
             }
             $hosts[$host['hostid']][$host['actionid']] = $host['actionid'];
             $hostids[$host['hostid']] = $host['hostid'];
         }
         $dbTemplates = DBselect('SELECT o.actionid,ot.templateid' . ' FROM operations o,optemplate ot' . ' WHERE o.operationid=ot.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($template = DBfetch($dbTemplates)) {
             if (!isset($hosts[$template['templateid']])) {
                 $hosts[$template['templateid']] = array();
             }
             $hosts[$template['templateid']][$template['actionid']] = $template['actionid'];
             $hostids[$template['templateid']] = $template['templateid'];
         }
         $allowedHosts = API::Host()->get(array('hostids' => $hostids, 'output' => array('hostid'), 'editable' => $options['editable'], 'templated_hosts' => true, 'preservekeys' => true));
         foreach ($hostids as $hostid) {
             if (isset($allowedHosts[$hostid])) {
                 continue;
             }
             foreach ($hosts[$hostid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedHosts);
         // check hostgroups
         $groups = $groupids = array();
         $dbGroups = DBselect('SELECT o.actionid,ocg.groupid' . ' FROM operations o,opcommand_grp ocg' . ' WHERE o.operationid=ocg.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($group = DBfetch($dbGroups)) {
             if (!isset($groups[$group['groupid']])) {
                 $groups[$group['groupid']] = array();
             }
             $groups[$group['groupid']][$group['actionid']] = $group['actionid'];
             $groupids[$group['groupid']] = $group['groupid'];
         }
         $dbGroups = DBselect('SELECT o.actionid,og.groupid' . ' FROM operations o,opgroup og' . ' WHERE o.operationid=og.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($group = DBfetch($dbGroups)) {
             if (!isset($groups[$group['groupid']])) {
                 $groups[$group['groupid']] = array();
             }
             $groups[$group['groupid']][$group['actionid']] = $group['actionid'];
             $groupids[$group['groupid']] = $group['groupid'];
         }
         $allowedGroups = API::HostGroup()->get(array('groupids' => $groupids, 'output' => array('groupid'), 'editable' => $options['editable'], 'preservekeys' => true));
         foreach ($groupids as $groupid) {
             if (isset($allowedGroups[$groupid])) {
                 continue;
             }
             foreach ($groups[$groupid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedGroups);
         // check scripts
         $scripts = $scriptids = array();
         $dbScripts = DBselect('SELECT o.actionid,oc.scriptid' . ' FROM operations o,opcommand oc' . ' WHERE o.operationid=oc.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids) . ' AND oc.type=' . ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT);
         while ($script = DBfetch($dbScripts)) {
             if (!isset($scripts[$script['scriptid']])) {
                 $scripts[$script['scriptid']] = array();
             }
             $scripts[$script['scriptid']][$script['actionid']] = $script['actionid'];
             $scriptids[$script['scriptid']] = $script['scriptid'];
         }
         $allowedScripts = API::Script()->get(array('scriptids' => $scriptids, 'output' => array('scriptid'), 'preservekeys' => true));
         foreach ($scriptids as $scriptid) {
             if (isset($allowedScripts[$scriptid])) {
                 continue;
             }
             foreach ($scripts[$scriptid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedScripts);
         // check users
         $users = $userids = array();
         $dbUsers = DBselect('SELECT o.actionid,omu.userid' . ' FROM operations o,opmessage_usr omu' . ' WHERE o.operationid=omu.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($user = DBfetch($dbUsers)) {
             if (!isset($users[$user['userid']])) {
                 $users[$user['userid']] = array();
             }
             $users[$user['userid']][$user['actionid']] = $user['actionid'];
             $userids[$user['userid']] = $user['userid'];
         }
         $allowedUsers = API::User()->get(array('userids' => $userids, 'output' => array('userid'), 'preservekeys' => true));
         foreach ($userids as $userid) {
             if (isset($allowedUsers[$userid])) {
                 continue;
             }
             foreach ($users[$userid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         // check usergroups
         $usrgrps = $usrgrpids = array();
         $dbUsergroups = DBselect('SELECT o.actionid,omg.usrgrpid' . ' FROM operations o,opmessage_grp omg' . ' WHERE o.operationid=omg.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($usrgrp = DBfetch($dbUsergroups)) {
             if (!isset($usrgrps[$usrgrp['usrgrpid']])) {
                 $usrgrps[$usrgrp['usrgrpid']] = array();
             }
             $usrgrps[$usrgrp['usrgrpid']][$usrgrp['actionid']] = $usrgrp['actionid'];
             $usrgrpids[$usrgrp['usrgrpid']] = $usrgrp['usrgrpid'];
         }
         $allowedUsergrps = API::UserGroup()->get(array('usrgrpids' => $usrgrpids, 'output' => array('usrgrpid'), 'preservekeys' => true));
         foreach ($usrgrpids as $usrgrpid) {
             if (isset($allowedUsergrps[$usrgrpid])) {
                 continue;
             }
             foreach ($usrgrps[$usrgrpid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
 /**
  * Get Actions data
  *
  * @param _array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['actionids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('actionid', 'name');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('actions' => 'a.actionid'), 'from' => array('actions' => 'actions a'), 'where' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'actionids' => null, 'triggerids' => null, 'mediatypeids' => null, 'userids' => null, 'nopermissions' => null, 'editable' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'extendoutput' => null, 'output' => API_OUTPUT_REFER, 'select_conditions' => null, 'select_operations' => null, 'countOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_conditions'])) {
             $options['select_conditions'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_operations'])) {
             $options['select_operations'] = API_OUTPUT_EXTEND;
         }
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || !is_null($options['nopermissions'])) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['from']['conditions'] = 'conditions c';
         $sql_parts['where']['ac'] = 'a.actionid=c.actionid';
         // condition hostgroup
         $sql_parts['where'][] = ' NOT EXISTS(' . ' SELECT cc.conditionid' . ' FROM conditions cc' . ' WHERE cc.conditiontype=' . CONDITION_TYPE_HOST_GROUP . ' AND cc.actionid=c.actionid' . ' AND (' . ' NOT EXISTS(' . ' SELECT rr.id' . ' FROM rights rr, users_groups ug' . ' WHERE rr.id=' . zbx_dbcast_2bigint('cc.value') . ' AND rr.groupid=ug.usrgrpid' . ' AND ug.userid=' . $userid . ' AND rr.permission>=' . $permission . ' )' . ' OR EXISTS(' . ' SELECT rr.id' . ' FROM rights rr, users_groups ugg' . ' WHERE rr.id=' . zbx_dbcast_2bigint('cc.value') . ' AND rr.groupid=ugg.usrgrpid' . ' AND ugg.userid=' . $userid . ' AND rr.permission<' . $permission . ' )' . ' )' . ' )';
         // condition host or template
         $sql_parts['where'][] = ' NOT EXISTS(' . ' SELECT cc.conditionid' . ' FROM conditions cc' . ' WHERE (cc.conditiontype=' . CONDITION_TYPE_HOST . ' OR cc.conditiontype=' . CONDITION_TYPE_HOST_TEMPLATE . ')' . ' AND cc.actionid=c.actionid' . ' AND (' . ' NOT EXISTS(' . ' SELECT hgg.hostid' . ' FROM hosts_groups hgg, rights r,users_groups ug' . ' WHERE hgg.hostid=' . zbx_dbcast_2bigint('cc.value') . ' AND r.id=hgg.groupid' . ' AND ug.userid=' . $userid . ' AND r.permission>=' . $permission . ' AND r.groupid=ug.usrgrpid)' . ' OR EXISTS(' . ' SELECT hgg.hostid' . ' FROM hosts_groups hgg, rights rr, users_groups gg' . ' WHERE hgg.hostid=' . zbx_dbcast_2bigint('cc.value') . ' AND rr.id=hgg.groupid' . ' AND rr.groupid=gg.usrgrpid' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ')' . ' )' . ' )';
         // condition trigger
         $sql_parts['where'][] = ' NOT EXISTS(' . ' SELECT cc.conditionid ' . ' FROM conditions cc ' . ' WHERE cc.conditiontype=' . CONDITION_TYPE_TRIGGER . ' AND cc.actionid=c.actionid' . ' AND (' . ' NOT EXISTS(' . ' SELECT f.triggerid' . ' FROM functions f, items i,hosts_groups hg, rights r, users_groups ug' . ' WHERE ug.userid=' . $userid . ' AND r.groupid=ug.usrgrpid' . ' AND r.permission>=' . $permission . ' AND hg.groupid=r.id' . ' AND i.hostid=hg.hostid' . ' AND f.itemid=i.itemid' . ' AND f.triggerid=' . zbx_dbcast_2bigint('cc.value') . ')' . ' OR EXISTS(' . ' SELECT ff.functionid' . ' FROM functions ff, items ii' . ' WHERE ff.triggerid=' . zbx_dbcast_2bigint('cc.value') . ' AND ii.itemid=ff.itemid' . ' AND EXISTS(' . ' SELECT hgg.groupid' . ' FROM hosts_groups hgg, rights rr, users_groups ugg' . ' WHERE hgg.hostid=ii.hostid' . ' AND rr.id=hgg.groupid' . ' AND rr.groupid=ugg.usrgrpid' . ' AND ugg.userid=' . $userid . ' AND rr.permission<' . $permission . '))' . ' )' . ' )';
         // condition users
         $sql_parts['where'][] = ' NOT EXISTS(' . ' SELECT o.operationid ' . ' FROM operations o ' . ' WHERE o.operationtype=' . OPERATION_TYPE_MESSAGE . ' AND o.actionid=a.actionid' . ' AND ((' . ' o.object=' . OPERATION_OBJECT_USER . ' AND o.objectid NOT IN (' . ' SELECT DISTINCT ug.userid' . ' FROM users_groups ug' . ' WHERE ug.usrgrpid IN (' . ' SELECT uug.usrgrpid' . ' FROM users_groups uug' . ' WHERE uug.userid=' . $USER_DETAILS['userid'] . ' )' . ' )' . ' ) OR (' . ' o.object=' . OPERATION_OBJECT_GROUP . ' AND o.objectid NOT IN (' . ' SELECT ug.usrgrpid' . ' FROM users_groups ug' . ' WHERE ug.userid=' . $USER_DETAILS['userid'] . ' )' . ' )' . ' )' . ' )';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // actionids
     if (!is_null($options['actionids'])) {
         zbx_value2array($options['actionids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['actionid'] = 'a.actionid';
         }
         $sql_parts['where'][] = DBcondition('a.actionid', $options['actionids']);
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupids'] = 'c.value';
         }
         $sql_parts['from']['conditions'] = 'conditions c';
         $sql_parts['where'][] = DBcondition('c.value', $options['groupids'], false, true);
         $sql_parts['where']['c'] = 'c.conditiontype=' . CONDITION_TYPE_HOST_GROUP;
         $sql_parts['where']['ac'] = 'a.actionid=c.actionid';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostids'] = 'c.value';
         }
         $sql_parts['from']['conditions'] = 'conditions c';
         $sql_parts['where'][] = DBcondition('c.value', $options['hostids'], false, true);
         $sql_parts['where']['c'] = 'c.conditiontype=' . CONDITION_TYPE_HOST;
         $sql_parts['where']['ac'] = 'a.actionid=c.actionid';
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['triggerids'] = 'c.value';
         }
         $sql_parts['from']['conditions'] = 'conditions c';
         $sql_parts['where'][] = DBcondition('c.value', $options['triggerids'], false, true);
         $sql_parts['where']['c'] = 'c.conditiontype=' . CONDITION_TYPE_TRIGGER;
         $sql_parts['where']['ac'] = 'a.actionid=c.actionid';
     }
     // mediatypeids
     if (!is_null($options['mediatypeids'])) {
         zbx_value2array($options['mediatypeids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['mediatypeid'] = 'opm.mediatypeid';
         }
         $sql_parts['from']['opmediatypes'] = 'opmediatypes opm';
         $sql_parts['from']['operations'] = 'operations o';
         $sql_parts['where'][] = DBcondition('opm.mediatypeid', $options['mediatypeids']);
         $sql_parts['where']['oopm'] = 'opm.operationid=o.operationid';
         $sql_parts['where']['ao'] = 'a.actionid=o.actionid';
     }
     // userids
     if (!is_null($options['userids'])) {
         zbx_value2array($options['userids']);
         // TODO:
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('actions a', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('actions a', $options, $sql_parts);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['actions'] = 'a.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('COUNT(DISTINCT a.actionid) as rowscount');
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 'a.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('a.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('a.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 'a.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //---------------
     $actionids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('a.actionid', $nodeids) . $sql_where . $sql_order;
     //SDI($sql);
     $db_res = DBselect($sql, $sql_limit);
     while ($action = DBfetch($db_res)) {
         if ($options['countOutput']) {
             $result = $action['rowscount'];
         } else {
             $actionids[$action['actionid']] = $action['actionid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$action['actionid']] = array('actionid' => $action['actionid']);
             } else {
                 if (!isset($result[$action['actionid']])) {
                     $result[$action['actionid']] = array();
                 }
                 if (!is_null($options['select_conditions']) && !isset($result[$action['actionid']]['conditions'])) {
                     $result[$action['actionid']]['conditions'] = array();
                 }
                 if (!is_null($options['select_operations']) && !isset($result[$action['actionid']]['operations'])) {
                     $result[$action['actionid']]['operations'] = array();
                 }
                 $result[$action['actionid']] += $action;
             }
         }
     }
     COpt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding Conditions
     if (!is_null($options['select_conditions']) && str_in_array($options['select_conditions'], $subselects_allowed_outputs)) {
         $sql = 'SELECT c.* FROM conditions c WHERE ' . DBcondition('c.actionid', $actionids);
         $res = DBselect($sql);
         while ($condition = DBfetch($res)) {
             $result[$condition['actionid']]['conditions'][] = $condition;
         }
     }
     // Adding Operations
     if (!is_null($options['select_operations']) && str_in_array($options['select_operations'], $subselects_allowed_outputs)) {
         $operations = array();
         $operationids = array();
         $sql = 'SELECT o.* ' . ' FROM operations o ' . ' WHERE ' . DBcondition('o.actionid', $actionids);
         $res = DBselect($sql);
         while ($operation = DBfetch($res)) {
             $operation['opconditions'] = array();
             $operation['opmediatypes'] = array();
             $operations[$operation['operationid']] = $operation;
             $operationids[$operation['operationid']] = $operation['operationid'];
         }
         $sql = 'SELECT op.* FROM opconditions op WHERE ' . DBcondition('op.operationid', $operationids);
         $res = DBselect($sql);
         while ($opcondition = DBfetch($res)) {
             $operations[$opcondition['operationid']]['opconditions'][] = $opcondition;
         }
         $sql = 'SELECT op.* FROM opmediatypes op WHERE ' . DBcondition('op.operationid', $operationids);
         $res = DBselect($sql);
         while ($opmediatype = DBfetch($res)) {
             $operations[$opmediatype['operationid']]['opmediatypes'][] = $opmediatype;
         }
         foreach ($operations as $num => $operation) {
             $result[$operation['actionid']]['operations'][] = $operation;
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Example #7
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 . '*(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 $id => $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'];
         }
 /**
  * Get Actions data
  *
  * @param _array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['actionids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  * @return array|int item data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('actionid', 'name', 'status');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     $sqlParts = array('select' => array('actions' => 'a.actionid'), 'from' => array('actions' => 'actions a'), 'where' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'actionids' => null, 'triggerids' => null, 'mediatypeids' => null, 'usrgrpids' => null, 'userids' => null, 'scriptids' => null, 'nopermissions' => null, 'editable' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectConditions' => null, 'selectOperations' => null, 'countOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['actions']);
         $dbTable = DB::getSchema('actions');
         $sqlParts['select']['actionid'] = 'a.actionid';
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 'a.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         // conditions are checked here by sql, operations after, by api queries
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $userGroups = getUserGroupsByUserId($userid);
         // condition hostgroup
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc' . ' LEFT JOIN rights r' . ' ON r.id=' . zbx_dbcast_2bigint('cc.value') . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND cc.conditiontype=' . CONDITION_TYPE_HOST_GROUP . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)<' . $permission . ')';
         // condition host or template
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND ' . zbx_dbcast_2bigint('cc.value') . '=hgg.hostid' . ' AND cc.conditiontype IN (' . CONDITION_TYPE_HOST . ',' . CONDITION_TYPE_HOST_TEMPLATE . ')' . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)<' . $permission . ')';
         // condition trigger
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc,functions f,items i,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND ' . zbx_dbcast_2bigint('cc.value') . '=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' AND cc.conditiontype=' . CONDITION_TYPE_TRIGGER . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)<' . $permission . ')';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // actionids
     if (!is_null($options['actionids'])) {
         zbx_value2array($options['actionids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['actionid'] = 'a.actionid';
         }
         $sqlParts['where'][] = dbConditionInt('a.actionid', $options['actionids']);
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['groupids'] = 'cg.value';
         }
         $sqlParts['from']['conditions_groups'] = 'conditions cg';
         $sqlParts['where'][] = dbConditionString('cg.value', $options['groupids']);
         $sqlParts['where']['ctg'] = 'cg.conditiontype=' . CONDITION_TYPE_HOST_GROUP;
         $sqlParts['where']['acg'] = 'a.actionid=cg.actionid';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['hostids'] = 'ch.value';
         }
         $sqlParts['from']['conditions_hosts'] = 'conditions ch';
         $sqlParts['where'][] = dbConditionString('ch.value', $options['hostids']);
         $sqlParts['where']['cth'] = 'ch.conditiontype=' . CONDITION_TYPE_HOST;
         $sqlParts['where']['ach'] = 'a.actionid=ch.actionid';
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['triggerids'] = 'ct.value';
         }
         $sqlParts['from']['conditions_triggers'] = 'conditions ct';
         $sqlParts['where'][] = dbConditionString('ct.value', $options['triggerids']);
         $sqlParts['where']['ctt'] = 'ct.conditiontype=' . CONDITION_TYPE_TRIGGER;
         $sqlParts['where']['act'] = 'a.actionid=ct.actionid';
     }
     // mediatypeids
     if (!is_null($options['mediatypeids'])) {
         zbx_value2array($options['mediatypeids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['mediatypeid'] = 'om.mediatypeid';
         }
         $sqlParts['from']['opmessage'] = 'opmessage om';
         $sqlParts['from']['operations_media'] = 'operations omed';
         $sqlParts['where'][] = dbConditionInt('om.mediatypeid', $options['mediatypeids']);
         $sqlParts['where']['aomed'] = 'a.actionid=omed.actionid';
         $sqlParts['where']['oom'] = 'omed.operationid=om.operationid';
     }
     // operation messages
     // usrgrpids
     if (!is_null($options['usrgrpids'])) {
         zbx_value2array($options['usrgrpids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['usrgrpid'] = 'omg.usrgrpid';
         }
         $sqlParts['from']['opmessage_grp'] = 'opmessage_grp omg';
         $sqlParts['from']['operations_usergroups'] = 'operations oug';
         $sqlParts['where'][] = dbConditionInt('omg.usrgrpid', $options['usrgrpids']);
         $sqlParts['where']['aoug'] = 'a.actionid=oug.actionid';
         $sqlParts['where']['oomg'] = 'oug.operationid=omg.operationid';
     }
     // userids
     if (!is_null($options['userids'])) {
         zbx_value2array($options['userids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['userid'] = 'omu.userid';
         }
         $sqlParts['from']['opmessage_usr'] = '******';
         $sqlParts['from']['operations_users'] = 'operations ou';
         $sqlParts['where'][] = dbConditionInt('omu.userid', $options['userids']);
         $sqlParts['where']['aou'] = 'a.actionid=ou.actionid';
         $sqlParts['where']['oomu'] = 'ou.operationid=omu.operationid';
     }
     // operation commands
     // scriptids
     if (!is_null($options['scriptids'])) {
         zbx_value2array($options['scriptids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['scriptid'] = 'oc.scriptid';
         }
         $sqlParts['from']['opcommand'] = 'opcommand oc';
         $sqlParts['from']['operations_scripts'] = 'operations os';
         $sqlParts['where'][] = '(' . dbConditionInt('oc.scriptid', $options['scriptids']) . ' AND oc.type=' . ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT . ')';
         $sqlParts['where']['aos'] = 'a.actionid=os.actionid';
         $sqlParts['where']['ooc'] = 'os.operationid=oc.operationid';
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('actions a', $options, $sqlParts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('actions a', $options, $sqlParts);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['actions'] = 'a.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('COUNT(DISTINCT a.actionid) AS rowscount');
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'a');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $actionids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= ' AND ' . implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('a.actionid', $nodeids) . $sqlWhere . $sqlOrder;
     $dbRes = DBselect($sql, $sqlLimit);
     while ($action = DBfetch($dbRes)) {
         if ($options['countOutput']) {
             $result = $action['rowscount'];
         } else {
             $actionids[$action['actionid']] = $action['actionid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$action['actionid']] = array('actionid' => $action['actionid']);
             } else {
                 if (!isset($result[$action['actionid']])) {
                     $result[$action['actionid']] = array();
                 }
                 if (!is_null($options['selectConditions']) && !isset($result[$action['actionid']]['conditions'])) {
                     $result[$action['actionid']]['conditions'] = array();
                 }
                 if (!is_null($options['selectOperations']) && !isset($result[$action['actionid']]['operations'])) {
                     $result[$action['actionid']]['operations'] = array();
                 }
                 $result[$action['actionid']] += $action;
                 // return mediatype as array
                 if (!empty($action['mediatypeid'])) {
                     $result[$action['actionid']]['mediatypeids'][] = $action['mediatypeid'];
                 }
                 unset($result[$action['actionid']]['mediatypeid']);
             }
         }
     }
     if (USER_TYPE_SUPER_ADMIN == $userType || !is_null($options['nopermissions'])) {
     } else {
         // check hosts, templates
         $hosts = $hostids = array();
         $sql = 'SELECT o.actionid,och.hostid' . ' FROM operations o,opcommand_hst och' . ' WHERE o.operationid=och.operationid' . ' AND och.hostid<>0' . ' AND ' . dbConditionInt('o.actionid', $actionids);
         $dbHosts = DBselect($sql);
         while ($host = DBfetch($dbHosts)) {
             if (!isset($hosts[$host['hostid']])) {
                 $hosts[$host['hostid']] = array();
             }
             $hosts[$host['hostid']][$host['actionid']] = $host['actionid'];
             $hostids[$host['hostid']] = $host['hostid'];
         }
         $dbTemplates = DBselect('SELECT o.actionid,ot.templateid' . ' FROM operations o,optemplate ot' . ' WHERE o.operationid=ot.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($template = DBfetch($dbTemplates)) {
             if (!isset($hosts[$template['templateid']])) {
                 $hosts[$template['templateid']] = array();
             }
             $hosts[$template['templateid']][$template['actionid']] = $template['actionid'];
             $hostids[$template['templateid']] = $template['templateid'];
         }
         $allowedHosts = API::Host()->get(array('hostids' => $hostids, 'output' => API_OUTPUT_SHORTEN, 'editable' => $options['editable'], 'templated_hosts' => true, 'preservekeys' => true));
         foreach ($hostids as $hostid) {
             if (isset($allowedHosts[$hostid])) {
                 continue;
             }
             foreach ($hosts[$hostid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedHosts);
         // check hostgroups
         $groups = $groupids = array();
         $dbGroups = DBselect('SELECT o.actionid,ocg.groupid' . ' FROM operations o,opcommand_grp ocg' . ' WHERE o.operationid=ocg.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($group = DBfetch($dbGroups)) {
             if (!isset($groups[$group['groupid']])) {
                 $groups[$group['groupid']] = array();
             }
             $groups[$group['groupid']][$group['actionid']] = $group['actionid'];
             $groupids[$group['groupid']] = $group['groupid'];
         }
         $dbGroups = DBselect('SELECT o.actionid,og.groupid' . ' FROM operations o,opgroup og' . ' WHERE o.operationid=og.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($group = DBfetch($dbGroups)) {
             if (!isset($groups[$group['groupid']])) {
                 $groups[$group['groupid']] = array();
             }
             $groups[$group['groupid']][$group['actionid']] = $group['actionid'];
             $groupids[$group['groupid']] = $group['groupid'];
         }
         $allowedGroups = API::HostGroup()->get(array('groupids' => $groupids, 'output' => API_OUTPUT_SHORTEN, 'editable' => $options['editable'], 'preservekeys' => true));
         foreach ($groupids as $groupid) {
             if (isset($allowedGroups[$groupid])) {
                 continue;
             }
             foreach ($groups[$groupid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedGroups);
         // check scripts
         $scripts = $scriptids = array();
         $dbScripts = DBselect('SELECT o.actionid,oc.scriptid' . ' FROM operations o,opcommand oc' . ' WHERE o.operationid=oc.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids) . ' AND oc.type=' . ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT);
         while ($script = DBfetch($dbScripts)) {
             if (!isset($scripts[$script['scriptid']])) {
                 $scripts[$script['scriptid']] = array();
             }
             $scripts[$script['scriptid']][$script['actionid']] = $script['actionid'];
             $scriptids[$script['scriptid']] = $script['scriptid'];
         }
         $allowedScripts = API::Script()->get(array('scriptids' => $scriptids, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => true));
         foreach ($scriptids as $scriptid) {
             if (isset($allowedScripts[$scriptid])) {
                 continue;
             }
             foreach ($scripts[$scriptid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedScripts);
         // check users
         $users = $userids = array();
         $dbUsers = DBselect('SELECT o.actionid,omu.userid' . ' FROM operations o,opmessage_usr omu' . ' WHERE o.operationid=omu.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($user = DBfetch($dbUsers)) {
             if (!isset($users[$user['userid']])) {
                 $users[$user['userid']] = array();
             }
             $users[$user['userid']][$user['actionid']] = $user['actionid'];
             $userids[$user['userid']] = $user['userid'];
         }
         $allowedUsers = API::User()->get(array('userids' => $userids, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => true));
         foreach ($userids as $userid) {
             if (isset($allowedUsers[$userid])) {
                 continue;
             }
             foreach ($users[$userid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         // check usergroups
         $usrgrps = $usrgrpids = array();
         $dbUsergroups = DBselect('SELECT o.actionid,omg.usrgrpid' . ' FROM operations o,opmessage_grp omg' . ' WHERE o.operationid=omg.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($usrgrp = DBfetch($dbUsergroups)) {
             if (!isset($usrgrps[$usrgrp['usrgrpid']])) {
                 $usrgrps[$usrgrp['usrgrpid']] = array();
             }
             $usrgrps[$usrgrp['usrgrpid']][$usrgrp['actionid']] = $usrgrp['actionid'];
             $usrgrpids[$usrgrp['usrgrpid']] = $usrgrp['usrgrpid'];
         }
         $allowedUsergrps = API::UserGroup()->get(array('usrgrpids' => $usrgrpids, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => true));
         foreach ($usrgrpids as $usrgrpid) {
             if (isset($allowedUsergrps[$usrgrpid])) {
                 continue;
             }
             foreach ($usrgrps[$usrgrpid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     /*
      * Adding objects
      */
     // adding conditions
     if (!is_null($options['selectConditions']) && str_in_array($options['selectConditions'], $subselectsAllowedOutputs)) {
         $res = DBselect('SELECT c.* FROM conditions c WHERE ' . dbConditionInt('c.actionid', $actionids));
         while ($condition = DBfetch($res)) {
             $result[$condition['actionid']]['conditions'][$condition['conditionid']] = $condition;
         }
     }
     // adding operations
     if (!is_null($options['selectOperations']) && str_in_array($options['selectOperations'], $subselectsAllowedOutputs)) {
         $operations = array();
         $operationids = array();
         $res = DBselect('SELECT o.*' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.actionid', $actionids));
         while ($operation = DBfetch($res)) {
             $operation['opconditions'] = array();
             $operations[$operation['operationid']] = $operation;
             $operationids[$operation['operationid']] = $operation['operationid'];
         }
         $res = DBselect('SELECT op.* FROM opconditions op WHERE ' . dbConditionInt('op.operationid', $operationids));
         while ($opcondition = DBfetch($res)) {
             if (!isset($operations[$opcondition['operationid']]['opconditions'])) {
                 $operations[$opcondition['operationid']]['opconditions'] = array();
             }
             $operations[$opcondition['operationid']]['opconditions'][] = $opcondition;
         }
         $opmessage = $opcommand = $opgroup = $optemplate = array();
         foreach ($operations as $operationid => $operation) {
             switch ($operation['operationtype']) {
                 case OPERATION_TYPE_MESSAGE:
                     $opmessage[] = $operationid;
                     break;
                 case OPERATION_TYPE_COMMAND:
                     $opcommand[] = $operationid;
                     break;
                 case OPERATION_TYPE_GROUP_ADD:
                 case OPERATION_TYPE_GROUP_REMOVE:
                     $opgroup[] = $operationid;
                     break;
                 case OPERATION_TYPE_TEMPLATE_ADD:
                 case OPERATION_TYPE_TEMPLATE_REMOVE:
                     $optemplate[] = $operationid;
                     break;
                 case OPERATION_TYPE_HOST_ADD:
                 case OPERATION_TYPE_HOST_REMOVE:
                 case OPERATION_TYPE_HOST_ENABLE:
                 case OPERATION_TYPE_HOST_DISABLE:
             }
         }
         // get OPERATION_TYPE_MESSAGE data
         if (!empty($opmessage)) {
             $dbOpmessages = DBselect('SELECT o.operationid,o.default_msg,o.subject,o.message,o.mediatypeid' . ' FROM opmessage o' . ' WHERE ' . dbConditionInt('o.operationid', $opmessage));
             while ($dbOpmessage = DBfetch($dbOpmessages)) {
                 $operations[$dbOpmessage['operationid']]['opmessage_grp'] = array();
                 $operations[$dbOpmessage['operationid']]['opmessage_usr'] = array();
                 $operations[$dbOpmessage['operationid']]['opmessage'] = $dbOpmessage;
             }
             $dbOpmessageGrp = DBselect('SELECT og.operationid,og.usrgrpid' . ' FROM opmessage_grp og' . ' WHERE ' . dbConditionInt('og.operationid', $opmessage));
             while ($opmessageGrp = DBfetch($dbOpmessageGrp)) {
                 $operations[$opmessageGrp['operationid']]['opmessage_grp'][] = $opmessageGrp;
             }
             $dbOpmessageUsr = DBselect('SELECT ou.operationid,ou.userid' . ' FROM opmessage_usr ou' . ' WHERE ' . dbConditionInt('ou.operationid', $opmessage));
             while ($opmessageUsr = DBfetch($dbOpmessageUsr)) {
                 $operations[$opmessageUsr['operationid']]['opmessage_usr'][] = $opmessageUsr;
             }
         }
         // get OPERATION_TYPE_COMMAND data
         if (!empty($opcommand)) {
             $dbOpcommands = DBselect('SELECT o.*' . ' FROM opcommand o' . ' WHERE ' . dbConditionInt('o.operationid', $opcommand));
             while ($dbOpcommand = DBfetch($dbOpcommands)) {
                 $operations[$dbOpcommand['operationid']]['opcommand_grp'] = array();
                 $operations[$dbOpcommand['operationid']]['opcommand_hst'] = array();
                 $operations[$dbOpcommand['operationid']]['opcommand'] = $dbOpcommand;
             }
             $dbOpcommandHst = DBselect('SELECT oh.opcommand_hstid,oh.operationid,oh.hostid' . ' FROM opcommand_hst oh' . ' WHERE ' . dbConditionInt('oh.operationid', $opcommand));
             while ($opcommandHst = DBfetch($dbOpcommandHst)) {
                 $operations[$opcommandHst['operationid']]['opcommand_hst'][] = $opcommandHst;
             }
             $dbOpcommandGrp = DBselect('SELECT og.opcommand_grpid,og.operationid,og.groupid' . ' FROM opcommand_grp og' . ' WHERE ' . dbConditionInt('og.operationid', $opcommand));
             while ($opcommandGrp = DBfetch($dbOpcommandGrp)) {
                 $operations[$opcommandGrp['operationid']]['opcommand_grp'][] = $opcommandGrp;
             }
         }
         // get OPERATION_TYPE_GROUP_ADD, OPERATION_TYPE_GROUP_REMOVE data
         if (!empty($opgroup)) {
             $dbOpgroup = DBselect('SELECT o.operationid,o.groupid' . ' FROM opgroup o' . ' WHERE ' . dbConditionInt('o.operationid', $opgroup));
             while ($opgroup = DBfetch($dbOpgroup)) {
                 if (!isset($operations[$opgroup['operationid']]['opgroup'])) {
                     $operations[$opgroup['operationid']]['opgroup'] = array();
                 }
                 $operations[$opgroup['operationid']]['opgroup'][] = $opgroup;
             }
         }
         // get OPERATION_TYPE_TEMPLATE_ADD, OPERATION_TYPE_TEMPLATE_REMOVE data
         if (!empty($optemplate)) {
             $dbOptemplate = DBselect('SELECT o.operationid,o.templateid' . ' FROM optemplate o' . ' WHERE ' . dbConditionInt('o.operationid', $optemplate));
             while ($optemplate = DBfetch($dbOptemplate)) {
                 if (!isset($operations[$optemplate['operationid']]['optemplate'])) {
                     $operations[$optemplate['operationid']]['optemplate'] = array();
                 }
                 $operations[$optemplate['operationid']]['optemplate'][] = $optemplate;
             }
         }
         foreach ($operations as $operation) {
             $result[$operation['actionid']]['operations'][$operation['operationid']] = $operation;
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }