reasonableframework/helper/zabbix.api.php

292 lines
9.2 KiB
PHP
Raw Normal View History

2019-04-08 00:12:14 +00:00
<?php
2019-04-08 06:52:03 +00:00
/**
* @file zabbix.api.php
* @date 2019-04-08
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Zabbix JSON-RPC API (3.0) interface module
* @documentation https://www.zabbix.com/documentation/3.0/manual/api
*/
2019-04-08 00:12:14 +00:00
2019-04-15 06:15:01 +00:00
if(!check_function_exists("get_zabbix_config")) {
2019-05-20 08:19:05 +00:00
function get_zabbix_config() {
$config = get_config();
return array(
"host" => get_value_in_array("zabbix_host", $config, "127.0.0.1"),
"username" => get_value_in_array("zabbix_username", $config, "Admin"),
"password" => get_value_in_array("zabbix_password", $config, "zabbix"),
"protocol" => get_value_in_array("zabbix_protocol", $config, "http"),
);
}
2019-04-15 06:15:01 +00:00
}
2019-04-08 01:19:23 +00:00
if(!check_function_exists("zabbix_get_base_url")) {
2019-05-20 08:19:05 +00:00
function zabbix_get_api_url() {
$cnf = get_zabbix_config();
return sprintf("%s://%s/zabbix/api_jsonrpc.php", $cnf['protocol'], $cnf['host']);
}
2019-04-08 01:19:23 +00:00
}
2019-04-09 05:55:56 +00:00
if(!check_function_exists("zabbix_get_id")) {
2019-05-20 08:19:05 +00:00
function zabbix_get_id() {
2020-01-27 10:47:03 +00:00
return 1;
2019-05-20 08:19:05 +00:00
}
2019-04-09 05:55:56 +00:00
}
2019-04-08 01:19:23 +00:00
if(!check_function_exists("zabbix_authenticate")) {
2019-05-20 08:19:05 +00:00
function zabbix_authenticate() {
$response = false;
// get zabbix configuration
$cnf = get_zabbix_config();
// get zabbix api url
$zabbix_api_url = zabbix_get_api_url($cnf['host'], $cnf['protocol']);
// connect to zabbix server
if(loadHelper("webpagetool")) {
2019-11-13 08:22:33 +00:00
$response = get_web_json($zabbix_api_url, "jsonrpc2.cache", array(
2019-11-12 07:41:50 +00:00
"method" => "user.login",
"params" => array(
"user" => $cnf['username'],
"password" => $cnf['password'],
2019-05-20 08:19:05 +00:00
),
2019-11-12 07:41:50 +00:00
"id" => zabbix_get_id(),
"auth" => null
2019-05-20 08:19:05 +00:00
));
}
// set connection to global scope
2020-01-28 04:36:56 +00:00
set_shared_var("zabbix_api_url", $zabbix_api_url);
set_shared_var("zabbix_auth", get_property_value("result", $response));
2019-05-20 08:19:05 +00:00
return $response;
}
2019-04-08 06:52:03 +00:00
}
2020-01-27 11:26:38 +00:00
if(!check_function_exists("zabbix_get_hosts")) {
function zabbix_get_hosts() {
2019-11-12 06:10:09 +00:00
$hosts = false;
2019-05-20 08:19:05 +00:00
$response = false;
// get zabbix authentication
2020-01-28 04:36:56 +00:00
$zabbix_api_url = get_shared_var("zabbix_api_url");
$zabbix_auth = get_shared_var("zabbix_auth");
2019-05-20 08:19:05 +00:00
// connect to zabbix server
if(loadHelper("webpagetool")) {
2019-11-13 08:22:33 +00:00
$response = get_web_json($zabbix_api_url, "jsonrpc2.cache", array(
2019-11-12 07:41:50 +00:00
"method" => "host.get",
"params" => array(
"output" => array("hostid", "host"),
"selectInterfaces" => array("interfaceid", "ip"),
2019-05-20 08:19:05 +00:00
),
2019-11-12 07:41:50 +00:00
"id" => zabbix_get_id(),
"auth" => $zabbix_auth
2019-05-20 08:19:05 +00:00
));
2019-11-12 06:10:09 +00:00
$hosts = get_property_value("result", $response);
2019-05-20 08:19:05 +00:00
}
2019-11-12 06:10:09 +00:00
return $hosts;
2019-05-20 08:19:05 +00:00
}
2019-04-09 05:55:56 +00:00
}
2020-01-27 11:26:38 +00:00
if(!check_function_exists("zabbix_retrieve_hosts")) {
function zabbix_retrieve_hosts() {
return zabbix_get_hosts();
}
}
2019-04-09 05:55:56 +00:00
if(!check_function_exists("zabbix_get_items")) {
2019-11-12 06:10:09 +00:00
function zabbix_get_items($hostids=null) {
$items = false;
$results = false;
2019-05-20 08:19:05 +00:00
$response = false;
// get zabbix authentication
2020-01-28 04:36:56 +00:00
$zabbix_api_url = get_shared_var("zabbix_api_url");
$zabbix_auth = get_shared_var("zabbix_auth");
2019-05-20 08:19:05 +00:00
// connect to zabbix server
if(loadHelper("webpagetool")) {
2019-11-13 08:22:33 +00:00
$response = get_web_json($zabbix_api_url, "jsonrpc2.cache", array(
2019-11-12 07:41:50 +00:00
"method" => "host.get",
"params" => array(
"selectInventory" => true,
2020-01-27 11:26:38 +00:00
"selectItems" => array("name", "key_", "status", "lastvalue", "units", "itemid", "lastclock", "value_type", "itemid"),
2019-11-12 07:41:50 +00:00
"output" => "extend",
"hostids" => $hostids,
"expandDescription" => 1,
"expandData" => 1,
2019-05-20 08:19:05 +00:00
),
2019-11-12 07:41:50 +00:00
"id" => zabbix_get_id(),
"auth" => $zabbix_auth
2019-05-20 08:19:05 +00:00
));
2019-11-12 06:10:09 +00:00
$results = get_property_value("result", $response);
foreach($results as $result) {
$items = get_property_value("items", $result);
break;
}
2019-05-20 08:19:05 +00:00
}
2019-11-12 06:10:09 +00:00
return $items;
}
}
if(!check_function_exists("zabbix_get_problems")) {
function zabbix_get_problems($hostids=null) {
$problems = false;
$response = false;
// get zabbix authentication
2020-01-28 04:36:56 +00:00
$zabbix_api_url = get_shared_var("zabbix_api_url");
$zabbix_auth = get_shared_var("zabbix_auth");
2019-11-12 06:10:09 +00:00
// connect to zabbix server
if(loadHelper("webpagetool")) {
2019-11-13 08:22:33 +00:00
$response = get_web_json($zabbix_api_url, "jsonrpc2.cache", array(
2019-11-12 07:41:50 +00:00
"method" => "problem.get",
"params" => array(
"output" => "extend",
"selectAcknowledges" => "extend",
"selectTags" => "extend",
"selectSuppressionData" => "extend",
"hostids" => $hostids,
"recent" => "false",
//"suppressed" => "false",
//"acknowledged" => "false",
//"sortfield" => ["eventid"],
//"sortorder" => "DESC",
//"time_from" => get_current_datetime(array("adjust" => "1 hour"))
2019-11-12 06:10:09 +00:00
),
2019-11-12 07:41:50 +00:00
"id" => zabbix_get_id(),
"auth" => $zabbix_auth
2019-11-12 06:10:09 +00:00
));
}
$problems = get_property_value("result", $response);
return $problems;
2019-05-20 08:19:05 +00:00
}
2019-04-08 01:19:23 +00:00
}
2019-11-12 07:41:50 +00:00
if(!check_function_exists("zabbix_get_triggers")) {
2019-11-13 08:20:35 +00:00
function zabbix_get_triggers($hostids=null) {
$triggers = false;
$response = false;
// get zabbix authentication
2020-01-28 04:36:56 +00:00
$zabbix_api_url = get_shared_var("zabbix_api_url");
$zabbix_auth = get_shared_var("zabbix_auth");
2019-11-13 08:20:35 +00:00
if(loadHelper("webpagetool")) {
2019-11-13 08:22:33 +00:00
$response = get_web_json($zabbix_api_url, "jsonrpc2.cache", array(
2019-11-13 08:20:35 +00:00
"method" => "trigger.get",
"params" => array(
"hostids" => $hostids,
"output" => "extend",
"selectFunctions" => "extend",
"filter" => array(
"value" => 1,
"status" => 0
)
),
"id" => zabbix_get_id(),
"auth" => $zabbix_auth
));
}
$triggers = get_property_value("result", $response);
2019-11-12 07:41:50 +00:00
2019-11-13 08:20:35 +00:00
return $triggers;
2019-11-12 07:41:50 +00:00
}
}
2020-01-27 10:47:03 +00:00
if(!check_function_exists("zabbix_get_alerts")) {
function zabbix_get_alerts($hostids=null, $time_from=0, $time_till=0) {
$alerts = false;
$response = false;
// get zabbix authentication
2020-01-28 04:36:56 +00:00
$zabbix_api_url = get_shared_var("zabbix_api_url");
$zabbix_auth = get_shared_var("zabbix_auth");
2020-01-27 10:47:03 +00:00
if(loadHelper("webpagetool")) {
$params = array(
"output" => "extend",
"hostids" => $hostids,
"sortfield" => array("clock", "eventid"),
"sortorder" => "DESC"
);
if($time_from > 0) {
$params['time_from'] = $time_from - 1;
}
if($time_till > 0) {
$params['time_till'] = $time_till + 1;
}
$response = get_web_json($zabbix_api_url, "jsonrpc2.cache", array(
"method" => "event.get",
"params" => array(
"output" => "extend",
"hostids" => $hostids,
"sortfield" => array("clock", "eventid"),
"sortorder" => "DESC"
),
"auth" => $zabbix_auth,
"id" => zabbix_get_id()
));
$alerts = get_property_value("result", $response);
}
return $alerts;
}
}
if(!check_function_exists("zabbix_get_records")) {
function zabbix_get_records($itemids, $now_dt="", $adjust="-24h") {
$records = false;
$response = false;
// get current datetime
if(empty($now_dt)) {
$now_dt = get_current_datetime();
}
// get zabbix authentication
2020-01-28 04:36:56 +00:00
$zabbix_api_url = get_shared_var("zabbix_api_url");
$zabbix_auth = get_shared_var("zabbix_auth");
2020-01-27 10:47:03 +00:00
// set time range variables
$time_from = get_current_timestamp(array("now" => $now_dt, "adjust" => $adjust));
$time_till = get_current_timestamp(array("now" => $now_dt));
// get history
if(loadHelper("webpagetool")) {
$params = array(
"output" => "extend",
"history" => 3,
"itemids" => $itemids,
"sortfield" => "clock",
"sortorder" => "DESC",
"time_from" => $time_from,
"time_till" => $time_till
);
$response = get_web_json($zabbix_api_url, "jsonrpc2.cache", array(
"method" => "history.get",
"params" => $params,
"auth" => $zabbix_auth,
"id" => zabbix_get_id()
));
$records = get_property_value("result", $response);
}
return $records;
}
}