//获取传过来的参数 if (isset($_POST['changeType'])) { $changedayList = array('1hour' => 3600, '2hour' => 7200, '1days' => 3600 * 24, '2days' => 3600 * 48, '7days' => 3600 * 24 * 7, '30days' => 3600 * 24 * 30, '1years' => 3600 * 24 * 365, '2years' => 3600 * 24 * 365 * 2); $changeType = $_POST['changeType']; $endTime = time(); $beginTime = $endTime - $changedayList[$changeType]; echo "{endTime:" . json_encode(date("Y-m-d H:i:s", $endTime)) . ",beginTime:" . json_encode(date("Y-m-d H:i:s", $beginTime)) . "}"; exit; } global $zabbix_api_config; $url_http = dirname(dirname('http://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"])); $zabbixApi = new ZabbixApi($url_http . '/' . trim($zabbix_api_config['api_url']), trim($zabbix_api_config['user']), trim($zabbix_api_config['passowrd'])); $groupid = isset($_GET["groupid"]) ? $_GET["groupid"] : 0; if ($groupid > 0) { //根据分组id查询分组下的机器 $hosts = $zabbixApi->hostGet(array("output" => "extend", "monitored_hosts" => true, "groupids" => array($groupid), "sortfield" => array("host"), "sortorder" => array("ASC"))); $new_list = array(); foreach ($hosts as $each_host) { //$new_list[ip2long($each_host->host)]=$each_host; $new_list[] = $each_host; } ksort($new_list); foreach ($new_list as &$each_host_new) { $each_host_new->target = 'rightFrame'; $each_host_new->groupids = $groupid; $each_host_new->url = 'graph.php?hostid=' . $each_host_new->hostid; } echo json_encode(array_values($new_list)); } else { //查询所有的分组列表 $groups = $zabbixApi->hostgroupGet(array("output" => "extend", "monitored_hosts" => true, "sortfield" => "name"));
/** * Функиция импорта оборудования из Zabbix * @return string */ function getZabbixImport() { try { $region = JRequest::getVar('region'); //Пределяем опорный регион $db = JFactory::getDBO(); // Подключаемся к базе. $query = "SELECT api AS api, login AS login, password AS password FROM #__neq_region WHERE id = " . $region; //Определяем запрос //echo $query; $db->setQuery($query); //Выполняем запрос $rList = $db->loadObjectList(); if ($rList = $db->loadObjectList()) { if ($rList[0]->api == '') { return ''; } $api = new ZabbixApi($rList[0]->api, $rList[0]->login, $rList[0]->password); } else { return $db->stderr(); } $rList = $api->hostGet(array('output' => 'extend', 'search' => array('host' => ''), 'sortfield' => 'host', 'selectInventory' => array('alias', 'asset_tag', 'chassis', 'contact', 'contract_number', 'date_hw_decomm', 'date_hw_expiry', 'date_hw_install', 'date_hw_purchase', 'deployment_status', 'hardware', 'hardware_full', 'host_netmask', 'host_networks', 'host_router', 'hw_arch', 'installer_name', 'inventory_mode', 'location', 'location_lat', 'location_lon', 'macaddress_a', 'macaddress_b', 'model', 'name_inv', 'notes', 'oob_ip', 'oob_netmask', 'oob_router', 'os', 'os_full', 'os_short', 'poc_1_cell', 'poc_1_email', 'poc_1_name', 'poc_1_notes', 'poc_1_phone_a', 'poc_1_phone_b', 'poc_1_screen', 'poc_2_cell', 'poc_2_email', 'poc_2_name', 'poc_2_notes', 'poc_2_phone_a', 'poc_2_phone_b', 'poc_2_screen', 'serialno_a', 'serialno_b', 'site_address_a', 'site_address_b', 'site_address_c', 'site_city', 'site_country', 'site_notes', 'site_rack', 'site_state', 'site_zip', 'software', 'software_app_a', 'software_app_b', 'software_app_c', 'software_app_d', 'software_app_e', 'software_full', 'tag', 'inv_type', 'type_full', 'url_a', 'url_b', 'url_c', 'vendor'))); foreach ($rList as $host) { $query = "SELECT COUNT(id) AS count FROM #__inv_zabbix WHERE #__inv_zabbix.hostid='{$host->hostid}' AND #__inv_zabbix.id_reg = {$region}"; $db->setQuery($query); $rows = $db->loadResult(); if ($rows > 0) { $db->setQuery("UPDATE #__inv_zabbix SET\r\n\t\t\t\t\t\t\t host='{$host->host}', available='{$host->available}', description='{$host->description}',\r\n\t\t\t\t\t\t\t name='{$host->name}', proxy_hostid='{$host->proxy_hostid}', snmp_available='{$host->snmp_available}', status='{$host->status}',\r\n\t\t\t\t\t\t\t alias='{$host->inventory->alias}', asset_tag='{$host->inventory->asset_tag}', chassis='{$host->inventory->chassis}',\r\n\t\t\t\t\t\t\t contact='{$host->inventory->contact}', contract_number='{$host->inventory->contract_number}',\r\n\t\t\t\t\t\t\t date_hw_decomm='{$host->inventory->date_hw_decomm}', date_hw_expiry='{$host->inventory->date_hw_expiry}',\r\n\t\t\t\t\t\t\t date_hw_install='{$host->inventory->date_hw_install}', date_hw_purchase='{$host->inventory->date_hw_purchase}',\r\n\t\t\t\t\t\t\t deployment_status='{$host->inventory->deployment_status}', hardware='{$host->inventory->hardware}',\r\n\t\t\t\t\t\t\t hardware_full='{$host->inventory->hardware_full}', host_netmask='{$host->inventory->host_netmask}',\r\n\t\t\t\t\t\t\t host_networks='{$host->inventory->host_networks}', host_router='{$host->inventory->host_router}',\r\n\t\t\t\t\t\t\t hw_arch='{$host->inventory->hw_arch}', installer_name='{$host->inventory->installer_name}',\r\n\t\t\t\t\t\t\t inventory_mode='{$host->inventory->inventory_mode}', location='{$host->inventory->location}',\r\n\t\t\t\t\t\t\t location_lat='{$host->inventory->location_lat}', location_lon='{$host->inventory->location_lon}',\r\n\t\t\t\t\t\t\t macaddress_a='{$host->inventory->macaddress_a}', macaddress_b='{$host->inventory->macaddress_b}',\r\n\t\t\t\t\t\t\t model='{$host->inventory->model}', inv_name='{$host->inventory->inv_name}', notes='{$host->inventory->notes}',\r\n\t\t\t\t\t\t\t oob_ip='{$host->inventory->oob_ip}',oob_netmask='{$host->inventory->oob_netmask}',\r\n\t\t\t\t\t\t\t\t oob_router='{$host->inventory->oob_router}', os='{$host->inventory->os}', os_full='{$host->inventory->os_full}',\r\n\t\t\t\t\t\t\t\t os_short='{$host->inventory->os_short}', poc_1_cell='{$host->inventory->poc_1_cell}',\r\n\t\t\t\t\t\t\t\t poc_1_email='{$host->inventory->poc_1_email}', poc_1_name='{$host->inventory->poc_1_name}',\r\n\t\t\t\t\t\t\t\t poc_1_notes='{$host->inventory->poc_1_notes}', poc_1_phone_a='{$host->inventory->poc_1_phone_a}',\r\n\t\t\t\t\t\t\t\t poc_1_phone_b='{$host->inventory->poc_1_phone_b}', poc_1_screen='{$host->inventory->poc_1_screen}',\r\n\t\t\t\t\t\t\t\t poc_2_cell='{$host->inventory->poc_2_cell}', poc_2_email='{$host->inventory->poc_2_email}',\r\n\t\t\t\t\t\t\t\t poc_2_name='{$host->inventory->poc_2_name}', poc_2_notes='{$host->inventory->poc_2_notes}',\r\n\t\t\t\t\t\t\t\t poc_2_phone_a='{$host->inventory->poc_2_phone_a}', poc_2_phone_b='{$host->inventory->poc_2_phone_b}',\r\n\t\t\t\t\t\t\t\t poc_2_screen='{$host->inventory->poc_2_screen}', serialno_a='{$host->inventory->serialno_a}',\r\n\t\t\t\t\t\t\t\t serialno_b='{$host->inventory->serialno_b}', site_address_a='{$host->inventory->site_address_a}',\r\n\t\t\t\t\t\t\t\t site_address_b='{$host->inventory->site_address_b}', site_address_c='{$host->inventory->site_address_c}',\r\n\t\t\t\t\t\t\t\t site_city='{$host->inventory->site_city}', site_country='{$host->inventory->site_country}',\r\n\t\t\t\t\t\t\t\t site_notes='{$host->inventory->site_notes}', site_rack='{$host->inventory->site_rack}',\r\n\t\t\t\t\t\t\t\t site_state='{$host->inventory->site_state}', site_zip='{$host->inventory->site_zip}',\r\n\t\t\t\t\t\t\t\t software='{$host->inventory->software}', software_app_a='{$host->inventory->software_app_a}',\r\n\t\t\t\t\t\t\t\t software_app_b='{$host->inventory->software_app_b}', software_app_c='{$host->inventory->software_app_c}',\r\n\t\t\t\t\t\t\t\t software_app_d='{$host->inventory->software_app_d}', software_app_e='{$host->inventory->software_app_e}',\r\n\t\t\t\t\t\t\t\t software_full='{$host->inventory->software_full}', tag='{$host->inventory->tag}',\r\n\t\t\t\t\t\t\t\t inv_type='{$host->inventory->inv_type}', type_full='{$host->inventory->type_full}',\r\n\t\t\t\t\t\t\t\t url_a='{$host->inventory->url_a}', url_b='{$host->inventory->url_b}', url_c='{$host->inventory->url_c}',\r\n\t\t\t\t\t\t\t\t vendor='{$host->inventory->vendor}'\r\n\t\t\t\t\t\t\t\tWHERE #__inv_zabbix.hostid='{$host->hostid}' AND #__inv_zabbix.id_reg = {$region}"); $db->query(); } else { $db->setQuery("INSERT INTO #__inv_zabbix\r\n\t\t\t\t\t\t\t\t( id_reg, hostid, host, available, description, name, proxy_hostid, snmp_available, status, alias, asset_tag,\r\n\t\t\t\t\t\t\t\t chassis, contact,contract_number, date_hw_decomm, date_hw_expiry, date_hw_install, date_hw_purchase,deployment_status,\r\n\t\t\t\t\t\t\t\t hardware, hardware_full, host_netmask, host_networks, host_router, hw_arch, installer_name, inventory_mode,\r\n\t\t\t\t\t\t\t\t location, location_lat, location_lon, macaddress_a, macaddress_b, model, inv_name, notes, oob_ip,oob_netmask,\r\n\t\t\t\t\t\t\t\t oob_router, os, os_full, os_short, poc_1_cell, poc_1_email, poc_1_name, poc_1_notes, poc_1_phone_a,\r\n\t\t\t\t\t\t\t\t poc_1_phone_b, poc_1_screen, poc_2_cell, poc_2_email, poc_2_name, poc_2_notes, poc_2_phone_a, poc_2_phone_b,\r\n\t\t\t\t\t\t\t\t poc_2_screen, serialno_a, serialno_b, site_address_a, site_address_b, site_address_c, site_city, site_country,\r\n\t\t\t\t\t\t\t\t site_notes, site_rack, site_state, site_zip, software, software_app_a, software_app_b, software_app_c,\r\n\t\t\t\t\t\t\t\t software_app_d, software_app_e, software_full, tag, inv_type, type_full, url_a, url_b, url_c, vendor)\r\n\t\t\t\t\t\t\t \r\n\t\t\t\t\t\t\tVALUES ({$region},'{$host->hostid}','{$host->host}','{$host->available}','{$host->description}','{$host->name}','{$host->proxy_hostid}',\r\n\t\t\t\t\t\t\t\t '{$host->snmp_available}','{$host->status}','{$host->inventory->alias}', '{$host->inventory->asset_tag}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->chassis}', '{$host->inventory->contact}', '{$host->inventory->contract_number}', '{$host->inventory->date_hw_decomm}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->date_hw_expiry}', '{$host->inventory->date_hw_install}', '{$host->inventory->date_hw_purchase}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->deployment_status}', '{$host->inventory->hardware}', '{$host->inventory->hardware_full}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->host_netmask}', '{$host->inventory->host_networks}', '{$host->inventory->host_router}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->hw_arch}', '{$host->inventory->installer_name}', '{$host->inventory->inventory_mode}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->location}', '{$host->inventory->location_lat}', '{$host->inventory->location_lon}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->macaddress_a}', '{$host->inventory->macaddress_b}', '{$host->inventory->model}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->name}', '{$host->inventory->notes}', '{$host->inventory->oob_ip}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->oob_netmask}', '{$host->inventory->oob_router}', '{$host->inventory->os}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->os_full}', '{$host->inventory->os_short}', '{$host->inventory->poc_1_cell}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->poc_1_email}', '{$host->inventory->poc_1_name}', '{$host->inventory->poc_1_notes}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->poc_1_phone_a}', '{$host->inventory->poc_1_phone_b}', '{$host->inventory->poc_1_screen}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->poc_2_cell}', '{$host->inventory->poc_2_email}', '{$host->inventory->poc_2_name}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->poc_2_notes}', '{$host->inventory->poc_2_phone_a}', '{$host->inventory->poc_2_phone_b}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->poc_2_screen}', '{$host->inventory->serialno_a}', '{$host->inventory->serialno_b}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->site_address_a}', '{$host->inventory->site_address_b}', '{$host->inventory->site_address_c}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->site_city}', '{$host->inventory->site_country}', '{$host->inventory->site_notes}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->site_rack}', '{$host->inventory->site_state}', '{$host->inventory->site_zip}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->software}', '{$host->inventory->software_app_a}', '{$host->inventory->software_app_b}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->software_app_c}', '{$host->inventory->software_app_d}', '{$host->inventory->software_app_e}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->software_full}', '{$host->inventory->tag}', '{$host->inventory->type}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->type_full}', '{$host->inventory->url_a}', '{$host->inventory->url_b}', '{$host->inventory->url_c}',\r\n\t\t\t\t\t\t\t\t '{$host->inventory->vendor}')"); $db->query(); } } } catch (PDOException $e) { return 'Ошибка базы данных: ' . $e->getMessage(); } }
SetCookie("itemkey", $itemkey, $curtime + 3600); SetCookie("stime", $stime, $curtime + 3600); SetCookie("endtime", $endtime, $curtime + 3600); SetCookie("order_key", $orderkey, $curtime + 3600); SetCookie("order_type", $ordertype, $curtime + 3600); } $period = strtotime($endtime) - strtotime($stime); $fortime = date("YmdHis", strtotime($stime)); $width = 286; //图形宽度 global $zabbix_api_config; $url_http = dirname(dirname('http://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"])); $zabbixApi = new ZabbixApi($url_http . '/' . trim($zabbix_api_config['api_url']), trim($zabbix_api_config['user']), trim($zabbix_api_config['passowrd'])); //域名下传主机名 if (isset($_REQUEST["host"]) && $_REQUEST["host"]) { $host_info_by = $zabbixApi->hostGet(array("output" => "extend", "filter" => array('host' => $_REQUEST["host"]))); if (isset($host_info_by[0])) { $hostid = $host_info_by[0]->hostid; } } ?> <form method="get" style="font-size:8px;text-align:left;padding-left:10px;" id="searchForm" > <div> <input type="hidden" name="hostid" value="<?php echo $hostid; ?> " id="hostid" /> <input type="hidden" name="group_class" value="<?php echo $group_class; ?> " id="group_class" />
<?php // load ZabbixApi require 'ZabbixApi.class.php'; $password = getenv("ZABBIX_PASSWORD"); $hostname = getenv("HOST"); $zabbixkey = getenv("KEY"); $time_range = getenv("RANGE"); // connect to Zabbix API $api = new ZabbixApi('http://localhost/zabbix/api_jsonrpc.php', 'admin', $password); // use extended output for all further requests $api->setDefaultParams(array('output' => 'extend')); $host = $api->hostGet(array("filter" => array($hostname))); if (count($host) != 1) { throw new Exception("host query returned 2 or more host."); } $item = $api->itemGet(array("filter" => array("key_" => $zabbixkey), "hostids" => $host[0]->hostid)); if (count($item) != 1) { throw new Exception("item query returned 2 or more host."); } $history = $api->historyGet(array('history' => $item[0]->value_type, 'sortfield' => array('itemid', 'clock'), 'itemids' => $item[0]->itemid, 'time_from' => time() - (int) $time_range)); echo "'{$hostname} {$zabbixkey}'\n"; foreach ($history as $hist) { echo $hist->value; echo "\n"; }