From 7c30a356dc737399041471e701d9d98a45dce5d1 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Mon, 14 Apr 2025 13:52:17 +0200 Subject: [PATCH] changed how zabbix syncs --- .../DeviceMonitoringController.php | 94 ++----------------- .../WarehouseShippingNoteController.php | 3 - lib/Zabbix/Zabbix.php | 25 ++--- .../DeviceMonitoringCongestion.js | 41 -------- .../run-device-congestion-cache.php | 21 ----- scripts/monitoring/run-zabbix-device-sync.php | 3 +- 6 files changed, 21 insertions(+), 166 deletions(-) delete mode 100644 public/js/pages/DeviceMonitoringCongestion/DeviceMonitoringCongestion.js delete mode 100644 scripts/monitoring/run-device-congestion-cache.php diff --git a/application/DeviceMonitoring/DeviceMonitoringController.php b/application/DeviceMonitoring/DeviceMonitoringController.php index 440dc1570..422a89991 100644 --- a/application/DeviceMonitoring/DeviceMonitoringController.php +++ b/application/DeviceMonitoring/DeviceMonitoringController.php @@ -22,96 +22,20 @@ class DeviceMonitoringController extends mfBaseController { } - protected function congestionAction() { - $congestionData = $this->getCongestionData(); - - Helper::renderVue($this, "DeviceMonitoringCongestion", $this->mod, - ["CONGESTION_DATA" => $congestionData]); - } - - protected function syncAction() { - $this->syncToolZabbix(); - die("Sync done"); - } - - public function getCongestionData(): array { - $filename = TEMP_DIR . "/DeviceMonitoring/interfacesWithCongestion.json"; - $interfacesWithCongestion = []; - $fetchedHosts = []; - - // If file exists and is not older than 3000 seconds, then return the cached data - if (file_exists($filename) && (time() - filemtime($filename)) < 3000) { - $interfacesWithCongestion = json_decode(file_get_contents($filename), true); - $fileCreateTime = date("Y-m-d H:i:s", filemtime($filename)); - - return ["interfacesWithCongestion" => $interfacesWithCongestion, "fileCreateTime" => $fileCreateTime]; - } - - $congestionInterfaces = $this->zabbix->getAllCongestionInterfaces(); - - foreach ($congestionInterfaces as $interface) { - $itemId = $interface['itemid']; - - // Check if congestion exists on this interface - $values = $this->zabbix->getItemValues($itemId); - - $highestValue = 0; - $highestValueTime = 0; - foreach ($values as $value) { - if ($value['value'] > $highestValue) { - $highestValue = $value['value']; - $highestValueTime = $value['clock']; - } - } - - if ($highestValue == 0) { - continue; - } - - $hostId = $interface['hostid']; - if (in_array($hostId, $fetchedHosts)) { - $host = $fetchedHosts[$hostId]; - } else { - $host = $this->zabbix->getHostById($hostId); - $fetchedHosts[$hostId] = $host; - } - - $interfacesWithCongestion[] = [ - "hostname" => $host[0]['name'], - "ip" => $host[0]['host'], - "name" => str_replace(": Congestion Packets", "", $interface['name']), - "zabbixUrl" => ZABBIX_URL . "/zabbix.php?action=latest.view&hostids%5B%5D=" . $hostId, - "grafanaUrl" => GRAFANA_URL . "/d/Ta3PtRWZk/mikrotik-dashboard?orgId=1&var-host=" . $host[0]['name'], - "highestValue" => $highestValue, - "highestValueTime" => $highestValueTime - ]; - } - - if (!file_exists(TEMP_DIR . "/DeviceMonitoring")) { - mkdir(TEMP_DIR . "/DeviceMonitoring"); - } - - file_put_contents($filename, json_encode($interfacesWithCongestion)); - return ["interfacesWithCongestion" => $interfacesWithCongestion, "fileCreateTime" => date("Y-m-d H:i:s")]; - } - public function syncToolZabbix() { - $devices = DeviceModel::getAll(); + foreach (DeviceModel::getAll() as $device) { + $ip = $device->ip; + $hosts = $this->zabbix->getHosts($ip); - foreach ($devices as $device) { - $hostname = $device->name; - - $hosts = $this->zabbix->getHosts($hostname); if (empty($hosts)) { - // TODO: implement any type of logging + echo "{$device->name}({$device->ip}) not found in Zabbix." . PHP_EOL; continue; } $hostId = $hosts[0]['hostid']; - $icmpItems = $this->zabbix->getICMPItems($hostId); - $status = 0; // 0 = unknown, 1 = down, 2 = up + $status = 0; foreach ($icmpItems as $icmpItem) { if (strpos($icmpItem['key_'], 'icmpping[') !== false) { $status = $icmpItem['lastvalue'] + 1; @@ -119,14 +43,10 @@ class DeviceMonitoringController extends mfBaseController { } } - $device->update([ - "zabbix_online" => $status, - "zabbix_host_id" => $hostId - ]); - + echo "{$device->name}({$device->ip}) found in Zabbix with host ID: {$hostId} and status: {$status}" . PHP_EOL; $device->zabbix_online = $status; $device->zabbix_host_id = $hostId; - $id = $device->save(); + if(!$device->save()) echo "{$device->name}({$device->ip}) failed to save Zabbix status." . PHP_EOL; } } } \ No newline at end of file diff --git a/application/WarehouseShippingNote/WarehouseShippingNoteController.php b/application/WarehouseShippingNote/WarehouseShippingNoteController.php index 160dc9691..2ad3902d4 100644 --- a/application/WarehouseShippingNote/WarehouseShippingNoteController.php +++ b/application/WarehouseShippingNote/WarehouseShippingNoteController.php @@ -468,9 +468,6 @@ class WarehouseShippingNoteController extends TTCrud { //TODO: export this to an api class for openstreetmap protected function getDistanceAction() { -// $filename = TEMP_DIR . "/DeviceMonitoring/interfacesWithCongestion.json"; - // use dir TEMP_DIR /OpenStreetMap/from-to.json to cache the results - $filename = TEMP_DIR . "/OpenStreetMap/" . urlencode($this->request->from) . "-" . urlencode($this->request->to) . ".json"; if (file_exists($filename)) { diff --git a/lib/Zabbix/Zabbix.php b/lib/Zabbix/Zabbix.php index 4578d60d3..950a4a197 100644 --- a/lib/Zabbix/Zabbix.php +++ b/lib/Zabbix/Zabbix.php @@ -32,13 +32,6 @@ class Zabbix { 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 @@ -57,11 +50,19 @@ class Zabbix { return $response['result']; } - public function getHosts($hostname) { - $response = $this->zabbixRequest('host.get', array( - 'search' => array('name' => array($hostname)) - )); - 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('ip' => array($ip)) + )); + return $response['result']; + } + return []; } public function getHostInterfaceItems($hostId) { diff --git a/public/js/pages/DeviceMonitoringCongestion/DeviceMonitoringCongestion.js b/public/js/pages/DeviceMonitoringCongestion/DeviceMonitoringCongestion.js deleted file mode 100644 index 0a2d83b09..000000000 --- a/public/js/pages/DeviceMonitoringCongestion/DeviceMonitoringCongestion.js +++ /dev/null @@ -1,41 +0,0 @@ -Vue.component('device-monitoring-congestion', { - //language=Vue - template: ` - - - -
Letzte Abfragezeit: {{ window.moment.utc(window['TT_CONFIG']['CONGESTION_DATA']['fileCreateTime']).add(1, 'hours').format('DD.MM.YYYY HH:mm:ss') }}
- - - - - - - - -
- `, - data() { - return { - window: window, - DeviceTableConfig: { - key: 'DeviceMonitoringCongestion', - tableHeader: 'Device Monitoring - Congestion', - defaultPageSize: 25, - headers: [ - {text: 'Hostname', key: 'hostname', sortable: true, class: 'text-nowrap'}, - {text: 'IP-Adresse', key: 'ip', filter: 'search', class: 'text-center'}, - {text: 'Name', key: 'name', filter: 'search', class: 'text-center'}, - {text: 'Höchster Wert', key: 'highestValue', filter: 'search', class: 'text-center'}, - {text: 'Zeit', key: 'highestValueTime', filter: 'search', class: 'text-center'}, - {text: 'Aktionen', key: 'actions', class: 'text-center', sortable: false, filter: false, priority: 9}, - ], - }, - } - } -}) \ No newline at end of file diff --git a/scripts/monitoring/run-device-congestion-cache.php b/scripts/monitoring/run-device-congestion-cache.php deleted file mode 100644 index f51c54941..000000000 --- a/scripts/monitoring/run-device-congestion-cache.php +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/php -id); -define("INTERNAL_USER_USERNAME", $me->username); - -$DeviceMonitoringController = new DeviceMonitoringController(false); - -$DeviceMonitoringController->getCongestionData(); \ No newline at end of file diff --git a/scripts/monitoring/run-zabbix-device-sync.php b/scripts/monitoring/run-zabbix-device-sync.php index 0615e1a0b..5ff162ea5 100644 --- a/scripts/monitoring/run-zabbix-device-sync.php +++ b/scripts/monitoring/run-zabbix-device-sync.php @@ -1,10 +1,9 @@ #!/usr/bin/php