Files
thetool/application/DeviceMonitoring/DeviceMonitoringController.php
2025-04-14 13:52:17 +02:00

52 lines
1.6 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);
}
public function syncToolZabbix() {
foreach (DeviceModel::getAll() as $device) {
$ip = $device->ip;
$hosts = $this->zabbix->getHosts($ip);
if (empty($hosts)) {
echo "{$device->name}({$device->ip}) not found in Zabbix." . PHP_EOL;
continue;
}
$hostId = $hosts[0]['hostid'];
$icmpItems = $this->zabbix->getICMPItems($hostId);
$status = 0;
foreach ($icmpItems as $icmpItem) {
if (strpos($icmpItem['key_'], 'icmpping[') !== false) {
$status = $icmpItem['lastvalue'] + 1;
break;
}
}
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;
if(!$device->save()) echo "{$device->name}({$device->ip}) failed to save Zabbix status." . PHP_EOL;
}
}
}