50 lines
1.6 KiB
PHP
50 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) {
|
|
$hosts = array_merge($this->zabbix->getHosts(null, $device->ip), $this->zabbix->getHosts($device->name));
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |