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']; } public function getAllHostsWithDetails() { $response = $this->zabbixRequest('host.get', [ 'output' => ['hostid', 'host', 'name', 'status'], 'selectInventory' => ['location_lat', 'location_lon'], 'selectParentTemplates' => ['templateid', 'name'], 'selectHostGroups' => 'extend' // This is the new line ]); return $response['result'] ?? []; } public function updateHostInventory($hostId, $inventoryData) { // First, get the current inventory to avoid overwriting existing fields $hostResponse = $this->zabbixRequest('host.get', [ 'hostids' => $hostId, 'selectInventory' => 'extend' ]); $currentInventory = $hostResponse['result'][0]['inventory'] ?? []; // Merge new coordinates into the existing 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, // Technical name is the IP 'name' => $visibleName, // Visible name 'interfaces' => [ // <-- Corrected structure [ '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 // Use the correctly formatted array ]; $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; } }