105 lines
2.9 KiB
PHP
105 lines
2.9 KiB
PHP
<?php
|
|
class Pinger extends MY_Controller
|
|
{
|
|
/*
|
|
|
|
create table pinger(
|
|
pingerID int not null auto_increment primary key,
|
|
pingerHost varchar(100),
|
|
pingerDescription varchar(300),
|
|
pingerLastPong datetime,
|
|
pingerRttMs int,
|
|
pingerIsActive varchar(1) default 'Y',
|
|
key(pingerIsActive),
|
|
key(pingerHost)
|
|
);
|
|
*/
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db->query("use x_logs");
|
|
}
|
|
function do($status = false)
|
|
{
|
|
$sql = "select * from pinger where pingerIsActive = 'Y'";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
echo "Err : " . $this->db->last_query();
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
foreach ($rows as $r) {
|
|
$dt = "[" . date("Y-m-d h:i:s") . "] ";
|
|
echo "$dt Ping " . $r["pingerHost"] . " " . $r["pingerDescription"] . " => ";
|
|
$this->do_ping($r["pingerID"], $r["pingerHost"], $r["pingerDescription"], $status);
|
|
sleep(1);
|
|
}
|
|
}
|
|
function status()
|
|
{
|
|
$this->do(true);
|
|
}
|
|
function do_ping($id, $host, $name, $status = false)
|
|
{
|
|
$rtt = $this->ping($host, 5);
|
|
if ($rtt == -1) {
|
|
$sql_u = "update pinger set pingerRttMs=? where pingerID = ?";
|
|
} else {
|
|
$sql_u = "update pinger set pingerLastPong=now(),pingerRttMs=? where pingerID = ?";
|
|
}
|
|
$qry = $this->db->query($sql_u, [$rtt, $id]);
|
|
$dt = "[" . date("Y-m-d h:i:s") . "] ";
|
|
if (!$qry) {
|
|
echo "$dt Err : " . $this->db->last_query();
|
|
exit;
|
|
}
|
|
$this->load->library("Wa_sas");
|
|
if ($rtt == -1) {
|
|
//sasone
|
|
//6282113702602-1584412485
|
|
$hp = "6282113702602-1584412485@g.us";
|
|
// hore
|
|
$hp = "120363280846797029@g.us";
|
|
$msg = "$name [$host] unreacheable";
|
|
$resp = $this->wa_sas->send_message($hp, $msg, true);
|
|
echo "$dt OK : unreachable. " . print_r($resp, true);
|
|
} else {
|
|
if ($status) {
|
|
$hp = "6282113702602-1584412485@g.us";
|
|
// hore
|
|
$hp = "120363280846797029@g.us";
|
|
$msg = "$name [$host] : ping avg $rtt ms";
|
|
$resp = $this->wa_sas->send_message($hp, $msg, true);
|
|
echo "$dt OK : unreacheable. " . print_r($resp, true);
|
|
} else {
|
|
echo "$dt OK : avg $rtt ms";
|
|
}
|
|
}
|
|
}
|
|
function ping($host, $count = 5)
|
|
{
|
|
$rtts = [];
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
// Use exec to ping the host and capture the output
|
|
$output = [];
|
|
exec("ping -c 1 -W 1 " . escapeshellarg($host), $output, $status);
|
|
|
|
// Check if ping was successful (status 0 means success)
|
|
if ($status === 0) {
|
|
// Extract the round-trip time from the output
|
|
if (preg_match('/time=([0-9.]+) ms/', implode("\n", $output), $matches)) {
|
|
$rtts[] = (float)$matches[1];
|
|
}
|
|
}
|
|
}
|
|
|
|
// If there are successful pings, calculate the average RTT; otherwise, return -1
|
|
if (count($rtts) > 0) {
|
|
return array_sum($rtts) / count($rtts);
|
|
} else {
|
|
return -1; // Host unreachable in all attempts
|
|
}
|
|
}
|
|
}
|