added zabbix sync and view for Device

This commit is contained in:
Luca Haid
2024-11-05 20:42:16 +01:00
parent bea341f869
commit eb9a1a3995
6 changed files with 118 additions and 63 deletions

View File

@@ -29,10 +29,17 @@ class DeviceMonitoringController extends mfBaseController {
["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 is older than 50 minutes, fetch new data
// 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));
@@ -42,16 +49,10 @@ class DeviceMonitoringController extends mfBaseController {
$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'];
// Check if congestion exists on this interface
$values = $this->zabbix->getItemValues($itemId);
$highestValue = 0;
@@ -63,11 +64,11 @@ class DeviceMonitoringController extends mfBaseController {
}
}
// if highest value is 0, then there is no congestion
if ($highestValue == 0) {
continue;
}
$hostId = $interface['hostid'];
if (in_array($hostId, $fetchedHosts)) {
$host = $fetchedHosts[$hostId];
} else {
@@ -75,16 +76,12 @@ class DeviceMonitoringController extends mfBaseController {
$fetchedHosts[$hostId] = $host;
}
$hostname = $host[0]['name'];
$ip = $host[0]['host'];
$interfacesWithCongestion[] = [
"hostname" => $hostname,
"ip" => $ip,
"name" => $name,
"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=" . $hostname,
"grafanaUrl" => GRAFANA_URL . "/d/Ta3PtRWZk/mikrotik-dashboard?orgId=1&var-host=" . $host[0]['name'],
"highestValue" => $highestValue,
"highestValueTime" => $highestValueTime
];
@@ -98,54 +95,38 @@ class DeviceMonitoringController extends mfBaseController {
return ["interfacesWithCongestion" => $interfacesWithCongestion, "fileCreateTime" => date("Y-m-d H:i:s")];
}
public function syncToolZabbix() {
$devices = DeviceModel::getAll();
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"),
];
foreach ($devices as $device) {
$hostname = $device->name;
$this->layout()->set("additionalCSS", ["css/views/VoiceCallActive.css"]);
$this->layout()->set("vueViewName", "VoiceCallActive");
$this->layout()->set("JSGlobals", $JSGlobals);
$this->layout()->setTemplate("VueViews/Vue");
}
$hosts = $this->zabbix->getHosts($hostname);
if (empty($hosts)) {
// TODO: implement any type of logging
continue;
}
protected function apiAction() {
$do = $this->request->do;
$hostId = $hosts[0]['hostid'];
if (!$this->me->isAdmin()) {
$this->redirect("dashboard");
$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();
}
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);
}
}