added new device consolidation
This commit is contained in:
@@ -349,6 +349,12 @@ class DeviceController extends mfBaseController
|
||||
case "deviceoltserviceportrefresh":
|
||||
$this->deviceoltserviceportrefresh($ip);
|
||||
break;
|
||||
case "getZabbixConsolidationData":
|
||||
$this->getZabbixConsolidationData();
|
||||
break;
|
||||
case "updateZabbixCoordinates":
|
||||
$this->updateZabbixCoordinates();
|
||||
break;
|
||||
default:
|
||||
$return = false;
|
||||
}
|
||||
@@ -512,4 +518,266 @@ class DeviceController extends mfBaseController
|
||||
|
||||
return $data ?? [];
|
||||
}
|
||||
|
||||
protected function zabbixConsolidationAction()
|
||||
{
|
||||
if (!$this->me->is(["Admin"])) {
|
||||
$this->layout()->setFlash("Keine Berechtigung", "error");
|
||||
$this->redirect("Dashboard");
|
||||
}
|
||||
|
||||
$JSGlobals = [
|
||||
"BASE_URL" => self::getUrl(""),
|
||||
"API_URL" => self::getUrl("Device", "api"),
|
||||
"DEVICE_TYPES" => DevicetypeModel::getAll(),
|
||||
"POPS" => PopModel::getAll(),
|
||||
"PAGE_TITLE" => "Zabbix Consolidation",
|
||||
"PATH" => [
|
||||
["text" => MFAPPNAME_SLUG, "href" => self::getUrl("Dashboard")],
|
||||
["text" => "Devices", "href" => self::getUrl("Device")],
|
||||
["text" => "Zabbix Consolidation", "href" => self::getUrl("Device", "zabbixConsolidation")]
|
||||
]
|
||||
];
|
||||
|
||||
$this->layout()->set("vueViewName", "DeviceZabbixConsolidation");
|
||||
$this->layout()->set("JSGlobals", $JSGlobals);
|
||||
$this->layout()->setTemplate("VueViews/Vue");
|
||||
}
|
||||
|
||||
protected function getZabbixConsolidationData() {
|
||||
$zabbix = new Zabbix(ZABBIX_API_URL, ZABBIX_API_KEY);
|
||||
$theToolDevices = DeviceModel::getAll();
|
||||
|
||||
$excludedGroupIds = [2, 4, 6, 7];
|
||||
|
||||
$allZabbixHosts = $zabbix->getAllHostsWithDetails();
|
||||
|
||||
$zabbixHosts = array_filter($allZabbixHosts, function($host) use ($excludedGroupIds) {
|
||||
$hostGroupIds = array_column($host['hostgroups'] ?? [], 'groupid');
|
||||
if (empty($hostGroupIds)) {
|
||||
return true;
|
||||
}
|
||||
return empty(array_intersect($hostGroupIds, $excludedGroupIds));
|
||||
});
|
||||
|
||||
$theToolDevicesByName = [];
|
||||
$theToolDevicesByIp = [];
|
||||
foreach ($theToolDevices as $device) {
|
||||
if ($device->ip === '127.0.0.1') continue;
|
||||
$theToolDevicesByName[$device->name] = $device;
|
||||
if (!empty($device->ip)) {
|
||||
$theToolDevicesByIp[$device->ip] = $device;
|
||||
}
|
||||
}
|
||||
|
||||
$zabbixHostsByVisibleName = [];
|
||||
$zabbixHostsByIp = [];
|
||||
foreach ($zabbixHosts as $host) {
|
||||
if (!empty($host['name'])) {
|
||||
$zabbixHostsByVisibleName[$host['name']] = $host;
|
||||
}
|
||||
if (!empty($host['host'])) {
|
||||
$zabbixHostsByIp[$host['host']] = $host;
|
||||
}
|
||||
}
|
||||
|
||||
$results = [
|
||||
'mismatchedNames' => [],
|
||||
'inTheToolOnly' => [],
|
||||
'inZabbixOnly' => [],
|
||||
'noSnmp' => [],
|
||||
'noCoordsInZabbix' => [],
|
||||
'mismatchedCoords' => [],
|
||||
];
|
||||
|
||||
foreach ($theToolDevices as $ttDevice) {
|
||||
if ($ttDevice->ip === '127.0.0.1') continue;
|
||||
|
||||
$deviceDetails = new Device($ttDevice->id);
|
||||
$theToolData = ['id' => $deviceDetails->id, 'data' => (array) $deviceDetails->data];
|
||||
if ($deviceDetails->pop && $deviceDetails->pop->id) {
|
||||
$theToolData['data']['pop'] = (array) $deviceDetails->pop->data;
|
||||
}
|
||||
|
||||
if (!isset($zabbixHostsByVisibleName[$ttDevice->name]) && !isset($zabbixHostsByIp[$ttDevice->ip])) {
|
||||
$results['inTheToolOnly'][] = $theToolData;
|
||||
} else {
|
||||
if (isset($zabbixHostsByVisibleName[$ttDevice->name])) {
|
||||
$zbxHost = $zabbixHostsByVisibleName[$ttDevice->name];
|
||||
$ttLat = (float)($theToolData['data']['pop']['gps_lat'] ?? $theToolData['data']['gps_lat'] ?? 0);
|
||||
$ttLon = (float)($theToolData['data']['pop']['gps_long'] ?? $theToolData['data']['gps_long'] ?? 0);
|
||||
$zbxLat = (float)($zbxHost['inventory']['location_lat'] ?? 0);
|
||||
$zbxLon = (float)($zbxHost['inventory']['location_lon'] ?? 0);
|
||||
|
||||
if (($ttLat || $ttLon || $zbxLat || $zbxLon) && (abs($ttLat - $zbxLat) > 0.0001 || abs($ttLon - $zbxLon) > 0.0001)) {
|
||||
$results['mismatchedCoords'][] = ['theTool' => $theToolData, 'zabbix' => $zbxHost];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($zabbixHosts as $zbxHost) {
|
||||
if (empty($zbxHost['name'])) continue;
|
||||
|
||||
if (!isset($theToolDevicesByName[$zbxHost['name']])) {
|
||||
$results['inZabbixOnly'][] = $zbxHost;
|
||||
}
|
||||
|
||||
$hasSnmp = false;
|
||||
if (!empty($zbxHost['parentTemplates'])) {
|
||||
foreach ($zbxHost['parentTemplates'] as $template) {
|
||||
if (stripos($template['name'], 'icmp ping') === false) {
|
||||
$hasSnmp = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$hasSnmp) {
|
||||
$results['noSnmp'][] = $zbxHost;
|
||||
}
|
||||
|
||||
$zbxLat = (float)($zbxHost['inventory']['location_lat'] ?? 0);
|
||||
$zbxLon = (float)($zbxHost['inventory']['location_lon'] ?? 0);
|
||||
|
||||
$theToolDeviceForCoords = $theToolDevicesByName[$zbxHost['name']] ?? null;
|
||||
$theToolDataForCoords = null;
|
||||
if($theToolDeviceForCoords){
|
||||
$deviceDetails = new Device($theToolDeviceForCoords->id);
|
||||
$theToolDataForCoords = ['id' => $deviceDetails->id, 'data' => (array) $deviceDetails->data];
|
||||
if ($deviceDetails->pop && $deviceDetails->pop->id) {
|
||||
$theToolDataForCoords['data']['pop'] = (array) $deviceDetails->pop->data;
|
||||
}
|
||||
}
|
||||
|
||||
if ($zbxLat == 0 && $zbxLon == 0) {
|
||||
$results['noCoordsInZabbix'][] = [
|
||||
'zabbix' => $zbxHost,
|
||||
'theTool' => $theToolDataForCoords
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($zabbixHosts as $zbxHost) {
|
||||
if(isset($theToolDevicesByIp[$zbxHost['host']])) {
|
||||
$ttDeviceFromIp = new Device($theToolDevicesByIp[$zbxHost['host']]->id);
|
||||
$theToolData = ['id' => $ttDeviceFromIp->id, 'data' => $ttDeviceFromIp->data];
|
||||
if ($ttDeviceFromIp->pop && $ttDeviceFromIp->pop->id) {
|
||||
$theToolData['data']['pop'] = $ttDeviceFromIp->pop->data;
|
||||
}
|
||||
|
||||
if($ttDeviceFromIp->name !== $zbxHost['name']){
|
||||
$results['mismatchedNames'][] = ['theTool' => $theToolData, 'zabbix' => $zbxHost];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::returnJson($results);
|
||||
}
|
||||
|
||||
protected function getZabbixTemplatesAction() {
|
||||
$zabbix = new Zabbix(ZABBIX_API_URL, ZABBIX_API_KEY);
|
||||
$templateNames = [
|
||||
"ICMP Ping",
|
||||
"1_Default XINON Template",
|
||||
"1_XINON Extreme EXOS by SNMP",
|
||||
"1_MIKROTIK_TEMPLATE"
|
||||
];
|
||||
$templates = $zabbix->getTemplatesByNames($templateNames);
|
||||
$formattedTemplates = array_map(function($template) {
|
||||
return ['value' => $template['templateid'], 'text' => $template['name']];
|
||||
}, $templates);
|
||||
|
||||
self::returnJson($formattedTemplates);
|
||||
}
|
||||
|
||||
protected function createZabbixHostAction() {
|
||||
$this->postData = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$deviceId = $this->postData['deviceId'] ?? null;
|
||||
$templateId = $this->postData['templateId'] ?? null;
|
||||
if (!$deviceId || !$templateId) {
|
||||
self::sendError("Device ID or Template ID is missing.");
|
||||
}
|
||||
|
||||
$device = DeviceModel::getOne($deviceId);
|
||||
if (!$device || !$device->id) {
|
||||
self::sendError("Device not found in TheTool.");
|
||||
}
|
||||
|
||||
$zabbix = new Zabbix(ZABBIX_API_URL, ZABBIX_API_KEY);
|
||||
$groupName = "Discovered hosts";
|
||||
|
||||
$existingHosts = $zabbix->getHosts($device->name, $device->ip);
|
||||
if (!empty($existingHosts)) {
|
||||
self::sendError("Host with this name or IP already exists in Zabbix.");
|
||||
}
|
||||
|
||||
$groupId = $zabbix->getHostGroupIdByName($groupName);
|
||||
if (!$groupId) {
|
||||
self::sendError("Host group '$groupName' not found in Zabbix.");
|
||||
}
|
||||
|
||||
$result = $zabbix->createHost($device->name, $device->ip, $groupId, $templateId);
|
||||
|
||||
if (isset($result['hostids'])) {
|
||||
$device->zabbix_host_id = $result['hostids'][0];
|
||||
$device->save();
|
||||
self::returnJson(['success' => true, 'message' => 'Host successfully created in Zabbix and linked in TheTool.']);
|
||||
} else {
|
||||
self::sendError("Failed to create host in Zabbix: " . json_encode($result['error'] ?? 'Unknown error'));
|
||||
}
|
||||
}
|
||||
|
||||
protected function updateTheToolCoordinatesAction() {
|
||||
$deviceId = $this->postData['deviceId'] ?? null;
|
||||
$lat = $this->postData['lat'] ?? null;
|
||||
$lon = $this->postData['lon'] ?? null;
|
||||
|
||||
if (!$deviceId || $lat === null || $lon === null) {
|
||||
self::sendError("Device ID or coordinates are missing.");
|
||||
}
|
||||
|
||||
$device = new Device($deviceId);
|
||||
if (!$device->id) {
|
||||
self::sendError("Device not found.");
|
||||
}
|
||||
|
||||
if ($device->pop_id) {
|
||||
self::sendError("Koordinaten können nicht geändert werden, da sie vom POP-Standort geerbt werden.");
|
||||
}
|
||||
|
||||
$device->update([
|
||||
'gps_lat' => $lat,
|
||||
'gps_long' => $lon
|
||||
]);
|
||||
$device->save();
|
||||
|
||||
self::returnJson(['success' => true, 'message' => 'TheTool coordinates updated successfully.']);
|
||||
}
|
||||
|
||||
protected function updateZabbixCoordinates() {
|
||||
$this->postData = json_decode(file_get_contents('php://input'), true);
|
||||
$hostId = $this->postData['hostId'] ?? null;
|
||||
$lat = $this->postData['lat'] ?? null;
|
||||
$lon = $this->postData['lon'] ?? null;
|
||||
|
||||
if (!$hostId || $lat === null || $lon === null) {
|
||||
self::sendError("Host ID or coordinates are missing.");
|
||||
}
|
||||
|
||||
$zabbix = new Zabbix(ZABBIX_API_URL, ZABBIX_API_KEY);
|
||||
$inventoryData = [
|
||||
'location_lat' => (string)$lat,
|
||||
'location_lon' => (string)$lon,
|
||||
];
|
||||
|
||||
$result = $zabbix->updateHostInventory($hostId, $inventoryData);
|
||||
|
||||
if (isset($result['hostids'])) {
|
||||
self::returnJson(['success' => true, 'message' => 'Coordinates updated successfully in Zabbix.']);
|
||||
} else {
|
||||
self::sendError("Failed to update coordinates in Zabbix: " . json_encode($result['error'] ?? 'Unknown error'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user