Example #1
0
 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);
 }
Example #2
0
function update_template_dependencies_for_host($hostid)
{
    $db_triggers = get_triggers_by_hostid($hostid);
    while ($trigger_data = DBfetch($db_triggers)) {
        $db_chd_triggers = get_triggers_by_templateid($trigger_data['triggerid']);
        while ($chd_trigger_data = DBfetch($db_chd_triggers)) {
            update_trigger($chd_trigger_data['triggerid'], NULL, NULL, NULL, NULL, NULL, NULL, NULL, replace_template_dependencies(get_trigger_dependencies_by_triggerid($trigger_data['triggerid']), $hostid), $trigger_data['triggerid']);
        }
    }
}
Example #3
0
     show_messages();
     if (!check_right_on_trigger_by_expression(PERM_READ_WRITE, $_REQUEST['expression'])) {
         access_deny();
     }
     $now = time();
     $status = isset($_REQUEST['status']) ? TRIGGER_STATUS_DISABLED : TRIGGER_STATUS_ENABLED;
     $type = $_REQUEST['type'];
     $deps = get_request('dependencies', array());
     if (isset($_REQUEST['triggerid'])) {
         $trigger_data = get_trigger_by_triggerid($_REQUEST['triggerid']);
         if ($trigger_data['templateid']) {
             $_REQUEST['description'] = $trigger_data['description'];
             $_REQUEST['expression'] = explode_exp($trigger_data['expression'], 0);
         }
         DBstart();
         $result = update_trigger($_REQUEST['triggerid'], $_REQUEST['expression'], $_REQUEST['description'], $type, $_REQUEST['priority'], $status, $_REQUEST['comments'], $_REQUEST['url'], $deps, $trigger_data['templateid']);
         $result = DBend($result);
         $triggerid = $_REQUEST['triggerid'];
         show_messages($result, S_TRIGGER_UPDATED, S_CANNOT_UPDATE_TRIGGER);
     } else {
         DBstart();
         $triggerid = add_trigger($_REQUEST['expression'], $_REQUEST['description'], $type, $_REQUEST['priority'], $status, $_REQUEST['comments'], $_REQUEST['url'], $deps);
         $result = DBend($triggerid);
         show_messages($result, S_TRIGGER_ADDED, S_CANNOT_ADD_TRIGGER);
     }
     if ($result) {
         unset($_REQUEST['form']);
     }
 } else {
     if (isset($_REQUEST['delete']) && isset($_REQUEST['triggerid'])) {
         $result = false;
Example #4
0
 } else {
     if ($_REQUEST['go'] == 'massupdate' && isset($_REQUEST['mass_save']) && isset($_REQUEST['g_triggerid'])) {
         show_messages();
         $result = false;
         $visible = get_request('visible', array());
         $_REQUEST['dependencies'] = get_request('dependencies', array());
         $options = array('triggerids' => $_REQUEST['g_triggerid'], 'select_dependencies' => 1, 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
         $triggers = CTrigger::get($options);
         DBstart();
         foreach ($triggers as $tnum => $db_trig) {
             foreach ($db_trig as $key => $value) {
                 if (isset($visible[$key])) {
                     $db_trig[$key] = $_REQUEST[$key];
                 }
             }
             $result = update_trigger($db_trig['triggerid'], null, null, null, $db_trig['priority'], null, null, null, $db_trig['dependencies'], null);
             if (!$result) {
                 break;
             }
         }
         $result = DBend($result);
         show_messages($result, S_TRIGGER_UPDATED, S_CANNOT_UPDATE_TRIGGER);
         if ($result) {
             unset($_REQUEST['massupdate']);
             unset($_REQUEST['form']);
             $url = new CUrl();
             $path = $url->getPath();
             insert_js('cookie.eraseArray("' . $path . '")');
         }
         $go_result = $result;
     } else {
 /**
  * Update triggers
  *
  * Trigger params: expression, description, type, priority, status, comments, url, templateid
  *
  * @param array $triggers
  * @return boolean
  */
 public static function update($triggers)
 {
     $triggers = zbx_toArray($triggers);
     $triggerids = zbx_objectValues($triggers, 'triggerid');
     try {
         self::BeginTransaction(__METHOD__);
         $options = array('triggerids' => $triggerids, 'editable' => 1, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1);
         $upd_triggers = self::get($options);
         foreach ($triggers as $gnum => $trigger) {
             if (!isset($upd_triggers[$trigger['triggerid']])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, S_NO_PERMISSIONS);
             }
         }
         foreach ($triggers as $tnum => $trigger) {
             $trigger_db_fields = $upd_triggers[$trigger['triggerid']];
             $trigger_db_fields['expression'] = explode_exp($trigger_db_fields['expression'], false);
             if (!check_db_fields($trigger_db_fields, $trigger)) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Wrong fields for trigger');
             }
             if ($trigger_db_fields['type'] == $trigger['type']) {
                 $trigger['type'] = null;
             }
             if ($trigger_db_fields['priority'] == $trigger['priority']) {
                 $trigger['priority'] = null;
             }
             if (strcmp($trigger_db_fields['comments'], $trigger['comments']) == 0) {
                 $trigger['comments'] = null;
             }
             if (strcmp($trigger_db_fields['url'], $trigger['url']) == 0) {
                 $trigger['url'] = null;
             }
             $result = update_trigger($trigger['triggerid'], $trigger['expression'], $trigger['description'], $trigger['type'], $trigger['priority'], $trigger['status'], $trigger['comments'], $trigger['url'], array(), $trigger['templateid']);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Trigger [' . $trigger['description'] . ' ]: cannot update');
             }
         }
         self::EndTransaction(true, __METHOD__);
         return array('triggerids' => $triggerids);
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, ZBX_API_ERROR_PARAMETERS, $error);
         return false;
     }
 }
