1
0
moha/daemons/presenceDaemon.php

99 lines
2.1 KiB
PHP
Raw Normal View History

2022-09-01 18:48:20 +02:00
#!/usr/bin/php
<?php
define("EOL", "\n");
$title = "PresenceDaemon";
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
declare(ticks = 1);
require "/etc/moha/liste_telephones.php";
$msg = "";
$result = array();
$presence = array();
$curlErr = 0;
$command = "sudo nmap -n -sP 192.168.1.*"; //need to configure /etc/sudo/sudoers to permit execution of nmap as superuser
2022-09-03 21:10:38 +02:00
2022-09-01 18:48:20 +02:00
echo "presenceDaemon is starting\n";
2022-09-03 21:10:38 +02:00
// init du tableau des présences à -1
2022-09-05 13:47:41 +02:00
foreach($macAddresses as $mac => $nom)
2022-09-03 21:10:38 +02:00
{
$presence[$nom] = -1;
}
2022-09-01 18:48:20 +02:00
while (1)
{
2022-09-03 21:10:38 +02:00
$presenceTemp = array();
2022-09-01 18:48:20 +02:00
exec($command, $result);
2022-09-03 21:10:38 +02:00
foreach ($result as $value)
2022-09-01 18:48:20 +02:00
{
2022-09-03 21:10:38 +02:00
//echo $value;
if (str_contains($value, "MAC Address:"))
2022-09-01 18:48:20 +02:00
{
2022-09-03 21:10:38 +02:00
//echo " contient une adresse MAC" . EOL;
search($value);
2022-09-01 18:48:20 +02:00
}
2022-09-03 21:10:38 +02:00
}
foreach($presenceTemp as $nom => $status)
{
$msg = "";
if ($presence[$nom] != (int)$status)
2022-09-01 18:48:20 +02:00
{
2022-09-03 21:10:38 +02:00
$msg .= "nom=" . $nom . "&presence=" . $status;
2022-09-01 18:48:20 +02:00
send($msg);
2022-09-03 21:10:38 +02:00
echo $nom . " est modifié dans tableau\n";
2022-09-01 18:48:20 +02:00
}
}
2022-09-05 13:47:41 +02:00
$result = array();
2022-09-01 18:48:20 +02:00
sleep(1);
}
2022-09-03 21:10:38 +02:00
function search($string)
2022-09-01 18:48:20 +02:00
{
2022-09-03 21:10:38 +02:00
global $macAddresses, $presenceTemp;
//echo "searching in " . $string . EOL;
2022-09-05 13:47:41 +02:00
foreach ($macAddresses as $needle => $nom)
2022-09-01 18:48:20 +02:00
{
2022-09-03 21:10:38 +02:00
//echo $nom ." => " . $needle . EOL;
2022-09-01 18:48:20 +02:00
if (str_contains($string, $needle))
{
2022-09-03 21:10:38 +02:00
echo "found " . $needle . EOL;
$presenceTemp[$nom] = true;
2022-09-01 18:48:20 +02:00
}
}
}
function send($msg)
{
global $curlErr;
$ch = curl_init();
$msg = "http://localhost:1025/present&" . $msg;
// set url
curl_setopt($ch, CURLOPT_URL, $msg );
// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $result contains the output string
2022-09-03 21:10:38 +02:00
echo _("Curl sending message -") . $msg . __FILE__ . ":" . __LINE__ . EOL;
2022-09-01 18:48:20 +02:00
if ($curlErr <= 5)
{
curl_exec($ch);
if (curl_errno($ch) != 0)
{
$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;
sleep (60);
}else
{
echo _("Curl return no error - ") . __FILE__ . ":" . __LINE__ . EOL;
$curlErr = 0;
}
}
}
?>