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

@@ -49,6 +49,8 @@ class DeviceController extends mfBaseController
"DEVICE_MANUFACTURERS" => $deviceManufacturers, "DEVICE_MANUFACTURERS" => $deviceManufacturers,
"DEVICE_TYPES" => $deviceTypes, "DEVICE_TYPES" => $deviceTypes,
"DEVICES" => $this->getDevices(), "DEVICES" => $this->getDevices(),
"ZABBIX_URL" => ZABBIX_URL,
"GRAFANA_URL" => GRAFANA_URL,
]; ];
$this->layout()->set("vueViewName", "Device"); $this->layout()->set("vueViewName", "Device");
@@ -398,7 +400,7 @@ class DeviceController extends mfBaseController
$locationText = ""; $locationText = "";
$locationUrl = ""; $locationUrl = "";
if (trim($device->pop->name)) { if (isset($device->pop) && trim($device->pop->name)) {
$locationText = $device->pop->name; $locationText = $device->pop->name;
$locationUrl = self::getUrl("Pop", "Detail", ["id" => $device->pop->id]); $locationUrl = self::getUrl("Pop", "Detail", ["id" => $device->pop->id]);
} else if (trim($device->addr_street)) { } else if (trim($device->addr_street)) {
@@ -431,6 +433,8 @@ class DeviceController extends mfBaseController
"ip" => $device->ip, "ip" => $device->ip,
"mac" => $device->mac, "mac" => $device->mac,
"serial" => $device->serial, "serial" => $device->serial,
"zabbix_online" => $device->zabbix_online,
"zabbix_host_id" => $device->zabbix_host_id,
"price" => $device->price != "0.00" ? $device->price : $device->devicetype->price, "price" => $device->price != "0.00" ? $device->price : $device->devicetype->price,
"power" => $device->power != "0.00" ? intval($device->power) : intval($device->devicetype->power), "power" => $device->power != "0.00" ? intval($device->power) : intval($device->devicetype->power),
"backup" => $backup, "backup" => $backup,

View File

@@ -29,10 +29,17 @@ class DeviceMonitoringController extends mfBaseController {
["CONGESTION_DATA" => $congestionData]); ["CONGESTION_DATA" => $congestionData]);
} }
protected function syncAction() {
$this->syncToolZabbix();
die("Sync done");
}
public function getCongestionData(): array { public function getCongestionData(): array {
$filename = TEMP_DIR . "/DeviceMonitoring/interfacesWithCongestion.json"; $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) { if (file_exists($filename) && (time() - filemtime($filename)) < 3000) {
$interfacesWithCongestion = json_decode(file_get_contents($filename), true); $interfacesWithCongestion = json_decode(file_get_contents($filename), true);
$fileCreateTime = date("Y-m-d H:i:s", filemtime($filename)); $fileCreateTime = date("Y-m-d H:i:s", filemtime($filename));
@@ -42,16 +49,10 @@ class DeviceMonitoringController extends mfBaseController {
$congestionInterfaces = $this->zabbix->getAllCongestionInterfaces(); $congestionInterfaces = $this->zabbix->getAllCongestionInterfaces();
$interfacesWithCongestion = [];
$fetchedHosts = [];
foreach ($congestionInterfaces as $interface) { foreach ($congestionInterfaces as $interface) {
$itemId = $interface['itemid']; $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); $values = $this->zabbix->getItemValues($itemId);
$highestValue = 0; $highestValue = 0;
@@ -63,11 +64,11 @@ class DeviceMonitoringController extends mfBaseController {
} }
} }
// if highest value is 0, then there is no congestion
if ($highestValue == 0) { if ($highestValue == 0) {
continue; continue;
} }
$hostId = $interface['hostid'];
if (in_array($hostId, $fetchedHosts)) { if (in_array($hostId, $fetchedHosts)) {
$host = $fetchedHosts[$hostId]; $host = $fetchedHosts[$hostId];
} else { } else {
@@ -75,16 +76,12 @@ class DeviceMonitoringController extends mfBaseController {
$fetchedHosts[$hostId] = $host; $fetchedHosts[$hostId] = $host;
} }
$hostname = $host[0]['name'];
$ip = $host[0]['host'];
$interfacesWithCongestion[] = [ $interfacesWithCongestion[] = [
"hostname" => $hostname, "hostname" => $host[0]['name'],
"ip" => $ip, "ip" => $host[0]['host'],
"name" => $name, "name" => str_replace(": Congestion Packets", "", $interface['name']),
"zabbixUrl" => ZABBIX_URL . "/zabbix.php?action=latest.view&hostids%5B%5D=" . $hostId, "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, "highestValue" => $highestValue,
"highestValueTime" => $highestValueTime "highestValueTime" => $highestValueTime
]; ];
@@ -98,54 +95,38 @@ class DeviceMonitoringController extends mfBaseController {
return ["interfacesWithCongestion" => $interfacesWithCongestion, "fileCreateTime" => date("Y-m-d H:i:s")]; return ["interfacesWithCongestion" => $interfacesWithCongestion, "fileCreateTime" => date("Y-m-d H:i:s")];
} }
public function syncToolZabbix() {
$devices = DeviceModel::getAll();
protected function indexAction(): void { foreach ($devices as $device) {
$JSGlobals = ["BASE_URL" => self::getUrl("VoiceCallActive"), $hostname = $device->name;
"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"]); $hosts = $this->zabbix->getHosts($hostname);
$this->layout()->set("vueViewName", "VoiceCallActive"); if (empty($hosts)) {
$this->layout()->set("JSGlobals", $JSGlobals); // TODO: implement any type of logging
$this->layout()->setTemplate("VueViews/Vue"); continue;
} }
protected function apiAction() { $hostId = $hosts[0]['hostid'];
$do = $this->request->do;
if (!$this->me->isAdmin()) { $icmpItems = $this->zabbix->getICMPItems($hostId);
$this->redirect("dashboard");
$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);
} }
} }