Example #6
0
 public static function parseMain($rules)
 {
     $triggers_for_dependencies = array();
     try {
         if (isset($rules['host']['exist']) || isset($rules['host']['missed'])) {
             $xpath = new DOMXPath(self::$xml);
             $hosts = $xpath->query('hosts/host');
             foreach ($hosts as $hnum => $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 ? CTemplate::exists($host_db) : CHost::exists($host_db);
                 if (!$current_host && !isset($rules['host']['missed'])) {
                     info('Host [' . $host_db['host'] . '] skipped - user rule');
                     continue;
                     // break if update nonexist
                 }
                 if ($current_host && !isset($rules['host']['exist'])) {
                     info('Host [' . $host_db['host'] . '] skipped - user rule');
                     continue;
                     // break if not update exist
                 }
                 if (isset($host_db['proxy_hostid'])) {
                     $proxy_exists = CProxy::get(array('proxyids' => $host_db['proxy_hostid']));
                     if (empty($proxy_exists)) {
                         $host_db['proxy_hostid'] = 0;
                     }
                 }
                 if ($current_host) {
                     $options = array('filter' => array('host' => $host_db['host']), 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                     if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                         $current_host = CTemplate::get($options);
                     } else {
                         $current_host = CHost::get($options);
                     }
                     if (empty($current_host)) {
                         throw new APIException(1, 'No permission for host [' . $host_db['host'] . ']');
                     } else {
                         $current_host = reset($current_host);
                     }
                 }
                 // HOST GROUPS {{{
                 $groups = $xpath->query('groups/group', $host);
                 $host_db['groups'] = array();
                 $groups_to_parse = array();
                 foreach ($groups as $gnum => $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 = CHostGroup::exists($group);
                     if ($current_group) {
                         $options = array('filter' => $group, 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                         $current_group = CHostGroup::get($options);
                         if (empty($current_group)) {
                             throw new APIException(1, 'No permissions for group ' . $group['name']);
                         }
                         $host_db['groups'][] = reset($current_group);
                     } else {
                         $result = CHostGroup::create($group);
                         if (!$result) {
                             throw new APIException(1, CHostGroup::resetErrors());
                         }
                         $options = array('groupids' => $result['groupids'], 'output' => API_OUTPUT_EXTEND);
                         $new_group = CHostgroup::get($options);
                         $host_db['groups'][] = reset($new_group);
                     }
                 }
                 // }}} HOST GROUPS
                 // MACROS
                 $macros = $xpath->query('macros/macro', $host);
                 $host_db['macros'] = array();
                 if ($macros->length > 0) {
                     foreach ($macros as $macro) {
                         $host_db['macros'][] = self::mapXML2arr($macro, XML_TAG_MACRO);
                     }
                 }
                 // }}} MACROS
                 // TEMPLATES {{{
                 if (isset($rules['template']['exist'])) {
                     $templates = $xpath->query('templates/template', $host);
                     $host_db['templates'] = array();
                     foreach ($templates as $tnum => $template) {
                         $options = array('filter' => array('host' => $template->nodeValue), 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                         $current_template = CTemplate::get($options);
                         if (empty($current_template)) {
                             throw new APIException(1, 'No permission for Template [' . $template->nodeValue . ']');
                         }
                         $current_template = reset($current_template);
                         if (!$current_template && !isset($rules['template']['missed'])) {
                             info('Template [' . $template->nodeValue . '] skipped - user rule');
                             continue;
                             // break if update nonexist
                         }
                         if ($current_template && !isset($rules['template']['exist'])) {
                             info('Template [' . $template->nodeValue . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         $host_db['templates'][] = $current_template;
                     }
                 }
                 // }}} TEMPLATES
                 // HOSTS
                 if ($current_host && isset($rules['host']['exist'])) {
                     if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                         $host_db['templateid'] = $current_host['hostid'];
                         $result = CTemplate::update($host_db);
                         if (!$result) {
                             throw new APIException(1, CTemplate::resetErrors());
                         }
                         $options = array('templateids' => $result['templateids'], 'output' => API_OUTPUT_EXTEND);
                         $current_host = CTemplate::get($options);
                     } else {
                         $host_db['hostid'] = $current_host['hostid'];
                         $result = CHost::update($host_db);
                         if (!$result) {
                             throw new APIException(1, CHost::resetErrors());
                         }
                         $options = array('hostids' => $result['hostids'], 'output' => API_OUTPUT_EXTEND);
                         $current_host = CHost::get($options);
                     }
                     if ($current_host === false) {
                         throw new APIException(1, $host_db['status'] == HOST_STATUS_TEMPLATE ? CTemplate::resetErrors() : CHost::resetErrors());
                     }
                 }
                 if (!$current_host && isset($rules['host']['missed'])) {
                     if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                         $result = CTemplate::create($host_db);
                         if (!$result) {
                             throw new APIException(1, CTemplate::resetErrors());
                         }
                         $options = array('templateids' => $result['templateids'], 'output' => API_OUTPUT_EXTEND);
                         $current_host = CTemplate::get($options);
                     } else {
                         $result = CHost::create($host_db);
                         if (!$result) {
                             throw new APIException(1, CHost::resetErrors());
                         }
                         $options = array('hostids' => $result['hostids'], 'output' => API_OUTPUT_EXTEND);
                         $current_host = CHost::get($options);
                     }
                 }
                 $current_host = reset($current_host);
                 // HOST PROFILES {{{
                 $profile_node = $xpath->query('host_profile/*', $host);
                 if ($profile_node->length > 0) {
                     $profile = array();
                     foreach ($profile_node as $num => $field) {
                         $profile[$field->nodeName] = $field->nodeValue;
                     }
                     delete_host_profile($current_host['hostid']);
                     add_host_profile($current_host['hostid'], $profile['devicetype'], $profile['name'], $profile['os'], $profile['serialno'], $profile['tag'], $profile['macaddress'], $profile['hardware'], $profile['software'], $profile['contact'], $profile['location'], $profile['notes']);
                 }
                 $profile_ext_node = $xpath->query('host_profiles_ext/*', $host);
                 if ($profile_ext_node->length > 0) {
                     $profile_ext = array();
                     foreach ($profile_ext_node as $num => $field) {
                         $profile_ext[$field->nodeName] = $field->nodeValue;
                     }
                     delete_host_profile_ext($current_host['hostid']);
                     add_host_profile_ext($current_host['hostid'], $profile_ext);
                 }
                 // }}} HOST PROFILES
                 // ITEMS {{{
                 if (isset($rules['item']['exist']) || isset($rules['item']['missed'])) {
                     $items = $xpath->query('items/item', $host);
                     foreach ($items as $inum => $item) {
                         $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                         $item_db['hostid'] = $current_host['hostid'];
                         if ($current_item = CItem::exists($item_db)) {
                             $options = array('filter' => array('hostid' => $item_db['hostid'], 'key_' => $item_db['key_']), 'webitems' => 1, 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                             $current_item = CItem::get($options);
                             if (empty($current_item)) {
                                 throw new APIException(1, 'No permission for Item [' . $item_db['key_'] . ']');
                             }
                             $current_item = reset($current_item);
                         }
                         if (!$current_item && !isset($rules['item']['missed'])) {
                             info('Item [' . $item_db['key_'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($current_item && !isset($rules['item']['exist'])) {
                             info('Item [' . $item_db['key_'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         // ITEM APPLICATIONS {{{
                         $applications = $xpath->query('applications/application', $item);
                         $item_applications = array();
                         $applications_to_add = array();
                         foreach ($applications as $application) {
                             $application_db = array('name' => $application->nodeValue, 'hostid' => $current_host['hostid']);
                             if ($current_application = CApplication::exists($application_db)) {
                                 $current_application = CApplication::get(array('filter' => $application_db, 'output' => API_OUTPUT_EXTEND));
                                 if (empty($current_application)) {
                                     throw new APIException(1, 'No permission for Application [' . $application_db['name'] . ']');
                                 }
                             }
                             if ($current_application) {
                                 $item_applications = array_merge($item_applications, $current_application);
                             } else {
                                 $applications_to_add[] = $application_db;
                             }
                         }
                         if (!empty($applications_to_add)) {
                             $result = CApplication::create($applications_to_add);
                             if (!$result) {
                                 throw new APIException(1, CApplication::resetErrors());
                             }
                             $options = array('applicationids' => $result['applicationids'], 'output' => API_OUTPUT_EXTEND);
                             $new_applications = CApplication::get($options);
                             $item_applications = array_merge($item_applications, $new_applications);
                         }
                         // }}} ITEM APPLICATIONS
                         if ($current_item && isset($rules['item']['exist'])) {
                             $item_db['itemid'] = $current_item['itemid'];
                             $result = CItem::update($item_db);
                             if (!$result) {
                                 throw new APIException(1, CItem::resetErrors());
                             }
                             $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND);
                             $current_item = CItem::get($options);
                         }
                         if (!$current_item && isset($rules['item']['missed'])) {
                             $result = CItem::create($item_db);
                             if (!$result) {
                                 throw new APIException(1, CItem::resetErrors());
                             }
                             $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND);
                             $current_item = CItem::get($options);
                         }
                         $r = CApplication::massAdd(array('applications' => $item_applications, 'items' => $current_item));
                         if ($r === false) {
                             throw new APIException(1, CApplication::resetErrors());
                         }
                     }
                 }
                 // }}} ITEMS
                 // TRIGGERS {{{
                 if (isset($rules['trigger']['exist']) || isset($rules['trigger']['missed'])) {
                     $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);
                         $trigger_db['expression'] = str_replace('{{HOSTNAME}:', '{' . $host_db['host'] . ':', $trigger_db['expression']);
                         $trigger_db['hostid'] = $current_host['hostid'];
                         if ($current_trigger = CTrigger::exists($trigger_db)) {
                             $ctriggers = CTrigger::get(array('filter' => array('description' => $trigger_db['description']), 'hostids' => $current_host['hostid'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                             $current_trigger = false;
                             foreach ($ctriggers as $tnum => $ct) {
                                 $tmp_exp = explode_exp($ct['expression'], false);
                                 if (strcmp($trigger_db['expression'], $tmp_exp) == 0) {
                                     $current_trigger = $ct;
                                     break;
                                 }
                             }
                             if (!$current_trigger) {
                                 throw new APIException(1, 'No permission for Trigger [' . $trigger_db['description'] . ']');
                             }
                         }
                         if (!$current_trigger && !isset($rules['trigger']['missed'])) {
                             info('Trigger [' . $trigger_db['description'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($current_trigger && !isset($rules['trigger']['exist'])) {
                             info('Trigger [' . $trigger_db['description'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($current_trigger && isset($rules['trigger']['exist'])) {
                             $trigger_db['triggerid'] = $current_trigger['triggerid'];
                             $triggers_to_upd[] = $trigger_db;
                         }
                         if (!$current_trigger && isset($rules['trigger']['missed'])) {
                             $triggers_to_add[] = $trigger_db;
                         }
                     }
                     if (!empty($triggers_to_upd)) {
                         $result = CTrigger::update($triggers_to_upd);
                         if (!$result) {
                             throw new APIException(1, CTrigger::resetErrors());
                         }
                         $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND);
                         $r = CTrigger::get($options);
                         $triggers_for_dependencies = array_merge($triggers_for_dependencies, $r);
                     }
                     if (!empty($triggers_to_add)) {
                         $result = CTrigger::create($triggers_to_add);
                         if (!$result) {
                             throw new APIException(1, CTrigger::resetErrors());
                         }
                         $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND);
                         $r = CTrigger::get($options);
                         $triggers_for_dependencies = array_merge($triggers_for_dependencies, $r);
                     }
                 }
                 // }}} TRIGGERS
                 // GRAPHS {{{
                 if (isset($rules['graph']['exist']) || isset($rules['graph']['missed'])) {
                     $graphs = $xpath->query('graphs/graph', $host);
                     $graphs_to_add = array();
                     $graphs_to_upd = array();
                     foreach ($graphs as $gnum => $graph) {
                         // GRAPH ITEMS {{{
                         $gitems = $xpath->query('graph_elements/graph_element', $graph);
                         $graph_hostids = array();
                         $graph_items = array();
                         foreach ($gitems as $ginum => $gitem) {
                             $gitem_db = self::mapXML2arr($gitem, XML_TAG_GRAPH_ELEMENT);
                             $data = explode(':', $gitem_db['host_key_']);
                             $gitem_host = array_shift($data);
                             $gitem_db['host'] = $gitem_host == '{HOSTNAME}' ? $host_db['host'] : $gitem_host;
                             $gitem_db['key_'] = implode(':', $data);
                             if ($current_item = CItem::exists($gitem_db)) {
                                 $current_item = CItem::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 APIException(1, 'No permission for Item [' . $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 APIException(1, 'Item [' . $gitem_db['host_key_'] . '] does not exists');
                             }
                         }
                         // }}} GRAPH ITEMS
                         $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH);
                         $graph_db['hostids'] = $graph_hostids;
                         if ($current_graph = CGraph::exists($graph_db)) {
                             $current_graph = CGraph::get(array('filter' => array('name' => $graph_db['name']), 'hostids' => $graph_db['hostids'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                             if (empty($current_graph)) {
                                 throw new APIException(1, 'No permission for Graph [' . $graph_db['name'] . ']');
                             }
                             $current_graph = reset($current_graph);
                         }
                         if (!$current_graph && !isset($rules['graph']['missed'])) {
                             info('Graph [' . $graph_db['name'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($current_graph && !isset($rules['graph']['exist'])) {
                             info('Graph [' . $graph_db['name'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         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 APIException(1, 'Incorrect y min item for graph [' . $graph_db['name'] . ']');
                             }
                             if (!($item = get_item_by_key($item_data[1], $item_data[0]))) {
                                 throw new APIException(1, 'Missing item [' . $graph_db['ymin_item_key'] . '] for host [' . $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 APIException(1, 'Incorrect y max item for graph [' . $graph_db['name'] . ']');
                             }
                             if (!($item = get_item_by_key($item_data[1], $item_data[0]))) {
                                 throw new APIException(1, 'Missing item [' . $graph_db['ymax_item_key'] . '] for host [' . $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 = CGraph::create($graphs_to_add);
                         if ($r === false) {
                             throw new APIException(1, CGraph::resetErrors());
                         }
                     }
                     if (!empty($graphs_to_upd)) {
                         $r = CGraph::update($graphs_to_upd);
                         if ($r === false) {
                             throw new APIException(1, CGraph::resetErrors());
                         }
                     }
                 }
             }
             // DEPENDENCIES
             $dependencies = $xpath->query('dependencies/dependency');
             if ($dependencies->length > 0) {
                 $triggers_for_dependencies = zbx_objectValues($triggers_for_dependencies, 'triggerid');
                 $triggers_for_dependencies = array_flip($triggers_for_dependencies);
                 foreach ($dependencies as $dependency) {
                     $triggers_to_add_dep = array();
                     $trigger_description = $dependency->getAttribute('description');
                     $current_triggerid = get_trigger_by_description($trigger_description);
                     // sdi('<b><u>Trigger Description: </u></b>'.$trigger_description.' | <b>Current_triggerid: </b>'. $current_triggerid['triggerid']);
                     if ($current_triggerid && isset($triggers_for_dependencies[$current_triggerid['triggerid']])) {
                         $depends_on_list = $xpath->query('depends', $dependency);
                         foreach ($depends_on_list as $depends_on) {
                             $depends_triggerid = get_trigger_by_description($depends_on->nodeValue);
                             // sdi('<b>depends on description: </b>'.$depends_on->nodeValue.' | <b>depends_triggerid: </b>'. $depends_triggerid['triggerid']);
                             if ($depends_triggerid['triggerid']) {
                                 $triggers_to_add_dep[] = $depends_triggerid['triggerid'];
                             }
                         }
                         $r = update_trigger($current_triggerid['triggerid'], null, $current_triggerid['description'], null, null, null, null, null, $triggers_to_add_dep, null);
                         if ($r === false) {
                             throw new APIException();
                         }
                     }
                 }
             }
         }
         return true;
     } catch (APIException $e) {
         error($e->getErrors());
         return false;
     }
 }
Example #7
0
 /**
  * Update triggers
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @param _array $triggers multidimensional array with triggers data
  * @param array $triggers[0,...]['expression']
  * @param array $triggers[0,...]['description'] OPTIONAL
  * @param array $triggers[0,...]['type'] OPTIONAL
  * @param array $triggers[0,...]['priority'] OPTIONAL
  * @param array $triggers[0,...]['status'] OPTIONAL
  * @param array $triggers[0,...]['comments'] OPTIONAL
  * @param array $triggers[0,...]['url'] OPTIONAL
  * @return boolean
  */
 public static function update($triggers)
 {
     $result = false;
     $triggerids = array();
     DBstart(false);
     foreach ($triggers as $trigger) {
         $trigger_db_fields = self::getById($trigger);
         if (!isset($trigger_db_fields)) {
             $result = false;
             break;
         }
         if (!check_db_fields($trigger_db_fields, $trigger)) {
             error('Incorrect arguments pasted to function [CTrigger::update]');
             $result = false;
             break;
         }
         $trigger['expression'] = explode_exp($trigger['expression'], false);
         $result = update_trigger($trigger['triggerid'], $trigger['expression'], $trigger['description'], $trigger['type'], $trigger['priority'], $trigger['status'], $trigger['comments'], $trigger['url']);
         if (!$result) {
             break;
         }
         $triggerids[$result] = $result;
     }
     $result = DBend($result);
     if ($result) {
         return $triggerids;
     } else {
         self::$error[] = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error');
         return false;
     }
 }
Example #8
0
function update_trigger($triggerid, $expression = NULL, $description = NULL, $type = NULL, $priority = NULL, $status = NULL, $comments = NULL, $url = NULL, $deps = array(), $templateid = 0)
{
    $trigger = get_trigger_by_triggerid($triggerid);
    $trig_hosts = get_hosts_by_triggerid($triggerid);
    $trig_host = DBfetch($trig_hosts);
    $event_to_unknown = false;
    if (is_null($expression)) {
        /* Restore expression */
        $expression = explode_exp($trigger['expression'], 0);
    } else {
        if ($expression != explode_exp($trigger['expression'], 0)) {
            $event_to_unknown = true;
        }
    }
    if (!validate_expression($expression)) {
        return false;
    }
    if (!validate_trigger_dependency($expression, $deps)) {
        return false;
    }
    $exp_hosts = get_hosts_by_expression($expression);
    if ($exp_hosts) {
        $chd_hosts = get_hosts_by_templateid($trig_host['hostid']);
        if (DBfetch($chd_hosts)) {
            $exp_host = DBfetch($exp_hosts);
            $db_chd_triggers = get_triggers_by_templateid($triggerid);
            while ($db_chd_trigger = DBfetch($db_chd_triggers)) {
                $chd_trig_hosts = get_hosts_by_triggerid($db_chd_trigger['triggerid']);
                $chd_trig_host = DBfetch($chd_trig_hosts);
                $newexpression = str_replace('{' . $exp_host['host'] . ':', '{' . $chd_trig_host['host'] . ':', $expression);
                // recursion
                update_trigger($db_chd_trigger['triggerid'], $newexpression, $description, $type, $priority, NULL, $comments, $url, replace_template_dependencies($deps, $chd_trig_host['hostid']), $triggerid);
            }
        }
    }
    $result = delete_function_by_triggerid($triggerid);
    if (!$result) {
        return $result;
    }
    $expression = implode_exp($expression, $triggerid);
    /* errors can be ignored cose function must return NULL */
    if ($event_to_unknown) {
        add_event($triggerid, TRIGGER_VALUE_UNKNOWN);
    }
    reset_items_nextcheck($triggerid);
    $sql = 'UPDATE triggers SET ';
    if (!is_null($expression)) {
        $sql .= ' expression=' . zbx_dbstr($expression) . ',';
    }
    if (!is_null($description)) {
        $sql .= ' description=' . zbx_dbstr($description) . ',';
    }
    if (!is_null($type)) {
        $sql .= ' type=' . $type . ',';
    }
    if (!is_null($priority)) {
        $sql .= ' priority=' . $priority . ',';
    }
    if (!is_null($status)) {
        $sql .= ' status=' . $status . ',';
    }
    if (!is_null($comments)) {
        $sql .= ' comments=' . zbx_dbstr($comments) . ',';
    }
    if (!is_null($url)) {
        $sql .= ' url=' . zbx_dbstr($url) . ',';
    }
    if (!is_null($templateid)) {
        $sql .= ' templateid=' . $templateid . ',';
    }
    $sql .= ' value=2 WHERE triggerid=' . $triggerid;
    $result = DBexecute($sql);
    delete_dependencies_by_triggerid($triggerid);
    foreach ($deps as $id => $triggerid_up) {
        if (!($result2 = add_trigger_dependency($triggerid, $triggerid_up))) {
            error(S_INCORRECT_DEPENDENCY . ' [' . expand_trigger_description($triggerid_up) . ']');
        }
        $result &= $result2;
    }
    if ($result) {
        $trig_hosts = get_hosts_by_triggerid($triggerid);
        $msg = 'Trigger "' . $trigger['description'] . '" updated';
        $trig_host = DBfetch($trig_hosts);
        if ($trig_host) {
            $msg .= ' for host "' . $trig_host['host'] . '"';
        }
        info($msg);
    }
    if ($result) {
        $trigger_new = get_trigger_by_triggerid($triggerid);
        add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_TRIGGER, $triggerid, $trigger['description'], 'triggers', $trigger, $trigger_new);
    }
    $result = $result ? $triggerid : $result;
    return $result;
}
function update_trigger($triggerid, $expression = NULL, $description = NULL, $type = NULL, $priority = NULL, $status = NULL, $comments = NULL, $url = NULL, $deps = array(), $templateid = 0)
{
    $trigger = get_trigger_by_triggerid($triggerid);
    $trig_hosts = get_hosts_by_triggerid($triggerid);
    $trig_host = DBfetch($trig_hosts);
    $event_to_unknown = false;
    // Restore expression
    if (is_null($expression)) {
        $expression = explode_exp($trigger['expression'], 0);
        $expr = new CTriggerExpression(array('expression' => $expression));
    } else {
        $expr = new CTriggerExpression(array('expression' => $expression));
        $event_to_unknown = empty($expr->errors) && $expression != explode_exp($trigger['expression'], 0);
    }
    if (!empty($expr->errors)) {
        foreach ($expr->errors as $error) {
            error($error);
        }
        return false;
    }
    if (!is_null($deps) && !validate_trigger_dependency($expression, $deps)) {
        error(S_WRONG_DEPENDENCY_ERROR);
        return false;
    }
    if (is_null($description)) {
        $description = $trigger['description'];
    }
    if (CTrigger::exists(array('description' => $description, 'expression' => $expression))) {
        $host = reset($expr->data['hosts']);
        $options = array('filter' => array('description' => $description, 'host' => $host), 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
        $triggers_exist = CTrigger::get($options);
        $trigger_exist = false;
        foreach ($triggers_exist as $tnum => $tr) {
            $tmp_exp = explode_exp($tr['expression'], false);
            if (strcmp($tmp_exp, $expression) == 0) {
                $trigger_exist = $tr;
                break;
            }
        }
        if ($trigger_exist && $trigger_exist['triggerid'] != $trigger['triggerid']) {
            error('Trigger with name "' . $trigger['description'] . '" and expression "' . $expression . '" already exists.');
            return false;
        } else {
            if (!$trigger_exist) {
                error('No Permissions');
                return false;
            }
        }
    }
    $exp_hosts = $expr->data['hosts'];
    if (!empty($exp_hosts)) {
        $chd_hosts = get_hosts_by_templateid($trig_host['hostid']);
        if (DBfetch($chd_hosts)) {
            $expHostName = reset($exp_hosts);
            $db_chd_triggers = get_triggers_by_templateid($triggerid);
            while ($db_chd_trigger = DBfetch($db_chd_triggers)) {
                $chd_trig_hosts = get_hosts_by_triggerid($db_chd_trigger['triggerid']);
                $chd_trig_host = DBfetch($chd_trig_hosts);
                $newexpression = str_replace('{' . $expHostName . ':', '{' . $chd_trig_host['host'] . ':', $expression);
                // recursion
                update_trigger($db_chd_trigger['triggerid'], $newexpression, $description, $type, $priority, $status, $comments, $url, is_null($deps) ? null : replace_template_dependencies($deps, $chd_trig_host['hostid']), $triggerid);
            }
        }
    }
    $result = delete_function_by_triggerid($triggerid);
    if (!$result) {
        return $result;
    }
    $expression = implode_exp($expression, $triggerid);
    if (is_null($expression)) {
        return false;
    }
    $update_values = array();
    if (!is_null($expression)) {
        $update_values['expression'] = $expression;
    }
    if (!is_null($description)) {
        $update_values['description'] = $description;
    }
    if (!is_null($type)) {
        $update_values['type'] = $type;
    }
    if (!is_null($priority)) {
        $update_values['priority'] = $priority;
    }
    if (!is_null($status)) {
        $update_values['status'] = $status;
    }
    if (!is_null($comments)) {
        $update_values['comments'] = $comments;
    }
    if (!is_null($url)) {
        $update_values['url'] = $url;
    }
    if (!is_null($templateid)) {
        $update_values['templateid'] = $templateid;
    }
    if ($event_to_unknown || !is_null($status) && $status != TRIGGER_STATUS_ENABLED) {
        if ($trigger['value'] != TRIGGER_VALUE_UNKNOWN) {
            addEvent($triggerid, TRIGGER_VALUE_UNKNOWN);
            $update_values['value'] = TRIGGER_VALUE_UNKNOWN;
            $update_values['lastchange'] = time();
        }
    }
    DB::update('triggers', array('values' => $update_values, 'where' => array('triggerid=' . $triggerid)));
    if (!is_null($deps)) {
        delete_dependencies_by_triggerid($triggerid);
        foreach ($deps as $id => $triggerid_up) {
            if (!($result2 = add_trigger_dependency($triggerid, $triggerid_up))) {
                error(S_INCORRECT_DEPENDENCY . ' [' . expand_trigger_description($triggerid_up) . ']');
            }
            $result &= $result2;
        }
    }
    if ($result) {
        $trig_hosts = get_hosts_by_triggerid($triggerid);
        $msg = S_TRIGGER . SPACE . '"' . $trigger['description'] . '"' . SPACE . S_UPDATED_SMALL;
        $trig_host = DBfetch($trig_hosts);
        if ($trig_host) {
            $msg .= SPACE . S_FOR_HOST_SMALL . SPACE . '"' . $trig_host['host'] . '"';
        }
        info($msg);
    }
    if ($result) {
        $trigger_new = get_trigger_by_triggerid($triggerid);
        add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_TRIGGER, $triggerid, $trig_host['host'] . ':' . $trigger['description'], 'triggers', $trigger, $trigger_new);
    }
    $result = $result ? $triggerid : $result;
    return $result;
}