1
0

debbuging presence

This commit is contained in:
Daniel Tartavel 2022-09-09 16:52:42 +02:00
parent c00b766cc4
commit 7eaaad0911

View File

@ -14,6 +14,7 @@ $result = array();
$presence = array(); $presence = array();
$curlErr = 0; $curlErr = 0;
$command = "sudo nmap -n -sP 192.168.1.*"; //need to configure /etc/sudo/sudoers to permit execution of nmap as superuser $command = "sudo nmap -n -sP 192.168.1.*"; //need to configure /etc/sudo/sudoers to permit execution of nmap as superuser
$presenceTemp = array();
echo "presenceDaemon is starting\n"; echo "presenceDaemon is starting\n";
// init du tableau des présences à -1 // init du tableau des présences à -1
@ -24,8 +25,9 @@ foreach($macAddresses as $mac => $nom)
while (1) while (1)
{ {
$presenceTemp = array(); echo "presenceDaemon: executing nmap" . EOL;
exec($command, $result); exec($command, $result);
$presenceTemp = array();
foreach ($result as $value) foreach ($result as $value)
{ {
@ -33,36 +35,44 @@ while (1)
if (str_contains($value, "MAC Address:")) if (str_contains($value, "MAC Address:"))
{ {
//echo " contient une adresse MAC" . EOL; //echo " contient une adresse MAC" . EOL;
search($value); $presenceTemp = array_merge($presenceTemp, search($value));
} }
} }
var_dump($presenceTemp);
foreach($presenceTemp as $nom => $status) foreach($presenceTemp as $nom => $status)
{ {
$msg = ""; $msg = "";
if ($presence[$nom] != (int)$status) if ($presence[$nom] != (int)$status)
{ {
$presence[$nom] = $status;
$msg .= "nom=" . $nom . "&presence=" . $status; $msg .= "nom=" . $nom . "&presence=" . $status;
send($msg); send($msg);
echo $nom . " est modifié dans tableau\n"; echo "presenceDaemon:" . $nom . " est modifié dans tableau à " . $status . EOL;
} }
} }
$result = array(); $result = array();
sleep(1); //ésleep(1);
} }
function search($string) function search($string)
{ {
global $macAddresses, $presenceTemp; global $macAddresses;
$presenceTemp = array();
$flag = false;
//echo "searching in " . $string . EOL; //echo "searching in " . $string . EOL;
foreach ($macAddresses as $needle => $nom) foreach ($macAddresses as $needle => $nom)
{ {
//echo $nom ." => " . $needle . EOL; echo $string . " ==> presenceD:" . $nom ." => " . $needle . EOL;
if (str_contains($string, $needle)) if (str_contains($string, $needle))
{ {
echo "found " . $needle . EOL; echo "presenceD: found " . $needle . EOL;
$presenceTemp[$nom] = true; $presenceTemp[$nom] = true;
$flag = true;
} }
} }
var_dump($presenceTemp);
return $presenceTemp;
} }
function send($msg) function send($msg)
@ -77,7 +87,7 @@ function send($msg)
// return the transfer as a string // return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $result contains the output string // $result contains the output string
echo _("Curl sending message -") . $msg . __FILE__ . ":" . __LINE__ . EOL; echo _("presenceDaemon: Curl sending message -") . $msg . __FILE__ . ":" . __LINE__ . EOL;
if ($curlErr <= 5) if ($curlErr <= 5)
{ {
@ -85,11 +95,11 @@ function send($msg)
if (curl_errno($ch) != 0) if (curl_errno($ch) != 0)
{ {
$curlErr += 1; $curlErr += 1;
echo sprintf( _("Sleeping 1 minute because Curl return error %d: %s when sending notification - "), curl_errno($ch), curl_error($ch)) . __FILE__ . ":" . __LINE__ . EOL; echo sprintf( _("presenceDaemon: Sleeping 1 minute because Curl return error %d: %s when sending notification - "), curl_errno($ch), curl_error($ch)) . __FILE__ . ":" . __LINE__ . EOL;
sleep (60); sleep (60);
}else }else
{ {
echo _("Curl return no error - ") . __FILE__ . ":" . __LINE__ . EOL; echo _("presenceDaemon: Curl return no error - ") . __FILE__ . ":" . __LINE__ . EOL;
$curlErr = 0; $curlErr = 0;
} }
} }