Skip to content

Instantly share code, notes, and snippets.

@bagf
Last active July 6, 2016 08:06
Show Gist options
  • Save bagf/960a27d909a73c6777df7787488982d6 to your computer and use it in GitHub Desktop.
Save bagf/960a27d909a73c6777df7787488982d6 to your computer and use it in GitHub Desktop.
Script that should run regularly to check the status of the internet and writes the results to a date stamped CSV.
<?php
date_default_timezone_set('Africa/Johannesburg');
/**
* Get this library here https://github.com/geerlingguy/Ping
*/
require_once __DIR__ .'/Ping/JJG/Ping.php';
$lock = __DIR__ ."/.lock";
$logfile = __DIR__ ."/data/".date('Y-m-d').".csv";
$hosts = array(
'google' => 'www.google.com',
// Add more hosts here
);
if (is_file($lock)) {
exit(1);
}
touch($lock);
if (!is_file($logfile)) {
$headHandle = fopen($logfile, 'w+');
fwrite($headHandle, "time,host,success,latency\n");
fclose($headHandle);
}
$handle = fopen($logfile, 'a');
$p = new JJG\Ping('localhost');
foreach ($hosts as $host => $url) {
$p->setHost($url);
$latency = $p->ping();
$line = [
date('H:i'),
$host,
($latency === false?'0':'1'),
"{$latency} ms",
];
fwrite($handle, implode(',', $line)."\n");
}
unlink($lock);
fclose($handle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment