true, // wichtig! "host" => "smtp.ionos.de", "port" => "587", "username" => "xxx", "password" => "xxx")); $json_stat_name = "/var/www/tm-freifunk/wd_stat.json"; if (! file_exists($json_stat_name)) create_json_sample(); check_hosts(); exit; //------------------------------------------------------------ function check_hosts() { global $json_stat_name; $hosts = read_json(); $update = false; foreach ($hosts as &$host) { if (array_key_exists('ignore', $host)) if ($host['ignore']) continue; if (array_key_exists('watchstart', $host)) if (array_key_exists('watchend', $host)) if ( (time() < strtotime($host['watchstart'])) || (time() > strtotime($host['watchend'])) ) continue; $lastseen = strtotime($host['lastseen']); $timedout = strtotime($host['timeout'], $lastseen); $offline = ($timedout < time()); $statechanged = ($offline != $host['wasoffline']); if ($statechanged) { $update = true; // write changes to file $host['wasoffline'] = $offline; if ($offline) { $subj = "OFFLINE: ".$host['hostname']; $body = 'Please check your device: ' . $host['hostname'] . "!"; } else { $subj = "ONLINE: ".$host['hostname']; $body = 'Device is online again: ' . $host['hostname'] . "."; } send_mail($host['mailto'], $host['mailfrom'], $subj, $body); } } if ($update) file_put_contents($json_stat_name, json_encode($hosts, JSON_PRETTY_PRINT)); } function read_json() { global $json_stat_name; $content = file_get_contents($json_stat_name); return json_decode($content, true); } function send_mail($mailto, $mailfrom, $mailsubj, $mailbody) { global $smtp; echo "send mail\n"; $mailheader = array( "Content-Type" => "text/plain; charset=\"UTF-8\"", "From" => $mailfrom, "To" => $mailto, "Subject" => $mailsubj); $mail = $smtp->send($mailto, $mailheader, $mailbody); if (PEAR::isError($mail)) { echo "error: " . $mail->getMessage() . "\n"; } # if not using PEAR::Mail, setup a local MTA #$mailhead='Content-Type: text/plain; charset="UTF-8"' . "\r\n" # . "From: $mailfrom" . "\r\n"; #if (! mail ($mailto, $mailsubj, $mailbody, $mailhead)) # echo "error"; } // create a sample file function create_json_sample() { global $json_stat_name; $host = array( "hostname" => "host", "lastseen" => date("Y-m-d H:i:s", strtotime('-2 hour')), "wasoffline" => false, "mailto" => "name@mail.tld", "mailfrom" => '"FF-Watchdog" ', "ignore" => false, "timeout" => "+1 hour", "watchstart" => "00:00:00", "watchend" => "23:59:59" ); $arr = array($host, $host); $json = json_encode($arr, JSON_PRETTY_PRINT); file_put_contents($json_stat_name, $json); } ?>