View File

@@ -0,0 +1,25 @@
<?php /** @noinspection ALL */
declare(strict_types = 1);
use Phinx\Migration\AbstractMigration;
final class DeviceAddZabbixStatus extends AbstractMigration {
public function up(): void {
if ($this->getEnvironment() == "thetool") {
$Device = $this->table("Device");
$Device->addColumn('zabbix_online', 'integer', ['null' => false, 'default' => 0]);
$Device->addColumn('zabbix_host_id', 'integer', ['null' => true]);
$Device->update();
}
}
public function down(): void {
if ($this->getEnvironment() == "thetool") {
$Device = $this->table("Device");
$Device->removeColumn('zabbix_online');
$Device->removeColumn('zabbix_host_id');
$Device->update();
}
}
}

View File

@@ -72,6 +72,14 @@ class Zabbix {
return $response['result']; return $response['result'];
} }
public function getICMPItems($hostId) {
$response = $this->zabbixRequest('item.get', array(
'hostids' => $hostId,
'search' => array('name' => array("ICMP"))
));
return $response['result'];
}
public function getUptimeItems($hostId) { public function getUptimeItems($hostId) {
$response = $this->zabbixRequest('item.get', array( $response = $this->zabbixRequest('item.get', array(
'hostids' => $hostId, 'hostids' => $hostId,

View File

@@ -74,6 +74,11 @@ Vue.component('DeviceTable', {
<template v-slot:actions="{ row }"> <template v-slot:actions="{ row }">
<a :href="window['TT_CONFIG']['BASE_URL'] +'/Device/edit/?id=' + row.id"><i class="far fa-edit" title="Bearbeiten"></i></a> <a :href="window['TT_CONFIG']['BASE_URL'] +'/Device/edit/?id=' + row.id"><i class="far fa-edit" title="Bearbeiten"></i></a>
<a :href="window['TT_CONFIG']['BASE_URL'] +'/Device/delete/?id=' + row.id" onclick="if(!confirm('Device wirklich löschen?')) return false;" class="text-danger" title="Löschen"><i class="fas fa-trash "></i></a> <a :href="window['TT_CONFIG']['BASE_URL'] +'/Device/delete/?id=' + row.id" onclick="if(!confirm('Device wirklich löschen?')) return false;" class="text-danger" title="Löschen"><i class="fas fa-trash "></i></a>
<template v-if="row.zabbix_host_id !== '0'">
<a :href="window['TT_CONFIG']['ZABBIX_URL'] + '/zabbix.php?action=latest.view&hostids%5B%5D=' + row.zabbix_host_id" target="_blank" class="text-info" title="Zabbix"><i class="fas fa-server"></i></a>
<a :href="window['TT_CONFIG']['GRAFANA_URL'] + '/d/Ta3PtRWZk/mikrotik-dashboard?orgId=1&var-host=' + row.name" target="_blank" class="text-info" title="Grafana"><i class="fas fa-chart-line"></i></a>
</template>
</template> </template>
</tt-table> </tt-table>
@@ -88,7 +93,7 @@ Vue.component('DeviceTable', {
headers: [ headers: [
{ text: 'Device Name', key: 'name', sortable: true, class: 'text-nowrap', priority: 10 }, { text: 'Device Name', key: 'name', sortable: true, class: 'text-nowrap', priority: 10 },
{ text: 'Hersteller', key: 'devicemanufactor', filter: 'select',class: 'text-nowrap text-center', filterOptions: deviceManufacturerFilterOptions }, { text: 'Hersteller', key: 'devicemanufactor', filter: 'select',class: 'text-nowrap text-center', filterOptions: deviceManufacturerFilterOptions },
{ text: 'Geräte Typ', key: 'devicetype', filter: 'autocomplete', class: 'text-nowrap text-center', filterOptions: deviceTypeFilterOptions , priority: 7}, { text: 'Geräte Typ', key: 'devicetype', filter: 'select', class: 'text-nowrap text-center', filterOptions: deviceTypeFilterOptions , priority: 7},
{ text: 'Pop/Adresse', key: 'locationText', filter: 'search' , class: 'text-nowrap text-center'}, { text: 'Pop/Adresse', key: 'locationText', filter: 'search' , class: 'text-nowrap text-center'},
{ text: 'IP-Adresse', key: 'ip', filter: 'search',class: 'text-center', priority: 8}, { text: 'IP-Adresse', key: 'ip', filter: 'search',class: 'text-center', priority: 8},
{ text: 'Mac-Adresse', key: 'mac', filter: 'search',class: 'text-center' }, { text: 'Mac-Adresse', key: 'mac', filter: 'search',class: 'text-center' },
@@ -107,6 +112,17 @@ Vue.component('DeviceTable', {
], ],
priority: 6, priority: 6,
sortable: false, sortable: false,
}, {
text: 'Status',
key: 'zabbix_online',
filter: 'iconSelect',
filterOptions: [
{ value: 0, text: 'Status unbekannt', icon: 'fa-regular fa-circle-xmark' },
{ value: 1, text: 'Offline', icon: 'fa-regular fa-circle-xmark text-danger' },
{ value: 2, text: 'Online', icon: 'fa-regular fa-circle-check text-success' },
],
priority: 5,
sortable: false,
}, },
{text: 'Aktionen', key: 'actions', class: 'text-center', sortable: false, filter: false, priority: 9}, {text: 'Aktionen', key: 'actions', class: 'text-center', sortable: false, filter: false, priority: 9},
], ],

View File

@@ -0,0 +1,21 @@
#!/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->syncToolZabbix();