forked from kevinowino869/mitrobill
added router online status, also add monitor and also report to admin when goes offline, report depend on cron runtime
This commit is contained in:
@ -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 "</pre>";
|
||||
}
|
Reference in New Issue
Block a user