Files
thetool/application/DeviceMonitoring/DeviceMonitoringController.php
2024-11-05 20:42:16 +01:00

132 lines
4.4 KiB
PHP

<?php
class DeviceMonitoringController extends mfBaseController {
private User $me;
private string $ZABBIX_API_URL = ZABBIX_API_URL;
private string $ZABBIX_API_KEY = ZABBIX_API_KEY;
private Zabbix $zabbix;
protected function init(): void {
$me = new User();
$me->loadMe();
$this->layout()->set("me", $me);
$this->me = $me;
if (!$this->me->isAdmin()) {
$this->redirect("dashboard");
}
$this->zabbix = new Zabbix($this->ZABBIX_API_URL, $this->ZABBIX_API_KEY);
}
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 ($devices as $device) {
$hostname = $device->name;
$hosts = $this->zabbix->getHosts($hostname);
if (empty($hosts)) {
// TODO: implement any type of logging
continue;
}
$hostId = $hosts[0]['hostid'];
$icmpItems = $this->zabbix->getICMPItems($hostId);
$status = 0; // 0 = unknown, 1 = down, 2 = up
foreach ($icmpItems as $icmpItem) {
if (strpos($icmpItem['key_'], 'icmpping[') !== false) {
$status = $icmpItem['lastvalue'] + 1;
break;
}
}
$device->update([
"zabbix_online" => $status,
"zabbix_host_id" => $hostId
]);
$device->zabbix_online = $status;
$device->zabbix_host_id = $hostId;
$id = $device->save();
}
}
}