$frmHostG->addItemToBottomRow(new CButtonCancel()); $proxies_wdgt->addItem($frmHostG); $proxies_wdgt->show(); } else { $numrows = new CDiv(); $numrows->setAttribute('name', 'numrows'); $proxies_wdgt->addHeader(S_PROXIES_BIG); $proxies_wdgt->addHeader($numrows); $form = new CForm('proxies.php', 'get'); $form->setName('hosts'); $table = new CTableInfo(S_NO_PROXIES_DEFINED); $table->setHeader(array(new CCheckBox('all_hosts', NULL, "checkAll('" . $form->GetName() . "','all_hosts','hosts');"), make_sorting_header(S_NAME, 'host'), S_MODE, S_LASTSEEN_AGE, S_HOST_COUNT, S_ITEM_COUNT, S_REQUIRED_PERFORMANCE, S_HOSTS)); $sortfield = getPageSortField('host'); $sortorder = getPageSortOrder(); $options = array('editable' => 1, 'select_hosts' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND, 'sortfield' => $sortfield, 'sortorder' => $sortorder, 'limit' => $config['search_limit'] + 1); $proxies = CProxy::get($options); $proxies = zbx_toHash($proxies, 'proxyid'); // ordering && paging order_result($proxies, $sortfield, $sortorder); $paging = getPagingLine($proxies); // -- // CALCULATE PERFORMANCE {{{ $proxyids = array_keys($proxies); $sql = 'SELECT h.proxy_hostid, sum(1.0/i.delay) as qps ' . ' FROM items i,hosts h ' . ' WHERE i.status=' . ITEM_STATUS_ACTIVE . ' AND i.hostid=h.hostid ' . ' AND h.status=' . HOST_STATUS_MONITORED . ' AND i.delay<>0' . ' AND ' . DBcondition('h.proxy_hostid', $proxyids) . ' GROUP BY h.proxy_hostid'; $db_perf = DBselect($sql); while ($perf = DBfetch($db_perf)) { $proxies[$perf['proxy_hostid']]['perf'] = round($perf['qps'], 2); } // }}} CALCULATE PERFORMANCE // CALCULATE ITEMS {{{ $options = array('groupCount' => 1, 'countOutput' => 1, 'proxyids' => $proxyids, 'webitems' => 1, 'monitored' => 1);
private static function proxy($action, $params) { CProxy::$error = array(); switch ($action) { default: $result = call_user_func(array('CProxy', $action), $params); } self::$result = $result; }
foreach ($hosts as $num => $host) { $templateids = array_merge($templateids, zbx_objectValues($host['parentTemplates'], 'templateid')); } $templateids = array_unique($templateids); $options = array('templateids' => $templateids, 'selectParentTemplates' => array('hostid', 'host')); $templates = CTemplate::get($options); $templates = zbx_toHash($templates, 'templateid'); //--------- foreach ($hosts as $num => $host) { $applications = array(new CLink(S_APPLICATIONS, 'applications.php?groupid=' . $_REQUEST['groupid'] . '&hostid=' . $host['hostid']), ' (' . $host['applications'] . ')'); $items = array(new CLink(S_ITEMS, 'items.php?filter_set=1&hostid=' . $host['hostid']), ' (' . $host['items'] . ')'); $triggers = array(new CLink(S_TRIGGERS, 'triggers.php?groupid=' . $_REQUEST['groupid'] . '&hostid=' . $host['hostid']), ' (' . $host['triggers'] . ')'); $graphs = array(new CLink(S_GRAPHS, 'graphs.php?groupid=' . $_REQUEST['groupid'] . '&hostid=' . $host['hostid']), ' (' . $host['graphs'] . ')'); $description = array(); if ($host['proxy_hostid']) { $proxy = CProxy::get(array('proxyids' => $host['proxy_hostid'], 'extendoutput' => 1)); $proxy = reset($proxy); $description[] = $proxy['host'] . ':'; } $description[] = new CLink($host['host'], 'hosts.php?form=update&hostid=' . $host['hostid'] . url_param('groupid')); $dns = empty($host['dns']) ? '-' : $host['dns']; $ip = empty($host['ip']) ? '-' : $host['ip']; $use = 1 == $host['useip'] ? 'ip' : 'dns'; ${$use} = bold(${$use}); $status_script = null; switch ($host['status']) { case HOST_STATUS_MONITORED: if ($host['maintenance_status'] == HOST_MAINTENANCE_STATUS_ON) { $status_caption = S_IN_MAINTENANCE; $status_class = 'orange'; } else {
function insert_host_form() { global $USER_DETAILS; $host_groups = get_request('groups', array()); if (isset($_REQUEST['groupid']) && $_REQUEST['groupid'] > 0 && empty($host_groups)) { array_push($host_groups, $_REQUEST['groupid']); } $newgroup = get_request('newgroup', ''); $host = get_request('host', ''); $port = get_request('port', CProfile::get('HOST_PORT', 10050)); $status = get_request('status', HOST_STATUS_MONITORED); $useip = get_request('useip', 1); $dns = get_request('dns', ''); $ip = get_request('ip', '0.0.0.0'); $proxy_hostid = get_request('proxy_hostid', ''); $useipmi = get_request('useipmi', 'no'); $ipmi_ip = get_request('ipmi_ip', ''); $ipmi_port = get_request('ipmi_port', 623); $ipmi_authtype = get_request('ipmi_authtype', -1); $ipmi_privilege = get_request('ipmi_privilege', 2); $ipmi_username = get_request('ipmi_username', ''); $ipmi_password = get_request('ipmi_password', ''); $useprofile = get_request('useprofile', 'no'); $devicetype = get_request('devicetype', ''); $name = get_request('name', ''); $os = get_request('os', ''); $serialno = get_request('serialno', ''); $tag = get_request('tag', ''); $macaddress = get_request('macaddress', ''); $hardware = get_request('hardware', ''); $software = get_request('software', ''); $contact = get_request('contact', ''); $location = get_request('location', ''); $notes = get_request('notes', ''); $_REQUEST['hostid'] = get_request('hostid', 0); // BEGIN: HOSTS PROFILE EXTENDED Section $useprofile_ext = get_request('useprofile_ext', 'no'); $ext_host_profiles = get_request('ext_host_profiles', array()); // END: HOSTS PROFILE EXTENDED Section $templates = get_request('templates', array()); $clear_templates = get_request('clear_templates', array()); $frm_title = S_HOST; if ($_REQUEST['hostid'] > 0) { $db_host = get_host_by_hostid($_REQUEST['hostid']); $frm_title .= SPACE . ' [' . $db_host['host'] . ']'; $original_templates = get_templates_by_hostid($_REQUEST['hostid']); } else { $original_templates = array(); } if ($_REQUEST['hostid'] > 0 && !isset($_REQUEST['form_refresh'])) { $proxy_hostid = $db_host['proxy_hostid']; $host = $db_host['host']; $port = $db_host['port']; $status = $db_host['status']; $useip = $db_host['useip']; $useipmi = $db_host['useipmi'] ? 'yes' : 'no'; $ip = $db_host['ip']; $dns = $db_host['dns']; $ipmi_ip = $db_host['ipmi_ip']; $ipmi_port = $db_host['ipmi_port']; $ipmi_authtype = $db_host['ipmi_authtype']; $ipmi_privilege = $db_host['ipmi_privilege']; $ipmi_username = $db_host['ipmi_username']; $ipmi_password = $db_host['ipmi_password']; // add groups $options = array('hostids' => $_REQUEST['hostid']); $host_groups = CHostGroup::get($options); $host_groups = zbx_objectValues($host_groups, 'groupid'); // read profile $db_profiles = DBselect('SELECT * FROM hosts_profiles WHERE hostid=' . $_REQUEST['hostid']); $useprofile = 'no'; $db_profile = DBfetch($db_profiles); if ($db_profile) { $useprofile = 'yes'; $devicetype = $db_profile['devicetype']; $name = $db_profile['name']; $os = $db_profile['os']; $serialno = $db_profile['serialno']; $tag = $db_profile['tag']; $macaddress = $db_profile['macaddress']; $hardware = $db_profile['hardware']; $software = $db_profile['software']; $contact = $db_profile['contact']; $location = $db_profile['location']; $notes = $db_profile['notes']; } // BEGIN: HOSTS PROFILE EXTENDED Section $useprofile_ext = 'no'; $db_profiles_alt = DBselect('SELECT * FROM hosts_profiles_ext WHERE hostid=' . $_REQUEST['hostid']); if ($ext_host_profiles = DBfetch($db_profiles_alt)) { $useprofile_ext = 'yes'; } else { $ext_host_profiles = array(); } // END: HOSTS PROFILE EXTENDED Section $templates = $original_templates; } $ext_profiles_fields = array('device_alias' => S_DEVICE_ALIAS, 'device_type' => S_DEVICE_TYPE, 'device_chassis' => S_DEVICE_CHASSIS, 'device_os' => S_DEVICE_OS, 'device_os_short' => S_DEVICE_OS_SHORT, 'device_hw_arch' => S_DEVICE_HW_ARCH, 'device_serial' => S_DEVICE_SERIAL, 'device_model' => S_DEVICE_MODEL, 'device_tag' => S_DEVICE_TAG, 'device_vendor' => S_DEVICE_VENDOR, 'device_contract' => S_DEVICE_CONTRACT, 'device_who' => S_DEVICE_WHO, 'device_status' => S_DEVICE_STATUS, 'device_app_01' => S_DEVICE_APP_01, 'device_app_02' => S_DEVICE_APP_02, 'device_app_03' => S_DEVICE_APP_03, 'device_app_04' => S_DEVICE_APP_04, 'device_app_05' => S_DEVICE_APP_05, 'device_url_1' => S_DEVICE_URL_1, 'device_url_2' => S_DEVICE_URL_2, 'device_url_3' => S_DEVICE_URL_3, 'device_networks' => S_DEVICE_NETWORKS, 'device_notes' => S_DEVICE_NOTES, 'device_hardware' => S_DEVICE_HARDWARE, 'device_software' => S_DEVICE_SOFTWARE, 'ip_subnet_mask' => S_IP_SUBNET_MASK, 'ip_router' => S_IP_ROUTER, 'ip_macaddress' => S_IP_MACADDRESS, 'oob_ip' => S_OOB_IP, 'oob_subnet_mask' => S_OOB_SUBNET_MASK, 'oob_router' => S_OOB_ROUTER, 'date_hw_buy' => S_DATE_HW_BUY, 'date_hw_install' => S_DATE_HW_INSTALL, 'date_hw_expiry' => S_DATE_HW_EXPIRY, 'date_hw_decomm' => S_DATE_HW_DECOMM, 'site_street_1' => S_SITE_STREET_1, 'site_street_2' => S_SITE_STREET_2, 'site_street_3' => S_SITE_STREET_3, 'site_city' => S_SITE_CITY, 'site_state' => S_SITE_STATE, 'site_country' => S_SITE_COUNTRY, 'site_zip' => S_SITE_ZIP, 'site_rack' => S_SITE_RACK, 'site_notes' => S_SITE_NOTES, 'poc_1_name' => S_POC_1_NAME, 'poc_1_email' => S_POC_1_EMAIL, 'poc_1_phone_1' => S_POC_1_PHONE_1, 'poc_1_phone_2' => S_POC_1_PHONE_2, 'poc_1_cell' => S_POC_1_CELL, 'poc_1_screen' => S_POC_1_SCREEN, 'poc_1_notes' => S_POC_1_NOTES, 'poc_2_name' => S_POC_2_NAME, 'poc_2_email' => S_POC_2_EMAIL, 'poc_2_phone_1' => S_POC_2_PHONE_1, 'poc_2_phone_2' => S_POC_2_PHONE_2, 'poc_2_cell' => S_POC_2_CELL, 'poc_2_screen' => S_POC_2_SCREEN, 'poc_2_notes' => S_POC_2_NOTES); foreach ($ext_profiles_fields as $field => $caption) { if (!isset($ext_host_profiles[$field])) { $ext_host_profiles[$field] = ''; } } $clear_templates = array_intersect($clear_templates, array_keys($original_templates)); $clear_templates = array_diff($clear_templates, array_keys($templates)); natcasesort($templates); $frmHost = new CForm('hosts.php', 'post'); $frmHost->setName('web.hosts.host.php.'); // $frmHost->setHelp('web.hosts.host.php'); // $frmHost->addVar('config',get_request('config',0)); $frmHost->addVar('form', get_request('form', 1)); $from_rfr = get_request('form_refresh', 0); $frmHost->addVar('form_refresh', $from_rfr + 1); $frmHost->addVar('clear_templates', $clear_templates); // HOST WIDGET { $host_tbl = new CTable('', 'tablestripped'); $host_tbl->setOddRowClass('form_odd_row'); $host_tbl->setEvenRowClass('form_even_row'); if ($_REQUEST['hostid'] > 0) { $frmHost->addVar('hostid', $_REQUEST['hostid']); } if ($_REQUEST['groupid'] > 0) { $frmHost->addVar('groupid', $_REQUEST['groupid']); } $host_tbl->addRow(array(S_NAME, new CTextBox('host', $host, 54))); $grp_tb = new CTweenBox($frmHost, 'groups', $host_groups, 10); $all_groups = CHostGroup::get(array('editable' => 1, 'extendoutput' => 1)); order_result($all_groups, 'name'); foreach ($all_groups as $group) { $grp_tb->addItem($group['groupid'], $group['name']); } $host_tbl->addRow(array(S_GROUPS, $grp_tb->get(S_IN_GROUPS, S_OTHER_GROUPS))); $host_tbl->addRow(array(S_NEW_GROUP, new CTextBox('newgroup', $newgroup))); // onchange does not work on some browsers: MacOS, KDE browser $host_tbl->addRow(array(S_DNS_NAME, new CTextBox('dns', $dns, '40'))); if (defined('ZBX_HAVE_IPV6')) { $host_tbl->addRow(array(S_IP_ADDRESS, new CTextBox('ip', $ip, '39'))); } else { $host_tbl->addRow(array(S_IP_ADDRESS, new CTextBox('ip', $ip, '15'))); } $cmbConnectBy = new CComboBox('useip', $useip); $cmbConnectBy->addItem(0, S_DNS_NAME); $cmbConnectBy->addItem(1, S_IP_ADDRESS); $host_tbl->addRow(array(S_CONNECT_TO, $cmbConnectBy)); $host_tbl->addRow(array(S_AGENT_PORT, new CNumericBox('port', $port, 5))); //Proxy $cmbProxy = new CComboBox('proxy_hostid', $proxy_hostid); $cmbProxy->addItem(0, S_NO_PROXY); $options = array('extendoutput' => 1); $db_proxies = CProxy::get($options); order_result($db_proxies, 'host'); foreach ($db_proxies as $proxy) { $cmbProxy->addItem($proxy['proxyid'], $proxy['host']); } $host_tbl->addRow(array(S_MONITORED_BY_PROXY, $cmbProxy)); //---------- $cmbStatus = new CComboBox('status', $status); $cmbStatus->addItem(HOST_STATUS_MONITORED, S_MONITORED); $cmbStatus->addItem(HOST_STATUS_NOT_MONITORED, S_NOT_MONITORED); $host_tbl->addRow(array(S_STATUS, $cmbStatus)); $host_tbl->addRow(array(S_USEIPMI, new CCheckBox('useipmi', $useipmi, 'submit()'))); if ($useipmi == 'yes') { $host_tbl->addRow(array(S_IPMI_IP_ADDRESS, new CTextBox('ipmi_ip', $ipmi_ip, defined('ZBX_HAVE_IPV6') ? 39 : 15))); $host_tbl->addRow(array(S_IPMI_PORT, new CNumericBox('ipmi_port', $ipmi_port, 5))); $cmbIPMIAuthtype = new CComboBox('ipmi_authtype', $ipmi_authtype); $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_DEFAULT, S_AUTHTYPE_DEFAULT); $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_NONE, S_AUTHTYPE_NONE); $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_MD2, S_AUTHTYPE_MD2); $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_MD5, S_AUTHTYPE_MD5); $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_STRAIGHT, S_AUTHTYPE_STRAIGHT); $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_OEM, S_AUTHTYPE_OEM); $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_RMCP_PLUS, S_AUTHTYPE_RMCP_PLUS); $host_tbl->addRow(array(S_IPMI_AUTHTYPE, $cmbIPMIAuthtype)); $cmbIPMIPrivilege = new CComboBox('ipmi_privilege', $ipmi_privilege); $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_CALLBACK, S_PRIVILEGE_CALLBACK); $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_USER, S_PRIVILEGE_USER); $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_OPERATOR, S_PRIVILEGE_OPERATOR); $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_ADMIN, S_PRIVILEGE_ADMIN); $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_OEM, S_PRIVILEGE_OEM); $host_tbl->addRow(array(S_IPMI_PRIVILEGE, $cmbIPMIPrivilege)); $host_tbl->addRow(array(S_IPMI_USERNAME, new CTextBox('ipmi_username', $ipmi_username, 16))); $host_tbl->addRow(array(S_IPMI_PASSWORD, new CTextBox('ipmi_password', $ipmi_password, 20))); } else { $frmHost->addVar('ipmi_ip', $ipmi_ip); $frmHost->addVar('ipmi_port', $ipmi_port); $frmHost->addVar('ipmi_authtype', $ipmi_authtype); $frmHost->addVar('ipmi_privilege', $ipmi_privilege); $frmHost->addVar('ipmi_username', $ipmi_username); $frmHost->addVar('ipmi_password', $ipmi_password); } if ($_REQUEST['form'] == 'full_clone') { // Host items $options = array('inherited' => 0, 'hostids' => $_REQUEST['hostid'], 'output' => API_OUTPUT_EXTEND, 'webitems' => 1); $host_items = CItem::get($options); if (!empty($host_items)) { $items_lbx = new CListBox('items', null, 8); $items_lbx->setAttribute('disabled', 'disabled'); order_result($host_items, 'description'); foreach ($host_items as $hitem) { $items_lbx->addItem($hitem['itemid'], item_description($hitem)); } $host_tbl->addRow(array(S_ITEMS, $items_lbx)); } // Host triggers $options = array('inherited' => 0, 'hostids' => $_REQUEST['hostid'], 'output' => API_OUTPUT_EXTEND, 'expandDescription' => true); $host_triggers = CTrigger::get($options); if (!empty($host_triggers)) { $trig_lbx = new CListBox('triggers', null, 8); $trig_lbx->setAttribute('disabled', 'disabled'); order_result($host_triggers, 'description'); foreach ($host_triggers as $htrigger) { $trig_lbx->addItem($htrigger['triggerid'], $htrigger['description']); } $host_tbl->addRow(array(S_TRIGGERS, $trig_lbx)); } // Host graphs $options = array('inherited' => 0, 'hostids' => $_REQUEST['hostid'], 'select_hosts' => API_OUTPUT_REFER, 'output' => API_OUTPUT_EXTEND); $host_graphs = CGraph::get($options); if (!empty($host_graphs)) { $graphs_lbx = new CListBox('graphs', null, 8); $graphs_lbx->setAttribute('disabled', 'disabled'); order_result($host_graphs, 'name'); foreach ($host_graphs as $hgraph) { if (count($hgraph['hosts']) > 1) { continue; } $graphs_lbx->addItem($hgraph['graphid'], $hgraph['name']); } if ($graphs_lbx->ItemsCount() > 1) { $host_tbl->addRow(array(S_GRAPHS, $graphs_lbx)); } } } $host_footer = array(); $host_footer[] = new CButton('save', S_SAVE); if ($_REQUEST['hostid'] > 0 && $_REQUEST['form'] != 'full_clone') { array_push($host_footer, SPACE, new CButton('clone', S_CLONE), SPACE, new CButton('full_clone', S_FULL_CLONE), SPACE, new CButtonDelete(S_DELETE_SELECTED_HOST_Q, url_param('form') . url_param('hostid') . url_param('groupid'))); } array_push($host_footer, SPACE, new CButtonCancel(url_param('groupid'))); $host_footer = new CCol($host_footer); $host_footer->setColSpan(2); $host_tbl->setFooter($host_footer); $host_wdgt = new CWidget(); $host_wdgt->setClass('header'); $host_wdgt->addHeader($frm_title); $host_wdgt->addItem($host_tbl); // } HOST WIDGET // TEMPLATES{ $template_tbl = new CTableInfo(S_NO_TEMPLATES_LINKED, 'tablestripped'); $template_tbl->setOddRowClass('form_odd_row'); $template_tbl->setEvenRowClass('form_even_row'); foreach ($templates as $id => $temp_name) { $frmHost->addVar('templates[' . $id . ']', $temp_name); $template_tbl->addRow(new CCol(array(new CCheckBox('templates_rem[' . $id . ']', 'no', null, $id), $temp_name))); } $footer = new CCol(array(new CButton('add_template', S_ADD, "return PopUp('popup.php?dstfrm=" . $frmHost->getName() . "&dstfld1=new_template&srctbl=templates&srcfld1=hostid&srcfld2=host" . url_param($templates, false, 'existed_templates') . "',450,450)", 'T'), SPACE, new CButton('unlink', S_UNLINK), SPACE, new CButton('unlink_and_clear', S_UNLINK_AND_CLEAR))); //$footer->setColSpan(2); $template_tbl->setFooter($footer); $template_wdgt = new CWidget(); $template_wdgt->setClass('header'); $template_wdgt->addHeader(S_LINKED_TEMPLATES); $template_wdgt->addItem($template_tbl); // } TEMPLATES // MACROS WIDGET { $macros_wdgt = get_macros_widget($_REQUEST['hostid']); // } MACROS WIDGET // PROFILE WIDGET { $profile_tbl = new CTable('', 'tablestripped'); $profile_tbl->setOddRowClass('form_odd_row'); $profile_tbl->setEvenRowClass('form_even_row'); $profile_tbl->addRow(array(S_USE_PROFILE, new CCheckBox('useprofile', $useprofile, 'submit()'))); if ($useprofile == 'yes') { $profile_tbl->addRow(array(S_DEVICE_TYPE, new CTextBox('devicetype', $devicetype, 61))); $profile_tbl->addRow(array(S_NAME, new CTextBox('name', $name, 61))); $profile_tbl->addRow(array(S_OS, new CTextBox('os', $os, 61))); $profile_tbl->addRow(array(S_SERIALNO, new CTextBox('serialno', $serialno, 61))); $profile_tbl->addRow(array(S_TAG, new CTextBox('tag', $tag, 61))); $profile_tbl->addRow(array(S_MACADDRESS, new CTextBox('macaddress', $macaddress, 61))); $profile_tbl->addRow(array(S_HARDWARE, new CTextArea('hardware', $hardware, 60, 4))); $profile_tbl->addRow(array(S_SOFTWARE, new CTextArea('software', $software, 60, 4))); $profile_tbl->addRow(array(S_CONTACT, new CTextArea('contact', $contact, 60, 4))); $profile_tbl->addRow(array(S_LOCATION, new CTextArea('location', $location, 60, 4))); $profile_tbl->addRow(array(S_NOTES, new CTextArea('notes', $notes, 60, 4))); } else { $frmHost->addVar('devicetype', $devicetype); $frmHost->addVar('name', $name); $frmHost->addVar('os', $os); $frmHost->addVar('serialno', $serialno); $frmHost->addVar('tag', $tag); $frmHost->addVar('macaddress', $macaddress); $frmHost->addVar('hardware', $hardware); $frmHost->addVar('software', $software); $frmHost->addVar('contact', $contact); $frmHost->addVar('location', $location); $frmHost->addVar('notes', $notes); } $profile_wdgt = new CWidget(); $profile_wdgt->setClass('header'); $profile_wdgt->addHeader(S_PROFILE); $profile_wdgt->addItem($profile_tbl); // } PROFILE WIDGET // EXT PROFILE WIDGET { $ext_profile_tbl = new CTable('', 'tablestripped'); $ext_profile_tbl->setOddRowClass('form_odd_row'); $ext_profile_tbl->setEvenRowClass('form_even_row'); $ext_profile_tbl->addRow(array(S_USE_EXTENDED_PROFILE, new CCheckBox('useprofile_ext', $useprofile_ext, 'submit()', 'yes'))); foreach ($ext_profiles_fields as $prof_field => $caption) { if ($useprofile_ext == 'yes') { $ext_profile_tbl->addRow(array($caption, new CTextBox('ext_host_profiles[' . $prof_field . ']', $ext_host_profiles[$prof_field], 40))); } else { $frmHost->addVar('ext_host_profiles[' . $prof_field . ']', $ext_host_profiles[$prof_field]); } } $ext_profile_wdgt = new CWidget(); $ext_profile_wdgt->setClass('header'); $ext_profile_wdgt->addHeader(S_EXTENDED_HOST_PROFILE); $ext_profile_wdgt->addItem($ext_profile_tbl); // } EXT PROFILE WIDGET $left_table = new CTable(); $left_table->setCellPadding(4); $left_table->setCellSpacing(4); $left_table->addRow($host_wdgt); $right_table = new CTable(); $right_table->setCellPadding(4); $right_table->setCellSpacing(4); $right_table->addRow($template_wdgt); $right_table->addRow($macros_wdgt); $right_table->addRow($profile_wdgt); $right_table->addRow($ext_profile_wdgt); $td_l = new CCol($left_table); $td_l->setAttribute('valign', 'top'); $td_r = new CCol($right_table); $td_r->setAttribute('valign', 'top'); $outer_table = new CTable(); $outer_table->addRow(array($td_l, $td_r)); $frmHost->addItem($outer_table); return $frmHost; }
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; } }