Files
thetool/lib/Zabbix/Zabbix.php
2024-11-05 19:12:19 +01:00

98 lines
2.7 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) {
$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)
)
);
$context = stream_context_create($options);
$result = file_get_contents($this->url, false, $context);
return json_decode($result, true);
}
public function getAllCongestionInterfaces() {
$response = $this->zabbixRequest('item.get', array(
'search' => array('name' => array("Congestion")),
));
return $response['result'];
}
public function getHostById($hostId) {
$response = $this->zabbixRequest('host.get', array(
'hostids' => $hostId
));
return $response['result'];
}
public function getItemValues($itemIds) {
$response = $this->zabbixRequest('history.get', array(
'itemids' => $itemIds,
'output' => 'extend',
'sortfield' => 'clock',
'sortorder' => 'DESC',
'limit' => 15
));
return $response['result'];
}
public function getHosts($hostname) {
$response = $this->zabbixRequest('host.get', array(
'search' => array('name' => array($hostname))
));
return $response['result'];
}
public function getInterfaceItems($hostId, $interfaceName) {
$response = $this->zabbixRequest('item.get', array(
'hostids' => $hostId,
'search' => array('name' => array($interfaceName, "Bits"))
));
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'];
}
}