Add condition to router check, if enable or not

This commit is contained in:
Focuslinkstech 2024-08-27 08:17:28 +01:00 committed by GitHub
parent 079b308bf1
commit edfa98e6b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 51 deletions

View File

@ -33,7 +33,7 @@ foreach ($d as $ds) {
$c = ORM::for_table('tbl_customers')->where('id', $ds['customer_id'])->find_one(); $c = ORM::for_table('tbl_customers')->where('id', $ds['customer_id'])->find_one();
$p = ORM::for_table('tbl_plans')->where('id', $u['plan_id'])->find_one(); $p = ORM::for_table('tbl_plans')->where('id', $u['plan_id'])->find_one();
$dvc = Package::getDevice($p); $dvc = Package::getDevice($p);
if($_app_stage != 'demo'){ if ($_app_stage != 'demo') {
if (file_exists($dvc)) { if (file_exists($dvc)) {
require_once $dvc; require_once $dvc;
(new $p['device'])->remove_customer($c, $p); (new $p['device'])->remove_customer($c, $p);
@ -79,63 +79,65 @@ foreach ($d as $ds) {
} }
} }
$routers = ORM::for_table('tbl_routers')->find_many(); if ($config['router_check']) {
if (!$routers) { $routers = ORM::for_table('tbl_routers')->find_many();
echo "No routers found in the database.\n"; if (!$routers) {
exit; echo "No routers found in the database.\n";
} exit;
}
foreach ($routers as $router) { foreach ($routers as $router) {
[$ip, $port] = explode(':', $router->ip_address); [$ip, $port] = explode(':', $router->ip_address);
$isOnline = false; $isOnline = false;
try { try {
$timeout = 5; $timeout = 5;
if (is_callable('fsockopen') && false === stripos(ini_get('disable_functions'), 'fsockopen')) { if (is_callable('fsockopen') && false === stripos(ini_get('disable_functions'), 'fsockopen')) {
$fsock = @fsockopen($ip, $port, $errno, $errstr, $timeout); $fsock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
if ($fsock) { if ($fsock) {
fclose($fsock); fclose($fsock);
$isOnline = true; $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 { } else {
throw new Exception("Unable to connect to $ip on port $port using fsockopen: $errstr ($errno)"); throw new Exception("Neither fsockopen nor stream_socket_client are enabled on the server.");
} }
} elseif (is_callable('stream_socket_client') && false === stripos(ini_get('disable_functions'), 'stream_socket_client')) { } catch (Exception $e) {
$connection = @stream_socket_client("$ip:$port", $errno, $errstr, $timeout); _log($e->getMessage());
if ($connection) { $adminEmail = $config['mail_from'];
fclose($connection); $subject = "Router Monitoring Error Alert";
$isOnline = true; $message = "An error occurred during the monitoring of router $ip: " . (string) $e->getMessage();
} else { Message::SendEmail($adminEmail, $subject, $message);
throw new Exception("Unable to connect to $ip on port $port using stream_socket_client: $errstr ($errno)"); sendTelegram($message);
}
} else {
throw new Exception("Neither fsockopen nor stream_socket_client are enabled on the server.");
} }
} catch (Exception $e) {
_log($e->getMessage()); if ($isOnline) {
$adminEmail = $config['mail_from']; $router->last_seen = date('Y-m-d H:i:s');
$subject = "Router Monitoring Error Alert"; $router->status = 'Online';
$message = "An error occurred during the monitoring of router $ip: " . (string) $e->getMessage(); } else {
Message::SendEmail($adminEmail, $subject, $message); $router->status = 'Offline';
sendTelegram($message); $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 ($isOnline) { if ($isCli) {
$router->last_seen = date('Y-m-d H:i:s'); echo "Cronjob finished\n";
$router->status = 'Online';
} else { } else {
$router->status = 'Offline'; echo "</pre>";
$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>";
} }

View File

@ -621,6 +621,21 @@
<p class="help-block col-md-4">{Lang::T('This is to notify you when new updates is available')} <p class="help-block col-md-4">{Lang::T('This is to notify you when new updates is available')}
</p> </p>
</div> </div>
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Router Check')}</label>
<div class="col-md-6">
<select name="router_check" id="router_check" class="form-control">
<option value="0" {if $_c['router_check']=='0' }selected="selected"
{/if}>{Lang::T('Disabled')}
</option>
<option value="1" {if $_c['router_check']=='1' }selected="selected"
{/if}>{Lang::T('Enabled')}
</option>
</select>
</div>
<p class="help-block col-md-4">{Lang::T('If enabled, the system will notify Admin when router goes Offline, If admin have 10 or more router and many customers, it will get overlapping, you can disabled')}
</p>
</div>
<div class="form-group"> <div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Phone OTP Required')}</label> <label class="col-md-2 control-label">{Lang::T('Phone OTP Required')}</label>
<div class="col-md-6"> <div class="col-md-6">