From 76ac9431b3090d40b30c104a3364247be0c79222 Mon Sep 17 00:00:00 2001 From: Focuslinkstech <45756999+Focuslinkstech@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:26:52 +0100 Subject: [PATCH] added router online status, also add monitor and also report to admin when goes offline, report depend on cron runtime --- install/phpnuxbill.sql | 2 ++ system/cron.php | 61 ++++++++++++++++++++++++++++++++++++++++++ system/updates.json | 4 +++ ui/ui/routers.tpl | 50 +++++++++++++++++++++------------- 4 files changed, 98 insertions(+), 19 deletions(-) diff --git a/install/phpnuxbill.sql b/install/phpnuxbill.sql index 659194f0..d6645f60 100644 --- a/install/phpnuxbill.sql +++ b/install/phpnuxbill.sql @@ -138,6 +138,8 @@ CREATE TABLE `tbl_routers` ( `password` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `description` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `coordinates` VARCHAR(50) NOT NULL DEFAULT '', + `status` ENUM('Online', 'Offline') DEFAULT 'Online', + `last_seen` DATETIME, `coverage` VARCHAR(8) NOT NULL DEFAULT '0', `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0 disabled' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; diff --git a/system/cron.php b/system/cron.php index caae5c05..a0862d23 100644 --- a/system/cron.php +++ b/system/cron.php @@ -78,3 +78,64 @@ foreach ($d as $ds) { echo " : ACTIVE \r\n"; } } + +$routers = ORM::for_table('tbl_routers')->find_many(); +if (!$routers) { + echo "No routers found in the database.\n"; + exit; +} + +foreach ($routers as $router) { + [$ip, $port] = explode(':', $router->ip_address); + $isOnline = false; + + try { + $timeout = 5; + if (is_callable('fsockopen') && false === stripos(ini_get('disable_functions'), 'fsockopen')) { + $fsock = @fsockopen($ip, $port, $errno, $errstr, $timeout); + if ($fsock) { + fclose($fsock); + $isOnline = true; + } else { + throw new Exception("Unable to connect to $ip on port $port using fsockopen: $errstr ($errno)"); + } + } elseif (is_callable('stream_socket_client') && false === stripos(ini_get('disable_functions'), 'stream_socket_client')) { + $connection = @stream_socket_client("$ip:$port", $errno, $errstr, $timeout); + if ($connection) { + fclose($connection); + $isOnline = true; + } else { + throw new Exception("Unable to connect to $ip on port $port using stream_socket_client: $errstr ($errno)"); + } + } else { + throw new Exception("Neither fsockopen nor stream_socket_client are enabled on the server."); + } + } catch (Exception $e) { + _log($e->getMessage()); + $adminEmail = $config['mail_from']; + $subject = "Router Monitoring Error Alert"; + $message = "An error occurred during the monitoring of router $ip: " . (string) $e->getMessage(); + Message::SendEmail($adminEmail, $subject, $message); + sendTelegram($message); + } + + if ($isOnline) { + $router->last_seen = date('Y-m-d H:i:s'); + $router->status = 'Online'; + } else { + $router->status = 'Offline'; + $adminEmail = $config['mail_from']; + $subject = "Router Offline Alert"; + $message = "Dear Administrator,\nThe router with Name: {$router->name} and IP: {$router->ip_address} appears to be offline.\nThe Router was last seen online on: {$router->last_seen}\nPlease check the router's status and take appropriate action.\n\nBest regards,\nRouter Monitoring System"; + Message::SendEmail($adminEmail, $subject, $message); + sendTelegram($message); + } + + $router->save(); +} + +if ($isCli) { + echo "Cronjob finished\n"; +} else { + echo ""; +} \ No newline at end of file diff --git a/system/updates.json b/system/updates.json index 00e214cc..f9f05bf1 100644 --- a/system/updates.json +++ b/system/updates.json @@ -151,5 +151,9 @@ ], "2024.8.7" : [ "ALTER TABLE `tbl_customers` CHANGE `coordinates` `coordinates` VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Latitude and Longitude coordinates';" + ], + "2024.8.27" : [ + "ALTER TABLE `tbl_routers` ADD `status` ENUM('Online', 'Offline') DEFAULT 'Online', AFTER `coordinates`;", + "ALTER TABLE `tbl_routers` ADD `last_seen` DATETIME AFTER `status`;" ] } \ No newline at end of file diff --git a/ui/ui/routers.tpl b/ui/ui/routers.tpl index 526c4446..3422a5b9 100644 --- a/ui/ui/routers.tpl +++ b/ui/ui/routers.tpl @@ -40,6 +40,8 @@