Files
thetool/application/DocumentationCheck/DocumentationCheckController.php
2024-05-08 12:54:26 +00:00

113 lines
3.4 KiB
PHP

<?php
class DocumentationCheckController 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);
}
protected function indexAction(): void {
$this->layout()->setTemplate("DocumentationCheck/DeviceTest");
}
protected function apiAction() {
$do = $this->request->do;
if (!$this->me->isAdmin()) {
$this->redirect("dashboard");
}
switch ($do) {
case "getDocumentationFails":
$return = $this->getDocumentationFails();
break;
default:
$return = false;
break;
}
if (!$return) {
$return = [
"status" => "error",
"message" => "Invalid request."
];
}
die(json_encode($return));
}
private function getDocumentationFails(): array {
$icmpItems = $this->zabbix->getICMPItems([]);
$zabbixICMPItems = [];
foreach ($icmpItems as $item) {
if (!isset($zabbixICMPItems[$item['hostid']])) {
$zabbixICMPItems[$item['hostid']] = [];
}
$zabbixICMPItems[$item['hostid']][] = $item;
}
$zabbixHostIPToId = [];
$zabbixHostNameToId = [];
$zabbixHosts = [];
foreach ($this->zabbix->getHosts("") as $host) {
$zabbixHostIPToId[$host['host']] = $host['hostid'];
$zabbixHostNameToId[$host['name']] = $host['hostid'];
$zabbixHosts[$host['hostid']] = $host;
}
$devices = DeviceModel::getAll();
//loop through all devices and join them by either IP or hostname and then return {device, zabbixHost}
$hosts = [];
foreach ($devices as $device) {
$deviceIP = $device->ip;
$deviceHostname = $device->name;
$zabbixHost = null;
if (isset($zabbixHostIPToId[$deviceIP])) {
$zabbixHost = $zabbixHosts[$zabbixHostIPToId[$deviceIP]];
} else if (isset($zabbixHostNameToId[$deviceHostname])) {
$zabbixHost = $zabbixHosts[$zabbixHostNameToId[$deviceHostname]];
}
$hosts[] = [
"zabbixHostId" => $zabbixHost['hostid'],
"zabbixHostName" => $zabbixHost['name'],
"zabbixHostIP" => $zabbixHost['host'],
"deviceName" => $device->name,
"deviceIP" => $device->ip,
"checkDeviceInZabbix" => $zabbixHost !== null,
"checkDeviceHasICMPPing" => isset($zabbixICMPItems[$zabbixHost['hostid']]),
"checkDeviceHasSameName" => $zabbixHost['name'] === $device->name,
"checkDeviceHasSameIP" => $zabbixHost['host'] === $device->ip
// "raw" => [
// "zabbixHost" => $zabbixHost,
// "device" => $device->toArray()
// ]
];
}
return [
"status" => "success",
"rows" => $hosts
];
}
}