changed how zabbix syncs

This commit is contained in:
Luca Haid
2025-04-14 13:52:17 +02:00
parent 1dd0c1de3b
commit 7c30a356dc
6 changed files with 21 additions and 166 deletions

View File

@@ -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;
}
}
}

View File

@@ -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)) {

View File

@@ -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) {

View File

@@ -1,41 +0,0 @@
Vue.component('device-monitoring-congestion', {
//language=Vue
template: `
<tt-card>
<!--@formatter:off-->
<div>Letzte Abfragezeit: {{ window.moment.utc(window['TT_CONFIG']['CONGESTION_DATA']['fileCreateTime']).add(1, 'hours').format('DD.MM.YYYY HH:mm:ss') }}</div>
<!--@formatter:on-->
<tt-table :data="window['TT_CONFIG']['CONGESTION_DATA']['interfacesWithCongestion']" :config="DeviceTableConfig" excel-export>
<template v-slot:actions="{ row }">
<a :href="row.zabbixUrl" target="_blank"><i class="fas fa-chart-line" title="Zabbix"></i></a>
<a :href="row.grafanaUrl" target="_blank"><i class="fas fa-chart-bar" title="Grafana"></i></a>
</template>
<template v-slot:highestvaluetime="{ row }">
{{ window.moment.unix(row.highestValueTime).format('DD.MM.YYYY HH:mm:ss') }}
</template>
</tt-table>
</tt-card>
`,
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},
],
},
}
}
})

View File

@@ -1,21 +0,0 @@
#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
define("INTERNAL_USER_ID", $me->id);
define("INTERNAL_USER_USERNAME", $me->username);
$DeviceMonitoringController = new DeviceMonitoringController(false);
$DeviceMonitoringController->getCongestionData();

View File

@@ -1,10 +1,9 @@
#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../../config/config.php");
define('FRONKDB_SQLDEBUG',false);
const FRONKDB_SQLDEBUG = false;
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");