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), 'timeout' => 30 ) ); 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) { if (empty($itemIds)) return []; $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', '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, 'output' => 'extend')); return $response['result']; } public function createTask($itemid) { $response = $this->zabbixRequest('task.create', array( 'type' => 6, 'request' => array('itemid' => $itemid) )); return $response['result']; } public function getAllHostsWithDetails() { $response = $this->zabbixRequest('host.get', [ 'output' => ['hostid', 'host', 'name', 'status'], 'selectInventory' => ['location_lat', 'location_lon'], 'selectParentTemplates' => ['templateid', 'name'], 'selectHostGroups' => 'extend' ]); return $response['result'] ?? []; } public function updateHostInventory($hostId, $inventoryData) { $hostResponse = $this->zabbixRequest('host.get', [ 'hostids' => $hostId, 'selectInventory' => 'extend' ]); $currentInventory = $hostResponse['result'][0]['inventory'] ?? []; $newInventory = array_merge($currentInventory, $inventoryData); $params = [ 'hostid' => $hostId, 'inventory_mode' => 0, // Set to manual mode 'inventory' => $newInventory ]; $response = $this->zabbixRequest('host.update', $params); return $response['result'] ?? ['error' => $response['error'] ?? 'Unknown error']; } public function getTemplateIdByName($templateName) { $response = $this->zabbixRequest('template.get', [ 'output' => ['templateid'], 'filter' => ['host' => [$templateName]] ]); return $response['result'][0]['templateid'] ?? null; } public function getTemplatesByNames(array $templateNames) { $response = $this->zabbixRequest('template.get', [ 'output' => ['templateid', 'name'], 'filter' => ['host' => $templateNames] ]); return $response['result'] ?? []; } public function createHost($visibleName, $ip, $groupId, $templateIds) { $templatesData = array_map(function ($id) { return ['templateid' => $id]; }, $templateIds); $params = [ 'host' => $ip, 'name' => $visibleName, 'interfaces' => [ [ 'type' => 2, // 2 for SNMP 'main' => 1, 'useip' => 1, 'ip' => $ip, 'dns' => '', 'port' => '161', 'details' => [ 'version' => 2, 'community' => 'public_xinon' ] ] ], 'groups' => [['groupid' => $groupId]], 'templates' => $templatesData ]; $response = $this->zabbixRequest('host.create', $params); return $response['result'] ?? ['error' => $response['error'] ?? 'Unknown error']; } public function getHostGroupIdByName($groupName) { $response = $this->zabbixRequest('hostgroup.get', [ 'output' => ['groupid'], 'filter' => ['name' => [$groupName]] ]); 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; } }