Device monitoring/v2
This commit is contained in:
@@ -1,32 +1,35 @@
|
||||
<?php
|
||||
|
||||
class Zabbix {
|
||||
class Zabbix
|
||||
{
|
||||
private $url;
|
||||
private $apiKey;
|
||||
|
||||
public function __construct($url, $apiKey) {
|
||||
public function __construct($url, $apiKey)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->apiKey = $apiKey;
|
||||
}
|
||||
|
||||
public function zabbixRequest($method, $params, $die = false) {
|
||||
public function zabbixRequest($method, $params, $die = false)
|
||||
{
|
||||
$data = array(
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => $method,
|
||||
'params' => $params,
|
||||
'id' => 1
|
||||
'method' => $method,
|
||||
'params' => $params,
|
||||
'id' => 1
|
||||
);
|
||||
|
||||
$options = array(
|
||||
'http' => array(
|
||||
'header' => "Content-Type: application/json\r\n" .
|
||||
'header' => "Content-Type: application/json\r\n" .
|
||||
"Authorization: Bearer " . $this->apiKey . "\r\n",
|
||||
'method' => 'POST',
|
||||
'content' => json_encode($data)
|
||||
'method' => 'POST',
|
||||
'content' => json_encode($data),
|
||||
'timeout' => 30
|
||||
)
|
||||
);
|
||||
|
||||
// var dump options and data and die
|
||||
if ($die) {
|
||||
var_dump($options);
|
||||
echo json_encode($data);
|
||||
@@ -39,25 +42,29 @@ class Zabbix {
|
||||
return json_decode($result, true);
|
||||
}
|
||||
|
||||
public function getHostById($hostId) {
|
||||
public function getHostById($hostId)
|
||||
{
|
||||
$response = $this->zabbixRequest('host.get', array(
|
||||
'hostids' => $hostId
|
||||
));
|
||||
return $response['result'];
|
||||
}
|
||||
|
||||
public function getItemValues($itemIds, $limit = 15) {
|
||||
public function getItemValues($itemIds, $limit = 15)
|
||||
{
|
||||
if (empty($itemIds)) return [];
|
||||
$response = $this->zabbixRequest('history.get', array(
|
||||
'itemids' => $itemIds,
|
||||
'output' => 'extend',
|
||||
'itemids' => $itemIds,
|
||||
'output' => 'extend',
|
||||
'sortfield' => 'clock',
|
||||
'sortorder' => 'DESC',
|
||||
'limit' => $limit
|
||||
'limit' => $limit
|
||||
));
|
||||
return $response['result'];
|
||||
}
|
||||
|
||||
public function getHosts($hostname = null, $ip = null) {
|
||||
public function getHosts($hostname = null, $ip = null)
|
||||
{
|
||||
if ($hostname) {
|
||||
$response = $this->zabbixRequest('host.get', array(
|
||||
'search' => array('name' => array($hostname))
|
||||
@@ -72,80 +79,84 @@ class Zabbix {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getHostInterfaceItems($hostId) {
|
||||
public function getHostInterfaceItems($hostId)
|
||||
{
|
||||
$response = $this->zabbixRequest('item.get', array(
|
||||
'hostids' => $hostId,
|
||||
'output' => ['itemid','name_resolved', 'key_', 'units'],
|
||||
'search' => ['name' => ["Bits received", "Bits sent"]],
|
||||
'hostids' => $hostId,
|
||||
'output' => ['itemid', 'name', 'key_', 'units'],
|
||||
'search' => ['name' => ["Bits received", "Bits sent"]],
|
||||
'searchByAny' => true,
|
||||
'sortfield' => 'name'
|
||||
'sortfield' => 'name'
|
||||
));
|
||||
return $response['result'];
|
||||
}
|
||||
|
||||
public function getICMPItems($hostId) {
|
||||
public function getICMPItems($hostId)
|
||||
{
|
||||
$response = $this->zabbixRequest('item.get', array(
|
||||
'hostids' => $hostId,
|
||||
'search' => array('name' => array("ICMP"))
|
||||
'search' => array('name' => array("ICMP"))
|
||||
));
|
||||
return $response['result'];
|
||||
}
|
||||
|
||||
public function getUptimeItems($hostId) {
|
||||
public function getUptimeItems($hostId)
|
||||
{
|
||||
$response = $this->zabbixRequest('item.get', array(
|
||||
'hostids' => $hostId,
|
||||
'search' => array('name' => array("Uptime"))
|
||||
'search' => array('name' => array("Uptime"))
|
||||
));
|
||||
return $response['result'];
|
||||
}
|
||||
|
||||
public function getHostInterfaces($hostIds) {
|
||||
$response = $this->zabbixRequest('hostinterface.get', array('hostids' => $hostIds));
|
||||
public function getHostInterfaces($hostIds)
|
||||
{
|
||||
$response = $this->zabbixRequest('hostinterface.get', array('hostids' => $hostIds, 'output' => 'extend'));
|
||||
return $response['result'];
|
||||
}
|
||||
|
||||
public function createTask($itemid) {
|
||||
public function createTask($itemid)
|
||||
{
|
||||
$response = $this->zabbixRequest('task.create', array(
|
||||
'type' => 6,
|
||||
'request' => array(
|
||||
'itemid' => $itemid
|
||||
)
|
||||
'type' => 6,
|
||||
'request' => array('itemid' => $itemid)
|
||||
));
|
||||
return $response['result'];
|
||||
}
|
||||
|
||||
public function getAllHostsWithDetails() {
|
||||
public function getAllHostsWithDetails()
|
||||
{
|
||||
$response = $this->zabbixRequest('host.get', [
|
||||
'output' => ['hostid', 'host', 'name', 'status'],
|
||||
'selectInventory' => ['location_lat', 'location_lon'],
|
||||
'output' => ['hostid', 'host', 'name', 'status'],
|
||||
'selectInventory' => ['location_lat', 'location_lon'],
|
||||
'selectParentTemplates' => ['templateid', 'name'],
|
||||
'selectHostGroups' => 'extend' // This is the new line
|
||||
'selectHostGroups' => 'extend'
|
||||
]);
|
||||
return $response['result'] ?? [];
|
||||
}
|
||||
|
||||
public function updateHostInventory($hostId, $inventoryData) {
|
||||
// First, get the current inventory to avoid overwriting existing fields
|
||||
public function updateHostInventory($hostId, $inventoryData)
|
||||
{
|
||||
$hostResponse = $this->zabbixRequest('host.get', [
|
||||
'hostids' => $hostId,
|
||||
'hostids' => $hostId,
|
||||
'selectInventory' => 'extend'
|
||||
]);
|
||||
|
||||
$currentInventory = $hostResponse['result'][0]['inventory'] ?? [];
|
||||
|
||||
// Merge new coordinates into the existing inventory
|
||||
$newInventory = array_merge($currentInventory, $inventoryData);
|
||||
|
||||
$params = [
|
||||
'hostid' => $hostId,
|
||||
'hostid' => $hostId,
|
||||
'inventory_mode' => 0, // Set to manual mode
|
||||
'inventory' => $newInventory
|
||||
'inventory' => $newInventory
|
||||
];
|
||||
|
||||
$response = $this->zabbixRequest('host.update', $params);
|
||||
return $response['result'] ?? ['error' => $response['error'] ?? 'Unknown error'];
|
||||
}
|
||||
public function getTemplateIdByName($templateName) {
|
||||
|
||||
public function getTemplateIdByName($templateName)
|
||||
{
|
||||
$response = $this->zabbixRequest('template.get', [
|
||||
'output' => ['templateid'],
|
||||
'filter' => ['host' => [$templateName]]
|
||||
@@ -153,7 +164,8 @@ class Zabbix {
|
||||
return $response['result'][0]['templateid'] ?? null;
|
||||
}
|
||||
|
||||
public function getTemplatesByNames(array $templateNames) {
|
||||
public function getTemplatesByNames(array $templateNames)
|
||||
{
|
||||
$response = $this->zabbixRequest('template.get', [
|
||||
'output' => ['templateid', 'name'],
|
||||
'filter' => ['host' => $templateNames]
|
||||
@@ -161,38 +173,39 @@ class Zabbix {
|
||||
return $response['result'] ?? [];
|
||||
}
|
||||
|
||||
|
||||
public function createHost($visibleName, $ip, $groupId, $templateIds) {
|
||||
$templatesData = array_map(function($id) {
|
||||
public function createHost($visibleName, $ip, $groupId, $templateIds)
|
||||
{
|
||||
$templatesData = array_map(function ($id) {
|
||||
return ['templateid' => $id];
|
||||
}, $templateIds);
|
||||
|
||||
$params = [
|
||||
'host' => $ip, // Technical name is the IP
|
||||
'name' => $visibleName, // Visible name
|
||||
'interfaces' => [ // <-- Corrected structure
|
||||
'host' => $ip,
|
||||
'name' => $visibleName,
|
||||
'interfaces' => [
|
||||
[
|
||||
'type' => 2, // 2 for SNMP
|
||||
'main' => 1,
|
||||
'useip' => 1,
|
||||
'ip' => $ip,
|
||||
'dns' => '',
|
||||
'port' => '161',
|
||||
'type' => 2, // 2 for SNMP
|
||||
'main' => 1,
|
||||
'useip' => 1,
|
||||
'ip' => $ip,
|
||||
'dns' => '',
|
||||
'port' => '161',
|
||||
'details' => [
|
||||
'version' => 2,
|
||||
'version' => 2,
|
||||
'community' => 'public_xinon'
|
||||
]
|
||||
]
|
||||
],
|
||||
'groups' => [['groupid' => $groupId]],
|
||||
'templates' => $templatesData // Use the correctly formatted array
|
||||
'groups' => [['groupid' => $groupId]],
|
||||
'templates' => $templatesData
|
||||
];
|
||||
|
||||
$response = $this->zabbixRequest('host.create', $params);
|
||||
return $response['result'] ?? ['error' => $response['error'] ?? 'Unknown error'];
|
||||
}
|
||||
|
||||
public function getHostGroupIdByName($groupName) {
|
||||
public function getHostGroupIdByName($groupName)
|
||||
{
|
||||
$response = $this->zabbixRequest('hostgroup.get', [
|
||||
'output' => ['groupid'],
|
||||
'filter' => ['name' => [$groupName]]
|
||||
@@ -200,4 +213,133 @@ class Zabbix {
|
||||
return $response['result'][0]['groupid'] ?? null;
|
||||
}
|
||||
|
||||
}
|
||||
public function getHostWithInterfaces($hostId)
|
||||
{
|
||||
$response = $this->zabbixRequest('host.get', [
|
||||
'hostids' => $hostId,
|
||||
'output' => ['hostid', 'host', 'name'],
|
||||
'selectInterfaces' => 'extend'
|
||||
]);
|
||||
return $response['result'][0] ?? null;
|
||||
}
|
||||
|
||||
public function getResolvedProblems($hostId, $time_from)
|
||||
{
|
||||
$response = $this->zabbixRequest('event.get', [
|
||||
'hostids' => $hostId,
|
||||
'output' => 'extend',
|
||||
'select_acknowledges' => ['message'],
|
||||
'sortfield' => ['clock'],
|
||||
'sortorder' => 'DESC',
|
||||
'time_from' => $time_from,
|
||||
'object' => 0,
|
||||
'value' => 0
|
||||
]);
|
||||
return $response['result'] ?? [];
|
||||
}
|
||||
|
||||
public function updateHostInterface($interfaceId, $details)
|
||||
{
|
||||
$params = ['interfaceid' => $interfaceId, 'details' => $details];
|
||||
$response = $this->zabbixRequest('hostinterface.update', $params);
|
||||
return $response['result'] ?? ['error' => $response['error'] ?? 'Unknown error'];
|
||||
}
|
||||
|
||||
public function getInterfaceOperationalStatusItems($hostId)
|
||||
{
|
||||
$response = $this->zabbixRequest('item.get', [
|
||||
'hostids' => $hostId,
|
||||
'output' => ['itemid', 'name', 'key_'],
|
||||
'search' => ['key_' => 'net.if.status'],
|
||||
'sortfield' => 'name'
|
||||
]);
|
||||
return $response['result'] ?? [];
|
||||
}
|
||||
|
||||
public function getTriggersForHostByDescription($hostId, $description)
|
||||
{
|
||||
$response = $this->zabbixRequest('trigger.get', [
|
||||
'hostids' => $hostId,
|
||||
'output' => ['triggerid', 'description'],
|
||||
'search' => ['description' => $description],
|
||||
'searchByAny' => true
|
||||
]);
|
||||
return $response['result'] ?? [];
|
||||
}
|
||||
|
||||
public function createInterfaceLinkDownTrigger($expression, $description, $priority = 4)
|
||||
{
|
||||
$params = [
|
||||
'description' => $description,
|
||||
'expression' => $expression,
|
||||
'priority' => $priority,
|
||||
];
|
||||
$response = $this->zabbixRequest('trigger.create', $params);
|
||||
return $response['result'] ?? ['error' => $response['error'] ?? 'Unknown error'];
|
||||
}
|
||||
|
||||
public function deleteTriggers(array $triggerIds)
|
||||
{
|
||||
if (empty($triggerIds)) return [];
|
||||
$response = $this->zabbixRequest('trigger.delete', $triggerIds);
|
||||
return $response['result'] ?? ['error' => $response['error'] ?? 'Unknown error'];
|
||||
}
|
||||
|
||||
public function getTrends(array $itemIds, $time_from)
|
||||
{
|
||||
if (empty($itemIds)) return [];
|
||||
$response = $this->zabbixRequest('trend.get', [
|
||||
'itemids' => $itemIds,
|
||||
'output' => ['itemid', 'num', 'value_min', 'value_avg', 'value_max'],
|
||||
'time_from' => $time_from
|
||||
]);
|
||||
return $response['result'] ?? [];
|
||||
}
|
||||
|
||||
public function getOverviewData($hostId)
|
||||
{
|
||||
$items = $this->zabbixRequest('item.get', [
|
||||
'hostids' => $hostId,
|
||||
'output' => ['itemid', 'name', 'units', 'key_'],
|
||||
'search' => ['key_' => ['icmpping', 'system.uptime']],
|
||||
'searchByAny' => true
|
||||
])['result'] ?? [];
|
||||
|
||||
$itemIds = [];
|
||||
$itemMap = [];
|
||||
$data = ['ping' => [], 'uptime' => []];
|
||||
foreach ($items as $item) {
|
||||
$itemIds[] = $item['itemid'];
|
||||
$type = str_contains($item['key_'], 'uptime') ? 'uptime' : 'ping';
|
||||
$itemMap[$item['itemid']] = ['type' => $type, 'name' => $item['name'], 'units' => $item['units']];
|
||||
}
|
||||
|
||||
if (!empty($itemIds)) {
|
||||
$history = $this->getItemValues($itemIds, 1);
|
||||
foreach ($history as $h) {
|
||||
if (!isset($itemMap[$h['itemid']])) continue;
|
||||
$info = $itemMap[$h['itemid']];
|
||||
$data[$info['type']][] = ['name' => $info['name'], 'value' => $h['value'], 'clock' => $h['clock'], 'units' => $info['units']];
|
||||
}
|
||||
}
|
||||
|
||||
$time30d = time() - 2592000;
|
||||
$problems = $this->zabbixRequest('problem.get', [
|
||||
'hostids' => $hostId,
|
||||
'time_from' => $time30d,
|
||||
'output' => ['clock']
|
||||
])['result'] ?? [];
|
||||
|
||||
$time7d = time() - 604800;
|
||||
$time24h = time() - 86400;
|
||||
$counts = ['24h' => 0, '7d' => 0, '30d' => 0];
|
||||
foreach ($problems as $p) {
|
||||
$counts['30d']++;
|
||||
if ($p['clock'] >= $time7d) $counts['7d']++;
|
||||
if ($p['clock'] >= $time24h) $counts['24h']++;
|
||||
}
|
||||
$data['problemCounts'] = $counts;
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user