improved zabbix import script using children processes

This commit is contained in:
Luca Haid
2025-04-14 15:18:27 +02:00
parent a00f702dfc
commit 959b0cd2b5
3 changed files with 69 additions and 68 deletions

View File

@@ -1,9 +0,0 @@
<?php
/**
* @property mixed|null $name
*/
class DeviceMonitoring extends mfBaseModel
{
}

View File

@@ -1,50 +0,0 @@
<?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;
}
}
}

View File

@@ -2,19 +2,79 @@
<?php
require("../../config/config.php");
require_once(LIBDIR . "/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseController.php");
const FRONKDB_SQLDEBUG = false;
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
const INTERNAL_USER_ID = 1;
const INTERNAL_USER_USERNAME = "admin";
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$zabbix = new Zabbix(ZABBIX_API_URL, ZABBIX_API_KEY);
$log = fn($msg) => 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();
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.");
?>