Files
thetool/application/DeviceMonitoring/DeviceMonitoringController.php
2024-11-05 19:12:19 +01:00

151 lines
5.0 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]);
}
public function getCongestionData(): array {
$filename = TEMP_DIR . "/DeviceMonitoring/interfacesWithCongestion.json";
// if file is older than 50 minutes, fetch new 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();
$interfacesWithCongestion = [];
$fetchedHosts = [];
foreach ($congestionInterfaces as $interface) {
$itemId = $interface['itemid'];
$name = $interface['name'];
$name = str_replace(": Congestion Packets", "", $name);
$hostId = $interface['hostid'];
$values = $this->zabbix->getItemValues($itemId);
$highestValue = 0;
$highestValueTime = 0;
foreach ($values as $value) {
if ($value['value'] > $highestValue) {
$highestValue = $value['value'];
$highestValueTime = $value['clock'];
}
}
// if highest value is 0, then there is no congestion
if ($highestValue == 0) {
continue;
}
if (in_array($hostId, $fetchedHosts)) {
$host = $fetchedHosts[$hostId];
} else {
$host = $this->zabbix->getHostById($hostId);
$fetchedHosts[$hostId] = $host;
}
$hostname = $host[0]['name'];
$ip = $host[0]['host'];
$interfacesWithCongestion[] = [
"hostname" => $hostname,
"ip" => $ip,
"name" => $name,
"zabbixUrl" => ZABBIX_URL . "/zabbix.php?action=latest.view&hostids%5B%5D=" . $hostId,
"grafanaUrl" => GRAFANA_URL . "/d/Ta3PtRWZk/mikrotik-dashboard?orgId=1&var-host=" . $hostname,
"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")];
}
protected function indexAction(): void {
$JSGlobals = ["BASE_URL" => self::getUrl("VoiceCallActive"),
"DASHBOARD_URL" => self::getUrl("Dashboard"),
"MFAPPNAME" => MFAPPNAME_SLUG,
"PAGE_TITLE" => "Active Voice Calls",
"PATH" => [
["text" => MFAPPNAME_SLUG, "href" => self::getUrl("Dashboard")],
["text" => "Active Voice Calls", "href" => self::getUrl("VoiceCallActive")]
],
"VOICE_CALL_ACTIVE_API_URL" => self::getUrl("VoiceCallActive/api"),
];
$this->layout()->set("additionalCSS", ["css/views/VoiceCallActive.css"]);
$this->layout()->set("vueViewName", "VoiceCallActive");
$this->layout()->set("JSGlobals", $JSGlobals);
$this->layout()->setTemplate("VueViews/Vue");
}
protected function apiAction() {
$do = $this->request->do;
if (!$this->me->isAdmin()) {
$this->redirect("dashboard");
}
switch ($do) {
case "getActiveCalls":
$return = $this->getActiveCalls();
break;
default:
$return = false;
break;
}
if (!$return) {
$return = [
"status" => "error",
"message" => "Invalid request."
];
}
die(json_encode($return));
}
private function getActiveCalls(): array {
$activeCalls = $this->kolmisoftMore->getActiveCalls();
return is_null($activeCalls) ? [] : (is_object($activeCalls) ? [$activeCalls] : $activeCalls);
}
}