function triggerIdents($triggerids) { $idents = array(); $options = array('triggerids' => $triggerids, 'select_hosts' => array('hostid', 'host'), 'output' => API_OUTPUT_EXTEND, 'nodeids' => get_current_nodeid(true)); $triggers = CTrigger::get($options); foreach ($triggers as $tnum => $trigger) { $host = reset($trigger['hosts']); $idents[$trigger['triggerid']] = array('node' => get_node_name_by_elid($host['hostid'], true), 'host' => $host['host'], 'description' => $trigger['description'], 'expression' => explode_exp($trigger['expression'], false)); } return $idents; }
} } //------------------------ <FORM> --------------------------- if (hasRequest('sform')) { $frmTRLog = new CFormTable(_('Trigger'), null, null, null, 'sform'); $frmTRLog->setName('sform'); $frmTRLog->addHelpIcon(); $frmTRLog->setTableClass('formlongtable formtable'); if (hasRequest('triggerid')) { $frmTRLog->addVar('triggerid', getRequest('triggerid')); } if (hasRequest('triggerid') && !hasRequest('form_refresh')) { $result = DBselect('SELECT t.expression,t.description,t.priority,t.comments,t.url,t.status,t.type' . ' FROM triggers t' . ' WHERE t.triggerid=' . zbx_dbstr(getRequest('triggerid')) . ' AND EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid ' . ' AND i.value_type IN (' . ITEM_VALUE_TYPE_LOG . ',' . ITEM_VALUE_TYPE_TEXT . ',' . ITEM_VALUE_TYPE_STR . ')' . ')'); if ($row = DBfetch($result)) { $description = $row['description']; $expression = explode_exp($row['expression']); $type = $row['type']; $priority = $row['priority']; $comments = $row['comments']; $url = $row['url']; $status = $row['status']; } // break expression into parts $expressions = $constructor->getPartsFromExpression($expression); } else { $description = getRequest('description', ''); $expressions = getRequest('expressions', array()); $type = getRequest('type', 0); $priority = getRequest('priority', 0); $comments = getRequest('comments', ''); $url = getRequest('url', '');
function make_trigger_details($trigger) { $hosts = reset($trigger['hosts']); $hostId = $hosts['hostid']; $hosts = API::Host()->get(array('output' => array('name', 'hostid', 'status'), 'hostids' => $hostId, 'selectScreens' => API_OUTPUT_COUNT, 'preservekeys' => true)); $host = reset($hosts); $scripts = API::Script()->getScriptsByHosts($hostId); $hostName = new CSpan($host['name'], 'link_menu'); $hostName->setMenuPopup(getMenuPopupHost($host, $scripts ? reset($scripts) : null)); $table = new CTableInfo(); if (is_show_all_nodes()) { $table->addRow(array(_('Node'), get_node_name_by_elid($trigger['triggerid']))); } $table->addRow(array(_('Host'), $hostName)); $table->addRow(array(_('Trigger'), CMacrosResolverHelper::resolveTriggerName($trigger))); $table->addRow(array(_('Severity'), getSeverityCell($trigger['priority']))); $table->addRow(array(_('Expression'), explode_exp($trigger['expression'], true, true))); $table->addRow(array(_('Event generation'), _('Normal') . (TRIGGER_MULT_EVENT_ENABLED == $trigger['type'] ? SPACE . '+' . SPACE . _('Multiple PROBLEM events') : ''))); $table->addRow(array(_('Disabled'), TRIGGER_STATUS_ENABLED == $trigger['status'] ? new CCol(_('No'), 'off') : new CCol(_('Yes'), 'on'))); return $table; }
if (isset($_REQUEST['favobj'])) { if ('hat' == $_REQUEST['favobj']) { update_profile('web.tr_events.hats.' . $_REQUEST['favid'] . '.state', $_REQUEST['state'], PROFILE_TYPE_INT); } } if (PAGE_TYPE_JS == $page['type'] || PAGE_TYPE_HTML_BLOCK == $page['type']) { exit; } //-------- if (!check_right_on_trigger_by_triggerid(PERM_READ_ONLY, $_REQUEST["triggerid"])) { access_deny(); } $trigger_data = DBfetch(DBselect('SELECT h.host, t.* ' . ' FROM hosts h, items i, functions f, triggers t ' . ' WHERE i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid ' . ' AND t.triggerid=' . $_REQUEST["triggerid"] . ' AND h.hostid=i.hostid ' . ' AND ' . DBin_node('t.triggerid'))); $p_elements = array(); //Header $trigger_data['exp_expr'] = explode_exp($trigger_data["expression"], 1); $trigger_data['exp_desc'] = expand_trigger_description_by_data($trigger_data); $text = array(S_EVENTS_BIG . ': "' . $trigger_data['exp_desc'] . '"'); $url = '?fullscreen=' . ($_REQUEST['fullscreen'] ? '0' : '1') . url_param('triggerid') . url_param('eventid'); $fs_icon = new CDiv(SPACE, 'fullscreen'); $fs_icon->addOption('title', $_REQUEST['fullscreen'] ? S_NORMAL . ' ' . S_VIEW : S_FULLSCREEN); $fs_icon->addAction('onclick', new CScript("javascript: document.location = '" . $url . "';")); //------- $left_tab = new CTable(); $left_tab->setCellPadding(3); $left_tab->setCellSpacing(3); $left_tab->addOption('border', 0); $left_tab->addRow(create_hat(S_EVENT . SPACE . S_SOURCE . SPACE . S_DETAILS, make_trigger_details($_REQUEST['triggerid'], $trigger_data), null, 'hat_triggerdetails')); $left_tab->addRow(create_hat(S_EVENT_DETAILS, make_event_details($_REQUEST['eventid']), null, 'hat_eventdetails')); $right_tab = new CTable(); $right_tab->setCellPadding(3);
/** * Checks that no trigger with the same description and expression as $trigger exist on the given host. * Assumes the given trigger is valid. * * @throws APIException if at least one trigger exists * * @param array $trigger a trigger with an exploded expression * @param null $hostid * * @return void */ protected function checkIfExistsOnHost(array $trigger, $hostId = null) { // skip the check if the description and expression haven't been changed if (!isset($trigger['description']) && !isset($trigger['expression'])) { return; } // make sure we have all the required data if (!isset($trigger['description']) || !isset($trigger['expression'])) { $explodeExpression = !isset($trigger['expression']); $trigger = $this->extendObject($this->tableName(), $trigger, array('description', 'expression')); if ($explodeExpression) { $trigger['expression'] = explode_exp($trigger['expression']); } } $filter = array('description' => $trigger['description']); if ($hostId) { $filter['hostid'] = $hostId; } else { $expressionData = new CTriggerExpression($trigger['expression']); $expressionData->parse($trigger['expression']); $expressionHosts = $expressionData->getHosts(); $filter['host'] = reset($expressionHosts); } $triggers = $this->get(array('filter' => $filter, 'output' => array('expression', 'triggerid'), 'nopermissions' => true)); foreach ($triggers as $dbTrigger) { $tmpExp = explode_exp($dbTrigger['expression']); // check if the expressions are also equal and that this is a different trigger $differentTrigger = !isset($trigger['triggerid']) || !idcmp($trigger['triggerid'], $dbTrigger['triggerid']); if (strcmp($tmpExp, $trigger['expression']) == 0 && $differentTrigger) { $options = array('output' => array('name'), 'templated_hosts' => true, 'nopermissions' => true, 'limit' => 1); if (isset($filter['host'])) { $options['filter'] = array('host' => $filter['host']); } else { $options['hostids'] = $hostId; } $host = API::Host()->get($options); $host = reset($host); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Trigger "%1$s" already exists on "%2$s".', $trigger['description'], $host['name'])); } } }
public function syncTemplates(array $data) { $data['templateids'] = zbx_toArray($data['templateids']); $data['hostids'] = zbx_toArray($data['hostids']); $triggers = $this->get(array('hostids' => $data['templateids'], 'preservekeys' => true, 'output' => array('triggerid', 'expression', 'description', 'url', 'status', 'priority', 'comments', 'type'))); foreach ($triggers as $trigger) { $trigger['expression'] = explode_exp($trigger['expression']); $this->inherit($trigger, $data['hostids']); } return true; }
} if (PAGE_TYPE_JS == $page['type'] || PAGE_TYPE_HTML_BLOCK == $page['type']) { include_once 'include/page_footer.php'; exit; } //-------- $options = array('triggerids' => $_REQUEST['triggerid'], 'output' => API_OUTPUT_EXTEND, 'select_hosts' => API_OUTPUT_EXTEND); $trigger = CTrigger::get($options); if (!$trigger) { access_deny(); } else { $trigger = reset($trigger); $trigger['host'] = reset($trigger['hosts']); $trigger['host'] = $trigger['host']['host']; } $trigger['exp_expr'] = explode_exp($trigger['expression'], 1, false, true); $trigger['exp_desc'] = expand_trigger_description_by_data($trigger); $tr_event_wdgt = new CWidget(); $tr_event_wdgt->setClass('header'); // Main widget header $text = array(S_EVENTS_BIG . ': "' . $trigger['exp_desc'] . '"'); $fs_icon = get_icon('fullscreen', array('fullscreen' => $_REQUEST['fullscreen'])); $tr_event_wdgt->addHeader($text, $fs_icon); //------- $left_tab = new CTable(); $left_tab->setCellPadding(3); $left_tab->setCellSpacing(3); $left_tab->setAttribute('border', 0); // tr details $tr_dtl = new CWidget('hat_triggerdetails', make_trigger_details($_REQUEST['triggerid'], $trigger)); $tr_dtl->setClass('header');
/** * Select trigger ids for previously added trigger names/expressions. */ protected function selectTriggers() { if (!empty($this->triggers)) { $this->triggersRefs = array(); $dbTriggers = API::Trigger()->get(array('output' => array('triggerid', 'expression', 'description'), 'filter' => array('description' => array_keys($this->triggers), 'flags' => array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_PROTOTYPE, ZBX_FLAG_DISCOVERY_CREATED)), 'editable' => true)); foreach ($dbTriggers as $dbTrigger) { $dbTriggerExpression = explode_exp($dbTrigger['expression']); if (isset($this->triggers[$dbTrigger['description']][$dbTriggerExpression])) { $this->triggersRefs[$dbTrigger['description']][$dbTriggerExpression] = $dbTrigger['triggerid']; } } } }
public function syncTemplates($data) { $data['templateids'] = zbx_toArray($data['templateids']); $data['hostids'] = zbx_toArray($data['hostids']); $triggers = $this->get(array('hostids' => $data['templateids'], 'preservekeys' => true, 'output' => API_OUTPUT_EXTEND, 'selectDependencies' => true)); foreach ($triggers as $trigger) { $trigger['expression'] = explode_exp($trigger['expression']); $this->inherit($trigger, $data['hostids']); } return true; }
function EndElement($parser, $name) { if (!$this->root) { return false; } global $USER_DETAILS; $data =& $this->data[$name]; switch ($name) { case XML_TAG_HOST: if ($data['skip'] || !isset($data['hostid']) || !$data['hostid']) { break; } // case if (!isset($data['port'])) { $data['port'] = 10050; } if (!isset($data['status'])) { $data['status'] = 0; } if (!isset($data['useip'])) { $data['useip'] = 0; } if (!isset($data['dns'])) { $data['dns'] = ''; } if (!isset($data['ip'])) { $data['ip'] = ''; } if (!isset($data['proxy'])) { $data['proxy'] = ''; } if (!zbx_empty($data['proxy'])) { $sql = 'SELECT hostid ' . ' FROM hosts ' . ' WHERE host=' . zbx_dbstr($data['proxy']) . ' AND status=' . HOST_STATUS_PROXY . ' AND ' . DBin_node('hostid', get_current_nodeid(false)); if ($host_data = DBfetch(DBselect($sql))) { $data['proxy'] = $host_data['hostid']; } else { $data['proxy'] = 0; } } else { $data['proxy'] = 0; } if (update_host($data['hostid'], $data['name'], $data['port'], $data['status'], $data['useip'], $data['dns'], $data['ip'], $data['proxy'], $data['templates'], 'no', '', 623, -1, 2, '', '', null, $data['groups'])) { info('Host [' . $data['name'] . '] updated'); } break; // case // based on mod by scricca // case // based on mod by scricca case XML_TAG_HOSTPROFILE: if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) { break; } //case if (!isset($data['devicetype'])) { $data['devicetype'] = ''; } if (!isset($data['name'])) { $data['name'] = ''; } if (!isset($data['os'])) { $data['os'] = ''; } if (!isset($data['serialno'])) { $data['serialno'] = ''; } if (!isset($data['tag'])) { $data['tag'] = ''; } if (!isset($data['macaddress'])) { $data['macaddress'] = ''; } if (!isset($data['hardware'])) { $data['hardware'] = ''; } if (!isset($data['software'])) { $data['software'] = ''; } if (!isset($data['contact'])) { $data['contact'] = ''; } if (!isset($data['location'])) { $data['location'] = ''; } if (!isset($data['notes'])) { $data['notes'] = ''; } delete_host_profile($this->data[XML_TAG_HOST]['hostid']); if (add_host_profile($this->data[XML_TAG_HOST]['hostid'], $data['devicetype'], $data['name'], $data['os'], $data['serialno'], $data['tag'], $data['macaddress'], $data['hardware'], $data['software'], $data['contact'], $data['location'], $data['notes'])) { info('Host Profile [' . $this->data[XML_TAG_HOST]['name'] . '] updated'); } break; // case //--- // Extended profiles // case //--- // Extended profiles case XML_TAG_HOSTPROFILE_EXT: if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) { break; } //case if (!isset($data['device_alias'])) { $data['device_alias'] = ''; } if (!isset($data['device_type'])) { $data['device_type'] = ''; } if (!isset($data['device_chassis'])) { $data['device_chassis'] = ''; } if (!isset($data['device_os'])) { $data['device_os'] = ''; } if (!isset($data['device_os_short'])) { $data['device_os_short'] = ''; } if (!isset($data['device_hw_arch'])) { $data['device_hw_arch'] = ''; } if (!isset($data['device_serial'])) { $data['device_serial'] = ''; } if (!isset($data['device_model'])) { $data['device_model'] = ''; } if (!isset($data['device_tag'])) { $data['device_tag'] = ''; } if (!isset($data['device_vendor'])) { $data['device_vendor'] = ''; } if (!isset($data['device_contract'])) { $data['device_contract'] = ''; } if (!isset($data['device_who'])) { $data['device_who'] = ''; } if (!isset($data['device_status'])) { $data['device_status'] = ''; } if (!isset($data['device_app_01'])) { $data['device_app_01'] = ''; } if (!isset($data['device_app_02'])) { $data['device_app_02'] = ''; } if (!isset($data['device_app_03'])) { $data['device_app_03'] = ''; } if (!isset($data['device_app_04'])) { $data['device_app_04'] = ''; } if (!isset($data['device_app_05'])) { $data['device_app_05'] = ''; } if (!isset($data['device_url_1'])) { $data['device_url_1'] = ''; } if (!isset($data['device_url_2'])) { $data['device_url_2'] = ''; } if (!isset($data['device_url_3'])) { $data['device_url_3'] = ''; } if (!isset($data['device_networks'])) { $data['device_networks'] = ''; } if (!isset($data['device_notes'])) { $data['device_notes'] = ''; } if (!isset($data['device_hardware'])) { $data['device_hardware'] = ''; } if (!isset($data['device_software'])) { $data['device_software'] = ''; } if (!isset($data['ip_subnet_mask'])) { $data['ip_subnet_mask'] = ''; } if (!isset($data['ip_router'])) { $data['ip_router'] = ''; } if (!isset($data['ip_macaddress'])) { $data['ip_macaddress'] = ''; } if (!isset($data['oob_ip'])) { $data['oob_ip'] = ''; } if (!isset($data['oob_subnet_mask'])) { $data['oob_subnet_mask'] = ''; } if (!isset($data['oob_router'])) { $data['oob_router'] = ''; } if (!isset($data['date_hw_buy'])) { $data['date_hw_buy'] = ''; } if (!isset($data['date_hw_install'])) { $data['date_hw_install'] = ''; } if (!isset($data['date_hw_expiry'])) { $data['date_hw_expiry'] = ''; } if (!isset($data['date_hw_decomm'])) { $data['date_hw_decomm'] = ''; } if (!isset($data['site_street_1'])) { $data['site_street_1'] = ''; } if (!isset($data['site_street_2'])) { $data['site_street_2'] = ''; } if (!isset($data['site_street_3'])) { $data['site_street_3'] = ''; } if (!isset($data['site_city'])) { $data['site_city'] = ''; } if (!isset($data['site_state'])) { $data['site_state'] = ''; } if (!isset($data['site_country'])) { $data['site_country'] = ''; } if (!isset($data['site_zip'])) { $data['site_zip'] = ''; } if (!isset($data['site_rack'])) { $data['site_rack'] = ''; } if (!isset($data['site_notes'])) { $data['site_notes'] = ''; } if (!isset($data['poc_1_name'])) { $data['poc_1_name'] = ''; } if (!isset($data['poc_1_email'])) { $data['poc_1_email'] = ''; } if (!isset($data['poc_1_phone_1'])) { $data['poc_1_phone_1'] = ''; } if (!isset($data['poc_1_phone_2'])) { $data['poc_1_phone_2'] = ''; } if (!isset($data['poc_1_cell'])) { $data['poc_1_cell'] = ''; } if (!isset($data['poc_1_screen'])) { $data['poc_1_screen'] = ''; } if (!isset($data['poc_1_notes'])) { $data['poc_1_notes'] = ''; } if (!isset($data['poc_2_name'])) { $data['poc_2_name'] = ''; } if (!isset($data['poc_2_email'])) { $data['poc_2_email'] = ''; } if (!isset($data['poc_2_phone_1'])) { $data['poc_2_phone_1'] = ''; } if (!isset($data['poc_2_phone_2'])) { $data['poc_2_phone_2'] = ''; } if (!isset($data['poc_2_cell'])) { $data['poc_2_cell'] = ''; } if (!isset($data['poc_2_screen'])) { $data['poc_2_screen'] = ''; } if (!isset($data['poc_2_notes'])) { $data['poc_2_notes'] = ''; } delete_host_profile_ext($this->data[XML_TAG_HOST]['hostid']); if (add_host_profile_ext($this->data[XML_TAG_HOST]['hostid'], $data)) { info('Host Extended Profile [' . $this->data[XML_TAG_HOST]['name'] . '] updated'); } break; // case //--- // case //--- case XML_TAG_GROUP: if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) { break; } //case $sql = 'SELECT groupid, name ' . ' FROM groups' . ' WHERE ' . DBin_node('groupid', get_current_nodeid(false)) . ' AND name=' . zbx_dbstr($this->element_data); if (!($group = DBfetch(DBselect($sql)))) { error('Missing group [' . $this->element_data . ']'); break; // case } if (!isset($this->available_groups[$group['groupid']])) { error('Group [' . $this->element_data . '] skipped - Access deny.'); break; // case } $this->data[XML_TAG_HOST]['groups'][$group['groupid']] = $group['groupid']; break; // case // case case XML_TAG_DEPENDENCY: if (!isset($data['triggerid_down']) || !$data['triggerid_down']) { break; } // case update_trigger($data['triggerid_down'], null, null, null, null, null, null, null, $data['triggerid_up'], null); break; // case // case case XML_TAG_DEPENDS: if (!isset($this->data[XML_TAG_DEPENDENCY]['triggerid_down']) || !$this->data[XML_TAG_DEPENDENCY]['triggerid_down']) { break; } //case if (!($trigger_up = get_trigger_by_description($this->element_data))) { break; } array_push($this->data[XML_TAG_DEPENDENCY]['triggerid_up'], $trigger_up['triggerid']); break; // case // case case XML_TAG_APPLICATION: if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) { break; } //case if (!isset($this->data[XML_TAG_ITEM])) { break; } //case $sql = 'SELECT applicationid ' . ' FROM applications' . ' WHERE ' . DBin_node('applicationid', get_current_nodeid(false)) . ' AND name=' . zbx_dbstr($this->element_data) . ' AND hostid=' . $this->data[XML_TAG_HOST]['hostid']; if (!($application = DBfetch(DBselect($sql)))) { $applicationid = add_application($this->element_data, $this->data[XML_TAG_HOST]['hostid']); } else { $applicationid = $application['applicationid']; } $this->data[XML_TAG_ITEM]['applications'][] = $applicationid; break; // case // case case XML_TAG_TEMPLATE: if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) { break; } //case $sql = 'SELECT DISTINCT host, hostid ' . ' FROM hosts' . ' WHERE ' . DBin_node('hostid') . ' AND host=' . zbx_dbstr($this->element_data) . ' AND status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ')'; if (!($template = DBfetch(DBselect($sql)))) { error('Missing template [' . $this->element_data . ']'); break; // case } if (!isset($this->available_hosts[$template['hostid']])) { error('Template [' . $this->element_data . '] skipped - Access deny.'); break; // case } $this->data[XML_TAG_HOST]['templates'][$template["hostid"]] = $template['host']; break; // case // case case XML_TAG_ITEM: if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) { if (isset($this->data[XML_TAG_HOST]['skip']) && $this->data[XML_TAG_HOST]['skip']) { info('Item [' . $data['description'] . '] skipped - user rule for host'); break; // case } error('Item [' . $data['description'] . '] skipped - missing host'); break; // case } if (!isset($data['description'])) { $data['description'] = ''; } if (!isset($data['delay'])) { $data['delay'] = 30; } if (!isset($data['history'])) { $data['history'] = 90; } if (!isset($data['trends'])) { $data['trends'] = 365; } if (!isset($data['status'])) { $data['status'] = 0; } if (!isset($data['units'])) { $data['units'] = ''; } if (!isset($data['multiplier'])) { $data['multiplier'] = 0; } if (!isset($data['delta'])) { $data['delta'] = 0; } if (!isset($data['formula'])) { $data['formula'] = ''; } if (!isset($data['lastlogsize'])) { $data['lastlogsize'] = 0; } if (!isset($data['logtimefmt'])) { $data['logtimefmt'] = ''; } if (!isset($data['delay_flex'])) { $data['delay_flex'] = ''; } if (!isset($data['trapper_hosts'])) { $data['trapper_hosts'] = ''; } if (!isset($data['snmp_community'])) { $data['snmp_community'] = ''; } if (!isset($data['snmp_oid'])) { $data['snmp_oid'] = ''; } if (!isset($data['snmp_port'])) { $data['snmp_port'] = 161; } if (!isset($data['snmpv3_securityname'])) { $data['snmpv3_securityname'] = ''; } if (!isset($data['snmpv3_securitylevel'])) { $data['snmpv3_securitylevel'] = 0; } if (!isset($data['snmpv3_authpassphrase'])) { $data['snmpv3_authpassphrase'] = ''; } if (!isset($data['snmpv3_privpassphrase'])) { $data['snmpv3_privpassphrase'] = ''; } if (!isset($data['valuemap'])) { $data['valuemap'] = ''; } if (!isset($data['params'])) { $data['params'] = ''; } if (!isset($data['ipmi_sensor'])) { $data['ipmi_sensor'] = ''; } if (!isset($data['applications'])) { $data['applications'] = array(); } if (!empty($data['valuemap'])) { $sql = 'SELECT valuemapid ' . ' FROM valuemaps ' . ' WHERE ' . DBin_node('valuemapid', get_current_nodeid(false)) . ' AND name=' . zbx_dbstr($data['valuemap']); if ($valuemap = DBfetch(DBselect($sql))) { $data['valuemapid'] = $valuemap['valuemapid']; } else { $data['valuemapid'] = add_valuemap($data['valuemap'], array()); } } $sql = 'SELECT itemid,valuemapid,templateid ' . ' FROM items ' . ' WHERE key_=' . zbx_dbstr($data['key']) . ' AND hostid=' . $this->data[XML_TAG_HOST]['hostid'] . ' AND ' . DBin_node('itemid', get_current_nodeid(false)); if ($item = DBfetch(DBselect($sql))) { /* exist */ if ($this->item['exist'] == 1) { info('Item [' . $data['description'] . '] skipped - user rule'); break; } if (!isset($data['valuemapid'])) { $data['valuemapid'] = $item['valuemapid']; } $data['key_'] = $data['key']; $data['hostid'] = $this->data[XML_TAG_HOST]['hostid']; $data['applications'] = array_unique(array_merge($data['applications'], get_applications_by_itemid($item['itemid']))); $data['templateid'] = $item['templateid']; check_db_fields($item, $data); update_item($item['itemid'], $data); } else { /* missed */ if ($this->item['missed'] == 1) { info('Item [' . $data['description'] . '] skipped - user rule'); break; // case } if (!isset($data['valuemapid'])) { $data['valuemapid'] = 0; } $data['hostid'] = $this->data[XML_TAG_HOST]['hostid']; $data['key_'] = $data['key']; add_item($data); } break; // case // case case XML_TAG_TRIGGER: if (!isset($data['expression'])) { $data['expression'] = ''; } if (!isset($data['description'])) { $data['description'] = ''; } if (!isset($data['type'])) { $data['type'] = 0; } if (!isset($data['priority'])) { $data['priority'] = 0; } if (!isset($data['status'])) { $data['status'] = 0; } if (!isset($data['comments'])) { $data['comments'] = ''; } if (!isset($data['url'])) { $data['url'] = ''; } if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) { if (isset($this->data[XML_TAG_HOST]['skip']) && $this->data[XML_TAG_HOST]['skip']) { // remember skipped triggers for dependencies $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'] . ':' . $data['description']; info('Trigger [' . $data['description'] . '] skipped - user rule for host'); break; // case } if (zbx_strstr($data['expression'], '{HOSTNAME}')) { // remember skipped triggers for dependencies $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'] . ':' . $data['description']; error('Trigger [' . $data['description'] . '] skipped - missing host'); break; // case } } else { $data['expression'] = str_replace('{{HOSTNAME}:', '{' . $this->data[XML_TAG_HOST]['name'] . ':', $data['expression']); $result = DBselect('SELECT DISTINCT t.triggerid,t.templateid,t.expression ' . ' FROM triggers t,functions f,items i ' . ' WHERE t.triggerid=f.triggerid ' . ' AND f.itemid=i.itemid' . ' AND i.hostid=' . $this->data[XML_TAG_HOST]['hostid'] . ' AND t.description=' . zbx_dbstr($data['description'])); while ($trigger = DBfetch($result)) { if (explode_exp($trigger['expression'], 0) == $data['expression']) { break; // while } } if (!empty($trigger)) { /* exist */ if ($this->trigger['exist'] == 1) { /* skip */ // remember skipped triggers for dependencies $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'] . ':' . $data['description']; info('Trigger [' . $data['description'] . '] skipped - user rule'); break; // case } update_trigger($trigger['triggerid'], $data['expression'], $data['description'], $data['type'], $data['priority'], $data['status'], $data['comments'], $data['url'], get_trigger_dependencies_by_triggerid($trigger['triggerid']), $trigger['templateid']); break; // case } else { /* missed */ // continue [add_trigger] } } if ($this->trigger['missed'] == 1) { // remember skipped triggers for dependencies $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'] . ':' . $data['description']; info('Trigger [' . $data['description'] . '] skipped - user rule'); break; // case } add_trigger($data['expression'], $data['description'], $data['type'], $data['priority'], $data['status'], $data['comments'], $data['url']); break; // case // case case XML_TAG_GRAPH: if (isset($data['error'])) { error('Graph [' . $data['name'] . '] skipped - error occured'); break; // case } if (!isset($data['ymin_type'])) { $data['ymin_type'] = 0; } if (!isset($data['ymax_type'])) { $data['ymax_type'] = 0; } if (!isset($data['ymin_item_key'])) { $data['ymin_item_key'] = ''; } if (!isset($data['ymax_item_key'])) { $data['ymax_item_key'] = ''; } if (!isset($data['ymin_itemid'])) { $data['ymin_itemid'] = 0; } if (!isset($data['ymax_itemid'])) { $data['ymax_itemid'] = 0; } if (!isset($data['show_work_period'])) { $data['show_work_period'] = 1; } if (!isset($data['show_triggers'])) { $data['show_triggers'] = 1; } if (!isset($data['graphtype'])) { $data['graphtype'] = 0; } if (!isset($data['yaxismin'])) { $data['yaxismin'] = 0; } if (!isset($data['yaxismax'])) { $data['yaxismax'] = 0; } if (!isset($data['show_legend'])) { $data['show_legend'] = 0; } if (!isset($data['show_3d'])) { $data['show_3d'] = 0; } if (!isset($data['percent_left'])) { $data['percent_left'] = 0; } if (!isset($data['percent_right'])) { $data['percent_right'] = 0; } if (!isset($data['items'])) { $data['items'] = array(); } if (!empty($data['ymin_item_key'])) { $data['ymin_item_key'] = explode(':', $data['ymin_item_key']); if (count($data['ymin_item_key']) < 2) { $this->data[XML_TAG_GRAPH]['error'] = true; error('Incorrect y min item for graph [' . $data['name'] . ']'); break; // case } $data['host'] = array_shift($data['ymin_item_key']); $data['ymin_item_key'] = implode(':', $data['ymin_item_key']); if (!($item = get_item_by_key($data['ymin_item_key'], $data['host']))) { $this->data[XML_TAG_GRAPH]['error'] = true; error('Missed item [' . $data['ymin_item_key'] . '] for host [' . $data['host'] . ']'); break; // case } $data['ymin_itemid'] = $item['itemid']; } if (!empty($data['ymax_item_key'])) { $data['ymax_item_key'] = explode(':', $data['ymax_item_key']); if (count($data['ymax_item_key']) < 2) { $this->data[XML_TAG_GRAPH]['error'] = true; error('Incorrect y max item for graph [' . $data['name'] . ']'); break; // case } $data['host'] = array_shift($data['ymax_item_key']); $data['ymax_item_key'] = implode(':', $data['ymax_item_key']); if (!($item = get_item_by_key($data['ymax_item_key'], $data['host']))) { $this->data[XML_TAG_GRAPH]['error'] = true; error('Missed item [' . $data['ymax_item_key'] . '] for host [' . $data['host'] . ']'); break; // case } $data['ymax_itemid'] = $item['itemid']; } if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) { if (isset($this->data[XML_TAG_HOST]['skip']) && $this->data[XML_TAG_HOST]['skip']) { info('Graph [' . $data['name'] . '] skipped - user rule for host'); break; // case } foreach ($data['items'] as $id) { if (zbx_strstr($data['name'], '{HOSTNAME}')) { error('Graph [' . $data['name'] . '] skipped - missing host'); break; // case } } } else { if ($graph = DBfetch(DBselect('SELECT DISTINCT g.graphid, g.templateid' . ' FROM graphs g, graphs_items gi, items i' . ' WHERE g.graphid=gi.graphid ' . ' AND gi.itemid=i.itemid' . ' AND g.name=' . zbx_dbstr($data['name']) . ' AND i.hostid=' . $this->data[XML_TAG_HOST]['hostid']))) { /* exist */ if ($this->graph['exist'] == 1) { /* skip */ info('Graph [' . $data['name'] . '] skipped - user rule'); break; // case } $data['graphid'] = $graph['graphid']; update_graph_with_items($data['graphid'], $data['name'], $data['width'], $data['height'], $data['ymin_type'], $data['ymax_type'], $data['yaxismin'], $data['yaxismax'], $data['ymin_itemid'], $data['ymax_itemid'], $data['show_work_period'], $data['show_triggers'], $data['graphtype'], $data['show_legend'], $data['show_3d'], $data['percent_left'], $data['percent_right'], $data['items'], $graph['templateid']); } else { /* missed */ // continue [add_group] } } if (!isset($data['graphid'])) { if ($this->graph['missed'] == 1) { /* skip */ info('Graph [' . $data['name'] . '] skipped - user rule'); break; // case } $data['graphid'] = add_graph_with_items($data['name'], $data['width'], $data['height'], $data['ymin_type'], $data['ymax_type'], $data['yaxismin'], $data['yaxismax'], $data['ymin_itemid'], $data['ymax_itemid'], $data['show_work_period'], $data['show_triggers'], $data['graphtype'], $data['show_legend'], $data['show_3d'], $data['percent_left'], $data['percent_right'], $data['items']); } break; // case // case case XML_TAG_GRAPH_ELEMENT: if (!isset($this->data[XML_TAG_GRAPH])) { break; } // case $data['key'] = explode(':', $data['item']); if (count($data['key']) < 2) { $this->data[XML_TAG_GRAPH]['error'] = true; error('Incorrect element for graph [' . $data['name'] . ']'); break; // case } $data['host'] = array_shift($data['key']); $data['key'] = implode(':', $data['key']); if (isset($this->data[XML_TAG_HOST]['name'])) { $data['host'] = str_replace('{HOSTNAME}', $this->data[XML_TAG_HOST]['name'], $data['host']); } if (!isset($data['drawtype'])) { $data['drawtype'] = 0; } if (!isset($data['sortorder'])) { $data['sortorder'] = 0; } if (!isset($data['color'])) { $data['color'] = 'Dark Green'; } if (!isset($data['yaxisside'])) { $data['yaxisside'] = 1; } if (!isset($data['calc_fnc'])) { $data['calc_fnc'] = 2; } if (!isset($data['type'])) { $data['type'] = 0; } if (!isset($data['periods_cnt'])) { $data['periods_cnt'] = 5; } if (!($item = get_item_by_key($data['key'], $data['host']))) { $this->data[XML_TAG_GRAPH]['error'] = true; error('Missing item [' . $data['key'] . '] for host [' . $data['host'] . ']'); break; // case } $data['itemid'] = $item['itemid']; array_push($this->data[XML_TAG_GRAPH]['items'], $data); break; // case /*case XML_TAG_SCREEN: case XML_TAG_SCREEN_ELEMENT: break; // case*/ // case /*case XML_TAG_SCREEN: case XML_TAG_SCREEN_ELEMENT: break; // case*/ default: if (isset($this->sub_node) && isset($this->main_node)) { $main_node = array_pop($this->main_node); $this->data[$main_node][$this->sub_node] = $this->element_data; array_push($this->main_node, $main_node); } $this->sub_node = null; return; } unset($this->data[$name], $data); array_pop($this->main_node); }
$now = time(); if (isset($_REQUEST['status'])) { $status = TRIGGER_STATUS_DISABLED; } else { $status = TRIGGER_STATUS_ENABLED; } //if(isset($_REQUEST['type'])) { $type=TRIGGER_MULT_EVENT_ENABLED; } //else{ $type=TRIGGER_MULT_EVENT_DISABLED; } $type = TRIGGER_MULT_EVENT_ENABLED; if (isset($_REQUEST['triggerid'])) { $options = array('triggerids' => $_REQUEST['triggerid'], 'output' => API_OUTPUT_EXTEND, 'selectDependencies' => API_OUTPUT_REFER); $triggersData = API::Trigger()->get($options); $triggerData = reset($triggersData); if ($triggerData['templateid']) { $_REQUEST['description'] = $triggerData['description']; $expression = explode_exp($triggerData['expression']); } $trigger = array(); $trigger['triggerid'] = $_REQUEST['triggerid']; $trigger['expression'] = $expression; $trigger['description'] = $_REQUEST['description']; $trigger['type'] = $type; $trigger['priority'] = $_REQUEST['priority']; $trigger['status'] = $status; $trigger['comments'] = $_REQUEST['comments']; $trigger['url'] = $_REQUEST['url']; DBstart(); $result = API::Trigger()->update($trigger); //REVERT $result = DBend($result); $triggerid = $_REQUEST['triggerid'];
if (isset($_REQUEST['save'])) { show_messages(); if (!check_right_on_trigger_by_expression(PERM_READ_WRITE, $_REQUEST['expression'])) { if (isset($_REQUEST['triggerid'])) { show_messages(false, null, S_CANNOT_UPDATE_TRIGGER); } else { show_messages(false, null, S_CANNOT_ADD_TRIGGER); } } else { $status = isset($_REQUEST['status']) ? TRIGGER_STATUS_DISABLED : TRIGGER_STATUS_ENABLED; $deps = get_request('dependencies', array()); if (isset($_REQUEST['triggerid'])) { $triggerData = get_trigger_by_triggerid($_REQUEST['triggerid']); if ($triggerData['templateid']) { $_REQUEST['description'] = $triggerData['description']; $_REQUEST['expression'] = explode_exp($triggerData['expression'], 0); } $current_deps = get_trigger_dependencies_by_triggerid($_REQUEST['triggerid']); sort($deps); sort($current_deps); if ($deps == $current_deps) { $deps = null; } $type = get_request('type'); $priority = get_request('priority'); $comments = get_request('comments'); $url = get_request('url'); if ($triggerData['type'] == $_REQUEST['type']) { $type = null; } if ($triggerData['priority'] == $_REQUEST['priority']) {
if ($trigger['status'] == TRIGGER_STATUS_DISABLED) { $status = new CLink(S_DISABLED, $status_link, 'disabled'); } else { if ($trigger['status'] == TRIGGER_STATUS_UNKNOWN) { $status = new CLink(S_UNKNOWN, $status_link, 'unknown'); } else { if ($trigger['status'] == TRIGGER_STATUS_ENABLED) { $status = new CLink(S_ENABLED, $status_link, 'enabled'); } } } if ($_REQUEST['hostid'] > 0) { $table->addRow(array(new CCheckBox('g_triggerid[' . $triggerid . ']', NULL, NULL, $triggerid), $priority, $status, $description, explode_exp($trigger['expression'], 1), $error)); } else { foreach ($trigger['hosts'] as $host) { $table->addRow(array(new CCheckBox('g_triggerid[' . $triggerid . ']', NULL, NULL, $triggerid), $priority, $status, $host['host'], $description, explode_exp($trigger['expression'], 1), $error)); } } } //----- GO ------ $goBox = new CComboBox('go'); $goBox->addItem('activate', S_ACTIVATE_SELECTED); $goBox->addItem('disable', S_DISABLE_SELECTED); $goBox->addItem('massupdate', S_MASS_UPDATE); $goBox->addItem('copy_to', S_COPY_SELECTED_TO); $goBox->addItem('delete', S_DELETE_SELECTED); // goButton name is necessary!!! $goButton = new CButton('goButton', S_GO . ' (0)'); $goButton->setAttribute('id', 'goButton'); zbx_add_post_js('chkbxRange.pageGoName = "g_triggerid";'); $table->setFooter(new CCol(array($goBox, $goButton)));
function make_trigger_details($trigger) { $hostNames = array(); $hostIds = zbx_objectValues($trigger['hosts'], 'hostid'); $hosts = API::Host()->get(array('output' => array('name', 'hostid', 'status'), 'hostids' => $hostIds, 'selectScreens' => API_OUTPUT_COUNT, 'selectGraphs' => API_OUTPUT_COUNT)); if (count($hosts) > 1) { order_result($hosts, 'name', ZBX_SORT_UP); } $scripts = API::Script()->getScriptsByHosts($hostIds); foreach ($hosts as $host) { $hostName = new CSpan($host['name'], 'link_menu'); $hostName->setMenuPopup(CMenuPopupHelper::getHost($host, $scripts[$host['hostid']])); $hostNames[] = $hostName; $hostNames[] = ', '; } array_pop($hostNames); $table = new CTableInfo(); $table->addRow(array(new CCol(_n('Host', 'Hosts', count($hosts))), new CCol($hostNames, 'wraptext'))); $table->addRow(array(new CCol(_('Trigger')), new CCol(CMacrosResolverHelper::resolveTriggerName($trigger), 'wraptext'))); $table->addRow(array(_('Severity'), getSeverityCell($trigger['priority']))); $table->addRow(array(new CCol(_('Expression')), new CCol(explode_exp($trigger['expression'], true, true), 'trigger-expression'))); $table->addRow(array(_('Event generation'), _('Normal') . (TRIGGER_MULT_EVENT_ENABLED == $trigger['type'] ? SPACE . '+' . SPACE . _('Multiple PROBLEM events') : ''))); $table->addRow(array(_('Disabled'), TRIGGER_STATUS_ENABLED == $trigger['status'] ? new CCol(_('No'), 'off') : new CCol(_('Yes'), 'on'))); return $table; }
function make_trigger_details($trigger) { $table = new CTableInfo(); if (is_show_all_nodes()) { $table->addRow(array(_('Node'), get_node_name_by_elid($trigger['triggerid']))); } $expression = explode_exp($trigger['expression'], true, true); $host = API::Host()->get(array('output' => array('name', 'hostid'), 'hostids' => $trigger['hosts'][0]['hostid'], 'selectScreens' => API_OUTPUT_COUNT, 'selectInventory' => array('hostid'), 'preservekeys' => true)); $host = reset($host); $hostScripts = API::Script()->getScriptsByHosts($host['hostid']); // host js link $hostSpan = new CSpan($host['name'], 'link_menu menu-host'); $scripts = $hostScripts[$host['hostid']]; $hostSpan->attr('data-menu', hostMenuData($host, $scripts)); // get visible name of the first host $table->addRow(array(_('Host'), $hostSpan)); $table->addRow(array(_('Trigger'), CTriggerHelper::expandDescription($trigger))); $table->addRow(array(_('Severity'), getSeverityCell($trigger['priority']))); $table->addRow(array(_('Expression'), $expression)); $table->addRow(array(_('Event generation'), _('Normal') . (TRIGGER_MULT_EVENT_ENABLED == $trigger['type'] ? SPACE . '+' . SPACE . _('Multiple PROBLEM events') : ''))); $table->addRow(array(_('Disabled'), TRIGGER_STATUS_ENABLED == $trigger['status'] ? new CCol(_('No'), 'off') : new CCol(_('Yes'), 'on'))); return $table; }
/** * Select trigger ids for previously added trigger names/expressions. */ protected function selectTriggers() { if (!empty($this->triggers)) { $this->triggersRefs = array(); $triggerIds = array(); $sql = 'SELECT t.triggerid,t.expression,t.description' . ' FROM triggers t' . ' WHERE ' . dbConditionString('t.description', array_keys($this->triggers)); $dbTriggers = DBselect($sql); while ($dbTrigger = DBfetch($dbTriggers)) { $dbExpr = explode_exp($dbTrigger['expression']); foreach ($this->triggers as $name => $expressions) { if ($name == $dbTrigger['description']) { foreach ($expressions as $expression) { if ($expression == $dbExpr) { $this->triggersRefs[$name][$expression] = $dbTrigger['triggerid']; $triggerIds[] = $dbTrigger['triggerid']; } } } } } $allowedTriggers = API::Trigger()->get(array('triggerids' => $triggerIds, 'output' => array('triggerid'), 'filter' => array('flags' => array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_PROTOTYPE)), 'editable' => true, 'preservekeys' => true)); foreach ($this->triggersRefs as $name => $expressions) { foreach ($expressions as $expression => $triggerId) { if (!isset($allowedTriggers[$triggerId])) { unset($this->triggersRefs[$name][$expression]); } } } } }
/** * Get triggers references by trigger ids. * * @param array $triggerIds * * @return array */ protected function getTriggersReferences(array $triggerIds) { $idents = array(); $triggers = API::Trigger()->get(array('triggerids' => $triggerIds, 'output' => array('description', 'expression'), 'nodeids' => get_current_nodeid(true), 'preservekeys' => true)); foreach ($triggers as $id => $trigger) { $idents[$id] = array('description' => $trigger['description'], 'expression' => explode_exp($trigger['expression'])); } return $idents; }
} foreach ($triggers as $trigger) { $usedHosts = array(); foreach ($trigger['hosts'] as $host) { $usedHosts[$host['hostid']] = $host['name']; } $usedHostCount = count($usedHosts); $triggerItems = array(); $trigger['items'] = CMacrosResolverHelper::resolveItemNames($trigger['items']); foreach ($trigger['items'] as $item) { $triggerItems[] = array('name' => $usedHostCount > 1 ? $usedHosts[$item['hostid']] . NAME_DELIMITER . $item['name_expanded'] : $item['name_expanded'], 'params' => array('itemid' => $item['itemid'], 'action' => in_array($item['value_type'], array(ITEM_VALUE_TYPE_FLOAT, ITEM_VALUE_TYPE_UINT64)) ? 'showgraph' : 'showvalues')); } $description = new CSpan($trigger['description'], 'link_menu'); $description->setMenuPopup(getMenuPopupTrigger($trigger, $triggerItems)); if ($_REQUEST['show_details']) { $description = array($description, BR(), explode_exp($trigger['expression'], true, true)); } if (!empty($trigger['dependencies'])) { $dependenciesTable = new CTableInfo(); $dependenciesTable->setAttribute('style', 'width: 200px;'); $dependenciesTable->addRow(bold(_('Depends on') . NAME_DELIMITER)); foreach ($trigger['dependencies'] as $dependency) { $dependenciesTable->addRow(' - ' . CMacrosResolverHelper::resolveTriggerNameById($dependency['triggerid'])); } $img = new Cimg('images/general/arrow_down2.png', 'DEP_UP'); $img->setAttribute('style', 'vertical-align: middle; border: 0px;'); $img->setHint($dependenciesTable); $description = array($img, SPACE, $description); } $dependency = false; $dependenciesTable = new CTableInfo();
$last_page['url'] = 'tr_status.php?hostid=' . get_profile('web.tr_status.hostid', 0); } redirect($last_page['url']); exit; } else { if (isset($_REQUEST['cancel'])) { $last_page = get_last_history_page(); if (!$last_page) { $last_page['url'] = 'tr_status.php?hostid=' . get_profile('web.tr_status.hostid', 0); } redirect($last_page['url']); exit; } } } $msg = $bulk ? ' BULK ACKNOWLEDGE ' : array('"' . expand_trigger_description_by_data($db_data) . '"', BR(), explode_exp($db_data["expression"], 1)); show_table_header(array(S_ALARM_ACKNOWLEDGES_BIG, ' : ', $msg)); echo SBR; if (!$bulk) { $table = new CTable(NULL, "ack_msgs"); $table->SetAlign("center"); $db_acks = get_acknowledges_by_eventid($db_data["eventid"]); while ($db_ack = DBfetch($db_acks)) { $db_user = get_user_by_userid($db_ack["userid"]); $table->AddRow(array(new CCol($db_user["alias"], "user"), new CCol(date("d-m-Y h:i:s A", $db_ack["clock"]), "time")), "title"); $msgCol = new CCol(zbx_nl2br($db_ack['message'])); $msgCol->SetColspan(2); $table->AddRow($msgCol, "msg"); } /**/ if ($table->GetNumRows() > 0) {
function insert_trigger_form() { global $USER_DETAILS; $frmTrig = new CFormTable(S_TRIGGER, "triggers.php"); $frmTrig->SetHelp("config_triggers.php"); if ($_REQUEST['hostid'] > 0) { $frmTrig->addVar("hostid", $_REQUEST['hostid']); } $dep_el = array(); $dependencies = get_request("dependencies", array()); $limited = null; if (isset($_REQUEST["triggerid"])) { $frmTrig->addVar("triggerid", $_REQUEST["triggerid"]); $trigger = get_trigger_by_triggerid($_REQUEST["triggerid"]); $frmTrig->SetTitle(S_TRIGGER . ' "' . htmlspecialchars($trigger["description"]) . '"'); $limited = $trigger['templateid'] ? 'yes' : null; } $expression = get_request("expression", ""); $description = get_request("description", ""); $type = get_request('type', 0); $priority = get_request("priority", 0); $status = get_request("status", 0); $comments = get_request("comments", ""); $url = get_request("url", ""); if (isset($_REQUEST["triggerid"]) && !isset($_REQUEST["form_refresh"]) || isset($limited)) { $description = $trigger["description"]; $expression = explode_exp($trigger["expression"], 0); if (!isset($limited) || !isset($_REQUEST["form_refresh"])) { $type = $trigger['type']; $priority = $trigger["priority"]; $status = $trigger["status"]; $comments = $trigger["comments"]; $url = $trigger["url"]; $trigs = DBselect('SELECT t.triggerid,t.description,t.expression ' . ' FROM triggers t,trigger_depends d ' . ' WHERE t.triggerid=d.triggerid_up ' . ' AND d.triggerid_down=' . $_REQUEST['triggerid']); while ($trig = DBfetch($trigs)) { if (uint_in_array($trig["triggerid"], $dependencies)) { continue; } array_push($dependencies, $trig["triggerid"]); } } } $frmTrig->addRow(S_NAME, new CTextBox("description", $description, 90, $limited)); $frmTrig->addRow(S_EXPRESSION, array(new CTextBox("expression", $expression, 75, $limited), $limited ? null : new CButton('insert', S_INSERT, "return PopUp('popup_trexpr.php?dstfrm=" . $frmTrig->GetName() . "&dstfld1=expression&srctbl=expression" . "&srcfld1=expression&expression=' + escape(getSelectedText(this.form.elements['expression'])),700,200);"))); /* dependencies */ foreach ($dependencies as $val) { array_push($dep_el, array(new CCheckBox("rem_dependence[]", 'no', null, strval($val)), expand_trigger_description($val)), BR()); $frmTrig->addVar("dependencies[]", strval($val)); } if (count($dep_el) == 0) { array_push($dep_el, S_NO_DEPENDENCES_DEFINED); } else { array_push($dep_el, new CButton('del_dependence', 'delete selected')); } $frmTrig->addRow(S_THE_TRIGGER_DEPENDS_ON, $dep_el); /* end dependencies */ /* new dependence */ // $frmTrig->addVar('new_dependence','0'); // $txtCondVal = new CTextBox('trigger','',75,'yes'); $btnSelect = new CButton('btn1', S_ADD, "return PopUp('popup.php?dstfrm=" . $frmTrig->GetName() . "&dstfld1=new_dependence[]&srctbl=triggers&multiselect=1&dstact=add_dependence&objname=triggers&srcfld1=1" . "',750,450);", 'T'); $frmTrig->addRow(S_NEW_DEPENDENCY, $btnSelect, 'new'); /* end new dependence */ $type_select = new CComboBox('type'); $type_select->additem(TRIGGER_MULT_EVENT_DISABLED, S_NORMAL, $type == TRIGGER_MULT_EVENT_ENABLED ? 'no' : 'yes'); $type_select->additem(TRIGGER_MULT_EVENT_ENABLED, S_NORMAL . SPACE . '+' . SPACE . S_MULTIPLE_TRUE_EVENTS, $type == TRIGGER_MULT_EVENT_ENABLED ? 'yes' : 'no'); $frmTrig->addRow(S_EVENT_GENERATION, $type_select); $cmbPrior = new CComboBox("priority", $priority); for ($i = 0; $i <= 5; $i++) { $cmbPrior->addItem($i, get_severity_description($i)); } $frmTrig->addRow(S_SEVERITY, $cmbPrior); $frmTrig->addRow(S_COMMENTS, new CTextArea("comments", $comments, 90, 7)); $frmTrig->addRow(S_URL, new CTextBox("url", $url, 90)); $frmTrig->addRow(S_DISABLED, new CCheckBox("status", $status)); $frmTrig->addItemToBottomRow(new CButton("save", S_SAVE)); if (isset($_REQUEST["triggerid"])) { $frmTrig->addItemToBottomRow(SPACE); $frmTrig->addItemToBottomRow(new CButton("clone", S_CLONE)); $frmTrig->addItemToBottomRow(SPACE); if (!$limited) { $frmTrig->addItemToBottomRow(new CButtonDelete("Delete trigger?", url_param("form") . url_param('groupid') . url_param("hostid") . url_param("triggerid"))); } } $frmTrig->addItemToBottomRow(SPACE); $frmTrig->addItemToBottomRow(new CButtonCancel(url_param('groupid') . url_param("hostid"))); $frmTrig->Show(); }
public static function parseMain($rules) { $triggersForDependencies = array(); if (!empty($rules['hosts']['updateExisting']) || !empty($rules['hosts']['createMissing']) || !empty($rules['templates']['createMissing']) || !empty($rules['templates']['updateExisting'])) { $xpath = new DOMXPath(self::$xml); $hosts = $xpath->query('hosts/host'); foreach ($hosts as $host) { $host_db = self::mapXML2arr($host, XML_TAG_HOST); if (!isset($host_db['status'])) { $host_db['status'] = HOST_STATUS_TEMPLATE; } $current_host = $host_db['status'] == HOST_STATUS_TEMPLATE ? API::Template()->exists($host_db) : API::Host()->exists($host_db); if (!$current_host && ($host_db['status'] == HOST_STATUS_TEMPLATE && empty($rules['templates']['createMissing']) || $host_db['status'] != HOST_STATUS_TEMPLATE && empty($rules['hosts']['createMissing']))) { continue; } if ($current_host && ($host_db['status'] == HOST_STATUS_TEMPLATE && empty($rules['templates']['updateExisting']) || $host_db['status'] != HOST_STATUS_TEMPLATE && empty($rules['hosts']['updateExisting']))) { continue; } // there were no host visible names in 1.8 if (!isset($host_db['name'])) { $host_db['name'] = $host_db['host']; } // host will have no interfaces - we will be creating them separately $host_db['interfaces'] = null; // it is possible, that data is imported from 1.8, where there was only one network interface per host /** * @todo when new XML format will be introduced, this check should be changed to XML version check */ $old_version_input = $host_db['status'] != HOST_STATUS_TEMPLATE; if ($old_version_input) { // rearranging host structure, so it would look more like 2.0 host $interfaces = array(); // the main interface is always "agent" type if (!is_null($host_db['ip'])) { $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_AGENT, 'useip' => $host_db['useip'], 'ip' => $host_db['ip'], 'dns' => $host_db['dns'], 'port' => $host_db['port']); } // now we need to check if host had SNMP items. If it had, we need and SNMP interface for every different port. $items = $xpath->query('items/item', $host); $snmp_interface_ports_created = array(); foreach ($items as $item) { $item_db = self::mapXML2arr($item, XML_TAG_ITEM); if (($item_db['type'] == ITEM_TYPE_SNMPV1 || $item_db['type'] == ITEM_TYPE_SNMPV2C || $item_db['type'] == ITEM_TYPE_SNMPV3) && !isset($snmp_interface_ports_created[$item_db['snmp_port']])) { $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_SNMP, 'useip' => $host_db['useip'], 'ip' => $host_db['ip'], 'dns' => $host_db['dns'], 'port' => $item_db['snmp_port']); $snmp_interface_ports_created[$item_db['snmp_port']] = 1; } } unset($snmp_interface_ports_created); // it was a temporary variable // we ned to add ipmi interface if at least one ipmi item exists foreach ($items as $item) { $item_db = self::mapXML2arr($item, XML_TAG_ITEM); if ($item_db['type'] == ITEM_TYPE_IPMI) { // when saving a host in 1.8, it's possible to set useipmi=1 and not to fill an IP address // we were not really sure what to do with this host, // and decided to take host IP address instead and show info message about this if ($host_db['ipmi_ip'] == '') { $ipmi_ip = $host_db['ip']; info(_s('Host "%s" has "useipmi" parameter checked, but has no "ipmi_ip" parameter! Using host IP address as an address for IPMI interface.', $host_db['host'])); } else { $ipmi_ip = $host_db['ipmi_ip']; } $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_IPMI, 'useip' => INTERFACE_USE_DNS, 'ip' => '', 'dns' => $ipmi_ip, 'port' => $host_db['ipmi_port']); // we need only one ipmi interface break; } } } if ($current_host) { $options = array('filter' => array('host' => $host_db['host']), 'output' => API_OUTPUT_EXTEND, 'editable' => 1, 'selectInterfaces' => API_OUTPUT_EXTEND); if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $current_host = API::Template()->get($options); } else { $current_host = API::Host()->get($options); } if (empty($current_host)) { throw new Exception(_s('No permission for host "%1$s".', $host_db['host'])); } else { $current_host = reset($current_host); } // checking if host already exists - then some of the interfaces may not need to be created if ($host_db['status'] != HOST_STATUS_TEMPLATE) { // for every interface we got based on XML foreach ($interfaces as $i => $interface_db) { // checking every interface of current host foreach ($current_host['interfaces'] as $interface) { // if all parameters of interface are identical if ($interface['type'] == $interface_db['type'] && $interface['ip'] == $interface_db['ip'] && $interface['dns'] == $interface_db['dns'] && $interface['port'] == $interface_db['port'] && $interface['useip'] == $interface_db['useip']) { // this interface is the same as existing one! $interfaces[$i]['interfaceid'] = $interface['interfaceid']; break; } } } } $interfaces_created_with_host = false; } else { if ($host_db['status'] != HOST_STATUS_TEMPLATE) { $host_db['interfaces'] = $interfaces; $interfaces_created_with_host = true; } } // HOST GROUPS {{{ $groups = $xpath->query('groups/group', $host); $host_db['groups'] = array(); $groups_to_parse = array(); foreach ($groups as $group) { $groups_to_parse[] = array('name' => $group->nodeValue); } if (empty($groups_to_parse)) { $groups_to_parse[] = array('name' => ZBX_DEFAULT_IMPORT_HOST_GROUP); } foreach ($groups_to_parse as $group) { $current_group = API::HostGroup()->exists($group); if ($current_group) { $options = array('filter' => $group, 'output' => API_OUTPUT_EXTEND, 'editable' => 1); $current_group = API::HostGroup()->get($options); if (empty($current_group)) { throw new Exception(_s('No permissions for group "%1$s".', $group['name'])); } $host_db['groups'][] = reset($current_group); } else { $result = API::HostGroup()->create($group); if (!$result) { throw new Exception(); } $options = array('groupids' => $result['groupids'], 'output' => API_OUTPUT_EXTEND); $new_group = API::HostGroup()->get($options); $host_db['groups'][] = reset($new_group); } } // }}} HOST GROUPS // MACROS $macros = $xpath->query('macros/macro', $host); if ($macros->length > 0) { $host_db['macros'] = array(); foreach ($macros as $macro) { $host_db['macros'][] = self::mapXML2arr($macro, XML_TAG_MACRO); } } // }}} MACROS // host inventory if ($old_version_input) { if (!isset($host_db['inventory'])) { $host_db['inventory'] = array(); } $inventoryNode = $xpath->query('host_profile/*', $host); if ($inventoryNode->length > 0) { foreach ($inventoryNode as $field) { $newInventoryName = self::mapInventoryName($field->nodeName); $host_db['inventory'][$newInventoryName] = $field->nodeValue; } } $inventoryNodeExt = $xpath->query('host_profiles_ext/*', $host); if ($inventoryNodeExt->length > 0) { foreach ($inventoryNodeExt as $field) { $newInventoryName = self::mapInventoryName($field->nodeName); if (isset($host_db['inventory'][$newInventoryName]) && $field->nodeValue !== '') { $host_db['inventory'][$newInventoryName] .= "\r\n\r\n"; $host_db['inventory'][$newInventoryName] .= $field->nodeValue; } else { $host_db['inventory'][$newInventoryName] = $field->nodeValue; } } } $host_db['inventory_mode'] = isset($host_db['inventory']) ? HOST_INVENTORY_MANUAL : HOST_INVENTORY_DISABLED; } // HOSTS if (isset($host_db['proxy_hostid'])) { $proxy_exists = API::Proxy()->get(array('proxyids' => $host_db['proxy_hostid'])); if (empty($proxy_exists)) { $host_db['proxy_hostid'] = 0; } } if ($current_host && (!empty($rules['hosts']['updateExisting']) || !empty($rules['templates']['updateExisting']))) { if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $host_db['templateid'] = $current_host['templateid']; $result = API::Template()->update($host_db); $current_hostid = $current_host['templateid']; } else { $host_db['hostid'] = $current_host['hostid']; $result = API::Host()->update($host_db); $current_hostid = $current_host['hostid']; } if (!$result) { throw new Exception(); } } if (!$current_host && (!empty($rules['hosts']['createMissing']) || !empty($rules['templates']['createMissing']))) { if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $result = API::Template()->create($host_db); if (!$result) { throw new Exception(); } $current_hostid = reset($result['templateids']); } else { $result = API::Host()->create($host_db); if (!$result) { throw new Exception(); } $current_hostid = reset($result['hostids']); } } $current_hostname = $host_db['host']; // TEMPLATES {{{ if (!empty($rules['templateLinkage']['createMissing'])) { $templates = $xpath->query('templates/template', $host); $templateLinkage = array(); foreach ($templates as $template) { $options = array('filter' => array('host' => $template->nodeValue), 'output' => array('templateid'), 'editable' => true); $current_template = API::Template()->get($options); if (empty($current_template)) { throw new Exception(_s('No permission for template "%1$s".', $template->nodeValue)); } $current_template = reset($current_template); $templateLinkage[] = $current_template; } if ($templateLinkage) { $result = API::Template()->massAdd(array('hosts' => array('hostid' => $current_hostid), 'templates' => $templateLinkage)); if (!$result) { throw new Exception(); } } } // }}} TEMPLATES // ITEMS {{{ if (!empty($rules['items']['updateExisting']) || !empty($rules['items']['createMissing'])) { $items = $xpath->query('items/item', $host); // if this is an export from 1.8, we need to make some adjustments to items if ($old_version_input) { if (!$interfaces_created_with_host) { // if host had another interfaces, we are not touching them: they remain as is foreach ($interfaces as $i => $interface) { // interface was not already created if (!isset($interface['interfaceid'])) { // creating interface $interface['hostid'] = $current_hostid; $ids = API::HostInterface()->create($interface); if ($ids === false) { throw new Exception(); } $interfaces[$i]['interfaceid'] = reset($ids['interfaceids']); } } } else { $options = array('hostids' => $current_hostid, 'output' => API_OUTPUT_EXTEND); $interfaces = API::HostInterface()->get($options); } // we must know interface ids to assign them to items $agent_interface_id = null; $ipmi_interface_id = null; $snmp_interfaces = array(); // hash 'port' => 'iterfaceid' foreach ($interfaces as $interface) { switch ($interface['type']) { case INTERFACE_TYPE_AGENT: $agent_interface_id = $interface['interfaceid']; break; case INTERFACE_TYPE_IPMI: $ipmi_interface_id = $interface['interfaceid']; break; case INTERFACE_TYPE_SNMP: $snmp_interfaces[$interface['port']] = $interface['interfaceid']; break; } } } foreach ($items as $item) { $item_db = self::mapXML2arr($item, XML_TAG_ITEM); $item_db['hostid'] = $current_hostid; // item needs interfaces if ($old_version_input) { // 'snmp_port' column was renamed to 'port' if ($item_db['snmp_port'] != 0) { // zabbix agent items have no ports $item_db['port'] = $item_db['snmp_port']; } unset($item_db['snmp_port']); // assigning appropriate interface depending on item type switch ($item_db['type']) { // zabbix agent interface case ITEM_TYPE_ZABBIX: case ITEM_TYPE_SIMPLE: case ITEM_TYPE_EXTERNAL: case ITEM_TYPE_SSH: case ITEM_TYPE_TELNET: $item_db['interfaceid'] = $agent_interface_id; break; // snmp interface // snmp interface case ITEM_TYPE_SNMPV1: case ITEM_TYPE_SNMPV2C: case ITEM_TYPE_SNMPV3: // for an item with different port - different interface $item_db['interfaceid'] = $snmp_interfaces[$item_db['port']]; break; case ITEM_TYPE_IPMI: $item_db['interfaceid'] = $ipmi_interface_id; break; // no interfaces required for these item types // no interfaces required for these item types case ITEM_TYPE_HTTPTEST: case ITEM_TYPE_CALCULATED: case ITEM_TYPE_AGGREGATE: case ITEM_TYPE_INTERNAL: case ITEM_TYPE_ZABBIX_ACTIVE: case ITEM_TYPE_TRAPPER: case ITEM_TYPE_DB_MONITOR: $item_db['interfaceid'] = null; break; } $item_db['key_'] = self::convertOldSimpleKey($item_db['key_']); } $options = array('filter' => array('hostid' => $item_db['hostid'], 'key_' => $item_db['key_']), 'webitems' => 1, 'output' => API_OUTPUT_EXTEND, 'editable' => 1); $current_item = API::Item()->get($options); $current_item = reset($current_item); if (!$current_item && empty($rules['items']['createMissing'])) { info(_s('Item "%1$s" skipped - user rule.', $item_db['key_'])); continue; // break if not update updateExisting } if ($current_item && empty($rules['items']['updateExisting'])) { info(_s('Item "%1$s" skipped - user rule.', $item_db['key_'])); continue; // break if not update updateExisting } // ITEM APPLICATIONS {{{ $applications = $xpath->query('applications/application', $item); $item_applications = array(); $applications_to_add = array(); $applicationsIds = array(); foreach ($applications as $application) { $application_db = array('name' => $application->nodeValue, 'hostid' => $current_hostid); $current_application = API::Application()->get(array('filter' => $application_db, 'output' => API_OUTPUT_EXTEND)); $applicationValue = reset($current_application); if ($current_application) { if (empty($item_applications)) { $item_applications = $current_application; $applicationsIds[] = $applicationValue['applicationid']; } else { if (!in_array($applicationValue['applicationid'], $applicationsIds)) { $item_applications = array_merge($item_applications, $current_application); $applicationsIds[] = $applicationValue['applicationid']; } } } else { $applications_to_add[] = $application_db; } } if (!empty($applications_to_add)) { $result = API::Application()->create($applications_to_add); if (!$result) { throw new Exception(); } $options = array('applicationids' => $result['applicationids'], 'output' => API_OUTPUT_EXTEND); $new_applications = API::Application()->get($options); $item_applications = array_merge($item_applications, $new_applications); } // }}} ITEM APPLICATIONS if ($current_item && !empty($rules['items']['updateExisting'])) { $item_db['itemid'] = $current_item['itemid']; $result = API::Item()->update($item_db); if (!$result) { throw new Exception(); } $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND); $current_item = API::Item()->get($options); } if (!$current_item && !empty($rules['items']['createMissing'])) { $result = API::Item()->create($item_db); if (!$result) { throw new Exception(); } $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND); $current_item = API::Item()->get($options); } if (!empty($item_applications)) { $r = API::Application()->massAdd(array('applications' => $item_applications, 'items' => $current_item)); if ($r === false) { throw new Exception(); } } } } // }}} ITEMS // TRIGGERS {{{ if (!empty($rules['triggers']['updateExisting']) || !empty($rules['triggers']['createMissing'])) { $triggers = $xpath->query('triggers/trigger', $host); $triggers_to_add = array(); $triggers_to_upd = array(); foreach ($triggers as $trigger) { $trigger_db = self::mapXML2arr($trigger, XML_TAG_TRIGGER); if ($old_version_input) { $expressionPart = explode(':', $trigger_db['expression']); $keyName = explode(',', $expressionPart[1], 2); if (count($keyName) == 2) { $keyValue = explode('.', $keyName[1], 2); $key = $keyName[0] . "," . $keyValue[0]; if (in_array($keyName[0], self::$oldKeys) || in_array($keyName[0], self::$oldKeysPref)) { $trigger_db['expression'] = str_replace($key, self::convertOldSimpleKey($key), $trigger_db['expression']); } } } // {HOSTNAME} is here for backward compatibility $trigger_db['expression'] = str_replace('{{HOSTNAME}:', '{' . $host_db['host'] . ':', $trigger_db['expression']); $trigger_db['expression'] = str_replace('{{HOST.HOST}:', '{' . $host_db['host'] . ':', $trigger_db['expression']); $trigger_db['hostid'] = $current_hostid; if ($current_trigger = API::Trigger()->exists($trigger_db)) { $ctriggers = API::Trigger()->get(array('filter' => array('description' => $trigger_db['description']), 'hostids' => $current_hostid, 'output' => API_OUTPUT_EXTEND, 'editable' => 1)); $current_trigger = false; foreach ($ctriggers as $ct) { $tmp_exp = explode_exp($ct['expression']); if (strcmp($trigger_db['expression'], $tmp_exp) == 0) { $current_trigger = $ct; break; } } if (!$current_trigger) { throw new Exception(_s('No permission for trigger "%s".', $trigger_db['description'])); } } unset($trigger_db['hostid']); if (!$current_trigger && empty($rules['triggers']['createMissing'])) { info(_s('Trigger "%1$s" skipped - user rule.', $trigger_db['description'])); continue; // break if not update updateExisting } if ($current_trigger && empty($rules['triggers']['updateExisting'])) { info(_s('Trigger "%1$s" skipped - user rule.', $trigger_db['description'])); continue; // break if not update updateExisting } if ($current_trigger && !empty($rules['triggers']['updateExisting'])) { $trigger_db['triggerid'] = $current_trigger['triggerid']; $triggers_to_upd[] = $trigger_db; } if (!$current_trigger && !empty($rules['triggers']['createMissing'])) { $triggers_to_add[] = $trigger_db; } } if (!empty($triggers_to_upd)) { $result = API::Trigger()->update($triggers_to_upd); if (!$result) { throw new Exception(); } $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND); $r = API::Trigger()->get($options); $triggersForDependencies = array_merge($triggersForDependencies, $r); } if (!empty($triggers_to_add)) { $result = API::Trigger()->create($triggers_to_add); if (!$result) { throw new Exception(); } $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND); $r = API::Trigger()->get($options); $triggersForDependencies = array_merge($triggersForDependencies, $r); } } // }}} TRIGGERS // GRAPHS {{{ if (!empty($rules['graphs']['updateExisting']) || !empty($rules['graphs']['createMissing'])) { $graphs = $xpath->query('graphs/graph', $host); $graphs_to_add = array(); $graphs_to_upd = array(); foreach ($graphs as $graph) { // GRAPH ITEMS {{{ $gitems = $xpath->query('graph_elements/graph_element', $graph); $graph_hostids = array(); $graph_items = array(); foreach ($gitems as $gitem) { $gitem_db = self::mapXML2arr($gitem, XML_TAG_GRAPH_ELEMENT); $data = explode(':', $gitem_db['host_key_']); $gitem_host = array_shift($data); // {HOSTNAME} is here for backward compatibility $gitem_db['host'] = $gitem_host == '{HOSTNAME}' ? $host_db['host'] : $gitem_host; $gitem_db['host'] = $gitem_host == '{HOST.HOST}' ? $host_db['host'] : $gitem_host; if ($old_version_input) { $data[0] = self::convertOldSimpleKey($data[0]); } $gitem_db['key_'] = implode(':', $data); if ($current_item = API::Item()->exists($gitem_db)) { $current_item = API::Item()->get(array('filter' => array('key_' => $gitem_db['key_']), 'webitems' => 1, 'host' => $gitem_db['host'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1)); if (empty($current_item)) { throw new Exception(_s('No permission for item "%1$s".', $gitem_db['key_'])); } $current_item = reset($current_item); $graph_hostids[] = $current_item['hostid']; $gitem_db['itemid'] = $current_item['itemid']; $graph_items[] = $gitem_db; } else { throw new Exception(_s('Item "%1$s" does not exist.', $gitem_db['host_key_'])); } } // }}} GRAPH ITEMS $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH); $graph_db['hostids'] = $graph_hostids; // do we need to show the graph legend, after it is imported? // in 1.8, this setting was present only for pie and exploded graphs // for other graph types we are always showing the legend if ($graph_db['graphtype'] != GRAPH_TYPE_PIE && $graph_db['graphtype'] != GRAPH_TYPE_EXPLODED) { $graph_db['show_legend'] = 1; } $current_graph = API::Graph()->exists($graph_db); if ($current_graph) { $current_graph = API::Graph()->get(array('filter' => array('name' => $graph_db['name']), 'hostids' => $graph_db['hostids'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1)); if (empty($current_graph)) { throw new Exception(_s('No permission for graph "%1$s".', $graph_db['name'])); } $current_graph = reset($current_graph); } if (!$current_graph && empty($rules['graphs']['createMissing'])) { info(_s('Graph "%1$s" skipped - user rule.', $graph_db['name'])); continue; // break if not update updateExisting } if ($current_graph && empty($rules['graphs']['updateExisting'])) { info(_s('Graph "%1$s" skipped - user rule.', $graph_db['name'])); continue; // break if not update updateExisting } if (!isset($graph_db['ymin_type'])) { throw new Exception(_s('No "ymin_type" field for graph "%s".', $graph_db['name'])); } if (!isset($graph_db['ymax_type'])) { throw new Exception(_s('No "ymax_type" field for graph "%s".', $graph_db['name'])); } if ($graph_db['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $item_data = explode(':', $graph_db['ymin_item_key'], 2); if (count($item_data) < 2) { throw new Exception(_s('Incorrect y min item for graph "%1$s".', $graph_db['name'])); } if (!($item = get_item_by_key($item_data[1], $item_data[0]))) { throw new Exception(_s('Missing item "%1$s" for host "%2$s".', $graph_db['ymin_item_key'], $host_db['host'])); } $graph_db['ymin_itemid'] = $item['itemid']; } if ($graph_db['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $item_data = explode(':', $graph_db['ymax_item_key'], 2); if (count($item_data) < 2) { throw new Exception(_s('Incorrect y max item for graph "%1$s".', $graph_db['name'])); } if (!($item = get_item_by_key($item_data[1], $item_data[0]))) { throw new Exception(_s('Missing item "%1$s" for host "%2$s".', $graph_db['ymax_item_key'], $host_db['host'])); } $graph_db['ymax_itemid'] = $item['itemid']; } $graph_db['gitems'] = $graph_items; if ($current_graph) { $graph_db['graphid'] = $current_graph['graphid']; $graphs_to_upd[] = $graph_db; } else { $graphs_to_add[] = $graph_db; } } if (!empty($graphs_to_add)) { $r = API::Graph()->create($graphs_to_add); if ($r === false) { throw new Exception(); } } if (!empty($graphs_to_upd)) { $r = API::Graph()->update($graphs_to_upd); if ($r === false) { throw new Exception(); } } } // SCREENS if (!empty($rules['screens']['updateExisting']) || !empty($rules['screens']['createMissing'])) { $screens_node = $xpath->query('screens', $host); if ($screens_node->length > 0) { $importScreens = self::XMLtoArray($screens_node->item(0)); foreach ($importScreens as $screen) { $current_screen = API::TemplateScreen()->get(array('filter' => array('name' => $screen['name']), 'templateids' => $current_hostid, 'output' => API_OUTPUT_EXTEND, 'editable' => 1)); $current_screen = reset($current_screen); if (!$current_screen && empty($rules['screens']['createMissing'])) { info(_s('Screen "%1$s" skipped - user rule.', $screen['name'])); continue; } if ($current_screen && empty($rules['screens']['updateExisting'])) { info(_s('Screen "%1$s" skipped - user rule.', $screen['name'])); continue; } if (isset($screen['screenitems'])) { foreach ($screen['screenitems'] as &$screenitem) { $nodeCaption = isset($screenitem['resourceid']['node']) ? $screenitem['resourceid']['node'] . ':' : ''; if (!isset($screenitem['resourceid'])) { $screenitem['resourceid'] = 0; } if (is_array($screenitem['resourceid'])) { switch ($screenitem['resourcetype']) { case SCREEN_RESOURCE_GRAPH: $db_graphs = API::Graph()->getObjects($screenitem['resourceid']); if (empty($db_graphs)) { $error = _s('Cannot find graph "%1$s" used in screen "%2$s".', $nodeCaption . $screenitem['resourceid']['host'] . ':' . $screenitem['resourceid']['name'], $screen['name']); throw new Exception($error); } $tmp = reset($db_graphs); $screenitem['resourceid'] = $tmp['graphid']; break; case SCREEN_RESOURCE_SIMPLE_GRAPH: case SCREEN_RESOURCE_PLAIN_TEXT: $db_items = API::Item()->getObjects($screenitem['resourceid']); if (empty($db_items)) { $error = _s('Cannot find item "%1$s" used in screen "%2$s".', $nodeCaption . $screenitem['resourceid']['host'] . ':' . $screenitem['resourceid']['key_'], $screen['name']); throw new Exception($error); } $tmp = reset($db_items); $screenitem['resourceid'] = $tmp['itemid']; break; default: $screenitem['resourceid'] = 0; break; } } } } $screen['templateid'] = $current_hostid; if ($current_screen) { $screen['screenid'] = $current_screen['screenid']; $result = API::TemplateScreen()->update($screen); if (!$result) { throw new Exception(_('Cannot update screen.')); } info('[' . $current_hostname . '] ' . _s('Screen "%1$s" updated.', $screen['name'])); } else { $result = API::TemplateScreen()->create($screen); if (!$result) { throw new Exception(_('Cannot create screen.')); } info('[' . $current_hostname . '] ' . _s('Screen "%1$s" added.', $screen['name'])); } } } } } // DEPENDENCIES $dependencies = $xpath->query('dependencies/dependency'); if ($dependencies->length > 0) { $triggersForDependencies = zbx_objectValues($triggersForDependencies, 'triggerid'); $triggersForDependencies = array_flip($triggersForDependencies); $newDependencies = array(); foreach ($dependencies as $dependency) { $triggerDescription = $dependency->getAttribute('description'); $currentTrigger = get_trigger_by_description($triggerDescription); if ($currentTrigger && isset($triggersForDependencies[$currentTrigger['triggerid']])) { $dependsOnList = $xpath->query('depends', $dependency); foreach ($dependsOnList as $dependsOn) { $depTrigger = get_trigger_by_description($dependsOn->nodeValue); if ($depTrigger['triggerid']) { $newDependencies[] = array('triggerid' => $currentTrigger['triggerid'], 'dependsOnTriggerid' => $depTrigger['triggerid']); } } } } if ($newDependencies) { API::Trigger()->addDependencies($newDependencies); } } } }
/** * Checks that none of the given triggers is inherited from a template. * * @throws APIException if one of the triggers is inherited * * @param array $triggerIds */ protected function checkNotInherited(array $triggerIds) { $trigger = DBfetch(DBselect('SELECT t.triggerid,t.description,t.expression' . ' FROM triggers t' . ' WHERE ' . dbConditionInt('t.triggerid', $triggerIds) . 'AND t.templateid IS NOT NULL', 1)); if ($trigger) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot delete templated trigger "%1$s:%2$s".', $trigger['description'], explode_exp($trigger['expression']))); } }
/** * Copies all of the triggers from the source discovery to the target discovery rule. * * @throws APIException if trigger saving fails * * @param array $srcDiscovery The source discovery rule to copy from * @param array $dstDiscovery The target discovery rule to copy to * @param array $srcHost The host the source discovery belongs to * @param array $dstHost The host the target discovery belongs to * * @return array */ protected function copyTriggerPrototypes(array $srcDiscovery, array $dstDiscovery, array $srcHost, array $dstHost) { $srcTriggers = API::TriggerPrototype()->get(array('discoveryids' => $srcDiscovery['itemid'], 'output' => API_OUTPUT_EXTEND, 'selectHosts' => API_OUTPUT_EXTEND, 'selectItems' => API_OUTPUT_EXTEND, 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectFunctions' => API_OUTPUT_EXTEND, 'preservekeys' => true)); if (!$srcTriggers) { return array(); } foreach ($srcTriggers as $id => $trigger) { // skip triggers with web items if (httpItemExists($trigger['items'])) { unset($srcTriggers[$id]); continue; } } // save new triggers $dstTriggers = $srcTriggers; foreach ($dstTriggers as $id => $trigger) { unset($dstTriggers[$id]['templateid']); unset($dstTriggers[$id]['triggerid']); // update expression $dstTriggers[$id]['expression'] = explode_exp($trigger['expression'], false, false, $srcHost['host'], $dstHost['host']); } $rs = API::TriggerPrototype()->create($dstTriggers); if (!$rs) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot clone trigger prototypes.')); } return $rs; }
public static function parseMain($rules) { $triggerExpressionConverter = new C24TriggerConverter(new CFunctionMacroParser(), new CMacroParser('#')); $triggersForDependencies = array(); if ($rules['hosts']['updateExisting'] || $rules['hosts']['createMissing'] || $rules['templates']['createMissing'] || $rules['templates']['updateExisting']) { $xpath = new DOMXPath(self::$xml); $hosts = $xpath->query('hosts/host'); // stores parsed host and template IDs $processedHostIds = array(); // stores converted trigger expressions for each host $triggerExpressions = array(); // stores converted item keys for each host $itemKeys = array(); // process hosts foreach ($hosts as $host) { $host_db = self::mapXML2arr($host, XML_TAG_HOST); if (!isset($host_db['status'])) { $host_db['status'] = HOST_STATUS_TEMPLATE; } if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $current_host = API::Template()->get(array('output' => array('templateid'), 'filter' => array('host' => $host_db['host']), 'nopermissions' => true, 'limit' => 1)); } else { $current_host = API::Host()->get(array('output' => array('hostid'), 'filter' => array('host' => $host_db['host']), 'nopermissions' => true, 'limit' => 1)); } if (!$current_host && ($host_db['status'] == HOST_STATUS_TEMPLATE && !$rules['templates']['createMissing'] || $host_db['status'] != HOST_STATUS_TEMPLATE && !$rules['hosts']['createMissing'])) { continue; } if ($current_host && ($host_db['status'] == HOST_STATUS_TEMPLATE && !$rules['templates']['updateExisting'] || $host_db['status'] != HOST_STATUS_TEMPLATE && !$rules['hosts']['updateExisting'])) { continue; } // there were no host visible names in 1.8 if (!isset($host_db['name'])) { $host_db['name'] = $host_db['host']; } // host will have no interfaces - we will be creating them separately $host_db['interfaces'] = null; // it is possible, that data is imported from 1.8, where there was only one network interface per host /** * @todo when new XML format will be introduced, this check should be changed to XML version check */ $oldVersionInput = $host_db['status'] != HOST_STATUS_TEMPLATE; $interfaces = array(); // rearranging host structure, so it would look more like 2.0 host if ($oldVersionInput) { // the main interface is always "agent" type if (!is_null($host_db['ip'])) { $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_AGENT, 'useip' => $host_db['useip'], 'ip' => $host_db['ip'], 'dns' => $host_db['dns'], 'port' => $host_db['port']); } // now we need to check if host had SNMP items. If it had, we need an SNMP interface for every different port. $items = $xpath->query('items/item', $host); $snmp_interface_ports_created = array(); foreach ($items as $item) { $item_db = self::mapXML2arr($item, XML_TAG_ITEM); if (($item_db['type'] == ITEM_TYPE_SNMPV1 || $item_db['type'] == ITEM_TYPE_SNMPV2C || $item_db['type'] == ITEM_TYPE_SNMPV3) && !isset($snmp_interface_ports_created[$item_db['snmp_port']])) { $interfaces[] = array('main' => $snmp_interface_ports_created ? INTERFACE_SECONDARY : INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_SNMP, 'useip' => $host_db['useip'], 'ip' => $host_db['ip'], 'dns' => $host_db['dns'], 'port' => $item_db['snmp_port']); $snmp_interface_ports_created[$item_db['snmp_port']] = 1; } } unset($snmp_interface_ports_created); // it was a temporary variable // we need to add ipmi interface if at least one ipmi item exists foreach ($items as $item) { $item_db = self::mapXML2arr($item, XML_TAG_ITEM); if ($item_db['type'] == ITEM_TYPE_IPMI) { // when saving a host in 1.8, it's possible to set useipmi=1 and not to fill an IP address // we were not really sure what to do with this host, // and decided to take host IP address instead and show info message about this if ($host_db['ipmi_ip'] === '') { $ipmi_ip = $host_db['ip']; info(_s('Host "%s" has "useipmi" parameter checked, but has no "ipmi_ip" parameter! Using host IP address as an address for IPMI interface.', $host_db['host'])); } else { $ipmi_ip = $host_db['ipmi_ip']; } $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_IPMI, 'useip' => INTERFACE_USE_IP, 'ip' => $ipmi_ip, 'dns' => '', 'port' => $host_db['ipmi_port']); // we need only one ipmi interface break; } } } if ($current_host) { $options = array('filter' => array('host' => $host_db['host']), 'output' => API_OUTPUT_EXTEND, 'editable' => true, 'selectInterfaces' => API_OUTPUT_EXTEND); if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $current_host = API::Template()->get($options); } else { $current_host = API::Host()->get($options); } if (empty($current_host)) { throw new Exception(_s('No permission for host "%1$s".', $host_db['host'])); } else { $current_host = reset($current_host); } // checking if host already exists - then some of the interfaces may not need to be created if ($host_db['status'] != HOST_STATUS_TEMPLATE) { $currentMainInterfaces = array(); $currentInterfacesByType = array(); // group existing main interfaces by interface type into $currentMainInterfaces // and group interfaces by type into $currentInterfacesByType foreach ($current_host['interfaces'] as $currentInterface) { if ($currentInterface['main'] == INTERFACE_PRIMARY) { $currentMainInterfaces[$currentInterface['type']] = $currentInterface; } $currentInterfacesByType[$currentInterface['type']][] = $currentInterface; } // loop through all interfaces we got from XML foreach ($interfaces as &$interfaceXml) { $interfaceXmlType = $interfaceXml['type']; // if this is the primary interface of some type and we have default interface of same type // in current (target) host, re-use "interfaceid" of the matching default interface // in current host if ($interfaceXml['main'] == INTERFACE_PRIMARY && isset($currentMainInterfaces[$interfaceXmlType])) { $interfaceXml['interfaceid'] = $currentMainInterfaces[$interfaceXmlType]['interfaceid']; } else { // otherwise, loop through all current (target) host interfaces with type of current // imported interface and re-use "interfaceid" in case if all interface parameters match if (isset($currentInterfacesByType[$interfaceXmlType])) { foreach ($currentInterfacesByType[$interfaceXmlType] as $currentInterface) { if ($currentInterface['ip'] == $interfaceXml['ip'] && $currentInterface['dns'] == $interfaceXml['dns'] && $currentInterface['port'] == $interfaceXml['port'] && $currentInterface['useip'] == $interfaceXml['useip']) { $interfaceXml['interfaceid'] = $currentInterface['interfaceid']; break; } } } } } unset($interfaceXml); $host_db['interfaces'] = $interfaces; } } elseif ($host_db['status'] != HOST_STATUS_TEMPLATE) { $host_db['interfaces'] = $interfaces; } // HOST GROUPS {{{ $groups = $xpath->query('groups/group', $host); $host_db['groups'] = array(); $groups_to_parse = array(); foreach ($groups as $group) { $groups_to_parse[] = array('name' => $group->nodeValue); } if (empty($groups_to_parse)) { $groups_to_parse[] = array('name' => ZBX_DEFAULT_IMPORT_HOST_GROUP); } foreach ($groups_to_parse as $group) { $hostGroup = API::HostGroup()->get(array('output' => API_OUTPUT_EXTEND, 'filter' => $group, 'editable' => true, 'limit' => 1)); if ($hostGroup) { $host_db['groups'][] = reset($hostGroup); } else { if ($rules['groups']['createMissing']) { $result = API::HostGroup()->create($group); if ($result) { $newHostGroup = API::HostGroup()->get(array('output' => API_OUTPUT_EXTEND, 'groupids' => $result['groupids'], 'limit' => 1)); $host_db['groups'][] = reset($newHostGroup); } } else { throw new Exception(_s('No permissions for host group "%1$s".', $group['name'])); } } } // }}} HOST GROUPS // MACROS $macros = $xpath->query('macros/macro', $host); if ($macros->length > 0) { $host_db['macros'] = array(); foreach ($macros as $macro) { $host_db['macros'][] = self::mapXML2arr($macro, XML_TAG_MACRO); } } // }}} MACROS // host inventory if ($oldVersionInput) { if (!isset($host_db['inventory'])) { $host_db['inventory'] = array(); } $inventoryNode = $xpath->query('host_profile/*', $host); if ($inventoryNode->length > 0) { foreach ($inventoryNode as $field) { $newInventoryName = self::mapInventoryName($field->nodeName); $host_db['inventory'][$newInventoryName] = $field->nodeValue; } } $inventoryNodeExt = $xpath->query('host_profiles_ext/*', $host); if ($inventoryNodeExt->length > 0) { foreach ($inventoryNodeExt as $field) { $newInventoryName = self::mapInventoryName($field->nodeName); if (isset($host_db['inventory'][$newInventoryName]) && $field->nodeValue !== '') { $host_db['inventory'][$newInventoryName] .= "\r\n\r\n"; $host_db['inventory'][$newInventoryName] .= $field->nodeValue; } else { $host_db['inventory'][$newInventoryName] = $field->nodeValue; } } } $host_db['inventory_mode'] = isset($host_db['inventory']) ? HOST_INVENTORY_MANUAL : HOST_INVENTORY_DISABLED; } if (isset($host_db['proxy_hostid'])) { $proxy_exists = API::Proxy()->get(array('output' => array('proxyid'), 'proxyids' => $host_db['proxy_hostid'])); if (empty($proxy_exists)) { $host_db['proxy_hostid'] = 0; } } if ($current_host && ($rules['hosts']['updateExisting'] || $rules['templates']['updateExisting'])) { if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $host_db['templateid'] = $current_host['templateid']; $result = API::Template()->update($host_db); $current_hostid = $current_host['templateid']; } else { $host_db['hostid'] = $current_host['hostid']; $result = API::Host()->update($host_db); $current_hostid = $current_host['hostid']; } } if (!$current_host && ($rules['hosts']['createMissing'] || $rules['templates']['createMissing'])) { if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $result = API::Template()->create($host_db); $current_hostid = reset($result['templateids']); } else { $result = API::Host()->create($host_db); $current_hostid = reset($result['hostids']); } } // store parsed host IDs $processedHostIds[$host_db['host']] = $current_hostid; } // gather triggers and convert old expressions $triggersXML = array(); // cycle each host and gather trigger descriptions and expressions foreach ($hosts as $host) { $host_db = self::mapXML2arr($host, XML_TAG_HOST); $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false; if ($current_hostid) { $triggersXML[$current_hostid] = array(); $triggerExpressions[$host_db['host']] = array(); $oldVersionInput = $host_db['status'] != HOST_STATUS_TEMPLATE; $triggers = $xpath->query('triggers/trigger', $host); foreach ($triggers as $trigger) { $trigger_db = self::mapXML2arr($trigger, XML_TAG_TRIGGER); $oldExpression = $trigger_db['expression']; if (!isset($triggerExpressions[$host_db['host']][$trigger_db['description']])) { $triggerExpressions[$host_db['host']][$trigger_db['description']] = array(); } if ($oldVersionInput) { $expressionPart = explode(':', $trigger_db['expression']); $keyName = explode(',', $expressionPart[1], 2); if (count($keyName) == 2) { $keyValue = explode('.', $keyName[1], 2); $key = $keyName[0] . "," . $keyValue[0]; if (in_array($keyName[0], self::$oldKeys) || in_array($keyName[0], self::$oldKeysPref)) { $trigger_db['expression'] = str_replace($key, self::convertOldSimpleKey($key), $trigger_db['expression']); } } } // {HOSTNAME} is here for backward compatibility $trigger_db['expression'] = str_replace('{{HOSTNAME}:', '{' . $host_db['host'] . ':', $trigger_db['expression']); $trigger_db['expression'] = str_replace('{{HOST.HOST}:', '{' . $host_db['host'] . ':', $trigger_db['expression']); $trigger_db['expression'] = $triggerExpressionConverter->convert($trigger_db['expression']); $triggersXML[$current_hostid][$trigger_db['description']][$trigger_db['expression']] = $trigger_db['expression']; $triggerExpressions[$host_db['host']][$trigger_db['description']][$oldExpression] = $trigger_db['expression']; } } } // delete missing triggers if ($rules['triggers']['deleteMissing']) { // select triggers from parsed hosts $dbTriggers = API::Trigger()->get(array('output' => array('triggerid', 'description', 'expression'), 'expandExpression' => true, 'hostids' => $processedHostIds, 'selectHosts' => array('hostid'), 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL))); // find corresponding trigger ID by description and expression $triggerIdsXML = array(); foreach ($dbTriggers as $dbTrigger) { $hostId = reset($dbTrigger['hosts']); if (isset($triggersXML[$hostId['hostid']][$dbTrigger['description']][$dbTrigger['expression']])) { $triggerIdsXML[$dbTrigger['triggerid']] = $dbTrigger['triggerid']; } } $triggersToDelete = array_diff_key($dbTriggers, $triggerIdsXML); $triggerIdsToDelete = array(); // check that potentially deletable trigger belongs to same hosts that are in XML // if some triggers belong to more hosts than current XML contains, don't delete them foreach ($triggersToDelete as $triggerId => $trigger) { $triggerHostIds = array_flip(zbx_objectValues($trigger['hosts'], 'hostid')); if (!array_diff_key($triggerHostIds, array_flip($processedHostIds))) { $triggerIdsToDelete[] = $triggerId; } } if ($triggerIdsToDelete) { API::Trigger()->delete($triggerIdsToDelete); } } // delete missing graphs if ($rules['graphs']['deleteMissing']) { $graphsXML = array(); // cycle each host and gather all graph names foreach ($hosts as $host) { $host_db = self::mapXML2arr($host, XML_TAG_HOST); $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false; if ($current_hostid) { $graphsXML[$current_hostid] = array(); $graphs = $xpath->query('graphs/graph', $host); foreach ($graphs as $graph) { $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH); $graphsXML[$current_hostid][$graph_db['name']] = $graph_db['name']; } } } // select graphs from already parsed hosts $dbGraphs = API::Graph()->get(array('output' => array('graphid', 'name'), 'hostids' => $processedHostIds, 'selectHosts' => array('hostid'), 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL))); $graphIdsXML = array(); foreach ($dbGraphs as $dbGraph) { $hostId = reset($dbGraph['hosts']); if (isset($graphsXML[$hostId['hostid']][$dbGraph['name']])) { $graphIdsXML[$dbGraph['graphid']] = $dbGraph['graphid']; } } $graphsToDelete = array_diff_key($dbGraphs, $graphIdsXML); $graphsIdsToDelete = array(); // check that potentially deletable graph belongs to same hosts that are in XML // if some graphs belong to more hosts than current XML contains, don't delete them foreach ($graphsToDelete as $graphId => $graph) { $graphHostIds = array_flip(zbx_objectValues($graph['hosts'], 'hostid')); if (!array_diff_key($graphHostIds, array_flip($processedHostIds))) { $graphsIdsToDelete[] = $graphId; } } if ($graphsIdsToDelete) { API::Graph()->delete($graphsIdsToDelete); } } // gather items and convert old keys $itemsXML = array(); foreach ($hosts as $host) { $host_db = self::mapXML2arr($host, XML_TAG_HOST); $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false; if ($current_hostid) { $itemsXML[$current_hostid] = array(); $itemKeys[$host_db['host']] = array(); $oldVersionInput = $host_db['status'] != HOST_STATUS_TEMPLATE; $items = $xpath->query('items/item', $host); foreach ($items as $item) { $item_db = self::mapXML2arr($item, XML_TAG_ITEM); if ($oldVersionInput) { $oldKey = $item_db['key_']; $item_db['key_'] = self::convertOldSimpleKey($item_db['key_']); $itemKeys[$host_db['host']][$oldKey] = $item_db['key_']; } $itemsXML[$current_hostid][$item_db['key_']] = $item_db['key_']; } } } // delete missing items if ($rules['items']['deleteMissing']) { $dbItems = API::Item()->get(array('output' => array('itemid', 'key_', 'hostid'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL))); $itemIdsXML = array(); foreach ($dbItems as $dbItem) { if (isset($itemsXML[$dbItem['hostid']][$dbItem['key_']])) { $itemIdsXML[$dbItem['itemid']] = $dbItem['itemid']; } } $itemsToDelete = array_diff_key($dbItems, $itemIdsXML); if ($itemsToDelete) { API::Item()->delete(array_keys($itemsToDelete)); } } // delete missing applications if ($rules['applications']['deleteMissing']) { $applicationsXML = array(); foreach ($hosts as $host) { $host_db = self::mapXML2arr($host, XML_TAG_HOST); $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false; if ($current_hostid) { $items = $xpath->query('items/item', $host); foreach ($items as $item) { $applications = $xpath->query('applications/application', $item); foreach ($applications as $application) { $applicationsXML[$current_hostid][$application->nodeValue] = $application->nodeValue; } } } } $dbApplications = API::Application()->get(array('output' => array('applicationid', 'hostid', 'name'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false)); $applicationsIdsXML = array(); foreach ($dbApplications as $dbApplication) { if (isset($applicationsXML[$dbApplication['hostid']][$dbApplication['name']])) { $applicationsIdsXML[$dbApplication['applicationid']] = $dbApplication['applicationid']; } } $applicationsToDelete = array_diff_key($dbApplications, $applicationsIdsXML); if ($applicationsToDelete) { API::Application()->delete(array_keys($applicationsToDelete)); } } // cycle each host again and create/update other objects foreach ($hosts as $host) { $host_db = self::mapXML2arr($host, XML_TAG_HOST); if (!isset($host_db['status'])) { $host_db['status'] = HOST_STATUS_TEMPLATE; } $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false; if (!$current_hostid) { continue; } $oldVersionInput = $host_db['status'] != HOST_STATUS_TEMPLATE; // TEMPLATES {{{ if (!empty($rules['templateLinkage']['createMissing'])) { $templates = $xpath->query('templates/template', $host); $templateLinkage = array(); foreach ($templates as $template) { $options = array('filter' => array('host' => $template->nodeValue), 'output' => array('templateid'), 'editable' => true); $current_template = API::Template()->get($options); if (empty($current_template)) { throw new Exception(_s('No permission for template "%1$s".', $template->nodeValue)); } $current_template = reset($current_template); $templateLinkage[] = $current_template; } if ($templateLinkage) { $result = API::Template()->massAdd(array('hosts' => array('hostid' => $current_hostid), 'templates' => $templateLinkage)); if (!$result) { throw new Exception(); } } } // }}} TEMPLATES // ITEMS {{{ if ($rules['items']['updateExisting'] || $rules['items']['createMissing'] || $rules['applications']['createMissing']) { // applications are located under items in version 1.8, // so we need to get item list in any of these cases $items = $xpath->query('items/item', $host); if ($oldVersionInput) { $interfaces = API::HostInterface()->get(array('hostids' => $current_hostid, 'output' => API_OUTPUT_EXTEND)); // we must know interface ids to assign them to items $agent_interface_id = null; $ipmi_interface_id = null; $snmp_interfaces = array(); // hash 'port' => 'interfaceid' foreach ($interfaces as $interface) { switch ($interface['type']) { case INTERFACE_TYPE_AGENT: $agent_interface_id = $interface['interfaceid']; break; case INTERFACE_TYPE_IPMI: $ipmi_interface_id = $interface['interfaceid']; break; case INTERFACE_TYPE_SNMP: $snmp_interfaces[$interface['port']] = $interface['interfaceid']; break; } } } // if this is an export from 1.8, we need to make some adjustments to items // cycle each XML item foreach ($items as $item) { if ($rules['items']['updateExisting'] || $rules['items']['createMissing']) { $item_db = self::mapXML2arr($item, XML_TAG_ITEM); $item_db['hostid'] = $current_hostid; // item needs interfaces if ($oldVersionInput) { // 'snmp_port' column was renamed to 'port' if ($item_db['snmp_port'] != 0) { // zabbix agent items have no ports $item_db['port'] = $item_db['snmp_port']; } unset($item_db['snmp_port']); // assigning appropriate interface depending on item type switch ($item_db['type']) { // zabbix agent interface case ITEM_TYPE_ZABBIX: case ITEM_TYPE_SIMPLE: case ITEM_TYPE_EXTERNAL: case ITEM_TYPE_SSH: case ITEM_TYPE_TELNET: $item_db['interfaceid'] = $agent_interface_id; break; // snmp interface // snmp interface case ITEM_TYPE_SNMPV1: case ITEM_TYPE_SNMPV2C: case ITEM_TYPE_SNMPV3: // for an item with different port - different interface $item_db['interfaceid'] = $snmp_interfaces[$item_db['port']]; break; case ITEM_TYPE_IPMI: $item_db['interfaceid'] = $ipmi_interface_id; break; // no interfaces required for these item types // no interfaces required for these item types case ITEM_TYPE_HTTPTEST: case ITEM_TYPE_CALCULATED: case ITEM_TYPE_AGGREGATE: case ITEM_TYPE_INTERNAL: case ITEM_TYPE_ZABBIX_ACTIVE: case ITEM_TYPE_TRAPPER: case ITEM_TYPE_DB_MONITOR: $item_db['interfaceid'] = null; break; } $item_db['key_'] = $itemKeys[$host_db['host']][$item_db['key_']]; } $current_item = API::Item()->get(array('filter' => array('hostid' => $item_db['hostid'], 'key_' => $item_db['key_']), 'webitems' => true, 'editable' => true, 'output' => array('itemid'))); $current_item = reset($current_item); } // create applications independently of create or update item options // in case we update items, we need to assign items to applications, // so we also gather application IDs independetly of selected application options $applications = $xpath->query('applications/application', $item); $itemApplications = array(); $applicationsToAdd = array(); $applicationsIds = array(); foreach ($applications as $application) { $application_db = array('name' => $application->nodeValue, 'hostid' => $current_hostid); $current_application = API::Application()->get(array('filter' => $application_db, 'output' => API_OUTPUT_EXTEND)); $applicationValue = reset($current_application); if ($current_application) { if (!$itemApplications) { $itemApplications = $current_application; } elseif (!in_array($applicationValue['applicationid'], $applicationsIds)) { $itemApplications = array_merge($itemApplications, $current_application); } $applicationsIds[] = $applicationValue['applicationid']; } else { $applicationsToAdd[] = $application_db; } } if ($applicationsToAdd && $rules['applications']['createMissing']) { $result = API::Application()->create($applicationsToAdd); $newApplications = API::Application()->get(array('applicationids' => $result['applicationids'], 'output' => API_OUTPUT_EXTEND)); $itemApplications = array_merge($itemApplications, $newApplications); } if ($rules['items']['updateExisting'] || $rules['items']['createMissing']) { // if item does not exist and there is no need to create it, skip item creation if (!$current_item && !$rules['items']['createMissing']) { info(_s('Item "%1$s" skipped - user rule.', $item_db['key_'])); continue; } // if item exists, but there there is no need for update, skip item update if ($current_item && !$rules['items']['updateExisting']) { info(_s('Item "%1$s" skipped - user rule.', $item_db['key_'])); continue; } if ($current_item && $rules['items']['updateExisting']) { $item_db['itemid'] = $current_item['itemid']; $result = API::Item()->update($item_db); $current_item = API::Item()->get(array('itemids' => $result['itemids'], 'webitems' => true, 'output' => array('itemid'))); } if (!$current_item && $rules['items']['createMissing']) { $result = API::Item()->create($item_db); $current_item = API::Item()->get(array('itemids' => $result['itemids'], 'webitems' => true, 'output' => array('itemid'))); } // after items are created or updated, see to if items need to assigned to applications if (isset($itemApplications) && $itemApplications) { API::Application()->massAdd(array('applications' => $itemApplications, 'items' => $current_item)); } } } } // }}} ITEMS // TRIGGERS {{{ if ($rules['triggers']['updateExisting'] || $rules['triggers']['createMissing']) { $triggers = $xpath->query('triggers/trigger', $host); $triggersToCreate = array(); $triggersToUpdate = array(); foreach ($triggers as $trigger) { $trigger_db = self::mapXML2arr($trigger, XML_TAG_TRIGGER); $trigger_db['expression'] = $triggerExpressions[$host_db['host']][$trigger_db['description']][$trigger_db['expression']]; $trigger_db['hostid'] = $current_hostid; $currentTrigger = API::Trigger()->get(array('output' => array('triggerid'), 'filter' => array('description' => $trigger_db['description']), 'hostids' => array($current_hostid), 'selectHosts' => array('hostid'), 'nopermissions' => true, 'limit' => 1)); $currentTrigger = reset($currentTrigger); if ($currentTrigger) { $dbTriggers = API::Trigger()->get(array('output' => API_OUTPUT_EXTEND, 'filter' => array('description' => $trigger_db['description']), 'hostids' => array($current_hostid), 'editable' => true)); foreach ($dbTriggers as $dbTrigger) { $expression = explode_exp($dbTrigger['expression']); if (strcmp($trigger_db['expression'], $expression) == 0) { $currentTrigger = $dbTrigger; break; } if (!$currentTrigger) { throw new Exception(_s('No permission for trigger "%1$s".', $trigger_db['description'])); } } } unset($trigger_db['hostid']); if (!$currentTrigger && !$rules['triggers']['createMissing']) { info(_s('Trigger "%1$s" skipped - user rule.', $trigger_db['description'])); continue; } if ($currentTrigger && !$rules['triggers']['updateExisting']) { info(_s('Trigger "%1$s" skipped - user rule.', $trigger_db['description'])); continue; } if (!$currentTrigger && $rules['triggers']['createMissing']) { $triggersToCreate[] = $trigger_db; } if ($currentTrigger && $rules['triggers']['updateExisting']) { $trigger_db['triggerid'] = $currentTrigger['triggerid']; $triggersToUpdate[] = $trigger_db; } } if ($triggersToUpdate) { $result = API::Trigger()->update($triggersToUpdate); $triggersUpdated = API::Trigger()->get(array('output' => API_OUTPUT_EXTEND, 'triggerids' => $result['triggerids'])); $triggersForDependencies = array_merge($triggersForDependencies, $triggersUpdated); } if ($triggersToCreate) { $result = API::Trigger()->create($triggersToCreate); $triggersCreated = API::Trigger()->get(array('output' => API_OUTPUT_EXTEND, 'triggerids' => $result['triggerids'])); $triggersForDependencies = array_merge($triggersForDependencies, $triggersCreated); } } // }}} TRIGGERS // GRAPHS {{{ if ($rules['graphs']['updateExisting'] || $rules['graphs']['createMissing']) { $graphs = $xpath->query('graphs/graph', $host); $graphs_to_add = array(); $graphs_to_upd = array(); foreach ($graphs as $graph) { // GRAPH ITEMS {{{ $gitems = $xpath->query('graph_elements/graph_element', $graph); $graph_hostids = array(); $graph_items = array(); foreach ($gitems as $gitem) { $gitem_db = self::mapXML2arr($gitem, XML_TAG_GRAPH_ELEMENT); $data = explode(':', $gitem_db['host_key_']); $gitem_host = array_shift($data); // {HOSTNAME} is here for backward compatibility $gitem_db['host'] = $gitem_host == '{HOSTNAME}' ? $host_db['host'] : $gitem_host; $gitem_db['host'] = $gitem_host == '{HOST.HOST}' ? $host_db['host'] : $gitem_host; if ($oldVersionInput) { $data[0] = self::convertOldSimpleKey($data[0]); } $gitem_db['key_'] = implode(':', $data); $itemExists = API::Item()->get(array('output' => array('itemid'), 'filter' => array('key_' => $gitem_db['key_']), 'webitems' => true, 'nopermissions' => true, 'limit' => 1)); if ($itemExists) { $current_item = API::Item()->get(array('filter' => array('key_' => $gitem_db['key_']), 'webitems' => true, 'editable' => true, 'host' => $gitem_db['host'], 'output' => array('itemid', 'hostid'))); if (empty($current_item)) { throw new Exception(_s('No permission for item "%1$s".', $gitem_db['key_'])); } $current_item = reset($current_item); $graph_hostids[] = $current_item['hostid']; $gitem_db['itemid'] = $current_item['itemid']; $graph_items[] = $gitem_db; } else { throw new Exception(_s('Item "%1$s" does not exist.', $gitem_db['host_key_'])); } } // }}} GRAPH ITEMS $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH); $graph_db['hostids'] = $graph_hostids; // do we need to show the graph legend, after it is imported? // in 1.8, this setting was present only for pie and exploded graphs // for other graph types we are always showing the legend if ($graph_db['graphtype'] != GRAPH_TYPE_PIE && $graph_db['graphtype'] != GRAPH_TYPE_EXPLODED) { $graph_db['show_legend'] = 1; } $current_graph = API::Graph()->get(array('output' => array('graphid'), 'selectHosts' => array('hostid', 'host'), 'hostids' => $graph_db['hostids'], 'filter' => array('name' => $graph_db['name']), 'nopermissions' => true, 'limit' => 1)); if ($current_graph) { $current_graph = API::Graph()->get(array('output' => API_OUTPUT_EXTEND, 'hostids' => $graph_db['hostids'], 'filter' => array('name' => $graph_db['name']), 'editable' => true)); if (empty($current_graph)) { throw new Exception(_s('No permission for graph "%1$s".', $graph_db['name'])); } $current_graph = reset($current_graph); } if (!$current_graph && empty($rules['graphs']['createMissing'])) { info(_s('Graph "%1$s" skipped - user rule.', $graph_db['name'])); continue; // break if not update updateExisting } if ($current_graph && empty($rules['graphs']['updateExisting'])) { info(_s('Graph "%1$s" skipped - user rule.', $graph_db['name'])); continue; // break if not update updateExisting } if (!isset($graph_db['ymin_type'])) { throw new Exception(_s('No "ymin_type" field for graph "%s".', $graph_db['name'])); } if (!isset($graph_db['ymax_type'])) { throw new Exception(_s('No "ymax_type" field for graph "%s".', $graph_db['name'])); } if ($graph_db['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $item_data = explode(':', $graph_db['ymin_item_key'], 2); if (count($item_data) < 2) { throw new Exception(_s('Incorrect y min item for graph "%1$s".', $graph_db['name'])); } if (!($item = get_item_by_key($item_data[1], $item_data[0]))) { throw new Exception(_s('Missing item "%1$s" for host "%2$s".', $graph_db['ymin_item_key'], $host_db['host'])); } $graph_db['ymin_itemid'] = $item['itemid']; } if ($graph_db['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $item_data = explode(':', $graph_db['ymax_item_key'], 2); if (count($item_data) < 2) { throw new Exception(_s('Incorrect y max item for graph "%1$s".', $graph_db['name'])); } if (!($item = get_item_by_key($item_data[1], $item_data[0]))) { throw new Exception(_s('Missing item "%1$s" for host "%2$s".', $graph_db['ymax_item_key'], $host_db['host'])); } $graph_db['ymax_itemid'] = $item['itemid']; } $graph_db['gitems'] = $graph_items; if ($current_graph) { $graph_db['graphid'] = $current_graph['graphid']; $graphs_to_upd[] = $graph_db; } else { $graphs_to_add[] = $graph_db; } } if (!empty($graphs_to_add)) { API::Graph()->create($graphs_to_add); } if (!empty($graphs_to_upd)) { API::Graph()->update($graphs_to_upd); } } } // DEPENDENCIES $dependencies = $xpath->query('dependencies/dependency'); if ($dependencies->length > 0) { $triggersForDependencies = zbx_objectValues($triggersForDependencies, 'triggerid'); $triggersForDependencies = array_flip($triggersForDependencies); $triggerDependencies = array(); foreach ($dependencies as $dependency) { $triggerDescription = $dependency->getAttribute('description'); $currentTrigger = get_trigger_by_description($triggerDescription); if ($currentTrigger && isset($triggersForDependencies[$currentTrigger['triggerid']])) { $dependsOnList = $xpath->query('depends', $dependency); foreach ($dependsOnList as $dependsOn) { $depTrigger = get_trigger_by_description($dependsOn->nodeValue); if ($depTrigger) { if (!isset($triggerDependencies[$currentTrigger['triggerid']])) { $triggerDependencies[$currentTrigger['triggerid']] = array('triggerid' => $currentTrigger['triggerid'], 'dependencies' => array()); } $triggerDependencies[$currentTrigger['triggerid']]['dependencies'][] = array('triggerid' => $depTrigger['triggerid']); } } } } if ($triggerDependencies) { API::Trigger()->update($triggerDependencies); } } } }
/** * Generate data for the trigger configuration form. * * @param string $exprAction expression constructor action, see remakeExpression() for a list of supported values * * @return array */ function getTriggerFormData($exprAction) { $data = array('form' => getRequest('form'), 'form_refresh' => getRequest('form_refresh'), 'parent_discoveryid' => getRequest('parent_discoveryid'), 'dependencies' => getRequest('dependencies', array()), 'db_dependencies' => array(), 'triggerid' => getRequest('triggerid'), 'expression' => getRequest('expression', ''), 'expr_temp' => getRequest('expr_temp', ''), 'description' => getRequest('description', ''), 'type' => getRequest('type', 0), 'priority' => getRequest('priority', 0), 'status' => getRequest('status', 0), 'comments' => getRequest('comments', ''), 'url' => getRequest('url', ''), 'input_method' => getRequest('input_method', IM_ESTABLISHED), 'limited' => false, 'templates' => array(), 'hostid' => getRequest('hostid', 0)); if (!empty($data['triggerid'])) { // get trigger $options = array('output' => API_OUTPUT_EXTEND, 'selectHosts' => array('hostid'), 'triggerids' => $data['triggerid']); $trigger = $data['parent_discoveryid'] ? API::TriggerPrototype()->get($options) : API::Trigger()->get($options); $data['trigger'] = reset($trigger); // get templates $tmp_triggerid = $data['triggerid']; do { $db_triggers = DBfetch(DBselect('SELECT t.triggerid,t.templateid,id.parent_itemid,h.name,h.hostid' . ' FROM triggers t' . ' LEFT JOIN functions f ON t.triggerid=f.triggerid' . ' LEFT JOIN items i ON f.itemid=i.itemid' . ' LEFT JOIN hosts h ON i.hostid=h.hostid' . ' LEFT JOIN item_discovery id ON i.itemid=id.itemid' . ' WHERE t.triggerid=' . zbx_dbstr($tmp_triggerid))); if (bccomp($data['triggerid'], $tmp_triggerid) != 0) { // parent trigger prototype link if ($data['parent_discoveryid']) { $link = 'trigger_prototypes.php?form=update&triggerid=' . $db_triggers['triggerid'] . '&parent_discoveryid=' . $db_triggers['parent_itemid'] . '&hostid=' . $db_triggers['hostid']; } else { $link = 'triggers.php?form=update&triggerid=' . $db_triggers['triggerid'] . '&hostid=' . $db_triggers['hostid']; } $data['templates'][] = new CLink(CHtml::encode($db_triggers['name']), $link, 'highlight underline weight_normal'); $data['templates'][] = SPACE . '⇒' . SPACE; } $tmp_triggerid = $db_triggers['templateid']; } while ($tmp_triggerid != 0); $data['templates'] = array_reverse($data['templates']); array_shift($data['templates']); $data['limited'] = $data['trigger']['templateid'] != 0; // select first host from triggers if gived not match $hosts = $data['trigger']['hosts']; if (count($hosts) > 0 && !in_array(array('hostid' => $data['hostid']), $hosts)) { $host = reset($hosts); $data['hostid'] = $host['hostid']; } } if (!empty($data['triggerid']) && !isset($_REQUEST['form_refresh']) || $data['limited']) { $data['expression'] = explode_exp($data['trigger']['expression']); if (!$data['limited'] || !isset($_REQUEST['form_refresh'])) { $data['description'] = $data['trigger']['description']; $data['type'] = $data['trigger']['type']; $data['priority'] = $data['trigger']['priority']; $data['status'] = $data['trigger']['status']; $data['comments'] = $data['trigger']['comments']; $data['url'] = $data['trigger']['url']; $db_triggers = DBselect('SELECT t.triggerid,t.description' . ' FROM triggers t,trigger_depends d' . ' WHERE t.triggerid=d.triggerid_up' . ' AND d.triggerid_down=' . zbx_dbstr($data['triggerid'])); while ($trigger = DBfetch($db_triggers)) { if (uint_in_array($trigger['triggerid'], $data['dependencies'])) { continue; } array_push($data['dependencies'], $trigger['triggerid']); } } } if ($data['input_method'] == IM_TREE) { $analyze = analyzeExpression($data['expression']); if ($analyze !== false) { list($data['outline'], $data['eHTMLTree']) = $analyze; if ($exprAction !== null && $data['eHTMLTree'] != null) { $new_expr = remakeExpression($data['expression'], $_REQUEST['expr_target_single'], $exprAction, $data['expr_temp']); if ($new_expr !== false) { $data['expression'] = $new_expr; $analyze = analyzeExpression($data['expression']); if ($analyze !== false) { list($data['outline'], $data['eHTMLTree']) = $analyze; } else { show_messages(false, '', _('Expression Syntax Error.')); } $data['expr_temp'] = ''; } else { show_messages(false, '', _('Expression Syntax Error.')); } } $data['expression_field_name'] = 'expr_temp'; $data['expression_field_value'] = $data['expr_temp']; $data['expression_field_readonly'] = true; } else { show_messages(false, '', _('Expression Syntax Error.')); $data['input_method'] = IM_ESTABLISHED; } } if ($data['input_method'] != IM_TREE) { $data['expression_field_name'] = 'expression'; $data['expression_field_value'] = $data['expression']; $data['expression_field_readonly'] = $data['limited']; } if (empty($data['parent_discoveryid'])) { $data['db_dependencies'] = API::Trigger()->get(array('triggerids' => $data['dependencies'], 'output' => array('triggerid', 'flags', 'description'), 'preservekeys' => true, 'selectHosts' => array('hostid', 'name'))); foreach ($data['db_dependencies'] as &$dependency) { if (count($dependency['hosts']) > 1) { order_result($dependency['hosts'], 'name', ZBX_SORT_UP); } $dependency['hosts'] = array_values($dependency['hosts']); $dependency['hostid'] = $dependency['hosts'][0]['hostid']; } unset($dependency); order_result($data['db_dependencies'], 'description'); } return $data; }
$triggers[$row_event['triggerid']]['events'][$row_event['eventid']] = $row_event; } foreach ($triggers as $triggerid => $row) { $elements = array(); $description = expand_trigger_description($row['triggerid']); if (!zbx_empty($_REQUEST['txt_select']) && (bool) zbx_stristr($description, $_REQUEST['txt_select']) == (bool) $_REQUEST['inverse_select']) { continue; } if (!zbx_empty($row['url'])) { $description = new CLink($description, $row['url'], null, null, true); } if ($_REQUEST['show_details']) { $font = new CTag('font', 'yes'); $font->setAttribute('color', '#000'); $font->setAttribute('size', '-2'); $font->addItem(explode_exp($row['expression'], 1)); $description = array($description, BR(), $font); } // dependency $dependency = false; $dep_table = new CTableInfo(); $dep_table->setAttribute('style', 'width: 200px;'); $dep_table->addRow(bold(S_DEPENDS_ON . ':')); $sql_dep = 'SELECT * FROM trigger_depends WHERE triggerid_down=' . $row['triggerid']; $dep_res = DBselect($sql_dep); while ($dep_row = DBfetch($dep_res)) { $dep_table->addRow(SPACE . '-' . SPACE . expand_trigger_description($dep_row['triggerid_up'])); $dependency = true; } if ($dependency) { $img = new Cimg('images/general/down_icon.png', 'DEP_DOWN');
function check_right_on_trigger_by_triggerid($permission, $triggerid) { $trigger_data = DBfetch(DBselect('select expression from triggers where triggerid=' . $triggerid)); if (!$trigger_data) { return false; } return check_right_on_trigger_by_expression($permission, explode_exp($trigger_data['expression'], 0)); }
/** * Unlinks the templates from the given hosts. If $tragetids is set to null, the templates will be unlinked from * all hosts. * * @param array $templateids * @param null|array $targetids the IDs of the hosts to unlink the templates from * @param bool $clear delete all of the inherited objects from the hosts */ protected function unlink($templateids, $targetids = null, $clear = false) { $flags = $clear ? array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_RULE) : array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_RULE, ZBX_FLAG_DISCOVERY_PROTOTYPE); // check that all triggers on templates that we unlink, don't have items from another templates $sql = 'SELECT DISTINCT t.description' . ' FROM triggers t,functions f,items i' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND ' . dbConditionInt('i.hostid', $templateids) . ' AND EXISTS (' . 'SELECT ff.triggerid' . ' FROM functions ff,items ii' . ' WHERE ff.itemid=ii.itemid' . ' AND ff.triggerid=t.triggerid' . ' AND ' . dbConditionInt('ii.hostid', $templateids, true) . ')' . ' AND t.flags=' . ZBX_FLAG_DISCOVERY_NORMAL; if ($dbTrigger = DBfetch(DBSelect($sql, 1))) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot unlink trigger "%s", it has items from template that is left linked to host.', $dbTrigger['description'])); } $sqlFrom = ' triggers t,hosts h'; $sqlWhere = ' EXISTS (' . 'SELECT ff.triggerid' . ' FROM functions ff,items ii' . ' WHERE ff.triggerid=t.templateid' . ' AND ii.itemid=ff.itemid' . ' AND ' . dbConditionInt('ii.hostid', $templateids) . ')' . ' AND ' . dbConditionInt('t.flags', $flags); if (!is_null($targetids)) { $sqlFrom = ' triggers t,functions f,items i,hosts h'; $sqlWhere .= ' AND ' . dbConditionInt('i.hostid', $targetids) . ' AND f.itemid=i.itemid' . ' AND t.triggerid=f.triggerid' . ' AND h.hostid=i.hostid'; } $sql = 'SELECT DISTINCT t.triggerid,t.description,t.flags,t.expression,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere; $dbTriggers = DBSelect($sql); $triggers = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY_PROTOTYPE => array()); $triggerids = array(); while ($trigger = DBfetch($dbTriggers)) { $triggers[$trigger['flags']][$trigger['triggerid']] = array('description' => $trigger['description'], 'expression' => explode_exp($trigger['expression']), 'triggerid' => $trigger['triggerid'], 'host' => $trigger['host']); if (!in_array($trigger['triggerid'], $triggerids)) { array_push($triggerids, $trigger['triggerid']); } } if (!empty($triggers[ZBX_FLAG_DISCOVERY_NORMAL])) { if ($clear) { $result = API::Trigger()->delete(array_keys($triggers[ZBX_FLAG_DISCOVERY_NORMAL]), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear triggers')); } } else { DB::update('triggers', array('values' => array('templateid' => 0), 'where' => array('triggerid' => array_keys($triggers[ZBX_FLAG_DISCOVERY_NORMAL])))); foreach ($triggers[ZBX_FLAG_DISCOVERY_NORMAL] as $trigger) { info(_s('Unlinked: Trigger "%1$s" on "%2$s".', $trigger['description'], $trigger['host'])); } } } if (!empty($triggers[ZBX_FLAG_DISCOVERY_PROTOTYPE])) { if ($clear) { $result = API::TriggerPrototype()->delete(array_keys($triggers[ZBX_FLAG_DISCOVERY_PROTOTYPE]), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear triggers')); } } else { DB::update('triggers', array('values' => array('templateid' => 0), 'where' => array('triggerid' => array_keys($triggers[ZBX_FLAG_DISCOVERY_PROTOTYPE])))); foreach ($triggers[ZBX_FLAG_DISCOVERY_PROTOTYPE] as $trigger) { info(_s('Unlinked: Trigger prototype "%1$s" on "%2$s".', $trigger['description'], $trigger['host'])); } } } /* ITEMS, DISCOVERY RULES {{{ */ $sqlFrom = ' items i1,items i2,hosts h'; $sqlWhere = ' i2.itemid=i1.templateid' . ' AND ' . dbConditionInt('i2.hostid', $templateids) . ' AND ' . dbConditionInt('i1.flags', $flags) . ' AND h.hostid=i1.hostid'; if (!is_null($targetids)) { $sqlWhere .= ' AND ' . dbConditionInt('i1.hostid', $targetids); } $sql = 'SELECT DISTINCT i1.itemid,i1.flags,i1.name,i1.hostid,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere; $dbItems = DBSelect($sql); $items = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY_RULE => array(), ZBX_FLAG_DISCOVERY_PROTOTYPE => array()); while ($item = DBfetch($dbItems)) { $items[$item['flags']][$item['itemid']] = array('name' => $item['name'], 'host' => $item['host']); } if (!empty($items[ZBX_FLAG_DISCOVERY_RULE])) { if ($clear) { $result = API::DiscoveryRule()->delete(array_keys($items[ZBX_FLAG_DISCOVERY_RULE]), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear discovery rules')); } } else { DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY_RULE])))); foreach ($items[ZBX_FLAG_DISCOVERY_RULE] as $discoveryRule) { info(_s('Unlinked: Discovery rule "%1$s" on "%2$s".', $discoveryRule['name'], $discoveryRule['host'])); } } } if (!empty($items[ZBX_FLAG_DISCOVERY_NORMAL])) { if ($clear) { $result = API::Item()->delete(array_keys($items[ZBX_FLAG_DISCOVERY_NORMAL]), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear items')); } } else { DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY_NORMAL])))); foreach ($items[ZBX_FLAG_DISCOVERY_NORMAL] as $item) { info(_s('Unlinked: Item "%1$s" on "%2$s".', $item['name'], $item['host'])); } } } if (!empty($items[ZBX_FLAG_DISCOVERY_PROTOTYPE])) { if ($clear) { $result = API::Itemprototype()->delete(array_keys($items[ZBX_FLAG_DISCOVERY_PROTOTYPE]), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear item prototypes')); } } else { DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY_PROTOTYPE])))); foreach ($items[ZBX_FLAG_DISCOVERY_PROTOTYPE] as $item) { info(_s('Unlinked: Item prototype "%1$s" on "%2$s".', $item['name'], $item['host'])); } } } /* }}} ITEMS, DISCOVERY RULES */ // host prototypes // we need only to unlink host prototypes. in case of unlink and clear they will be deleted together with LLD rules. if (!$clear && isset($items[ZBX_FLAG_DISCOVERY_RULE])) { $discoveryRuleIds = array_keys($items[ZBX_FLAG_DISCOVERY_RULE]); $hostPrototypes = DBfetchArrayAssoc(DBSelect('SELECT DISTINCT h.hostid,h.host,h3.host AS parent_host' . ' FROM hosts h' . ' INNER JOIN host_discovery hd ON h.hostid=hd.hostid' . ' INNER JOIN hosts h2 ON h.templateid=h2.hostid' . ' INNER JOIN host_discovery hd2 ON h.hostid=hd.hostid' . ' INNER JOIN items i ON hd.parent_itemid=i.itemid' . ' INNER JOIN hosts h3 ON i.hostid=h3.hostid' . ' WHERE ' . dbConditionInt('hd.parent_itemid', $discoveryRuleIds)), 'hostid'); if ($hostPrototypes) { DB::update('hosts', array('values' => array('templateid' => 0), 'where' => array('hostid' => array_keys($hostPrototypes)))); DB::update('group_prototype', array('values' => array('templateid' => 0), 'where' => array('hostid' => array_keys($hostPrototypes)))); foreach ($hostPrototypes as $hostPrototype) { info(_s('Unlinked: Host prototype "%1$s" on "%2$s".', $hostPrototype['host'], $hostPrototype['parent_host'])); } } } /* GRAPHS {{{ */ $sqlFrom = ' graphs g,hosts h'; $sqlWhere = ' EXISTS (' . 'SELECT ggi.graphid' . ' FROM graphs_items ggi,items ii' . ' WHERE ggi.graphid=g.templateid' . ' AND ii.itemid=ggi.itemid' . ' AND ' . dbConditionInt('ii.hostid', $templateids) . ')' . ' AND ' . dbConditionInt('g.flags', $flags); if (!is_null($targetids)) { $sqlFrom = ' graphs g,graphs_items gi,items i,hosts h'; $sqlWhere .= ' AND ' . dbConditionInt('i.hostid', $targetids) . ' AND gi.itemid=i.itemid' . ' AND g.graphid=gi.graphid' . ' AND h.hostid=i.hostid'; } $sql = 'SELECT DISTINCT g.graphid,g.name,g.flags,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere; $dbGraphs = DBSelect($sql); $graphs = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY_PROTOTYPE => array()); while ($graph = DBfetch($dbGraphs)) { $graphs[$graph['flags']][$graph['graphid']] = array('name' => $graph['name'], 'graphid' => $graph['graphid'], 'host' => $graph['host']); } if (!empty($graphs[ZBX_FLAG_DISCOVERY_PROTOTYPE])) { if ($clear) { $result = API::GraphPrototype()->delete(array_keys($graphs[ZBX_FLAG_DISCOVERY_PROTOTYPE]), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear graph prototypes')); } } else { DB::update('graphs', array('values' => array('templateid' => 0), 'where' => array('graphid' => array_keys($graphs[ZBX_FLAG_DISCOVERY_PROTOTYPE])))); foreach ($graphs[ZBX_FLAG_DISCOVERY_PROTOTYPE] as $graph) { info(_s('Unlinked: Graph prototype "%1$s" on "%2$s".', $graph['name'], $graph['host'])); } } } if (!empty($graphs[ZBX_FLAG_DISCOVERY_NORMAL])) { if ($clear) { $result = API::Graph()->delete(array_keys($graphs[ZBX_FLAG_DISCOVERY_NORMAL]), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear graphs.')); } } else { DB::update('graphs', array('values' => array('templateid' => 0), 'where' => array('graphid' => array_keys($graphs[ZBX_FLAG_DISCOVERY_NORMAL])))); foreach ($graphs[ZBX_FLAG_DISCOVERY_NORMAL] as $graph) { info(_s('Unlinked: Graph "%1$s" on "%2$s".', $graph['name'], $graph['host'])); } } } /* }}} GRAPHS */ // http tests $sqlWhere = ''; if (!is_null($targetids)) { $sqlWhere = ' AND ' . dbConditionInt('ht1.hostid', $targetids); } $sql = 'SELECT DISTINCT ht1.httptestid,ht1.name,h.name as host' . ' FROM httptest ht1' . ' INNER JOIN httptest ht2 ON ht2.httptestid=ht1.templateid' . ' INNER JOIN hosts h ON h.hostid=ht1.hostid' . ' WHERE ' . dbConditionInt('ht2.hostid', $templateids) . $sqlWhere; $dbHttpTests = DBSelect($sql); $httpTests = array(); while ($httpTest = DBfetch($dbHttpTests)) { $httpTests[$httpTest['httptestid']] = array('name' => $httpTest['name'], 'host' => $httpTest['host']); } if (!empty($httpTests)) { if ($clear) { $result = API::HttpTest()->delete(array_keys($httpTests), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear Web scenarios.')); } } else { DB::update('httptest', array('values' => array('templateid' => 0), 'where' => array('httptestid' => array_keys($httpTests)))); foreach ($httpTests as $httpTest) { info(_s('Unlinked: Web scenario "%1$s" on "%2$s".', $httpTest['name'], $httpTest['host'])); } } } /* APPLICATIONS {{{ */ $sql = 'SELECT at.application_templateid,at.applicationid,h.name,h.host,h.hostid' . ' FROM applications a1,application_template at,applications a2,hosts h' . ' WHERE a1.applicationid=at.applicationid' . ' AND at.templateid=a2.applicationid' . ' AND ' . dbConditionInt('a2.hostid', $templateids) . ' AND a1.hostid=h.hostid'; if ($targetids) { $sql .= ' AND ' . dbConditionInt('a1.hostid', $targetids); } $query = DBselect($sql); $applicationTemplates = array(); while ($applicationTemplate = DBfetch($query)) { $applicationTemplates[] = array('applicationid' => $applicationTemplate['applicationid'], 'application_templateid' => $applicationTemplate['application_templateid'], 'name' => $applicationTemplate['name'], 'hostid' => $applicationTemplate['hostid'], 'host' => $applicationTemplate['host']); } if ($applicationTemplates) { // unlink applications from templates DB::delete('application_template', array('application_templateid' => zbx_objectValues($applicationTemplates, 'application_templateid'))); if ($clear) { // delete inherited applications that are no longer linked to any templates $applications = DBfetchArray(DBselect('SELECT a.applicationid' . ' FROM applications a' . ' LEFT JOIN application_template at ON a.applicationid=at.applicationid ' . ' WHERE ' . dbConditionInt('a.applicationid', zbx_objectValues($applicationTemplates, 'applicationid')) . ' AND at.applicationid IS NULL')); $result = API::Application()->delete(zbx_objectValues($applications, 'applicationid'), true); if (!$result) { self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear applications.')); } } else { foreach ($applicationTemplates as $application) { info(_s('Unlinked: Application "%1$s" on "%2$s".', $application['name'], $application['host'])); } } } /* }}} APPLICATIONS */ parent::unlink($templateids, $targetids); }
$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']}]"; } $description->addAction('onclick', "javascript: create_mon_trigger_menu(event, new Array({'triggerid': '" . $trigger['triggerid'] . "', 'lastchange': '" . $trigger['lastchange'] . "'}, " . $menu_trigger_conf . ", " . $menu_trigger_url . ")," . zbx_jsvalue($items, true) . ");"); // }}} trigger description js menu if ($_REQUEST['show_details']) { $font = new CTag('font', 'yes'); $font->setAttribute('color', '#000'); $font->setAttribute('size', '-2'); $font->addItem(explode_exp($trigger['expression'], 1, false, true)); $description = array($description, BR(), $font); } // DEPENDENCIES {{{ if (!empty($trigger['dependencies'])) { $dep_table = new CTableInfo(); $dep_table->setAttribute('style', 'width: 200px;'); $dep_table->addRow(bold(S_DEPENDS_ON . ':')); foreach ($trigger['dependencies'] as $dep) { $dep_table->addRow(' - ' . expand_trigger_description($dep['triggerid'])); } $img = new Cimg('images/general/down_icon.png', 'DEP_UP'); $img->setAttribute('style', 'vertical-align: middle; border: 0px;'); $img->setHint($dep_table); $description = array($img, SPACE, $description); }
function getTriggerFormData() { $data = array('form' => get_request('form'), 'form_refresh' => get_request('form_refresh'), 'parent_discoveryid' => get_request('parent_discoveryid'), 'dependencies' => get_request('dependencies', array()), 'db_dependencies' => array(), 'triggerid' => get_request('triggerid'), 'expression' => get_request('expression', ''), 'expr_temp' => get_request('expr_temp', ''), 'description' => get_request('description', ''), 'type' => get_request('type', 0), 'priority' => get_request('priority', 0), 'status' => get_request('status', 0), 'comments' => get_request('comments', ''), 'url' => get_request('url', ''), 'input_method' => get_request('input_method', IM_ESTABLISHED), 'limited' => null, 'templates' => array(), 'hostid' => get_request('hostid', 0)); if (!empty($data['triggerid'])) { // get trigger $options = array('output' => API_OUTPUT_EXTEND, 'selectHosts' => array('hostid'), 'triggerids' => $data['triggerid']); $trigger = $data['parent_discoveryid'] ? API::TriggerPrototype()->get($options) : API::Trigger()->get($options); $data['trigger'] = reset($trigger); // get templates $tmp_triggerid = $data['triggerid']; do { $db_triggers = DBfetch(DBselect('SELECT t.triggerid,t.templateid,id.parent_itemid,h.name,h.hostid' . ' FROM triggers t' . ' LEFT JOIN functions f ON t.triggerid=f.triggerid' . ' LEFT JOIN items i ON f.itemid=i.itemid' . ' LEFT JOIN hosts h ON i.hostid=h.hostid' . ' LEFT JOIN item_discovery id ON i.itemid=id.itemid' . ' WHERE t.triggerid=' . zbx_dbstr($tmp_triggerid))); if (bccomp($data['triggerid'], $tmp_triggerid) != 0) { // parent trigger prototype link if ($data['parent_discoveryid']) { $link = 'trigger_prototypes.php?form=update&triggerid=' . $db_triggers['triggerid'] . '&parent_discoveryid=' . $db_triggers['parent_itemid'] . '&hostid=' . $db_triggers['hostid']; } else { $link = 'triggers.php?form=update&triggerid=' . $db_triggers['triggerid'] . '&hostid=' . $db_triggers['hostid']; } $data['templates'][] = new CLink($db_triggers['name'], $link, 'highlight underline weight_normal'); $data['templates'][] = SPACE . RARR . SPACE; } $tmp_triggerid = $db_triggers['templateid']; } while ($tmp_triggerid != 0); $data['templates'] = array_reverse($data['templates']); array_shift($data['templates']); $data['limited'] = $data['trigger']['templateid'] ? 'yes' : null; // if no host has been selected for the navigation panel, use the first trigger host if (!$data['hostid']) { $hosts = reset($data['trigger']['hosts']); $data['hostid'] = $hosts['hostid']; } } if (!empty($data['triggerid']) && !isset($_REQUEST['form_refresh']) || !empty($data['limited'])) { $data['expression'] = explode_exp($data['trigger']['expression']); if (empty($data['limited']) || !isset($_REQUEST['form_refresh'])) { $data['description'] = $data['trigger']['description']; $data['type'] = $data['trigger']['type']; $data['priority'] = $data['trigger']['priority']; $data['status'] = $data['trigger']['status']; $data['comments'] = $data['trigger']['comments']; $data['url'] = $data['trigger']['url']; $db_triggers = DBselect('SELECT t.triggerid,t.description' . ' FROM triggers t,trigger_depends d' . ' WHERE t.triggerid=d.triggerid_up' . ' AND d.triggerid_down=' . zbx_dbstr($data['triggerid'])); while ($trigger = DBfetch($db_triggers)) { if (uint_in_array($trigger['triggerid'], $data['dependencies'])) { continue; } array_push($data['dependencies'], $trigger['triggerid']); } } } if ($data['input_method'] == IM_TREE) { $analyze = analyzeExpression($data['expression']); if ($analyze !== false) { list($data['outline'], $data['eHTMLTree']) = $analyze; if (isset($_REQUEST['expr_action']) && $data['eHTMLTree'] != null) { $new_expr = remakeExpression($data['expression'], $_REQUEST['expr_target_single'], $_REQUEST['expr_action'], $data['expr_temp']); if ($new_expr !== false) { $data['expression'] = $new_expr; $analyze = analyzeExpression($data['expression']); if ($analyze !== false) { list($data['outline'], $data['eHTMLTree']) = $analyze; } else { show_messages(false, '', _('Expression Syntax Error.')); } $data['expr_temp'] = ''; } else { show_messages(false, '', _('Expression Syntax Error.')); } } $data['expression_field_name'] = 'expr_temp'; $data['expression_field_value'] = $data['expr_temp']; $data['expression_field_readonly'] = 'yes'; $data['expression_field_params'] = 'this.form.elements["' . $data['expression_field_name'] . '"].value'; $data['expression_macro_button'] = new CButton('insert_macro', _('Insert macro'), 'return call_ins_macro_menu(event);', 'formlist'); if ($data['limited'] == 'yes') { $data['expression_macro_button']->setAttribute('disabled', 'disabled'); } } else { show_messages(false, '', _('Expression Syntax Error.')); $data['input_method'] = IM_ESTABLISHED; } } if ($data['input_method'] != IM_TREE) { $data['expression_field_name'] = 'expression'; $data['expression_field_value'] = $data['expression']; $data['expression_field_readonly'] = $data['limited']; } if (empty($data['parent_discoveryid'])) { $data['db_dependencies'] = API::Trigger()->get(array('triggerids' => $data['dependencies'], 'output' => array('triggerid', 'description'), 'preservekeys' => true, 'selectHosts' => array('name'))); foreach ($data['db_dependencies'] as &$dependency) { if (!empty($dependency['hosts'][0]['name'])) { $dependency['host'] = $dependency['hosts'][0]['name']; } unset($dependency['hosts']); } order_result($data['db_dependencies'], 'description'); } return $data; }