117 lines
3.4 KiB
PHP
117 lines
3.4 KiB
PHP
<?php
|
|
|
|
class Zabbix {
|
|
private $url;
|
|
private $apiKey;
|
|
|
|
public function __construct($url, $apiKey) {
|
|
$this->url = $url;
|
|
$this->apiKey = $apiKey;
|
|
}
|
|
|
|
public function zabbixRequest($method, $params, $die = false) {
|
|
$data = array(
|
|
'jsonrpc' => '2.0',
|
|
'method' => $method,
|
|
'params' => $params,
|
|
'id' => 1
|
|
);
|
|
|
|
$options = array(
|
|
'http' => array(
|
|
'header' => "Content-Type: application/json\r\n" .
|
|
"Authorization: Bearer " . $this->apiKey . "\r\n",
|
|
'method' => 'POST',
|
|
'content' => json_encode($data)
|
|
)
|
|
);
|
|
|
|
// var dump options and data and die
|
|
if ($die) {
|
|
var_dump($options);
|
|
echo json_encode($data);
|
|
die();
|
|
}
|
|
|
|
$context = stream_context_create($options);
|
|
$result = file_get_contents($this->url, false, $context);
|
|
|
|
return json_decode($result, true);
|
|
}
|
|
|
|
public function getHostById($hostId) {
|
|
$response = $this->zabbixRequest('host.get', array(
|
|
'hostids' => $hostId
|
|
));
|
|
return $response['result'];
|
|
}
|
|
|
|
public function getItemValues($itemIds, $limit = 15) {
|
|
$response = $this->zabbixRequest('history.get', array(
|
|
'itemids' => $itemIds,
|
|
'output' => 'extend',
|
|
'sortfield' => 'clock',
|
|
'sortorder' => 'DESC',
|
|
'limit' => $limit
|
|
));
|
|
return $response['result'];
|
|
}
|
|
|
|
public function getHosts($hostname = null, $ip = null) {
|
|
if ($hostname) {
|
|
$response = $this->zabbixRequest('host.get', array(
|
|
'search' => array('name' => array($hostname))
|
|
));
|
|
return $response['result'];
|
|
} elseif ($ip) {
|
|
$response = $this->zabbixRequest('host.get', array(
|
|
'search' => array('host' => array($ip))
|
|
));
|
|
return $response['result'];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public function getHostInterfaceItems($hostId) {
|
|
$response = $this->zabbixRequest('item.get', array(
|
|
'hostids' => $hostId,
|
|
'output' => ['itemid','name_resolved', 'key_', 'units'],
|
|
'search' => ['name' => ["Bits received", "Bits sent"]],
|
|
'searchByAny' => true,
|
|
'sortfield' => 'name'
|
|
));
|
|
return $response['result'];
|
|
}
|
|
|
|
public function getICMPItems($hostId) {
|
|
$response = $this->zabbixRequest('item.get', array(
|
|
'hostids' => $hostId,
|
|
'search' => array('name' => array("ICMP"))
|
|
));
|
|
return $response['result'];
|
|
}
|
|
|
|
public function getUptimeItems($hostId) {
|
|
$response = $this->zabbixRequest('item.get', array(
|
|
'hostids' => $hostId,
|
|
'search' => array('name' => array("Uptime"))
|
|
));
|
|
return $response['result'];
|
|
}
|
|
|
|
public function getHostInterfaces($hostIds) {
|
|
$response = $this->zabbixRequest('hostinterface.get', array('hostids' => $hostIds));
|
|
return $response['result'];
|
|
}
|
|
|
|
public function createTask($itemid) {
|
|
$response = $this->zabbixRequest('task.create', array(
|
|
'type' => 6,
|
|
'request' => array(
|
|
'itemid' => $itemid
|
|
)
|
|
));
|
|
return $response['result'];
|
|
}
|
|
}
|