public function addItem($value, $caption = '', $selected = null, $enabled = 'yes') { if ($value instanceof CComboItem || $value instanceof COptGroup) { parent::addItem($value); } else { $title = false; // if caption is too long ( > 44 symbols), we add new class - 'selectShorten', // so that the select box would not stretch if (zbx_strlen($caption) > 44 && !$this->hasClass('selectShorten')) { $this->setAttribute('class', $this->getAttribute('class') . ' selectShorten'); $title = true; } if (is_null($selected)) { $selected = 'no'; if (is_array($this->value)) { if (str_in_array($value, $this->value)) { $selected = 'yes'; } } elseif (strcmp($value, $this->value) == 0) { $selected = 'yes'; } } else { $selected = 'yes'; } $citem = new CComboItem($value, $caption, $selected, $enabled); if ($title) { $citem->setTitle($caption); } parent::addItem($citem); } }
public function addItem($value, $caption = '', $selected = null, $enabled = true, $class = null) { if ($value instanceof CComboItem || $value instanceof COptGroup) { parent::addItem($value); } else { if (is_null($selected)) { $selected = 'no'; if (is_array($this->value)) { if (str_in_array($value, $this->value)) { $selected = 'yes'; } } elseif (strcmp($value, $this->value) == 0) { $selected = 'yes'; } } else { $selected = 'yes'; } $citem = new CComboItem($value, $caption, $selected, $enabled); if ($class !== null) { $citem->addClass($class); } parent::addItem($citem); } return $this; }
public function addItem($value, $caption = '', $selected = null, $enabled = 'yes', $class = null) { if (is_null($selected)) { if (is_array($this->value)) { if (str_in_array($value, $this->value)) { $this->value_exist = 1; } } elseif (strcmp($value, $this->value) == 0) { $this->value_exist = 1; } } parent::addItem($value, $caption, $selected, $enabled, $class); }
public function addItem($value, $caption, $selected = null, $enabled = 'yes') { if (is_null($selected)) { if (is_array($this->value)) { if (str_in_array($value, $this->value)) { $selected = 1; } } else { if (strcmp($value, $this->value) == 0) { $selected = 1; } } } if (is_bool($selected) && $selected || is_int($selected) && $selected != 0 || is_string($selected) && ($selected == 'yes' || $selected == 'selected' || $selected == 'on')) { $this->lbox->addItem($value, $caption, null, $enabled); $this->form->addVar($this->varname . '[' . $value . ']', $value); } else { $this->rbox->addItem($value, $caption, null, $enabled); } }
public function addItem($value, $caption = '', $selected = NULL, $enabled = 'yes') { // if($enabled=='no') return; /* disable item method 1 */ if (strtolower(get_class($value)) == 'ccomboitem') { parent::addItem($value); } else { if (is_null($selected)) { $selected = 'no'; if (is_array($this->value)) { if (str_in_array($value, $this->value)) { $selected = 'yes'; } } else { if (strcmp($value, $this->value) == 0) { $selected = 'yes'; } } } parent::addItem(new CComboItem($value, $caption, $selected, $enabled)); } }
public function addItem($value, $caption = '', $selected = NULL, $enabled = 'yes') { // if($enabled=='no') return; /* disable item method 1 */ if (is_object($value) && zbx_strtolower(get_class($value)) == 'ccomboitem') { parent::addItem($value); } else { if (zbx_strlen($caption) > 44) { $this->setAttribute('class', 'select selectShorten'); } if (is_null($selected)) { $selected = 'no'; if (is_array($this->value)) { if (str_in_array($value, $this->value)) { $selected = 'yes'; } } else { if (strcmp($value, $this->value) == 0) { $selected = 'yes'; } } } parent::addItem(new CComboItem($value, $caption, $selected, $enabled)); } }
/** * Get Service data * * @param _array $options * @param array $options['nodeids'] Node IDs * @param array $options['groupids'] ServiceGroup IDs * @param array $options['hostids'] Service IDs * @param boolean $options['monitored_hosts'] only monitored Services * @param boolean $options['templated_hosts'] include templates in result * @param boolean $options['with_items'] only with items * @param boolean $options['with_historical_items'] only with historical items * @param boolean $options['with_triggers'] only with triggers * @param boolean $options['with_httptests'] only with http tests * @param boolean $options['with_graphs'] only with graphs * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins * @param boolean $options['selectGroups'] select ServiceGroups * @param boolean $options['selectTemplates'] select Templates * @param boolean $options['selectItems'] select Items * @param boolean $options['selectTriggers'] select Triggers * @param boolean $options['selectGraphs'] select Graphs * @param boolean $options['selectApplications'] select Applications * @param boolean $options['selectMacros'] select Macros * @param int $options['count'] count Services, returned column name is rowscount * @param string $options['pattern'] search hosts by pattern in Service name * @param string $options['extendPattern'] search hosts by pattern in Service name, ip and DNS * @param int $options['limit'] limit selection * @param string $options['sortfield'] field to sort by * @param string $options['sortorder'] sort order * @return array|boolean Service data as array or false if error */ public function get($options = array()) { $result = array(); $nodeCheck = false; $userType = self::$userData['type']; // allowed columns for sorting $sortColumns = array('dserviceid', 'dhostid', 'ip'); // allowed output options for [ select_* ] params $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM); $sqlParts = array('select' => array('dservices' => 'ds.dserviceid'), 'from' => array('dservices' => 'dservices ds'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'dserviceids' => null, 'dhostids' => null, 'dcheckids' => null, 'druleids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectDRules' => null, 'selectDHosts' => null, 'selectDChecks' => null, 'selectHosts' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null); $options = zbx_array_merge($defOptions, $options); if (is_array($options['output'])) { unset($sqlParts['select']['dservices']); $dbTable = DB::getSchema('dservices'); foreach ($options['output'] as $field) { if (isset($dbTable['fields'][$field])) { $sqlParts['select'][$field] = 's.' . $field; } } $options['output'] = API_OUTPUT_CUSTOM; } // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $userType) { } elseif (is_null($options['editable']) && self::$userData['type'] == USER_TYPE_ZABBIX_ADMIN) { } elseif (!is_null($options['editable']) && self::$userData['type'] != USER_TYPE_SUPER_ADMIN) { return array(); } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // dserviceids if (!is_null($options['dserviceids'])) { zbx_value2array($options['dserviceids']); $sqlParts['where']['dserviceid'] = dbConditionInt('ds.dserviceid', $options['dserviceids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'][] = DBin_node('ds.dserviceid', $nodeids); } } // dhostids if (!is_null($options['dhostids'])) { zbx_value2array($options['dhostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['dhostid'] = 'ds.dhostid'; } $sqlParts['where'][] = dbConditionInt('ds.dhostid', $options['dhostids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['dhostid'] = 'ds.dhostid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'][] = DBin_node('ds.dhostid', $nodeids); } } // dcheckids if (!is_null($options['dcheckids'])) { zbx_value2array($options['dcheckids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['dcheckid'] = 'dc.dcheckid'; } $sqlParts['from']['dhosts'] = 'dhosts dh'; $sqlParts['from']['dchecks'] = 'dchecks dc'; $sqlParts['where'][] = dbConditionInt('dc.dcheckid', $options['dcheckids']); $sqlParts['where']['dhds'] = 'dh.hostid=ds.hostid'; $sqlParts['where']['dcdh'] = 'dc.druleid=dh.druleid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['dcheckid'] = 'dc.dcheckid'; } } // druleids if (!is_null($options['druleids'])) { zbx_value2array($options['druleids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['druleid'] = 'dh.druleid'; } $sqlParts['from']['dhosts'] = 'dhosts dh'; $sqlParts['where']['druleid'] = dbConditionInt('dh.druleid', $options['druleids']); $sqlParts['where']['dhds'] = 'dh.dhostid=ds.dhostid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['druleid'] = 'dh.druleid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'][] = DBin_node('dh.druleid', $nodeids); } } // node check !!!!! // should last, after all ****IDS checks if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'][] = DBin_node('ds.dserviceid', $nodeids); } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sqlParts['select']['dservices'] = 'ds.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sqlParts['select'] = array('count(DISTINCT ds.dserviceid) as rowscount'); //groupCount if (!is_null($options['groupCount'])) { foreach ($sqlParts['group'] as $key => $fields) { $sqlParts['select'][$key] = $fields; } } } // filter if (is_array($options['filter'])) { $this->dbFilter('dservices ds', $options, $sqlParts); } // search if (is_array($options['search'])) { zbx_db_search('dservices ds', $options, $sqlParts); } // sorting zbx_db_sorting($sqlParts, $options, $sortColumns, 'ds'); // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sqlParts['limit'] = $options['limit']; } //------- $dserviceids = array(); $sqlParts['select'] = array_unique($sqlParts['select']); $sqlParts['from'] = array_unique($sqlParts['from']); $sqlParts['where'] = array_unique($sqlParts['where']); $sqlParts['group'] = array_unique($sqlParts['group']); $sqlParts['order'] = array_unique($sqlParts['order']); $sqlSelect = ''; $sqlFrom = ''; $sqlWhere = ''; $sqlGroup = ''; $sqlOrder = ''; if (!empty($sqlParts['select'])) { $sqlSelect .= implode(',', $sqlParts['select']); } if (!empty($sqlParts['from'])) { $sqlFrom .= implode(',', $sqlParts['from']); } if (!empty($sqlParts['where'])) { $sqlWhere .= implode(' AND ', $sqlParts['where']); } if (!empty($sqlParts['group'])) { $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']); } if (!empty($sqlParts['order'])) { $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']); } $sqlLimit = $sqlParts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere . $sqlGroup . $sqlOrder; //SDI($sql); $res = DBselect($sql, $sqlLimit); while ($dservice = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $dservice; } else { $result = $dservice['rowscount']; } } else { $dserviceids[$dservice['dserviceid']] = $dservice['dserviceid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$dservice['dserviceid']] = array('dserviceid' => $dservice['dserviceid']); } else { if (!isset($result[$dservice['dserviceid']])) { $result[$dservice['dserviceid']] = array(); } if (!is_null($options['selectDRules']) && !isset($result[$dservice['dserviceid']]['drules'])) { $result[$dservice['dserviceid']]['drules'] = array(); } if (!is_null($options['selectDHosts']) && !isset($result[$dservice['dserviceid']]['dhosts'])) { $result[$dservice['dserviceid']]['dhosts'] = array(); } if (!is_null($options['selectDChecks']) && !isset($result[$dservice['dserviceid']]['dchecks'])) { $result[$dservice['dserviceid']]['dchecks'] = array(); } if (!is_null($options['selectHosts']) && !isset($result[$dservice['dserviceid']]['hosts'])) { $result[$dservice['dserviceid']]['hosts'] = array(); } // druleids if (isset($dservice['druleid']) && is_null($options['selectDRules'])) { if (!isset($result[$dservice['dserviceid']]['drules'])) { $result[$dservice['dserviceid']]['drules'] = array(); } $result[$dservice['dserviceid']]['drules'][] = array('druleid' => $dservice['druleid']); } // dhostids if (isset($dservice['dhostid']) && is_null($options['selectDHosts'])) { if (!isset($result[$dservice['dserviceid']]['dhosts'])) { $result[$dservice['dserviceid']]['dhosts'] = array(); } $result[$dservice['dserviceid']]['dhosts'][] = array('dhostid' => $dservice['dhostid']); } // dcheckids if (isset($dservice['dcheckid']) && is_null($options['selectDChecks'])) { if (!isset($result[$dservice['dserviceid']]['dchecks'])) { $result[$dservice['dserviceid']]['dchecks'] = array(); } $result[$dservice['dserviceid']]['dchecks'][] = array('dcheckid' => $dservice['dcheckid']); } $result[$dservice['dserviceid']] += $dservice; } } } if (!is_null($options['countOutput'])) { return $result; } // Adding Objects // select_drules if (!is_null($options['selectDRules'])) { $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, 'preservekeys' => 1); if (is_array($options['selectDRules']) || str_in_array($options['selectDRules'], $subselectsAllowedOutputs)) { $objParams['output'] = $options['selectDRules']; $drules = API::DRule()->get($objParams); if (!is_null($options['limitSelects'])) { order_result($drules, 'name'); } foreach ($drules as $druleid => $drule) { unset($drules[$druleid]['dservices']); $count = array(); foreach ($drule['dservices'] as $dnum => $dservice) { if (!is_null($options['limitSelects'])) { if (!isset($count[$dservice['dserviceid']])) { $count[$dservice['dserviceid']] = 0; } $count[$dservice['dserviceid']]++; if ($count[$dservice['dserviceid']] > $options['limitSelects']) { continue; } } $result[$dservice['dserviceid']]['drules'][] =& $drules[$druleid]; } } } elseif (API_OUTPUT_COUNT == $options['selectDRules']) { $objParams['countOutput'] = 1; $objParams['groupCount'] = 1; $drules = API::DRule()->get($objParams); $drules = zbx_toHash($drules, 'dserviceid'); foreach ($result as $dserviceid => $dservice) { if (isset($drules[$dserviceid])) { $result[$dserviceid]['drules'] = $drules[$dserviceid]['rowscount']; } else { $result[$dserviceid]['drules'] = 0; } } } } // selectDHosts if (!is_null($options['selectDHosts'])) { $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, 'preservekeys' => 1); if (is_array($options['selectDHosts']) || str_in_array($options['selectDHosts'], $subselectsAllowedOutputs)) { $objParams['output'] = $options['selectDHosts']; $dhosts = API::DHost()->get($objParams); if (!is_null($options['limitSelects'])) { order_result($dhosts, 'dhostid'); } foreach ($dhosts as $dhostid => $dhost) { unset($dhosts[$dhostid]['dservices']); foreach ($dhost['dservices'] as $snum => $dservice) { if (!is_null($options['limitSelects'])) { if (!isset($count[$dservice['dserviceid']])) { $count[$dservice['dserviceid']] = 0; } $count[$dservice['dserviceid']]++; if ($count[$dservice['dserviceid']] > $options['limitSelects']) { continue; } } $result[$dservice['dserviceid']]['dhosts'][] =& $dhosts[$dhostid]; } } } elseif (API_OUTPUT_COUNT == $options['selectDHosts']) { $objParams['countOutput'] = 1; $objParams['groupCount'] = 1; $dhosts = API::DHost()->get($objParams); $dhosts = zbx_toHash($dhosts, 'dhostid'); foreach ($result as $dserviceid => $dservice) { if (isset($dhosts[$dserviceid])) { $result[$dserviceid]['dhosts'] = $dhosts[$dserviceid]['rowscount']; } else { $result[$dserviceid]['dhosts'] = 0; } } } } // selectHosts if (!is_null($options['selectHosts'])) { $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, 'preservekeys' => 1, 'sortfield' => 'status'); if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) { $objParams['output'] = $options['selectHosts']; $hosts = API::Host()->get($objParams); if (!is_null($options['limitSelects'])) { order_result($hosts, 'hostid'); } foreach ($hosts as $hostid => $host) { unset($hosts[$hostid]['dservices']); foreach ($host['dservices'] as $dnum => $dservice) { if (!is_null($options['limitSelects'])) { if (!isset($count[$dservice['dserviceid']])) { $count[$dservice['dserviceid']] = 0; } $count[$dservice['dserviceid']]++; if ($count[$dservice['dserviceid']] > $options['limitSelects']) { continue; } } $result[$dservice['dserviceid']]['hosts'][] =& $hosts[$hostid]; } } } elseif (API_OUTPUT_COUNT == $options['selectHosts']) { $objParams['countOutput'] = 1; $objParams['groupCount'] = 1; $hosts = API::Host()->get($objParams); $hosts = zbx_toHash($hosts, 'hostid'); foreach ($result as $dserviceid => $dservice) { if (isset($hosts[$dserviceid])) { $result[$dserviceid]['hosts'] = $hosts[$dserviceid]['rowscount']; } else { $result[$dserviceid]['hosts'] = 0; } } } } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * Get history data * * {@source} * @access public * @static * @since 1.8.3 * @version 1.3 * * @param array $options * @param array $options['itemids'] * @param boolean $options['editable'] * @param string $options['pattern'] * @param int $options['limit'] * @param string $options['order'] * @return array|int item data as array or false if error */ public static function get($options = array()) { global $USER_DETAILS; $nodeCheck = false; $result = array(); $sort_columns = array('itemid', 'clock'); // 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('history' => 'h.itemid'), 'from' => array(), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $def_options = array('history' => ITEM_VALUE_TYPE_UINT64, 'nodeids' => null, 'hostids' => null, 'itemids' => null, 'triggerids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'time_from' => null, 'time_till' => null, 'output' => API_OUTPUT_REFER, 'countOutput' => null, 'groupCount' => null, 'groupOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($def_options, $options); switch ($options['history']) { case ITEM_VALUE_TYPE_LOG: $sql_parts['from']['history'] = 'history_log h'; $sort_columns[] = 'id'; break; case ITEM_VALUE_TYPE_TEXT: $sql_parts['from']['history'] = 'history_text h'; $sort_columns[] = 'id'; break; case ITEM_VALUE_TYPE_STR: $sql_parts['from']['history'] = 'history_str h'; break; case ITEM_VALUE_TYPE_UINT64: $sql_parts['from']['history'] = 'history_uint h'; break; case ITEM_VALUE_TYPE_FLOAT: default: $sql_parts['from']['history'] = 'history h'; } // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $USER_DETAILS['type'] || $options['nopermissions']) { } else { $itemOptions = array('editable' => $options['editable'], 'preservekeys' => 1); if (!is_null($options['itemids'])) { $itemOptions['itemids'] = $options['itemids']; } $items = CItem::get($itemOptions); $options['itemids'] = array_keys($items); } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); $sql_parts['where']['itemid'] = DBcondition('h.itemid', $options['itemids']); if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('h.itemid', $nodeids); } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['hostid'] = 'i.hostid'; } $sql_parts['from']['items'] = 'items i'; $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']); $sql_parts['where']['hi'] = 'h.itemid=i.itemid'; if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('i.hostid', $nodeids); } } // node check !!!!! // should be last, after all ****IDS checks if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('h.itemid', $nodeids); } // time_from if (!is_null($options['time_from'])) { $sql_parts['select']['clock'] = 'h.clock'; $sql_parts['where']['clock_from'] = 'h.clock>=' . $options['time_from']; } // time_till if (!is_null($options['time_till'])) { $sql_parts['select']['clock'] = 'h.clock'; $sql_parts['where']['clock_till'] = 'h.clock<=' . $options['time_till']; } // filter if (is_array($options['filter'])) { zbx_db_filter($sql_parts['from']['history'], $options, $sql_parts); } // search if (is_array($options['search'])) { zbx_db_search($sql_parts['from']['history'], $options, $sql_parts); } // output if ($options['output'] == API_OUTPUT_EXTEND) { unset($sql_parts['select']['clock']); $sql_parts['select']['history'] = 'h.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('count(DISTINCT h.hostid) as rowscount'); //groupCount if (!is_null($options['groupCount'])) { foreach ($sql_parts['group'] as $key => $fields) { $sql_parts['select'][$key] = $fields; } } } // groupOutput $groupOutput = false; if (!is_null($options['groupOutput'])) { if (str_in_array('h.' . $options['groupOutput'], $sql_parts['select']) || str_in_array('h.*', $sql_parts['select'])) { $groupOutput = true; } } // 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; if ($options['sortfield'] == 'clock') { $sql_parts['order']['itemid'] = 'h.itemid ' . $sortorder; } $sql_parts['order'][$options['sortfield']] = 'h.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('h.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('h.*', $sql_parts['select'])) { $sql_parts['select'][$options['sortfield']] = 'h.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; } //--------------- $itemids = array(); $triggerids = 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 .= 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 ' . $sql_where . $sql_order; $db_res = DBselect($sql, $sql_limit); //SDI($sql); $count = 0; $group = array(); while ($data = DBfetch($db_res)) { if ($options['countOutput']) { $result = $data; } else { $itemids[$data['itemid']] = $data['itemid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$count] = array('itemid' => $data['itemid']); } else { $result[$count] = array(); // hostids if (isset($data['hostid'])) { if (!isset($result[$count]['hosts'])) { $result[$count]['hosts'] = array(); } $result[$count]['hosts'][] = array('hostid' => $data['hostid']); unset($data['hostid']); } // triggerids if (isset($data['triggerid'])) { if (!isset($result[$count]['triggers'])) { $result[$count]['triggers'] = array(); } $result[$count]['triggers'][] = array('triggerid' => $data['triggerid']); unset($data['triggerid']); } // itemids // if(isset($data['itemid']) && !is_null($options['itemids'])){ // if(!isset($result[$count]['items'])) $result[$count]['items'] = array(); // $result[$count]['items'][] = array('itemid' => $data['itemid']); // } $result[$count] += $data; // grouping if ($groupOutput) { $dataid = $data[$options['groupOutput']]; if (!isset($group[$dataid])) { $group[$dataid] = array(); } $group[$dataid][] = $result[$count]; } $count++; } } } COpt::memoryPick(); if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * Adds a specific property from the 'sortfield' parameter to the $sqlParts array. * * @param string $sortfield * @param string $sortorder * @param string $alias * @param array $sqlParts * * @return array */ protected function applyQuerySortField($sortfield, $sortorder, $alias, array $sqlParts) { // add sort field to select if distinct is used if (count($sqlParts['from']) > 1 && !str_in_array($alias . '.' . $sortfield, $sqlParts['select']) && !str_in_array($alias . '.*', $sqlParts['select'])) { $sqlParts['select'][$sortfield] = $alias . '.' . $sortfield; } $sqlParts['order'][$alias . '.' . $sortfield] = $alias . '.' . $sortfield . $sortorder; return $sqlParts; }
function add_host_profile_ext($hostid, $ext_host_profiles = array()) { $ext_profiles_fields = array('device_alias', 'device_type', 'device_chassis', 'device_os', 'device_os_short', 'device_hw_arch', 'device_serial', 'device_model', 'device_tag', 'device_vendor', 'device_contract', 'device_who', 'device_status', 'device_app_01', 'device_app_02', 'device_app_03', 'device_app_04', 'device_app_05', 'device_url_1', 'device_url_2', 'device_url_3', 'device_networks', 'device_notes', 'device_hardware', 'device_software', 'ip_subnet_mask', 'ip_router', 'ip_macaddress', 'oob_ip', 'oob_subnet_mask', 'oob_router', 'date_hw_buy', 'date_hw_install', 'date_hw_expiry', 'date_hw_decomm', 'site_street_1', 'site_street_2', 'site_street_3', 'site_city', 'site_state', 'site_country', 'site_zip', 'site_rack', 'site_notes', 'poc_1_name', 'poc_1_email', 'poc_1_phone_1', 'poc_1_phone_2', 'poc_1_cell', 'poc_1_screen', 'poc_1_notes', 'poc_2_name', 'poc_2_email', 'poc_2_phone_1', 'poc_2_phone_2', 'poc_2_cell', 'poc_2_screen', 'poc_2_notes'); $result = DBselect('SELECT * FROM hosts_profiles_ext WHERE hostid=' . $hostid); if (DBfetch($result)) { error(S_HOST_PROFILE . SPACE . S_ALREADY_EXISTS); return false; } $sql = 'INSERT INTO hosts_profiles_ext (hostid,'; $values = ' VALUES (' . $hostid . ','; foreach ($ext_host_profiles as $field => $value) { if (str_in_array($field, $ext_profiles_fields)) { $sql .= $field . ','; $values .= zbx_dbstr($value) . ','; } } $sql = rtrim($sql, ',') . ')'; $values = rtrim($values, ',') . ')'; $result = DBexecute($sql . $values); return $result; }
$result = API::Itemprototype()->update($item); } else { $result = API::Itemprototype()->create($item); } } $result = DBend($result); if (hasRequest('add')) { show_messages($result, _('Item prototype added'), _('Cannot add item prototype')); } else { show_messages($result, _('Item prototype updated'), _('Cannot update item prototype')); } if ($result) { unset($_REQUEST['itemid'], $_REQUEST['form']); uncheckTableRows(getRequest('parent_discoveryid')); } } elseif (hasRequest('action') && str_in_array(getRequest('action'), ['itemprototype.massenable', 'itemprototype.massdisable']) && hasRequest('group_itemid')) { $groupItemId = getRequest('group_itemid'); $enable = getRequest('action') == 'itemprototype.massenable'; DBstart(); $result = $enable ? activate_item($groupItemId) : disable_item($groupItemId); $result = DBend($result); $updated = count($groupItemId); $messageSuccess = $enable ? _n('Item prototype enabled', 'Item prototypes enabled', $updated) : _n('Item prototype disabled', 'Item prototypes disabled', $updated); $messageFailed = $enable ? _n('Cannot enable item prototype', 'Cannot enable item prototypes', $updated) : _n('Cannot disable item prototype', 'Cannot disable item prototypes', $updated); if ($result) { uncheckTableRows(getRequest('parent_discoveryid')); } show_messages($result, $messageSuccess, $messageFailed); } elseif (hasRequest('action') && getRequest('action') == 'itemprototype.massdelete' && hasRequest('group_itemid')) { DBstart(); $result = API::Itemprototype()->delete(getRequest('group_itemid'));
/** * Get Applications data * * @param array $options * @param array $options['itemids'] * @param array $options['hostids'] * @param array $options['groupids'] * @param array $options['triggerids'] * @param array $options['applicationids'] * @param boolean $options['status'] * @param boolean $options['editable'] * @param boolean $options['count'] * @param string $options['pattern'] * @param int $options['limit'] * @param string $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']; $sortColumns = array('applicationid', 'name'); $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND); $sqlParts = array('select' => array('apps' => 'a.applicationid'), 'from' => array('applications' => 'applications a'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'itemids' => null, 'applicationids' => null, 'templated' => null, 'editable' => null, 'inherited' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'exludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'expandData' => null, 'selectHosts' => null, 'selectItems' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($defOptions, $options); // editable + PERMISSION CHECK if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY; $userGroups = getUserGroupsByUserId($userid); $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>=' . $permission . ')'; } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['groupid'] = 'hg.groupid'; } $sqlParts['from']['hosts_groups'] = 'hosts_groups hg'; $sqlParts['where']['ahg'] = 'a.hostid=hg.hostid'; $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['hg'] = 'hg.groupid'; } } // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $options['hostids'] = array_merge($options['hostids'], $options['templateids']); } else { $options['hostids'] = $options['templateids']; } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); if ($options['output'] != API_OUTPUT_EXTEND) { $sqlParts['select']['hostid'] = 'a.hostid'; } $sqlParts['where']['hostid'] = dbConditionInt('a.hostid', $options['hostids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['hostid'] = 'a.hostid'; } } // expandData if (!is_null($options['expandData'])) { $sqlParts['select']['host'] = 'h.host'; $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['ah'] = 'a.hostid=h.hostid'; } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['itemid'] = 'ia.itemid'; } $sqlParts['from']['items_applications'] = 'items_applications ia'; $sqlParts['where'][] = dbConditionInt('ia.itemid', $options['itemids']); $sqlParts['where']['aia'] = 'a.applicationid=ia.applicationid'; } // applicationids if (!is_null($options['applicationids'])) { zbx_value2array($options['applicationids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['applicationid'] = 'a.applicationid'; } $sqlParts['where'][] = dbConditionInt('a.applicationid', $options['applicationids']); } // templated if (!is_null($options['templated'])) { $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['ah'] = 'a.hostid=h.hostid'; if ($options['templated']) { $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE; } else { $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE; } } // inherited if (!is_null($options['inherited'])) { if ($options['inherited']) { $sqlParts['where'][] = 'a.templateid IS NOT NULL'; } else { $sqlParts['where'][] = 'a.templateid IS NULL'; } } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sqlParts['select']['apps'] = 'a.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sqlParts['select'] = array('count(DISTINCT a.applicationid) as rowscount'); // groupCount if (!is_null($options['groupCount'])) { foreach ($sqlParts['group'] as $key => $fields) { $sqlParts['select'][$key] = $fields; } } } // search if (is_array($options['search'])) { zbx_db_search('applications a', $options, $sqlParts); } // filter if (is_array($options['filter'])) { $this->dbFilter('applications a', $options, $sqlParts); } // sorting zbx_db_sorting($sqlParts, $options, $sortColumns, 'a'); // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sqlParts['limit'] = $options['limit']; } $applicationids = array(); $sqlParts['select'] = array_unique($sqlParts['select']); $sqlParts['from'] = array_unique($sqlParts['from']); $sqlParts['where'] = array_unique($sqlParts['where']); $sqlParts['group'] = array_unique($sqlParts['group']); $sqlParts['order'] = array_unique($sqlParts['order']); $sqlSelect = ''; $sqlFrom = ''; $sqlWhere = ''; $sqlGroup = ''; $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['group'])) { $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']); } if (!empty($sqlParts['order'])) { $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']); } $sqlLimit = $sqlParts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('a.applicationid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder; $res = DBselect($sql, $sqlLimit); while ($application = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $application; } else { $result = $application['rowscount']; } } else { $applicationids[$application['applicationid']] = $application['applicationid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$application['applicationid']] = array('applicationid' => $application['applicationid']); } else { if (!isset($result[$application['applicationid']])) { $result[$application['applicationid']] = array(); } if (!is_null($options['selectHosts']) && !isset($result[$application['applicationid']]['hosts'])) { $result[$application['applicationid']]['hosts'] = array(); } if (!is_null($options['selectItems']) && !isset($result[$application['applicationid']]['items'])) { $result[$application['applicationid']]['items'] = array(); } // hostids if (isset($application['hostid']) && is_null($options['selectHosts'])) { if (!isset($result[$application['applicationid']]['hosts'])) { $result[$application['applicationid']]['hosts'] = array(); } $result[$application['applicationid']]['hosts'][] = array('hostid' => $application['hostid']); } // itemids if (isset($application['itemid']) && is_null($options['selectItems'])) { if (!isset($result[$application['applicationid']]['items'])) { $result[$application['applicationid']]['items'] = array(); } $result[$application['applicationid']]['items'][] = array('itemid' => $application['itemid']); unset($application['itemid']); } $result[$application['applicationid']] += $application; } } } if (!is_null($options['countOutput'])) { return $result; } // adding objects // adding hosts if ($options['selectHosts'] !== null && (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs))) { $objParams = array('output' => $options['selectHosts'], 'applicationids' => $applicationids, 'nopermissions' => 1, 'templated_hosts' => true, 'preservekeys' => 1); $hosts = API::Host()->get($objParams); foreach ($hosts as $hostid => $host) { $iapplications = $host['applications']; unset($host['applications']); foreach ($iapplications as $application) { $result[$application['applicationid']]['hosts'][] = $host; } } } // adding objects // adding items if (!is_null($options['selectItems']) && str_in_array($options['selectItems'], $subselectsAllowedOutputs)) { $objParams = array('output' => $options['selectItems'], 'applicationids' => $applicationids, 'nopermissions' => true, 'preservekeys' => true); $items = API::Item()->get($objParams); foreach ($items as $itemid => $item) { $iapplications = $item['applications']; unset($item['applications']); foreach ($iapplications as $application) { $result[$application['applicationid']]['items'][] = $item; } } } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
$result = API::DiscoveryRule()->update($newItem); $result = DBend($result); } else { $result = API::DiscoveryRule()->create([$newItem]); } } if (hasRequest('add')) { show_messages($result, _('Discovery rule created'), _('Cannot add discovery rule')); } else { show_messages($result, _('Discovery rule updated'), _('Cannot update discovery rule')); } if ($result) { unset($_REQUEST['itemid'], $_REQUEST['form']); uncheckTableRows(getRequest('hostid')); } } elseif (hasRequest('action') && str_in_array(getRequest('action'), ['discoveryrule.massenable', 'discoveryrule.massdisable']) && hasRequest('g_hostdruleid')) { $groupHostDiscoveryRuleId = getRequest('g_hostdruleid'); $enable = getRequest('action') == 'discoveryrule.massenable'; DBstart(); $result = $enable ? activate_item($groupHostDiscoveryRuleId) : disable_item($groupHostDiscoveryRuleId); $result = DBend($result); if ($result) { uncheckTableRows(getRequest('hostid')); } $updated = count($groupHostDiscoveryRuleId); $messageSuccess = $enable ? _n('Discovery rule enabled', 'Discovery rules enabled', $updated) : _n('Discovery rule disabled', 'Discovery rules disabled', $updated); $messageFailed = $enable ? _n('Cannot enable discovery rule', 'Cannot enable discovery rules', $updated) : _n('Cannot disable discovery rule', 'Cannot disable discovery rules', $updated); show_messages($result, $messageSuccess, $messageFailed); } elseif (hasRequest('action') && getRequest('action') == 'discoveryrule.massdelete' && hasRequest('g_hostdruleid')) { $result = API::DiscoveryRule()->delete(getRequest('g_hostdruleid')); if ($result) {
/** * Check items data. * * Any system field passed to the function will be unset. * * @throw APIException * * @param array $items passed by reference * @param bool $update * * @return void */ protected function checkInput(array &$items, $update = false) { if ($update) { $itemDbFields = array('itemid' => null); $dbItemsFields = array('itemid', 'templateid'); foreach ($this->fieldRules as $field => $rule) { if (!isset($rule['system'])) { $dbItemsFields[] = $field; } } $dbItems = $this->get(array('output' => $dbItemsFields, 'itemids' => zbx_objectValues($items, 'itemid'), 'editable' => true, 'preservekeys' => true)); $dbHosts = API::Host()->get(array('output' => array('hostid', 'status', 'name'), 'hostids' => zbx_objectValues($dbItems, 'hostid'), 'templated_hosts' => true, 'editable' => true, 'selectApplications' => API_OUTPUT_REFER, 'preservekeys' => true)); } else { $itemDbFields = array('name' => null, 'key_' => null, 'hostid' => null, 'type' => null, 'value_type' => null, 'delay' => '0', 'delay_flex' => ''); $dbHosts = API::Host()->get(array('output' => array('hostid', 'status', 'name'), 'hostids' => zbx_objectValues($items, 'hostid'), 'templated_hosts' => true, 'editable' => true, 'selectApplications' => API_OUTPUT_REFER, 'preservekeys' => true)); } // interfaces $interfaces = API::HostInterface()->get(array('output' => array('interfaceid', 'hostid', 'type'), 'hostids' => zbx_objectValues($dbHosts, 'hostid'), 'nopermissions' => true, 'preservekeys' => true)); if ($update) { $updateDiscoveredValidator = new CUpdateDiscoveredValidator(array('allowed' => array('itemid', 'status'), 'messageAllowedField' => _('Cannot update "%1$s" for a discovered item.'))); foreach ($items as $item) { // check permissions if (!isset($dbItems[$item['itemid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } // discovered fields, except status, cannot be updated $this->checkPartialValidator($item, $updateDiscoveredValidator, $dbItems[$item['itemid']]); } $items = $this->extendObjects($this->tableName(), $items, array('name')); } foreach ($items as $inum => &$item) { $item = $this->clearValues($item); $fullItem = $items[$inum]; if (!check_db_fields($itemDbFields, $item)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } if ($update) { check_db_fields($dbItems[$item['itemid']], $fullItem); $this->checkNoParameters($item, array('templateid', 'state'), _('Cannot update "%1$s" for item "%2$s".'), $item['name']); // apply rules foreach ($this->fieldRules as $field => $rules) { if (0 != $fullItem['templateid'] && isset($rules['template']) || isset($rules['system'])) { unset($item[$field]); } } if (!isset($item['key_'])) { $item['key_'] = $fullItem['key_']; } if (!isset($item['hostid'])) { $item['hostid'] = $fullItem['hostid']; } // if a templated item is being assigned to an interface with a different type, ignore it $itemInterfaceType = itemTypeInterface($dbItems[$item['itemid']]['type']); if ($fullItem['templateid'] && isset($item['interfaceid']) && isset($interfaces[$item['interfaceid']]) && $itemInterfaceType !== INTERFACE_TYPE_ANY && $interfaces[$item['interfaceid']]['type'] != $itemInterfaceType) { unset($item['interfaceid']); } } else { if (!isset($dbHosts[$item['hostid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } check_db_fields($itemDbFields, $fullItem); $this->checkNoParameters($item, array('templateid', 'state'), _('Cannot set "%1$s" for item "%2$s".'), $item['name']); } $host = $dbHosts[$fullItem['hostid']]; if ($fullItem['type'] == ITEM_TYPE_ZABBIX_ACTIVE) { $item['delay_flex'] = ''; } if ($fullItem['value_type'] == ITEM_VALUE_TYPE_STR) { $item['delta'] = 0; } if ($fullItem['value_type'] != ITEM_VALUE_TYPE_UINT64) { $item['data_type'] = 0; } // check if the item requires an interface $itemInterfaceType = itemTypeInterface($fullItem['type']); if ($itemInterfaceType !== false && $host['status'] != HOST_STATUS_TEMPLATE) { if (!$fullItem['interfaceid']) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No interface found.')); } elseif (!isset($interfaces[$fullItem['interfaceid']]) || bccomp($interfaces[$fullItem['interfaceid']]['hostid'], $fullItem['hostid']) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Item uses host interface from non-parent host.')); } elseif ($itemInterfaceType !== INTERFACE_TYPE_ANY && $interfaces[$fullItem['interfaceid']]['type'] != $itemInterfaceType) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Item uses incorrect interface type.')); } } else { $item['interfaceid'] = 0; } // item key if ($fullItem['type'] == ITEM_TYPE_DB_MONITOR && strcmp($fullItem['key_'], ZBX_DEFAULT_KEY_DB_MONITOR) == 0 || $fullItem['type'] == ITEM_TYPE_SSH && strcmp($fullItem['key_'], ZBX_DEFAULT_KEY_SSH) == 0 || $fullItem['type'] == ITEM_TYPE_TELNET && strcmp($fullItem['key_'], ZBX_DEFAULT_KEY_TELNET) == 0 || $fullItem['type'] == ITEM_TYPE_JMX && strcmp($fullItem['key_'], ZBX_DEFAULT_KEY_JMX) == 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Check the key, please. Default example was passed.')); } // key $itemKey = new CItemKey($fullItem['key_']); if (!$itemKey->isValid()) { self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_INVALID_KEY), array($fullItem['key_'], $fullItem['name'], $host['name'], $itemKey->getError()))); } // parameters if ($fullItem['type'] == ITEM_TYPE_AGGREGATE) { $params = $itemKey->getParameters(); if (!str_in_array($itemKey->getKeyId(), array('grpmax', 'grpmin', 'grpsum', 'grpavg')) || count($params) != 4 || !str_in_array($params[2], array('last', 'min', 'max', 'avg', 'sum', 'count'))) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Key "%1$s" does not match <grpmax|grpmin|grpsum|grpavg>["Host group(s)", "Item key",' . ' "<last|min|max|avg|sum|count>", "parameter"].', $itemKey->getKeyId())); } } // type of information if ($fullItem['type'] == ITEM_TYPE_AGGREGATE && $fullItem['value_type'] != ITEM_VALUE_TYPE_FLOAT && $fullItem['value_type'] != ITEM_VALUE_TYPE_UINT64) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Type of information must be "Numeric (float)" for aggregate items.')); } // log if ($fullItem['value_type'] != ITEM_VALUE_TYPE_LOG && str_in_array($itemKey->getKeyId(), array('log', 'logrt', 'eventlog'))) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Type of information must be "Log" for log key.')); } // update interval if ($fullItem['type'] != ITEM_TYPE_TRAPPER && $fullItem['type'] != ITEM_TYPE_SNMPTRAP) { $res = calculateItemNextcheck(0, $fullItem['type'], $fullItem['delay'], $fullItem['delay_flex'], time()); if ($res == ZBX_JAN_2038) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Item will not be refreshed. Please enter a correct update interval.')); } } // ssh, telnet if ($fullItem['type'] == ITEM_TYPE_SSH || $fullItem['type'] == ITEM_TYPE_TELNET) { if (zbx_empty($fullItem['username'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No authentication user name specified.')); } if ($fullItem['type'] == ITEM_TYPE_SSH && $fullItem['authtype'] == ITEM_AUTHTYPE_PUBLICKEY) { if (zbx_empty($fullItem['publickey'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No public key file specified.')); } if (zbx_empty($fullItem['privatekey'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No private key file specified.')); } } } // snmp trap if ($fullItem['type'] == ITEM_TYPE_SNMPTRAP && strcmp($fullItem['key_'], 'snmptrap.fallback') != 0 && strcmp($itemKey->getKeyId(), 'snmptrap') != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _('SNMP trap key is invalid.')); } // snmp oid if (in_array($fullItem['type'], array(ITEM_TYPE_SNMPV1, ITEM_TYPE_SNMPV2C, ITEM_TYPE_SNMPV3)) && zbx_empty($fullItem['snmp_oid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No SNMP OID specified.')); } // snmp community if (in_array($fullItem['type'], array(ITEM_TYPE_SNMPV1, ITEM_TYPE_SNMPV2C)) && zbx_empty($fullItem['snmp_community'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No SNMP community specified.')); } // snmp port if (isset($fullItem['port']) && !zbx_empty($fullItem['port']) && !validatePortNumberOrMacro($fullItem['port'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Item "%1$s:%2$s" has invalid port: "%3$s".', $fullItem['name'], $fullItem['key_'], $fullItem['port'])); } if (isset($fullItem['snmpv3_securitylevel']) && $fullItem['snmpv3_securitylevel'] != ITEM_SNMPV3_SECURITYLEVEL_NOAUTHNOPRIV) { // snmpv3 authprotocol if (str_in_array($fullItem['snmpv3_securitylevel'], array(ITEM_SNMPV3_SECURITYLEVEL_AUTHNOPRIV, ITEM_SNMPV3_SECURITYLEVEL_AUTHPRIV))) { if (isset($fullItem['snmpv3_authprotocol']) && (zbx_empty($fullItem['snmpv3_authprotocol']) || !str_in_array($fullItem['snmpv3_authprotocol'], array(ITEM_AUTHPROTOCOL_MD5, ITEM_AUTHPROTOCOL_SHA)))) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect authentication protocol for item "%1$s".', $fullItem['name'])); } } // snmpv3 privprotocol if ($fullItem['snmpv3_securitylevel'] == ITEM_SNMPV3_SECURITYLEVEL_AUTHPRIV) { if (isset($fullItem['snmpv3_privprotocol']) && (zbx_empty($fullItem['snmpv3_privprotocol']) || !str_in_array($fullItem['snmpv3_privprotocol'], array(ITEM_PRIVPROTOCOL_DES, ITEM_PRIVPROTOCOL_AES)))) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect privacy protocol for item "%1$s".', $fullItem['name'])); } } } // check that the given applications belong to the item's host if (isset($item['applications']) && $item['applications']) { $dbApplicationIds = zbx_objectValues($host['applications'], 'applicationid'); foreach ($item['applications'] as $appId) { if (!in_array($appId, $dbApplicationIds)) { $error = _s('Application with ID "%1$s" is not available on "%2$s".', $appId, $host['name']); self::exception(ZBX_API_ERROR_PARAMETERS, $error); } } } $this->checkDelayFlex($fullItem); $this->checkSpecificFields($fullItem); } unset($item); $this->checkExistingItems($items); }
/** * Returns the next data set from a DB resource or false if there are no more results. * * @param resource $cursor * @param bool $convertNulls convert all null values to string zeroes * * @return array|bool */ function DBfetch($cursor, $convertNulls = true) { global $DB; $result = false; if (!isset($DB['DB']) || empty($DB['DB']) || is_bool($cursor)) { return $result; } switch ($DB['TYPE']) { case ZBX_DB_MYSQL: $result = mysqli_fetch_assoc($cursor); if (!$result) { mysqli_free_result($cursor); } break; case ZBX_DB_POSTGRESQL: if (!($result = pg_fetch_assoc($cursor))) { pg_free_result($cursor); } break; case ZBX_DB_ORACLE: if ($row = oci_fetch_assoc($cursor)) { $result = array(); foreach ($row as $key => $value) { $field_type = strtolower(oci_field_type($cursor, $key)); // Oracle does not support NULL values for string fields, so if the string is empty, it will return NULL // convert it to an empty string to be consistent with other databases $value = str_in_array($field_type, array('varchar', 'varchar2', 'blob', 'clob')) && is_null($value) ? '' : $value; if (is_object($value) && strpos($field_type, 'lob') !== false) { $value = $value->load(); } $result[strtolower($key)] = $value; } } break; case ZBX_DB_DB2: if (!($result = db2_fetch_assoc($cursor))) { db2_free_result($cursor); } else { // cast all of the values to string to be consistent with other DB drivers: all of them return // only strings. foreach ($result as &$value) { if ($value !== null) { $value = (string) $value; } } unset($value); } break; case ZBX_DB_SQLITE3: if ($DB['TRANSACTIONS'] == 0) { lock_sqlite3_access(); } if (!($result = $cursor->fetchArray(SQLITE3_ASSOC))) { unset($cursor); } else { // cast all of the values to string to be consistent with other DB drivers: all of them return // only strings. foreach ($result as &$value) { $value = (string) $value; } unset($value); } if ($DB['TRANSACTIONS'] == 0) { unlock_sqlite3_access(); } break; } if ($result) { if ($convertNulls) { foreach ($result as $key => $val) { if (is_null($val)) { $result[$key] = '0'; } } } return $result; } return false; }
DBstart(); while ($db_group = DBfetch($db_groups)) { if (!uint_in_array($db_group['groupid'], $groups)) { continue; } /* if(!$group = get_hostgroup_by_groupid($db_group['groupid'])) continue;*/ $result &= delete_host_group($db_group['groupid']); /* if($result){ add_audit(AUDIT_ACTION_DELETE,AUDIT_RESOURCE_HOST_GROUP, S_HOST_GROUP.' ['.$group['name'].' ] ['.$group['groupid'].']'); }*/ } $result = DBend($result); show_messages(true, S_GROUP_DELETED, S_CANNOT_DELETE_GROUP); } else { if (str_in_array($_REQUEST['go'], array('activate', 'disable'))) { $result = true; $status = $_REQUEST['go'] == 'activate' ? HOST_STATUS_MONITORED : HOST_STATUS_NOT_MONITORED; $groups = get_request('groups', array()); $db_hosts = DBselect('select h.hostid, hg.groupid ' . ' from hosts_groups hg, hosts h' . ' where h.hostid=hg.hostid ' . ' and h.status in (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')' . ' and ' . DBin_node('h.hostid')); DBstart(); while ($db_host = DBfetch($db_hosts)) { if (!uint_in_array($db_host['groupid'], $groups)) { continue; } $host = get_host_by_hostid($db_host['hostid']); $result &= update_host_status($db_host['hostid'], $status); /* add_audit(AUDIT_ACTION_UPDATE,AUDIT_RESOURCE_HOST, 'Old status ['.$host['status'].'] '.'New status ['.$status.']');*/ } $result = DBend($result);
$newOperationsTable->addRow(array(_('Conditions'), new CDiv($operationConditionsTable, 'objectgroup inlineblock border_dotted ui-corner-all')), 'indent_top'); } // append new operation condition to form list if (isset($_REQUEST['new_opcondition'])) { $newOperationConditionTable = new CTable(null, 'formElementTable'); $allowedOpConditions = get_opconditions_by_eventsource($this->data['eventsource']); $new_opcondition = get_request('new_opcondition', array()); if (!is_array($new_opcondition)) { $new_opcondition = array(); } if (empty($new_opcondition)) { $new_opcondition['conditiontype'] = CONDITION_TYPE_EVENT_ACKNOWLEDGED; $new_opcondition['operator'] = CONDITION_OPERATOR_LIKE; $new_opcondition['value'] = 0; } if (!str_in_array($new_opcondition['conditiontype'], $allowedOpConditions)) { $new_opcondition['conditiontype'] = $allowedOpConditions[0]; } $rowCondition = array(); $conditionTypeComboBox = new CComboBox('new_opcondition[conditiontype]', $new_opcondition['conditiontype'], 'submit()'); foreach ($allowedOpConditions as $opcondition) { $conditionTypeComboBox->addItem($opcondition, condition_type2str($opcondition)); } array_push($rowCondition, $conditionTypeComboBox); $operationConditionComboBox = new CComboBox('new_opcondition[operator]'); foreach (get_operators_by_conditiontype($new_opcondition['conditiontype']) as $operationCondition) { $operationConditionComboBox->addItem($operationCondition, condition_operator2str($operationCondition)); } array_push($rowCondition, $operationConditionComboBox); if ($new_opcondition['conditiontype'] == CONDITION_TYPE_EVENT_ACKNOWLEDGED) { $operationConditionValueComboBox = new CComboBox('new_opcondition[value]', $new_opcondition['value']);
function zbx_define_menu_restrictions($page, $ZBX_MENU) { foreach ($ZBX_MENU as $section) { foreach ($section['pages'] as $menu_page) { if ($menu_page['url'] == $page['file'] || isset($menu_page['sub_pages']) && str_in_array($page['file'], $menu_page['sub_pages'])) { if (isset($section['force_disable_all_nodes']) && !defined('ZBX_NOT_ALLOW_ALL_NODES')) { define('ZBX_NOT_ALLOW_ALL_NODES', 1); } if (isset($section['hide_node_selection']) && !defined('ZBX_HIDE_NODE_SELECTION')) { define('ZBX_HIDE_NODE_SELECTION', 1); } return null; } } } }
/** * Get GraphPrototype data * * @param array $options * @return array */ public function get($options = array()) { $result = array(); $userType = self::$userData['type']; $userid = self::$userData['userid']; // allowed columns for sorting $sortColumns = array('graphid', 'name', 'graphtype'); // allowed output options for [ select_* ] params $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM); $sqlParts = array('select' => array('graphs' => 'g.graphid'), 'from' => array('graphs' => 'graphs g'), 'where' => array('g.flags=' . ZBX_FLAG_DISCOVERY_CHILD), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'graphids' => null, 'itemids' => null, 'discoveryids' => null, 'type' => null, 'templated' => null, 'inherited' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectTemplates' => null, 'selectHosts' => null, 'selectItems' => null, 'selectGraphItems' => null, 'selectDiscoveryRule' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($defOptions, $options); if (is_array($options['output'])) { unset($sqlParts['select']['graphs']); $dbTable = DB::getSchema('graphs'); foreach ($options['output'] as $field) { if (isset($dbTable['fields'][$field])) { $sqlParts['select'][$field] = 'g.' . $field; } } $options['output'] = API_OUTPUT_CUSTOM; } // editable + PERMISSION CHECK if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY; $userGroups = getUserGroupsByUserId($userid); $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM graphs_items gi,items i,hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE g.graphid=gi.graphid' . ' AND gi.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY gi.graphid' . ' HAVING MIN(r.permission)>=' . $permission . ')'; } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['groupid'] = 'hg.groupid'; } $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts_groups'] = 'hosts_groups hg'; $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']); $sqlParts['where'][] = 'hg.hostid=i.hostid'; $sqlParts['where']['gig'] = 'gi.graphid=g.graphid'; $sqlParts['where']['igi'] = 'i.itemid=gi.itemid'; $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['hg'] = 'hg.groupid'; } } // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $options['hostids'] = array_merge($options['hostids'], $options['templateids']); } else { $options['hostids'] = $options['templateids']; } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['hostid'] = 'i.hostid'; } $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where'][] = dbConditionInt('i.hostid', $options['hostids']); $sqlParts['where']['gig'] = 'gi.graphid=g.graphid'; $sqlParts['where']['igi'] = 'i.itemid=gi.itemid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['i'] = 'i.hostid'; } } // graphids if (!is_null($options['graphids'])) { zbx_value2array($options['graphids']); $sqlParts['where'][] = dbConditionInt('g.graphid', $options['graphids']); } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['itemid'] = 'gi.itemid'; } $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['where']['gig'] = 'gi.graphid=g.graphid'; $sqlParts['where'][] = dbConditionInt('gi.itemid', $options['itemids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['gi'] = 'gi.itemid'; } } // discoveryids if (!is_null($options['discoveryids'])) { zbx_value2array($options['discoveryids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['itemid'] = 'id.parent_itemid'; } $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['from']['item_discovery'] = 'item_discovery id'; $sqlParts['where']['gig'] = 'gi.graphid=g.graphid'; $sqlParts['where']['giid'] = 'gi.itemid=id.itemid'; $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['id'] = 'id.parent_itemid'; } } // type if (!is_null($options['type'])) { $sqlParts['where'][] = 'g.type=' . zbx_dbstr($options['type']); } // templated if (!is_null($options['templated'])) { $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['igi'] = 'i.itemid=gi.itemid'; $sqlParts['where']['ggi'] = 'g.graphid=gi.graphid'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; if ($options['templated']) { $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE; } else { $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE; } } // inherited if (!is_null($options['inherited'])) { if ($options['inherited']) { $sqlParts['where'][] = 'g.templateid IS NOT NULL'; } else { $sqlParts['where'][] = 'g.templateid IS NULL'; } } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sqlParts['select']['graphs'] = 'g.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sqlParts['select'] = array('count(DISTINCT g.graphid) as rowscount'); // groupCount if (!is_null($options['groupCount'])) { foreach ($sqlParts['group'] as $key => $fields) { $sqlParts['select'][$key] = $fields; } } } // search if (is_array($options['search'])) { zbx_db_search('graphs g', $options, $sqlParts); } // filter if (is_array($options['filter'])) { $this->dbFilter('graphs g', $options, $sqlParts); if (isset($options['filter']['host'])) { zbx_value2array($options['filter']['host']); $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['gig'] = 'gi.graphid=g.graphid'; $sqlParts['where']['igi'] = 'i.itemid=gi.itemid'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; $sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']); } if (isset($options['filter']['hostid'])) { zbx_value2array($options['filter']['hostid']); $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where']['gig'] = 'gi.graphid=g.graphid'; $sqlParts['where']['igi'] = 'i.itemid=gi.itemid'; $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']); } } // sorting zbx_db_sorting($sqlParts, $options, $sortColumns, 'g'); // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sqlParts['limit'] = $options['limit']; } $graphids = array(); $sqlParts['select'] = array_unique($sqlParts['select']); $sqlParts['from'] = array_unique($sqlParts['from']); $sqlParts['where'] = array_unique($sqlParts['where']); $sqlParts['group'] = array_unique($sqlParts['group']); $sqlParts['order'] = array_unique($sqlParts['order']); $sqlSelect = ''; $sqlFrom = ''; $sqlWhere = ''; $sqlGroup = ''; $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['group'])) { $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']); } if (!empty($sqlParts['order'])) { $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']); } $sqlLimit = $sqlParts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('g.graphid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder; $dbRes = DBselect($sql, $sqlLimit); while ($graph = DBfetch($dbRes)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $graph; } else { $result = $graph['rowscount']; } } else { $graphids[$graph['graphid']] = $graph['graphid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$graph['graphid']] = array('graphid' => $graph['graphid']); } else { if (!isset($result[$graph['graphid']])) { $result[$graph['graphid']] = array(); } if (!is_null($options['selectHosts']) && !isset($result[$graph['graphid']]['hosts'])) { $result[$graph['graphid']]['hosts'] = array(); } if (!is_null($options['selectGraphItems']) && !isset($result[$graph['graphid']]['gitems'])) { $result[$graph['graphid']]['gitems'] = array(); } if (!is_null($options['selectTemplates']) && !isset($result[$graph['graphid']]['templates'])) { $result[$graph['graphid']]['templates'] = array(); } if (!is_null($options['selectItems']) && !isset($result[$graph['graphid']]['items'])) { $result[$graph['graphid']]['items'] = array(); } if (!is_null($options['selectDiscoveryRule']) && !isset($result[$graph['graphid']]['discoveryRule'])) { $result[$graph['graphid']]['discoveryRule'] = array(); } // hostids if (isset($graph['hostid']) && is_null($options['selectHosts'])) { if (!isset($result[$graph['graphid']]['hosts'])) { $result[$graph['graphid']]['hosts'] = array(); } $result[$graph['graphid']]['hosts'][] = array('hostid' => $graph['hostid']); unset($graph['hostid']); } // itemids if (isset($graph['itemid']) && is_null($options['selectItems'])) { if (!isset($result[$graph['graphid']]['items'])) { $result[$graph['graphid']]['items'] = array(); } $result[$graph['graphid']]['items'][] = array('itemid' => $graph['itemid']); unset($graph['itemid']); } $result[$graph['graphid']] += $graph; } } } if (!is_null($options['countOutput'])) { return $result; } // adding GraphItems if (!is_null($options['selectGraphItems']) && str_in_array($options['selectGraphItems'], $subselectsAllowedOutputs)) { $gitems = API::GraphItem()->get(array('nodeids' => $nodeids, 'output' => $options['selectGraphItems'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true)); foreach ($gitems as $gitem) { $ggraphs = $gitem['graphs']; unset($gitem['graphs']); foreach ($ggraphs as $graph) { $result[$graph['graphid']]['gitems'][$gitem['gitemid']] = $gitem; } } } // adding Hostgroups if (!is_null($options['selectGroups'])) { if (is_array($options['selectGroups']) || str_in_array($options['selectGroups'], $subselectsAllowedOutputs)) { $groups = API::HostGroup()->get(array('nodeids' => $nodeids, 'output' => $options['selectGroups'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true)); foreach ($groups as $group) { $ggraphs = $group['graphs']; unset($group['graphs']); foreach ($ggraphs as $graph) { $result[$graph['graphid']]['groups'][] = $group; } } } } // adding Hosts if (!is_null($options['selectHosts'])) { if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) { $hosts = API::Host()->get(array('nodeids' => $nodeids, 'output' => $options['selectHosts'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true)); foreach ($hosts as $host) { $hgraphs = $host['graphs']; unset($host['graphs']); foreach ($hgraphs as $graph) { $result[$graph['graphid']]['hosts'][] = $host; } } } } // adding Templates if (!is_null($options['selectTemplates']) && str_in_array($options['selectTemplates'], $subselectsAllowedOutputs)) { $templates = API::Template()->get(array('nodeids' => $nodeids, 'output' => $options['selectTemplates'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true)); foreach ($templates as $template) { $tgraphs = $template['graphs']; unset($template['graphs']); foreach ($tgraphs as $graph) { $result[$graph['graphid']]['templates'][] = $template; } } } // adding Items if (!is_null($options['selectItems']) && str_in_array($options['selectItems'], $subselectsAllowedOutputs)) { $items = API::Item()->get(array('nodeids' => $nodeids, 'output' => $options['selectItems'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true, 'filter' => array('flags' => null))); foreach ($items as $item) { $igraphs = $item['graphs']; unset($item['graphs']); foreach ($igraphs as $graph) { $result[$graph['graphid']]['items'][] = $item; } } } // adding discoveryRule if (!is_null($options['selectDiscoveryRule'])) { $ruleids = $ruleMap = array(); $dbRules = DBselect('SELECT id.parent_itemid,gi.graphid' . ' FROM item_discovery id,graphs_items gi' . ' WHERE ' . dbConditionInt('gi.graphid', $graphids) . ' AND gi.itemid=id.itemid'); while ($rule = DBfetch($dbRules)) { $ruleids[$rule['parent_itemid']] = $rule['parent_itemid']; $ruleMap[$rule['graphid']] = $rule['parent_itemid']; } $objParams = array('nodeids' => $nodeids, 'itemids' => $ruleids, 'nopermissions' => true, 'preservekeys' => true); if (is_array($options['selectDiscoveryRule']) || str_in_array($options['selectDiscoveryRule'], $subselectsAllowedOutputs)) { $objParams['output'] = $options['selectDiscoveryRule']; $discoveryRules = API::DiscoveryRule()->get($objParams); foreach ($result as $graphid => $graph) { if (isset($ruleMap[$graphid]) && isset($discoveryRules[$ruleMap[$graphid]])) { $result[$graphid]['discoveryRule'] = $discoveryRules[$ruleMap[$graphid]]; } } } } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * Get Itemprototype data */ public function get($options = array()) { $result = array(); $userType = self::$userData['type']; $userid = self::$userData['userid']; // allowed columns for sorting $sortColumns = array('itemid', 'name', 'key_', 'delay', 'history', 'trends', 'type', 'status'); // allowed output options for [ select_* ] params $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM); $sqlParts = array('select' => array('items' => 'i.itemid'), 'from' => array('items' => 'items i'), 'where' => array('i.flags=' . ZBX_FLAG_DISCOVERY_CHILD), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'itemids' => null, 'discoveryids' => null, 'graphids' => null, 'triggerids' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectHosts' => null, 'selectApplications' => null, 'selectTriggers' => null, 'selectGraphs' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null); $options = zbx_array_merge($defOptions, $options); if (is_array($options['output'])) { unset($sqlParts['select']['items']); $dbTable = DB::getSchema('items'); $sqlParts['select']['itemid'] = 'i.itemid'; foreach ($options['output'] as $field) { if (isset($dbTable['fields'][$field])) { $sqlParts['select'][$field] = 'i.' . $field; } } $options['output'] = API_OUTPUT_CUSTOM; } // editable + PERMISSION CHECK if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY; $userGroups = getUserGroupsByUserId($userid); $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE i.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>=' . $permission . ')'; } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $options['hostids'] = array_merge($options['hostids'], $options['templateids']); } else { $options['hostids'] = $options['templateids']; } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); if ($options['output'] != API_OUTPUT_EXTEND) { $sqlParts['select']['hostid'] = 'i.hostid'; } $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['i'] = 'i.hostid'; } } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); $sqlParts['where']['itemid'] = dbConditionInt('i.itemid', $options['itemids']); } // discoveryids if (!is_null($options['discoveryids'])) { zbx_value2array($options['discoveryids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['discoveryid'] = 'id.parent_itemid'; } $sqlParts['from']['item_discovery'] = 'item_discovery id'; $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']); $sqlParts['where']['idi'] = 'i.itemid=id.itemid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['id'] = 'id.parent_itemid'; } } // triggerids if (!is_null($options['triggerids'])) { zbx_value2array($options['triggerids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['triggerid'] = 'f.triggerid'; } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']); $sqlParts['where']['if'] = 'i.itemid=f.itemid'; } // graphids if (!is_null($options['graphids'])) { zbx_value2array($options['graphids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['graphid'] = 'gi.graphid'; } $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['where'][] = dbConditionInt('gi.graphid', $options['graphids']); $sqlParts['where']['igi'] = 'i.itemid=gi.itemid'; } // inherited if (!is_null($options['inherited'])) { if ($options['inherited']) { $sqlParts['where'][] = 'i.templateid IS NOT NULL'; } else { $sqlParts['where'][] = 'i.templateid IS NULL'; } } // templated if (!is_null($options['templated'])) { $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; if ($options['templated']) { $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE; } else { $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE; } } // monitored if (!is_null($options['monitored'])) { $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; if ($options['monitored']) { $sqlParts['where'][] = 'h.status=' . HOST_STATUS_MONITORED; $sqlParts['where'][] = 'i.status=' . ITEM_STATUS_ACTIVE; } else { $sqlParts['where'][] = '(h.status<>' . HOST_STATUS_MONITORED . ' OR i.status<>' . ITEM_STATUS_ACTIVE . ')'; } } // search if (is_array($options['search'])) { zbx_db_search('items i', $options, $sqlParts); } // --- FILTER --- if (is_array($options['filter'])) { $this->dbFilter('items i', $options, $sqlParts); if (isset($options['filter']['host'])) { zbx_value2array($options['filter']['host']); $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; $sqlParts['where']['h'] = dbConditionString('h.host', $options['filter']['host']); } } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sqlParts['select']['items'] = 'i.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sqlParts['select'] = array('count(DISTINCT i.itemid) as rowscount'); //groupCount if (!is_null($options['groupCount'])) { foreach ($sqlParts['group'] as $key => $fields) { $sqlParts['select'][$key] = $fields; } } } // sorting zbx_db_sorting($sqlParts, $options, $sortColumns, 'i'); // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sqlParts['limit'] = $options['limit']; } //---------- $itemids = array(); $sqlParts['select'] = array_unique($sqlParts['select']); $sqlParts['from'] = array_unique($sqlParts['from']); $sqlParts['where'] = array_unique($sqlParts['where']); $sqlParts['group'] = array_unique($sqlParts['group']); $sqlParts['order'] = array_unique($sqlParts['order']); $sqlSelect = ''; $sqlFrom = ''; $sqlWhere = ''; $sqlGroup = ''; $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['group'])) { $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']); } if (!empty($sqlParts['order'])) { $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']); } $sqlLimit = $sqlParts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('i.itemid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder; //SDI($sql); $res = DBselect($sql, $sqlLimit); while ($item = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $item; } else { $result = $item['rowscount']; } } else { $itemids[$item['itemid']] = $item['itemid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$item['itemid']] = array('itemid' => $item['itemid']); } else { if (!isset($result[$item['itemid']])) { $result[$item['itemid']] = array(); } if (!is_null($options['selectHosts']) && !isset($result[$item['itemid']]['hosts'])) { $result[$item['itemid']]['hosts'] = array(); } if (!is_null($options['selectApplications']) && !isset($result[$item['itemid']]['applications'])) { $result[$item['itemid']]['applications'] = array(); } if (!is_null($options['selectTriggers']) && !isset($result[$item['itemid']]['triggers'])) { $result[$item['itemid']]['triggers'] = array(); } if (!is_null($options['selectGraphs']) && !isset($result[$item['itemid']]['graphs'])) { $result[$item['itemid']]['graphs'] = array(); } // hostids if (isset($item['hostid']) && is_null($options['selectHosts'])) { if (!isset($result[$item['itemid']]['hosts'])) { $result[$item['itemid']]['hosts'] = array(); } $result[$item['itemid']]['hosts'][] = array('hostid' => $item['hostid']); } // triggerids if (isset($item['triggerid']) && is_null($options['selectTriggers'])) { if (!isset($result[$item['itemid']]['triggers'])) { $result[$item['itemid']]['triggers'] = array(); } $result[$item['itemid']]['triggers'][] = array('triggerid' => $item['triggerid']); unset($item['triggerid']); } // graphids if (isset($item['graphid']) && is_null($options['selectGraphs'])) { if (!isset($result[$item['itemid']]['graphs'])) { $result[$item['itemid']]['graphs'] = array(); } $result[$item['itemid']]['graphs'][] = array('graphid' => $item['graphid']); unset($item['graphid']); } // discoveryids if (isset($item['discoveryids'])) { if (!isset($result[$item['itemid']]['discovery'])) { $result[$item['itemid']]['discovery'] = array(); } $result[$item['itemid']]['discovery'][] = array('ruleid' => $item['item_parentid']); unset($item['item_parentid']); } $result[$item['itemid']] += $item; } } } if (!is_null($options['countOutput'])) { return $result; } // Adding Objects // Adding hosts if (!is_null($options['selectHosts'])) { if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) { $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'templated_hosts' => 1, 'output' => $options['selectHosts'], 'nopermissions' => 1, 'preservekeys' => 1); $hosts = API::Host()->get($objParams); foreach ($hosts as $host) { $hitems = $host['items']; unset($host['items']); foreach ($hitems as $inum => $item) { $result[$item['itemid']]['hosts'][] = $host; } } $templates = API::Template()->get($objParams); foreach ($templates as $template) { $titems = $template['items']; unset($template['items']); foreach ($titems as $inum => $item) { $result[$item['itemid']]['hosts'][] = $template; } } } } // Adding triggers if (!is_null($options['selectTriggers'])) { $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'preservekeys' => true); if (in_array($options['selectTriggers'], $subselectsAllowedOutputs)) { $objParams['output'] = $options['selectTriggers']; $triggers = API::TriggerPrototype()->get($objParams); if (!is_null($options['limitSelects'])) { order_result($triggers, 'description'); } $count = array(); foreach ($triggers as $triggerid => $trigger) { unset($triggers[$triggerid]['items']); foreach ($trigger['items'] as $item) { if (!is_null($options['limitSelects'])) { if (!isset($count[$item['itemid']])) { $count[$item['itemid']] = 0; } $count[$item['itemid']]++; if ($count[$item['itemid']] > $options['limitSelects']) { continue; } } $result[$item['itemid']]['triggers'][] =& $triggers[$triggerid]; } } } elseif (API_OUTPUT_COUNT == $options['selectTriggers']) { $objParams['countOutput'] = 1; $objParams['groupCount'] = 1; $triggers = API::TriggerPrototype()->get($objParams); $triggers = zbx_toHash($triggers, 'parent_itemid'); foreach ($result as $itemid => $item) { if (isset($triggers[$itemid])) { $result[$itemid]['triggers'] = $triggers[$itemid]['rowscount']; } else { $result[$itemid]['triggers'] = 0; } } } } // Adding applications if (!is_null($options['selectApplications']) && str_in_array($options['selectApplications'], $subselectsAllowedOutputs)) { $objParams = array('nodeids' => $nodeids, 'output' => $options['selectApplications'], 'itemids' => $itemids, 'preservekeys' => 1); $applications = API::Application()->get($objParams); foreach ($applications as $applicationid => $application) { $aitems = $application['items']; unset($application['items']); foreach ($aitems as $inum => $item) { $result[$item['itemid']]['applications'][] = $application; } } } // Adding graphs if (!is_null($options['selectGraphs'])) { $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'preservekeys' => true); if (in_array($options['selectGraphs'], $subselectsAllowedOutputs)) { $objParams['output'] = $options['selectGraphs']; $graphs = API::GraphPrototype()->get($objParams); if (!is_null($options['limitSelects'])) { order_result($graphs, 'name'); } $count = array(); foreach ($graphs as $graphid => $graph) { unset($graphs[$graphid]['items']); foreach ($graph['items'] as $item) { if (!is_null($options['limitSelects'])) { if (!isset($count[$item['itemid']])) { $count[$item['itemid']] = 0; } $count[$item['itemid']]++; if ($count[$item['itemid']] > $options['limitSelects']) { continue; } } $result[$item['itemid']]['graphs'][] =& $graphs[$graphid]; } } } elseif (API_OUTPUT_COUNT == $options['selectGraphs']) { $objParams['countOutput'] = 1; $objParams['groupCount'] = 1; $graphs = API::GraphPrototype()->get($objParams); $graphs = zbx_toHash($graphs, 'parent_itemid'); foreach ($result as $itemid => $item) { if (isset($graphs[$itemid])) { $result[$itemid]['graphs'] = $graphs[$itemid]['rowscount']; } else { $result[$itemid]['graphs'] = 0; } } } } if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
access_deny(); } } /* * Filter */ if (isset($_REQUEST['favobj'])) { if ($_REQUEST['favobj'] == 'filter') { CProfile::update('web.screens.filter.state', $_REQUEST['favstate'], PROFILE_TYPE_INT); } if ($_REQUEST['favobj'] == 'timeline') { if (isset($_REQUEST['elementid']) && isset($_REQUEST['period'])) { navigation_bar_calc('web.screens', $_REQUEST['elementid'], true); } } if (str_in_array($_REQUEST['favobj'], array('screenid', 'slideshowid'))) { $result = false; if ($_REQUEST['favaction'] == 'add') { $result = CFavorite::add('web.favorite.screenids', $_REQUEST['favid'], $_REQUEST['favobj']); if ($result) { echo '$("addrm_fav").title = "' . _('Remove from favourites') . '";' . "\n" . '$("addrm_fav").onclick = function() { rm4favorites("' . $_REQUEST['favobj'] . '", "' . $_REQUEST['favid'] . '", 0); }' . "\n"; } } elseif ($_REQUEST['favaction'] == 'remove') { $result = CFavorite::remove('web.favorite.screenids', $_REQUEST['favid'], $_REQUEST['favobj']); if ($result) { echo '$("addrm_fav").title = "' . _('Add to favourites') . '";' . "\n" . '$("addrm_fav").onclick = function() { add2favorites("' . $_REQUEST['favobj'] . '", "' . $_REQUEST['favid'] . '"); }' . "\n"; } } if ($page['type'] == PAGE_TYPE_JS && $result) { echo 'switchElementsClass("addrm_fav", "iconminus", "iconplus");'; }
} else { $result = true; } } } } else { $result = true; } } } } } } if ($result) { if (!isset($new_timeperiod['id'])) { if (!str_in_array($new_timeperiod, $_REQUEST['timeperiods'])) { array_push($_REQUEST['timeperiods'], $new_timeperiod); } } else { $id = $new_timeperiod['id']; unset($new_timeperiod['id']); $_REQUEST['timeperiods'][$id] = $new_timeperiod; } unset($_REQUEST['new_timeperiod']); } } else { if (inarr_isset(array('del_timeperiod', 'g_timeperiodid'))) { $_REQUEST['timeperiods'] = get_request('timeperiods', array()); foreach ($_REQUEST['g_timeperiodid'] as $val) { unset($_REQUEST['timeperiods'][$val]); }
$items = array(); $used_hosts = array(); foreach ($trigger['hosts'] as $th) { $used_hosts[$th['hostid']] = $th['host']; } $used_host_count = count($used_hosts); foreach ($trigger['items'] as $inum => $item) { $item_description = item_description($item); //if we have items from different hosts, we must prefix a host name if ($used_host_count > 1) { $item_description = $used_hosts[$item['hostid']] . ':' . $item_description; } $items[$inum]['itemid'] = $item['itemid']; $items[$inum]['value_type'] = $item['value_type']; //ZBX-3059: So it would be possible to show different caption for history for chars and numbers (KB) $items[$inum]['action'] = str_in_array($item['value_type'], array(ITEM_VALUE_TYPE_FLOAT, ITEM_VALUE_TYPE_UINT64)) ? 'showgraph' : 'showvalues'; $items[$inum]['description'] = $item_description; } $trigger['items'] = $items; //---- $description = new CSpan($description, 'link_menu'); // trigger description js menu {{{ $hosts = reset($trigger['hosts']); $menu_trigger_conf = 'null'; if ($admin_links) { $menu_trigger_conf = "['" . S_CONFIGURATION_OF_TRIGGERS . "',\"javascript:\n\t\t\t\tredirect('triggers.php?form=update&triggerid=" . $trigger['triggerid'] . '&switch_node=' . id2nodeid($trigger['triggerid']) . "')\",\n\t\t\t\tnull, {'outer' : ['pum_o_item'],'inner' : ['pum_i_item']}]"; } $menu_trigger_url = 'null'; if (!zbx_empty($trigger['url'])) { $menu_trigger_url = "['" . S_URL . "',\"javascript: window.location.href='" . $trigger['url'] . "'\",\n\t\t\t\tnull, {'outer' : ['pum_o_item'],'inner' : ['pum_i_item']}]"; }
private function getUserData($user) { if (!$this->connect()) { return false; } // force superuser bind if wanted and not bound as superuser yet if (!empty($this->cnf['bind_dn']) && !empty($this->cnf['bind_password']) && $this->bound < 2) { if (!ldap_bind($this->ds, $this->cnf['bind_dn'], $this->cnf['bind_password'])) { return false; } $this->bound = 2; } // with no superuser creds we continue as user or anonymous here $info['user'] = $user; $info['host'] = $this->cnf['host']; // get info for given user $base = $this->makeFilter($this->cnf['base_dn'], $info); if (isset($this->cnf['userfilter']) && !empty($this->cnf['userfilter'])) { $filter = $this->makeFilter($this->cnf['userfilter'], $info); } else { $filter = '(ObjectClass=*)'; } $sr = ldap_search($this->ds, $base, $filter); $result = ldap_get_entries($this->ds, $sr); // don't accept more or less than one response if ($result['count'] != 1) { error('LDAP: User not found.'); return false; } $user_result = $result[0]; ldap_free_result($sr); // general user info $info['dn'] = $user_result['dn']; $info['name'] = $user_result['cn'][0]; $info['grps'] = array(); // overwrite if other attribs are specified. if (is_array($this->cnf['mapping'])) { foreach ($this->cnf['mapping'] as $localkey => $key) { $info[$localkey] = isset($user_result[$key]) ? $user_result[$key][0] : null; } } $user_result = zbx_array_merge($info, $user_result); // get groups for given user if grouptree is given if (isset($this->cnf['grouptree']) && isset($this->cnf['groupfilter'])) { $base = $this->makeFilter($this->cnf['grouptree'], $user_result); $filter = $this->makeFilter($this->cnf['groupfilter'], $user_result); $sr = ldap_search($this->ds, $base, $filter, array($this->cnf['groupkey'])); if (!$sr) { error('LDAP: Reading group memberships failed.'); return false; } $result = ldap_get_entries($this->ds, $sr); foreach ($result as $grp) { if (!empty($grp[$this->cnf['groupkey']][0])) { $info['grps'][] = $grp[$this->cnf['groupkey']][0]; } } } // always add the default group to the list of groups if (isset($conf['defaultgroup']) && !str_in_array($conf['defaultgroup'], $info['grps'])) { $info['grps'][] = $conf['defaultgroup']; } return $info; }
$item['snmpv3_privprotocol'] = ITEM_PRIVPROTOCOL_DES; } $item = CArrayHelper::unsetEqualValues($item, $dbItem); $item['itemid'] = $itemId; $result = API::DiscoveryRule()->update($item); $result = DBend($result); show_messages($result, _('Discovery rule updated'), _('Cannot update discovery rule')); } else { $result = API::DiscoveryRule()->create(array($item)); show_messages($result, _('Discovery rule created'), _('Cannot add discovery rule')); } if ($result) { unset($_REQUEST['itemid'], $_REQUEST['form']); clearCookies($result, $_REQUEST['hostid']); } } elseif (str_in_array(getRequest('go'), array('activate', 'disable')) && hasRequest('g_hostdruleid')) { $groupHostDiscoveryRuleId = getRequest('g_hostdruleid'); $enable = getRequest('go') == 'activate'; DBstart(); $result = $enable ? activate_item($groupHostDiscoveryRuleId) : disable_item($groupHostDiscoveryRuleId); $result = DBend($result); $updated = count($groupHostDiscoveryRuleId); $messageSuccess = $enable ? _n('Discovery rule enabled', 'Discovery rules enabled', $updated) : _n('Discovery rule disabled', 'Discovery rules disabled', $updated); $messageFailed = $enable ? _n('Cannot enable discovery rules', 'Cannot enable discovery rules', $updated) : _n('Cannot disable discovery rules', 'Cannot disable discovery rules', $updated); show_messages($result, $messageSuccess, $messageFailed); clearCookies($result, getRequest('hostid')); } elseif ($_REQUEST['go'] == 'delete' && isset($_REQUEST['g_hostdruleid'])) { $goResult = API::DiscoveryRule()->delete($_REQUEST['g_hostdruleid']); show_messages($goResult, _('Discovery rules deleted'), _('Cannot delete discovery rules')); clearCookies($goResult, $_REQUEST['hostid']); }
function getItemFilterForm(&$items) { $filter_groupId = $_REQUEST['filter_groupid']; $filter_hostId = $_REQUEST['filter_hostid']; $filter_application = $_REQUEST['filter_application']; $filter_name = $_REQUEST['filter_name']; $filter_type = $_REQUEST['filter_type']; $filter_key = $_REQUEST['filter_key']; $filter_snmp_community = $_REQUEST['filter_snmp_community']; $filter_snmpv3_securityname = $_REQUEST['filter_snmpv3_securityname']; $filter_snmp_oid = $_REQUEST['filter_snmp_oid']; $filter_port = $_REQUEST['filter_port']; $filter_value_type = $_REQUEST['filter_value_type']; $filter_data_type = $_REQUEST['filter_data_type']; $filter_delay = $_REQUEST['filter_delay']; $filter_history = $_REQUEST['filter_history']; $filter_trends = $_REQUEST['filter_trends']; $filter_status = $_REQUEST['filter_status']; $filter_state = $_REQUEST['filter_state']; $filter_templated_items = $_REQUEST['filter_templated_items']; $filter_with_triggers = $_REQUEST['filter_with_triggers']; $subfilter_hosts = $_REQUEST['subfilter_hosts']; $subfilter_apps = $_REQUEST['subfilter_apps']; $subfilter_types = $_REQUEST['subfilter_types']; $subfilter_value_types = $_REQUEST['subfilter_value_types']; $subfilter_status = $_REQUEST['subfilter_status']; $subfilter_state = $_REQUEST['subfilter_state']; $subfilter_templated_items = $_REQUEST['subfilter_templated_items']; $subfilter_with_triggers = $_REQUEST['subfilter_with_triggers']; $subfilter_history = $_REQUEST['subfilter_history']; $subfilter_trends = $_REQUEST['subfilter_trends']; $subfilter_interval = $_REQUEST['subfilter_interval']; $form = new CForm('get'); $form->setAttribute('name', 'zbx_filter'); $form->setAttribute('id', 'zbx_filter'); $form->addVar('subfilter_hosts', $subfilter_hosts); $form->addVar('subfilter_apps', $subfilter_apps); $form->addVar('subfilter_types', $subfilter_types); $form->addVar('subfilter_value_types', $subfilter_value_types); $form->addVar('subfilter_status', $subfilter_status); $form->addVar('subfilter_state', $subfilter_state); $form->addVar('subfilter_templated_items', $subfilter_templated_items); $form->addVar('subfilter_with_triggers', $subfilter_with_triggers); $form->addVar('subfilter_history', $subfilter_history); $form->addVar('subfilter_trends', $subfilter_trends); $form->addVar('subfilter_interval', $subfilter_interval); // type select $fTypeVisibility = array(); $cmbType = new CComboBox('filter_type', $filter_type); $cmbType->setAttribute('id', 'filter_type'); $cmbType->addItem(-1, _('all')); foreach (array('filter_delay_label', 'filter_delay') as $vItem) { zbx_subarray_push($fTypeVisibility, -1, $vItem); } $itemTypes = item_type2str(); unset($itemTypes[ITEM_TYPE_HTTPTEST]); // httptest items are only for internal zabbix logic $cmbType->addItems($itemTypes); foreach ($itemTypes as $typeNum => $typeLabel) { if ($typeNum != ITEM_TYPE_TRAPPER) { zbx_subarray_push($fTypeVisibility, $typeNum, 'filter_delay_label'); zbx_subarray_push($fTypeVisibility, $typeNum, 'filter_delay'); } switch ($typeNum) { case ITEM_TYPE_SNMPV1: case ITEM_TYPE_SNMPV2C: $snmp_types = array('filter_snmp_community_label', 'filter_snmp_community', 'filter_snmp_oid_label', 'filter_snmp_oid', 'filter_port_label', 'filter_port'); foreach ($snmp_types as $vItem) { zbx_subarray_push($fTypeVisibility, $typeNum, $vItem); } break; case ITEM_TYPE_SNMPV3: foreach (array('filter_snmpv3_securityname_label', 'filter_snmpv3_securityname', 'filter_snmp_oid_label', 'filter_snmp_oid', 'filter_port_label', 'filter_port') as $vItem) { zbx_subarray_push($fTypeVisibility, $typeNum, $vItem); } break; } } zbx_add_post_js("var filterTypeSwitcher = new CViewSwitcher('filter_type', 'change', " . zbx_jsvalue($fTypeVisibility, true) . ');'); // type of information select $fVTypeVisibility = array(); $cmbValType = new CComboBox('filter_value_type', $filter_value_type); $cmbValType->addItem(-1, _('all')); $cmbValType->addItem(ITEM_VALUE_TYPE_UINT64, _('Numeric (unsigned)')); $cmbValType->addItem(ITEM_VALUE_TYPE_FLOAT, _('Numeric (float)')); $cmbValType->addItem(ITEM_VALUE_TYPE_STR, _('Character')); $cmbValType->addItem(ITEM_VALUE_TYPE_LOG, _('Log')); $cmbValType->addItem(ITEM_VALUE_TYPE_TEXT, _('Text')); foreach (array('filter_data_type_label', 'filter_data_type') as $vItem) { zbx_subarray_push($fVTypeVisibility, ITEM_VALUE_TYPE_UINT64, $vItem); } zbx_add_post_js("var filterValueTypeSwitcher = new CViewSwitcher('filter_value_type', 'change', " . zbx_jsvalue($fVTypeVisibility, true) . ');'); // status select $cmbStatus = new CComboBox('filter_status', $filter_status); $cmbStatus->addItem(-1, _('all')); foreach (array(ITEM_STATUS_ACTIVE, ITEM_STATUS_DISABLED) as $status) { $cmbStatus->addItem($status, item_status2str($status)); } // state select $cmbState = new CComboBox('filter_state', $filter_state); $cmbState->addItem(-1, _('all')); foreach (array(ITEM_STATE_NORMAL, ITEM_STATE_NOTSUPPORTED) as $state) { $cmbState->addItem($state, itemState($state)); } // update interval $updateIntervalLabel = new CSpan(array(bold(_('Update interval')), SPACE . _('(in sec)') . NAME_DELIMITER)); $updateIntervalLabel->setAttribute('id', 'filter_delay_label'); $updateIntervalInput = new CNumericBox('filter_delay', $filter_delay, 5, false, true); $updateIntervalInput->setEnabled('no'); // data type $dataTypeLabel = new CSpan(bold(_('Data type') . NAME_DELIMITER)); $dataTypeLabel->setAttribute('id', 'filter_data_type_label'); $dataTypeInput = new CComboBox('filter_data_type', $filter_data_type); $dataTypeInput->addItem(-1, _('all')); $dataTypeInput->addItems(item_data_type2str()); $dataTypeInput->setEnabled('no'); // filter table $table = new CTable('', 'filter'); $table->setCellPadding(0); $table->setCellSpacing(0); // SNMP community $snmpCommunityLabel = new CSpan(array(bold(_('SNMP community')), SPACE . _('like') . NAME_DELIMITER)); $snmpCommunityLabel->setAttribute('id', 'filter_snmp_community_label'); $snmpCommunityField = new CTextBox('filter_snmp_community', $filter_snmp_community, ZBX_TEXTBOX_FILTER_SIZE); $snmpCommunityField->setEnabled('no'); // SNMPv3 security name $snmpSecurityLabel = new CSpan(array(bold(_('Security name')), SPACE . _('like') . NAME_DELIMITER)); $snmpSecurityLabel->setAttribute('id', 'filter_snmpv3_securityname_label'); $snmpSecurityField = new CTextBox('filter_snmpv3_securityname', $filter_snmpv3_securityname, ZBX_TEXTBOX_FILTER_SIZE); $snmpSecurityField->setEnabled('no'); // SNMP OID $snmpOidLabel = new CSpan(array(bold(_('SNMP OID')), SPACE . _('like') . NAME_DELIMITER)); $snmpOidLabel->setAttribute('id', 'filter_snmp_oid_label'); $snmpOidField = new CTextBox('filter_snmp_oid', $filter_snmp_oid, ZBX_TEXTBOX_FILTER_SIZE); $snmpOidField->setEnabled('no'); // port $portLabel = new CSpan(array(bold(_('Port')), SPACE . _('like') . NAME_DELIMITER)); $portLabel->setAttribute('id', 'filter_port_label'); $portField = new CNumericBox('filter_port', $filter_port, 5, false, true); $portField->setEnabled('no'); // row 1 $groupFilter = null; if (!empty($filter_groupId)) { $getHostInfo = API::HostGroup()->get(array('groupids' => $filter_groupId, 'output' => array('name'))); $getHostInfo = reset($getHostInfo); if (!empty($getHostInfo)) { $groupFilter[] = array('id' => $getHostInfo['groupid'], 'name' => $getHostInfo['name']); } } $table->addRow(array(new CCol(bold(_('Host group') . NAME_DELIMITER), 'label col1'), new CCol(array(new CMultiSelect(array('name' => 'filter_groupid', 'selectedLimit' => 1, 'objectName' => 'hostGroup', 'objectOptions' => array('editable' => true), 'data' => $groupFilter, 'popup' => array('parameters' => 'srctbl=host_groups&dstfrm=' . $form->getName() . '&dstfld1=filter_groupid' . '&srcfld1=groupid&writeonly=1', 'width' => 450, 'height' => 450, 'buttonClass' => 'input filter-multiselect-select-button')))), 'col1'), new CCol(bold(_('Type') . NAME_DELIMITER), 'label col2'), new CCol($cmbType, 'col2'), new CCol(bold(_('Type of information') . NAME_DELIMITER), 'label col3'), new CCol($cmbValType, 'col3'), new CCol(bold(_('State') . NAME_DELIMITER), 'label'), new CCol($cmbState, 'col4')), 'item-list-row'); // row 2 $hostFilterData = null; if (!empty($filter_hostId)) { $getHostInfo = API::Host()->get(array('hostids' => $filter_hostId, 'templated_hosts' => true, 'output' => array('name'))); $getHostInfo = reset($getHostInfo); if (!empty($getHostInfo)) { $hostFilterData[] = array('id' => $getHostInfo['hostid'], 'name' => $getHostInfo['name']); } } $table->addRow(array(new CCol(bold(_('Host') . NAME_DELIMITER), 'label'), new CCol(array(new CMultiSelect(array('name' => 'filter_hostid', 'selectedLimit' => 1, 'objectName' => 'hosts', 'objectOptions' => array('editable' => true, 'templated_hosts' => true), 'data' => $hostFilterData, 'popup' => array('parameters' => 'srctbl=host_templates&dstfrm=' . $form->getName() . '&dstfld1=filter_hostid' . '&srcfld1=hostid&writeonly=1', 'width' => 450, 'height' => 450, 'buttonClass' => 'input filter-multiselect-select-button')))), 'col1'), new CCol($updateIntervalLabel, 'label'), new CCol($updateIntervalInput), new CCol($dataTypeLabel, 'label'), new CCol($dataTypeInput), new CCol(bold(_('Status') . NAME_DELIMITER), 'label col4'), new CCol($cmbStatus, 'col4')), 'item-list-row'); // row 3 $table->addRow(array(new CCol(bold(_('Application') . NAME_DELIMITER), 'label'), new CCol(array(new CTextBox('filter_application', $filter_application, ZBX_TEXTBOX_FILTER_SIZE), new CButton('btn_app', _('Select'), 'return PopUp("popup.php?srctbl=applications&srcfld1=name' . '&dstfrm=' . $form->getName() . '&dstfld1=filter_application' . '&with_applications=1' . '" + (jQuery("input[name=\'filter_hostid\']").length > 0 ? "&hostid="+jQuery("input[name=\'filter_hostid\']").val() : "")' . ', 550, 450, "application");', 'filter-select-button')), 'col1'), new CCol(array($snmpCommunityLabel, $snmpSecurityLabel), 'label'), new CCol(array($snmpCommunityField, $snmpSecurityField)), new CCol(array(bold(_('History')), SPACE . _('(in days)') . NAME_DELIMITER), 'label'), new CCol(new CNumericBox('filter_history', $filter_history, 8, false, true)), new CCol(bold(_('Triggers') . NAME_DELIMITER), 'label'), new CCol(new CComboBox('filter_with_triggers', $filter_with_triggers, null, array(-1 => _('all'), 1 => _('With triggers'), 0 => _('Without triggers'))))), 'item-list-row'); // row 4 $table->addRow(array(new CCol(array(bold(_('Name')), SPACE . _('like') . NAME_DELIMITER), 'label'), new CCol(new CTextBox('filter_name', $filter_name, ZBX_TEXTBOX_FILTER_SIZE), 'col1'), new CCol($snmpOidLabel, 'label'), new CCol($snmpOidField), new CCol(array(bold(_('Trends')), SPACE . _('(in days)') . NAME_DELIMITER), 'label'), new CCol(new CNumericBox('filter_trends', $filter_trends, 8, false, true)), new CCol(bold(_('Template') . NAME_DELIMITER), 'label'), new CCol(new CComboBox('filter_templated_items', $filter_templated_items, null, array(-1 => _('all'), 1 => _('Templated items'), 0 => _('Not Templated items'))))), 'item-list-row'); // row 5 $table->addRow(array(new CCol(array(bold(_('Key')), SPACE . _('like') . NAME_DELIMITER), 'label'), new CCol(new CTextBox('filter_key', $filter_key, ZBX_TEXTBOX_FILTER_SIZE), 'col1'), new CCol($portLabel, 'label'), new CCol($portField), new CCol(null, 'label'), new CCol(), new CCol(null, 'label'), new CCol()), 'item-list-row'); $filter = new CSubmit('filter_set', _('Filter'), 'chkbxRange.clearSelectedOnFilterChange();'); $filter->useJQueryStyle('main'); $reset = new CSubmit('filter_rst', _('Reset'), 'chkbxRange.clearSelectedOnFilterChange();'); $reset->useJQueryStyle(); $div_buttons = new CDiv(array($filter, SPACE, $reset)); $div_buttons->setAttribute('style', 'padding: 4px 0px;'); $footer = new CCol($div_buttons, 'controls', 8); $table->addRow($footer); $form->addItem($table); // subfilters $table_subfilter = new CTable(null, 'filter sub-filter'); // array contains subfilters and number of items in each $item_params = array('hosts' => array(), 'applications' => array(), 'types' => array(), 'value_types' => array(), 'status' => array(), 'state' => array(), 'templated_items' => array(), 'with_triggers' => array(), 'history' => array(), 'trends' => array(), 'interval' => array()); // generate array with values for subfilters of selected items foreach ($items as $item) { // hosts if (zbx_empty($filter_hostId)) { $host = reset($item['hosts']); if (!isset($item_params['hosts'][$host['hostid']])) { $item_params['hosts'][$host['hostid']] = array('name' => $host['name'], 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_hosts') { continue; } $show_item &= $value; } if ($show_item) { $host = reset($item['hosts']); $item_params['hosts'][$host['hostid']]['count']++; } } // applications if (!empty($item['applications'])) { foreach ($item['applications'] as $application) { if (!isset($item_params['applications'][$application['name']])) { $item_params['applications'][$application['name']] = array('name' => $application['name'], 'count' => 0); } } } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_apps') { continue; } $show_item &= $value; } $sel_app = false; if ($show_item) { // if any of item applications are selected foreach ($item['applications'] as $app) { if (str_in_array($app['name'], $subfilter_apps)) { $sel_app = true; break; } } foreach ($item['applications'] as $app) { if (str_in_array($app['name'], $subfilter_apps) || !$sel_app) { $item_params['applications'][$app['name']]['count']++; } } } // types if ($filter_type == -1) { if (!isset($item_params['types'][$item['type']])) { $item_params['types'][$item['type']] = array('name' => item_type2str($item['type']), 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_types') { continue; } $show_item &= $value; } if ($show_item) { $item_params['types'][$item['type']]['count']++; } } // value types if ($filter_value_type == -1) { if (!isset($item_params['value_types'][$item['value_type']])) { $item_params['value_types'][$item['value_type']] = array('name' => itemValueTypeString($item['value_type']), 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_value_types') { continue; } $show_item &= $value; } if ($show_item) { $item_params['value_types'][$item['value_type']]['count']++; } } // status if ($filter_status == -1) { if (!isset($item_params['status'][$item['status']])) { $item_params['status'][$item['status']] = array('name' => item_status2str($item['status']), 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_status') { continue; } $show_item &= $value; } if ($show_item) { $item_params['status'][$item['status']]['count']++; } } // state if ($filter_state == -1) { if (!isset($item_params['state'][$item['state']])) { $item_params['state'][$item['state']] = array('name' => itemState($item['state']), 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_state') { continue; } $show_item &= $value; } if ($show_item) { $item_params['state'][$item['state']]['count']++; } } // template if ($filter_templated_items == -1) { if ($item['templateid'] == 0 && !isset($item_params['templated_items'][0])) { $item_params['templated_items'][0] = array('name' => _('Not Templated items'), 'count' => 0); } elseif ($item['templateid'] > 0 && !isset($item_params['templated_items'][1])) { $item_params['templated_items'][1] = array('name' => _('Templated items'), 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_templated_items') { continue; } $show_item &= $value; } if ($show_item) { if ($item['templateid'] == 0) { $item_params['templated_items'][0]['count']++; } else { $item_params['templated_items'][1]['count']++; } } } // with triggers if ($filter_with_triggers == -1) { if (count($item['triggers']) == 0 && !isset($item_params['with_triggers'][0])) { $item_params['with_triggers'][0] = array('name' => _('Without triggers'), 'count' => 0); } elseif (count($item['triggers']) > 0 && !isset($item_params['with_triggers'][1])) { $item_params['with_triggers'][1] = array('name' => _('With triggers'), 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_with_triggers') { continue; } $show_item &= $value; } if ($show_item) { if (count($item['triggers']) == 0) { $item_params['with_triggers'][0]['count']++; } else { $item_params['with_triggers'][1]['count']++; } } } // trends if (zbx_empty($filter_trends)) { if (!isset($item_params['trends'][$item['trends']])) { $item_params['trends'][$item['trends']] = array('name' => $item['trends'], 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_trends') { continue; } $show_item &= $value; } if ($show_item) { $item_params['trends'][$item['trends']]['count']++; } } // history if (zbx_empty($filter_history)) { if (!isset($item_params['history'][$item['history']])) { $item_params['history'][$item['history']] = array('name' => $item['history'], 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_history') { continue; } $show_item &= $value; } if ($show_item) { $item_params['history'][$item['history']]['count']++; } } // interval if (zbx_empty($filter_delay) && $filter_type != ITEM_TYPE_TRAPPER) { if (!isset($item_params['interval'][$item['delay']])) { $item_params['interval'][$item['delay']] = array('name' => $item['delay'], 'count' => 0); } $show_item = true; foreach ($item['subfilters'] as $name => $value) { if ($name == 'subfilter_interval') { continue; } $show_item &= $value; } if ($show_item) { $item_params['interval'][$item['delay']]['count']++; } } } // output if (zbx_empty($filter_hostId) && count($item_params['hosts']) > 1) { $hosts_output = prepareSubfilterOutput($item_params['hosts'], $subfilter_hosts, 'subfilter_hosts'); $table_subfilter->addRow(array(_('Hosts'), $hosts_output)); } if (!empty($item_params['applications']) && count($item_params['applications']) > 1) { $application_output = prepareSubfilterOutput($item_params['applications'], $subfilter_apps, 'subfilter_apps'); $table_subfilter->addRow(array(_('Applications'), $application_output)); } if ($filter_type == -1 && count($item_params['types']) > 1) { $type_output = prepareSubfilterOutput($item_params['types'], $subfilter_types, 'subfilter_types'); $table_subfilter->addRow(array(_('Types'), $type_output)); } if ($filter_value_type == -1 && count($item_params['value_types']) > 1) { $value_types_output = prepareSubfilterOutput($item_params['value_types'], $subfilter_value_types, 'subfilter_value_types'); $table_subfilter->addRow(array(_('Type of information'), $value_types_output)); } if ($filter_status == -1 && count($item_params['status']) > 1) { $status_output = prepareSubfilterOutput($item_params['status'], $subfilter_status, 'subfilter_status'); $table_subfilter->addRow(array(_('Status'), $status_output)); } if ($filter_state == -1 && count($item_params['state']) > 1) { $state_output = prepareSubfilterOutput($item_params['state'], $subfilter_state, 'subfilter_state'); $table_subfilter->addRow(array(_('State'), $state_output)); } if ($filter_templated_items == -1 && count($item_params['templated_items']) > 1) { $templated_items_output = prepareSubfilterOutput($item_params['templated_items'], $subfilter_templated_items, 'subfilter_templated_items'); $table_subfilter->addRow(array(_('Template'), $templated_items_output)); } if ($filter_with_triggers == -1 && count($item_params['with_triggers']) > 1) { $with_triggers_output = prepareSubfilterOutput($item_params['with_triggers'], $subfilter_with_triggers, 'subfilter_with_triggers'); $table_subfilter->addRow(array(_('With triggers'), $with_triggers_output)); } if (zbx_empty($filter_history) && count($item_params['history']) > 1) { $history_output = prepareSubfilterOutput($item_params['history'], $subfilter_history, 'subfilter_history'); $table_subfilter->addRow(array(_('History'), $history_output)); } if (zbx_empty($filter_trends) && count($item_params['trends']) > 1) { $trends_output = prepareSubfilterOutput($item_params['trends'], $subfilter_trends, 'subfilter_trends'); $table_subfilter->addRow(array(_('Trends'), $trends_output)); } if (zbx_empty($filter_delay) && $filter_type != ITEM_TYPE_TRAPPER && count($item_params['interval']) > 1) { $interval_output = prepareSubfilterOutput($item_params['interval'], $subfilter_interval, 'subfilter_interval'); $table_subfilter->addRow(array(_('Interval'), $interval_output)); } $form->addItem(new CDiv(_('Subfilter [affects only filtered data!]'), 'thin_header')); $form->addItem($table_subfilter); return $form; }
/** * Get Host data * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param _array $options * @param array $options['nodeids'] Node IDs * @param array $options['groupids'] HostGroup IDs * @param array $options['hostids'] Host IDs * @param boolean $options['monitored_hosts'] only monitored Hosts * @param boolean $options['templated_hosts'] include templates in result * @param boolean $options['with_items'] only with items * @param boolean $options['with_monitored_items'] only with monitored items * @param boolean $options['with_historical_items'] only with historical items * @param boolean $options['with_triggers'] only with triggers * @param boolean $options['with_monitored_triggers'] only with monitored triggers * @param boolean $options['with_httptests'] only with http tests * @param boolean $options['with_monitored_httptests'] only with monitored http tests * @param boolean $options['with_graphs'] only with graphs * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins * @param int $options['extendoutput'] return all fields for Hosts * @param boolean $options['select_groups'] select HostGroups * @param boolean $options['select_templates'] select Templates * @param boolean $options['select_items'] select Items * @param boolean $options['select_triggers'] select Triggers * @param boolean $options['select_graphs'] select Graphs * @param boolean $options['select_applications'] select Applications * @param boolean $options['select_macros'] select Macros * @param boolean $options['select_profile'] select Profile * @param int $options['count'] count Hosts, returned column name is rowscount * @param string $options['pattern'] search hosts by pattern in Host name * @param string $options['extendPattern'] search hosts by pattern in Host name, ip and DNS * @param int $options['limit'] limit selection * @param string $options['sortfield'] field to sort by * @param string $options['sortorder'] sort order * @return array|boolean Host data as array or false if error */ public static function get($options = array()) { global $USER_DETAILS; $result = array(); $nodeCheck = false; $user_type = $USER_DETAILS['type']; $userid = $USER_DETAILS['userid']; $sort_columns = array('hostid', 'host', 'status', 'dns', 'ip'); // allowed columns for sorting $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM); // allowed output options for [ select_* ] params $sql_parts = array('select' => array('hosts' => 'h.hostid'), 'from' => array('hosts' => 'hosts h'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'proxyids' => null, 'templateids' => null, 'itemids' => null, 'triggerids' => null, 'maintenanceids' => null, 'graphids' => null, 'dhostids' => null, 'dserviceids' => null, 'monitored_hosts' => null, 'templated_hosts' => null, 'proxy_hosts' => null, 'with_items' => null, 'with_monitored_items' => null, 'with_historical_items' => null, 'with_triggers' => null, 'with_monitored_triggers' => null, 'with_httptests' => null, 'with_monitored_httptests' => null, 'with_graphs' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'selectParentTemplates' => null, 'select_items' => null, 'select_triggers' => null, 'select_graphs' => null, 'select_dhosts' => null, 'select_dservices' => null, 'select_applications' => null, 'select_macros' => null, 'select_profile' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null); $options = zbx_array_merge($def_options, $options); if (!is_null($options['extendoutput'])) { $options['output'] = API_OUTPUT_EXTEND; if (!is_null($options['select_groups'])) { $options['select_groups'] = API_OUTPUT_EXTEND; } if (!is_null($options['selectParentTemplates'])) { $options['selectParentTemplates'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_items'])) { $options['select_items'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_triggers'])) { $options['select_triggers'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_graphs'])) { $options['select_graphs'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_applications'])) { $options['select_applications'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_macros'])) { $options['select_macros'] = API_OUTPUT_EXTEND; } } if (is_array($options['output'])) { unset($sql_parts['select']['hosts']); $sql_parts['select']['hostid'] = ' h.hostid'; foreach ($options['output'] as $key => $field) { $sql_parts['select'][$field] = ' h.' . $field; } $options['output'] = API_OUTPUT_CUSTOM; } // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) { } else { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY; $sql_parts['where'][] = 'EXISTS (' . ' SELECT hh.hostid ' . ' FROM hosts hh, hosts_groups hgg, rights r, users_groups ug ' . ' WHERE hh.hostid=h.hostid ' . ' AND hh.hostid=hgg.hostid ' . ' AND r.id=hgg.groupid ' . ' AND r.groupid=ug.usrgrpid ' . ' AND ug.userid=' . $userid . ' AND r.permission>=' . $permission . ' AND NOT EXISTS( ' . ' SELECT hggg.groupid ' . ' FROM hosts_groups hggg, rights rr, users_groups gg ' . ' WHERE hggg.hostid=hgg.hostid ' . ' AND rr.id=hggg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ' )) '; } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $sql_parts['where']['hostid'] = DBcondition('h.hostid', $options['hostids']); if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('h.hostid', $nodeids); } } // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['groupid'] = 'hg.groupid'; } $sql_parts['from']['hosts_groups'] = 'hosts_groups hg'; $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']); $sql_parts['where']['hgh'] = 'hg.hostid=h.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['groupid'] = 'hg.groupid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('hg.groupid', $nodeids); } } // proxyids if (!is_null($options['proxyids'])) { zbx_value2array($options['proxyids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['proxy_hostid'] = 'h.proxy_hostid'; } $sql_parts['where'][] = DBcondition('h.proxy_hostid', $options['proxyids']); } // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['templateid'] = 'ht.templateid'; } $sql_parts['from']['hosts_templates'] = 'hosts_templates ht'; $sql_parts['where'][] = DBcondition('ht.templateid', $options['templateids']); $sql_parts['where']['hht'] = 'h.hostid=ht.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['templateid'] = 'ht.templateid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('ht.templateid', $nodeids); } } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['itemid'] = 'i.itemid'; } $sql_parts['from']['items'] = 'items i'; $sql_parts['where'][] = DBcondition('i.itemid', $options['itemids']); $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('i.itemid', $nodeids); } } // triggerids if (!is_null($options['triggerids'])) { zbx_value2array($options['triggerids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['triggerid'] = 'f.triggerid'; } $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']); $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('f.triggerid', $nodeids); } } // graphids if (!is_null($options['graphids'])) { zbx_value2array($options['graphids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['graphid'] = 'gi.graphid'; } $sql_parts['from']['graphs_items'] = 'graphs_items gi'; $sql_parts['from']['items'] = 'items i'; $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']); $sql_parts['where']['igi'] = 'i.itemid=gi.itemid'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('gi.graphid', $nodeids); } } // dhostids if (!is_null($options['dhostids'])) { zbx_value2array($options['dhostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['dhostid'] = 'ds.dhostid'; } $sql_parts['from']['dservices'] = 'dservices ds'; $sql_parts['where'][] = DBcondition('ds.dhostid', $options['dhostids']); $sql_parts['where']['dsh'] = 'ds.ip=h.ip'; if (!is_null($options['groupCount'])) { $sql_parts['group']['dhostid'] = 'ds.dhostid'; } } // dserviceids if (!is_null($options['dserviceids'])) { zbx_value2array($options['dserviceids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['dserviceid'] = 'ds.dserviceid'; } $sql_parts['from']['dservices'] = 'dservices ds'; $sql_parts['where'][] = DBcondition('ds.dserviceid', $options['dserviceids']); $sql_parts['where']['dsh'] = 'ds.ip=h.ip'; if (!is_null($options['groupCount'])) { $sql_parts['group']['dserviceid'] = 'ds.dserviceid'; } } // maintenanceids if (!is_null($options['maintenanceids'])) { zbx_value2array($options['maintenanceids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['maintenanceid'] = 'mh.maintenanceid'; } $sql_parts['from']['maintenances_hosts'] = 'maintenances_hosts mh'; $sql_parts['where'][] = DBcondition('mh.maintenanceid', $options['maintenanceids']); $sql_parts['where']['hmh'] = 'h.hostid=mh.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['maintenanceid'] = 'mh.maintenanceid'; } } // node check !!!!! // should last, after all ****IDS checks if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('h.hostid', $nodeids); } // monitored_hosts, templated_hosts if (!is_null($options['monitored_hosts'])) { $sql_parts['where']['status'] = 'h.status=' . HOST_STATUS_MONITORED; } else { if (!is_null($options['templated_hosts'])) { $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ')'; } else { if (!is_null($options['proxy_hosts'])) { $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_PROXY_ACTIVE . ',' . HOST_STATUS_PROXY_PASSIVE . ')'; } else { $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')'; } } } // with_items, with_monitored_items, with_historical_items if (!is_null($options['with_items'])) { $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid )'; } else { if (!is_null($options['with_monitored_items'])) { $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status=' . ITEM_STATUS_ACTIVE . ')'; } else { if (!is_null($options['with_historical_items'])) { $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND (i.status=' . ITEM_STATUS_ACTIVE . ' OR i.status=' . ITEM_STATUS_NOTSUPPORTED . ') AND i.lastvalue IS NOT NULL)'; } } } // with_triggers, with_monitored_triggers if (!is_null($options['with_triggers'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid)'; } else { if (!is_null($options['with_monitored_triggers'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid ' . ' AND t.status=' . TRIGGER_STATUS_ENABLED . ')'; } } // with_httptests, with_monitored_httptests if (!is_null($options['with_httptests'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid)'; } else { if (!is_null($options['with_monitored_httptests'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid ' . ' AND ht.status=' . HTTPTEST_STATUS_ACTIVE . ')'; } } // with_graphs if (!is_null($options['with_graphs'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT DISTINCT i.itemid ' . ' FROM items i, graphs_items gi ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=gi.itemid)'; } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sql_parts['select']['hosts'] = 'h.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('count(DISTINCT h.hostid) as rowscount'); //groupCount if (!is_null($options['groupCount'])) { foreach ($sql_parts['group'] as $key => $fields) { $sql_parts['select'][$key] = $fields; } } } // search if (is_array($options['search'])) { zbx_db_search('hosts h', $options, $sql_parts); } // filter if (is_array($options['filter'])) { zbx_db_filter('hosts h', $options, $sql_parts); } // 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'][$options['sortfield']] = 'h.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('h.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('h.*', $sql_parts['select'])) { $sql_parts['select'][$options['sortfield']] = 'h.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; } //------- $hostids = 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['group'] = array_unique($sql_parts['group']); $sql_parts['order'] = array_unique($sql_parts['order']); $sql_select = ''; $sql_from = ''; $sql_where = ''; $sql_group = ''; $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 .= implode(' AND ', $sql_parts['where']); } if (!empty($sql_parts['group'])) { $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']); } if (!empty($sql_parts['order'])) { $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']); } $sql_limit = $sql_parts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_group . $sql_order; //SDI($sql); $res = DBselect($sql, $sql_limit); while ($host = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $host; } else { $result = $host['rowscount']; } } else { $hostids[$host['hostid']] = $host['hostid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$host['hostid']] = array('hostid' => $host['hostid']); } else { if (!isset($result[$host['hostid']])) { $result[$host['hostid']] = array(); } if (!is_null($options['select_groups']) && !isset($result[$host['hostid']]['groups'])) { $result[$host['hostid']]['groups'] = array(); } if (!is_null($options['selectParentTemplates']) && !isset($result[$host['hostid']]['parentTemplates'])) { $result[$host['hostid']]['parentTemplates'] = array(); } if (!is_null($options['select_items']) && !isset($result[$host['hostid']]['items'])) { $result[$host['hostid']]['items'] = array(); } if (!is_null($options['select_profile']) && !isset($result[$host['hostid']]['profile'])) { $result[$host['hostid']]['profile'] = array(); $result[$host['hostid']]['profile_ext'] = array(); } if (!is_null($options['select_triggers']) && !isset($result[$host['hostid']]['triggers'])) { $result[$host['hostid']]['triggers'] = array(); } if (!is_null($options['select_graphs']) && !isset($result[$host['hostid']]['graphs'])) { $result[$host['hostid']]['graphs'] = array(); } if (!is_null($options['select_dhosts']) && !isset($result[$host['hostid']]['dhosts'])) { $result[$host['hostid']]['dhosts'] = array(); } if (!is_null($options['select_dservices']) && !isset($result[$host['hostid']]['dservices'])) { $result[$host['hostid']]['dservices'] = array(); } if (!is_null($options['select_applications']) && !isset($result[$host['hostid']]['applications'])) { $result[$host['hostid']]['applications'] = array(); } if (!is_null($options['select_macros']) && !isset($result[$host['hostid']]['macros'])) { $result[$host['hostid']]['macros'] = array(); } // if(!is_null($options['select_maintenances']) && !isset($result[$host['hostid']]['maintenances'])){ // $result[$host['hostid']]['maintenances'] = array(); // } // groupids if (isset($host['groupid']) && is_null($options['select_groups'])) { if (!isset($result[$host['hostid']]['groups'])) { $result[$host['hostid']]['groups'] = array(); } $result[$host['hostid']]['groups'][] = array('groupid' => $host['groupid']); unset($host['groupid']); } // templateids if (isset($host['templateid'])) { if (!isset($result[$host['hostid']]['templates'])) { $result[$host['hostid']]['templates'] = array(); } $result[$host['hostid']]['templates'][] = array('templateid' => $host['templateid']); unset($host['templateid']); } // triggerids if (isset($host['triggerid']) && is_null($options['select_triggers'])) { if (!isset($result[$host['hostid']]['triggers'])) { $result[$host['hostid']]['triggers'] = array(); } $result[$host['hostid']]['triggers'][] = array('triggerid' => $host['triggerid']); unset($host['triggerid']); } // itemids if (isset($host['itemid']) && is_null($options['select_items'])) { if (!isset($result[$host['hostid']]['items'])) { $result[$host['hostid']]['items'] = array(); } $result[$host['hostid']]['items'][] = array('itemid' => $host['itemid']); unset($host['itemid']); } // graphids if (isset($host['graphid']) && is_null($options['select_graphs'])) { if (!isset($result[$host['hostid']]['graphs'])) { $result[$host['hostid']]['graphs'] = array(); } $result[$host['hostid']]['graphs'][] = array('graphid' => $host['graphid']); unset($host['graphid']); } // dhostids if (isset($host['dhostid']) && is_null($options['select_dhosts'])) { if (!isset($result[$host['hostid']]['dhosts'])) { $result[$host['hostid']]['dhosts'] = array(); } $result[$host['hostid']]['dhosts'][] = array('dhostid' => $host['dhostid']); unset($host['dhostid']); } // dserviceids if (isset($host['dserviceid']) && is_null($options['select_dservices'])) { if (!isset($result[$host['hostid']]['dservices'])) { $result[$host['hostid']]['dservices'] = array(); } $result[$host['hostid']]['dservices'][] = array('dserviceid' => $host['dserviceid']); unset($host['dserviceid']); } // maintenanceids if (isset($host['maintenanceid'])) { if (!isset($result[$host['hostid']]['maintenanceid'])) { $result[$host['hostid']]['maintenances'] = array(); } $result[$host['hostid']]['maintenances'][] = array('maintenanceid' => $host['maintenanceid']); // unset($host['maintenanceid']); } //--- $result[$host['hostid']] += $host; } } } Copt::memoryPick(); if (!is_null($options['countOutput'])) { if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; } // Adding Objects // Adding Groups if (!is_null($options['select_groups']) && str_in_array($options['select_groups'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_groups'], 'hostids' => $hostids, 'preservekeys' => 1); $groups = CHostgroup::get($obj_params); foreach ($groups as $groupid => $group) { $ghosts = $group['hosts']; unset($group['hosts']); foreach ($ghosts as $num => $host) { $result[$host['hostid']]['groups'][] = $group; } } } // Adding Profiles if (!is_null($options['select_profile'])) { $sql = 'SELECT hp.* ' . ' FROM hosts_profiles hp ' . ' WHERE ' . DBcondition('hp.hostid', $hostids); $db_profile = DBselect($sql); while ($profile = DBfetch($db_profile)) { $result[$profile['hostid']]['profile'] = $profile; } $sql = 'SELECT hpe.* ' . ' FROM hosts_profiles_ext hpe ' . ' WHERE ' . DBcondition('hpe.hostid', $hostids); $db_profile_ext = DBselect($sql); while ($profile_ext = DBfetch($db_profile_ext)) { $result[$profile_ext['hostid']]['profile_ext'] = $profile_ext; } } // Adding Templates if (!is_null($options['selectParentTemplates'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'preservekeys' => 1); if (is_array($options['selectParentTemplates']) || str_in_array($options['selectParentTemplates'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['selectParentTemplates']; $templates = CTemplate::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($templates, 'host'); } foreach ($templates as $templateid => $template) { unset($templates[$templateid]['hosts']); $count = array(); foreach ($template['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['parentTemplates'][] =& $templates[$templateid]; } } } else { if (API_OUTPUT_COUNT == $options['selectParentTemplates']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $templates = CTemplate::get($obj_params); $templates = zbx_toHash($templates, 'hostid'); foreach ($result as $hostid => $host) { if (isset($templates[$hostid])) { $result[$hostid]['templates'] = $templates[$hostid]['rowscount']; } else { $result[$hostid]['templates'] = 0; } } } } } // Adding Items if (!is_null($options['select_items'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_items']) || str_in_array($options['select_items'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_items']; $items = CItem::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($items, 'description'); } foreach ($items as $itemid => $item) { unset($items[$itemid]['hosts']); foreach ($item['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['items'][] =& $items[$itemid]; } } } else { if (API_OUTPUT_COUNT == $options['select_items']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $items = CItem::get($obj_params); $items = zbx_toHash($items, 'hostid'); foreach ($result as $hostid => $host) { if (isset($items[$hostid])) { $result[$hostid]['items'] = $items[$hostid]['rowscount']; } else { $result[$hostid]['items'] = 0; } } } } } // Adding triggers if (!is_null($options['select_triggers'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_triggers']) || str_in_array($options['select_triggers'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_triggers']; $triggers = CTrigger::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($triggers, 'description'); } foreach ($triggers as $triggerid => $trigger) { unset($triggers[$triggerid]['hosts']); foreach ($trigger['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['triggers'][] =& $triggers[$triggerid]; } } } else { if (API_OUTPUT_COUNT == $options['select_triggers']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $triggers = CTrigger::get($obj_params); $triggers = zbx_toHash($triggers, 'hostid'); foreach ($result as $hostid => $host) { if (isset($triggers[$hostid])) { $result[$hostid]['triggers'] = $triggers[$hostid]['rowscount']; } else { $result[$hostid]['triggers'] = 0; } } } } } // Adding graphs if (!is_null($options['select_graphs'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_graphs']) || str_in_array($options['select_graphs'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_graphs']; $graphs = CGraph::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($graphs, 'name'); } foreach ($graphs as $graphid => $graph) { unset($graphs[$graphid]['hosts']); foreach ($graph['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['graphs'][] =& $graphs[$graphid]; } } } else { if (API_OUTPUT_COUNT == $options['select_graphs']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $graphs = CGraph::get($obj_params); $graphs = zbx_toHash($graphs, 'hostid'); foreach ($result as $hostid => $host) { if (isset($graphs[$hostid])) { $result[$hostid]['graphs'] = $graphs[$hostid]['rowscount']; } else { $result[$hostid]['graphs'] = 0; } } } } } // Adding discovery hosts if (!is_null($options['select_dhosts'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_dhosts']) || str_in_array($options['select_dhosts'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_dhosts']; $dhosts = CDHost::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($dhosts, 'dhostid'); } foreach ($dhosts as $dhostid => $dhost) { unset($dhosts[$dhostid]['hosts']); foreach ($dhost['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['dhosts'][] =& $dhosts[$dhostid]; } } } else { if (API_OUTPUT_COUNT == $options['select_dhosts']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $dhosts = CDHost::get($obj_params); $dhosts = zbx_toHash($dhosts, 'hostid'); foreach ($result as $hostid => $host) { if (isset($dhosts[$hostid])) { $result[$hostid]['dhosts'] = $dhosts[$hostid]['rowscount']; } else { $result[$hostid]['dhosts'] = 0; } } } } } // Adding applications if (!is_null($options['select_applications'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_applications']) || str_in_array($options['select_applications'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_applications']; $applications = CApplication::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($applications, 'name'); } foreach ($applications as $applicationid => $application) { unset($applications[$applicationid]['hosts']); foreach ($application['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['applications'][] =& $applications[$applicationid]; } } } else { if (API_OUTPUT_COUNT == $options['select_applications']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $applications = CApplication::get($obj_params); $applications = zbx_toHash($applications, 'hostid'); foreach ($result as $hostid => $host) { if (isset($applications[$hostid])) { $result[$hostid]['applications'] = $applications[$hostid]['rowscount']; } else { $result[$hostid]['applications'] = 0; } } } } } // Adding macros if (!is_null($options['select_macros']) && str_in_array($options['select_macros'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_macros'], 'hostids' => $hostids, 'preservekeys' => 1); $macros = CUserMacro::get($obj_params); foreach ($macros as $macroid => $macro) { $mhosts = $macro['hosts']; unset($macro['hosts']); foreach ($mhosts as $num => $host) { $result[$host['hostid']]['macros'][] = $macro; } } } Copt::memoryPick(); // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
$media = get_request('media', -1); $sendto = get_request('sendto', ''); $mediatypeid = get_request('mediatypeid', 0); $active = get_request('active', 0); $period = get_request('period', '1-7,00:00-23:59'); $frmMedia = new CFormTable(S_NEW_MEDIA); $frmMedia->SetHelp("web.media.php"); $frmMedia->AddVar("media", $media); $frmMedia->AddVar("dstfrm", $_REQUEST["dstfrm"]); $cmbType = new CComboBox("mediatypeid", $mediatypeid); $types = DBselect('SELECT mediatypeid,description ' . ' FROM media_type' . ' WHERE ' . DBin_node('mediatypeid') . ' ORDER BY type'); while ($type = DBfetch($types)) { $cmbType->AddItem($type["mediatypeid"], get_node_name_by_elid($type["mediatypeid"]) . $type["description"]); } $frmMedia->AddRow(S_TYPE, $cmbType); $frmMedia->AddRow(S_SEND_TO, new CTextBox("sendto", $sendto, 20)); $frmMedia->AddRow(S_WHEN_ACTIVE, new CTextBox("period", $period, 48)); $frm_row = array(); for ($i = 0; $i <= 5; $i++) { array_push($frm_row, array(new CCheckBox("severity[{$i}]", str_in_array($i, $severity) ? 'yes' : 'no', null, $i), get_severity_description($i)), BR()); } $frmMedia->AddRow(S_USE_IF_SEVERITY, $frm_row); $cmbStat = new CComboBox("active", $active); $cmbStat->AddItem(0, S_ENABLED); $cmbStat->AddItem(1, S_DISABLED); $frmMedia->AddRow("Status", $cmbStat); $frmMedia->AddItemToBottomRow(new CButton("add", $media > -1 ? S_SAVE : S_ADD)); $frmMedia->AddItemToBottomRow(SPACE); $frmMedia->AddItemToBottomRow(new CButtonCancel(null, 'close_window();')); $frmMedia->Show(); include_once "include/page_footer.php";
if (!$graphs) { access_deny(); } } $pageFilter = new CPageFilter(array('groups' => array('real_hosts' => true, 'with_graphs' => true), 'hosts' => array('with_graphs' => true), 'groupid' => getRequest('groupid'), 'hostid' => getRequest('hostid'), 'graphs' => array('templated' => 0), 'graphid' => getRequest('graphid'))); /* * Ajax */ if (hasRequest('filterState')) { CProfile::update('web.charts.filter.state', getRequest('filterState'), PROFILE_TYPE_INT); } if (isset($_REQUEST['favobj'])) { if (getRequest('favobj') === 'timelinefixedperiod' && hasRequest('favid')) { CProfile::update('web.screens.timelinefixed', getRequest('favid'), PROFILE_TYPE_INT); } if (str_in_array($_REQUEST['favobj'], array('itemid', 'graphid'))) { $result = false; DBstart(); if ($_REQUEST['favaction'] == 'add') { $result = CFavorite::add('web.favorite.graphids', $_REQUEST['favid'], $_REQUEST['favobj']); if ($result) { echo '$("addrm_fav").title = "' . _('Remove from favourites') . '";' . "\n"; echo '$("addrm_fav").onclick = function() { rm4favorites("graphid", "' . $_REQUEST['favid'] . '"); }' . "\n"; } } elseif ($_REQUEST['favaction'] == 'remove') { $result = CFavorite::remove('web.favorite.graphids', $_REQUEST['favid'], $_REQUEST['favobj']); if ($result) { echo '$("addrm_fav").title = "' . _('Add to favourites') . '";' . "\n"; echo '$("addrm_fav").onclick = function() { add2favorites("graphid", "' . $_REQUEST['favid'] . '"); }' . "\n"; } }
function DBfetch(&$cursor) { global $DB; $result = false; if (isset($DB['DB']) && !empty($DB['DB'])) { switch ($DB['TYPE']) { case 'MYSQL': $result = mysql_fetch_assoc($cursor); break; case 'POSTGRESQL': $result = pg_fetch_assoc($cursor); break; case 'ORACLE': if (ocifetchinto($cursor, $row, OCI_ASSOC + OCI_RETURN_NULLS)) { $result = array(); foreach ($row as $key => $value) { $result[strtolower($key)] = str_in_array(strtolower(ocicolumntype($cursor, $key)), array('varchar', 'varchar2', 'blob', 'clob')) && is_null($value) ? '' : $value; } } break; case 'SQLITE3': if ($cursor) { $result = array_shift($cursor); if (is_null($result)) { $result = false; } } break; } } return $result; }