changed how zabbix syncs
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user