diff --git a/application/DeviceMonitoring/DeviceMonitoring.php b/application/DeviceMonitoring/DeviceMonitoring.php deleted file mode 100644 index 453aacd12..000000000 --- a/application/DeviceMonitoring/DeviceMonitoring.php +++ /dev/null @@ -1,9 +0,0 @@ -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; - } - } -} \ No newline at end of file diff --git a/scripts/monitoring/run-zabbix-device-sync.php b/scripts/monitoring/run-zabbix-device-sync.php index 5ff162ea5..45ea9bd13 100644 --- a/scripts/monitoring/run-zabbix-device-sync.php +++ b/scripts/monitoring/run-zabbix-device-sync.php @@ -2,19 +2,79 @@ print "ZABBIX-DEVICE-SYNC: [" . date("Y-m-d H:i:s") . "] $msg\n"; -$me = new User(1); +if (isset($argv[1])) { + $deviceId = $argv[1]; + $log("Child started for device ID: $deviceId"); -define("INTERNAL_USER_ID", $me->id); -define("INTERNAL_USER_USERNAME", $me->username); + if (!$device = DeviceModel::getOne($deviceId)) { + $log("Device $deviceId not found."); + exit(1); + } -$DeviceMonitoringController = new DeviceMonitoringController(false); + $log("Processing: {$device->name} ({$device->ip})"); + $hosts = array_merge($zabbix->getHosts(null, $device->ip), $zabbix->getHosts($device->name)); -$DeviceMonitoringController->syncToolZabbix(); \ No newline at end of file + if (empty($hosts)) { + $log("{$device->name}({$device->ip}) not found in Zabbix."); + exit(0); + } + + $hostId = $hosts[0]['hostid']; + $status = array_reduce($zabbix->getICMPItems($hostId), fn($carry, $item) => strpos($item['key_'], 'icmpping[') !== false ? $item['lastvalue'] + 1 : $carry, 0); + + $log("Found in Zabbix: hostId=$hostId, status=$status"); + $device->zabbix_online = $status; + $device->zabbix_host_id = $hostId; + $log($device->save() ? "Saved Zabbix status." : "Failed to save Zabbix status."); + $log("Child finished for device ID: $deviceId"); + exit(0); +} + +$devices = DeviceModel::getAll(); +$maxProcesses = 32; +$activeProcesses = []; + +$log("Parent started. Total devices: " . count($devices)); + +foreach ($devices as $device) { + if (count($activeProcesses) >= $maxProcesses) { + $log("Max processes reached. Waiting..."); + $pid = pcntl_wait($status); + unset($activeProcesses[$pid]); + $log("Child $pid finished."); + } + + if (($pid = pcntl_fork()) == -1) { + $log("Fork failed for device ID: {$device->id}"); + die('Fork failed'); + } + + if ($pid) { + $activeProcesses[$pid] = true; + $log("Forked child $pid for device ID: {$device->id}"); + } else { + exec("php " . __FILE__ . " " . $device->id); + exit(0); + } +} + +while (count($activeProcesses) > 0) { + $log("Waiting for remaining children..."); + $pid = pcntl_wait($status); + unset($activeProcesses[$pid]); + $log("Child $pid finished."); +} + +$log("All children finished. Parent exiting."); +?> \ No newline at end of file