Website / Hosting monitor

 

This was another small project for my Raspberry Pi.

For some time I’ve wanted to keep tabs automatically on this site. After playing around with setting up Lighttpd, MySql, and PHP on my Pi I decided to use it for this.

If you have access to only one server and can’t use a machine at home as one then this project could be put on a free host such as www.000webhost.com/  Some free hosts may not give you access to cron scheduling (000wehost says they do) but you could run it on a browser page from home using the same technique as in www.digitalham.co.uk/dynamic-dns-woes/ – ie a meta-refresh in the header to make the script run every 5 minutes <meta http-equiv='refresh' content='900'>.



I decided to store all results in a simple MySQL table. Here is the table definition…

CREATE TABLE IF NOT EXISTS `monitoring` (
`recno` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`url` varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`load_time` decimal(4,3) NOT NULL,
`datasize` mediumint(9) NOT NULL DEFAULT '0',
PRIMARY KEY (`recno`),
KEY `timestamp` (`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

By monitoring another site on the same server as digitaham, a site on a different host entirely, and google I can look at what sort of results all those got when there is an error and get an idea of the answers to – is my server down? running slowly? or maybe just my home internet performing badly.

I used sudo crontab -e to run the script every 5 minutes

*/5 * * * * /usr/bin/php /var/www/sitechk.php > /dev/null

I found on my Pi 2 using the NOOBS operating system that came with the kit I bought that the path to PHP was different and the crontab entry on that became
/usr/lib/cgi-bin/php /var/www/sitechk.php

Make sure that the script has the execute privilege for world by either using your ftp program or chmod to 755.

The data collection script
It stores the results, emails error alerts, prunes old entries, and mostly for testing does simple echo reporting as it runs.
The source of the monitoring and reporting scripts is shown below and include in this zip file together with the required css file for downloading

<?php
# Change as required

# Database
$db_host = 'localhost';
$db_user = '########';
$db_pass = '########';
$db_name = '########';

# Sites to monitor
$site_1 = "https://www.digitalham.co.uk/";
$site_2 = "https://digitalham.asuscomm.com/favourites/index.php";

# End of required changes

function visit($url,$conn)
 {
  echo "Checking $url<br>";

  $time = microtime();
  $time = explode(' ', $time);
  $time = $time[1] + $time[0];
  $start = $time;

  $page = file_get_contents ($url);
  $datasize = strlen($page);

  $time = microtime();
  $time = explode(' ', $time);
  $time = $time[1] + $time[0];
  $finish = $time;
  $load_time = round(($finish - $start), 3);

  $add_query = "INSERT INTO monitoring (url,load_time,datasize) VALUES('$url','$load_time','$datasize')";
  echo "$add_query<br>";
  $result = mysqli_query($conn,$add_query);

  echo "Request time:$load_time seconds<br>Data size:$datasize<br><br>";
 }

echo "Running SiteChk<br><br>";
$connection = @mysqli_connect($db_host,$db_user,$db_pass,$db_name);

visit($site_1,$connection);
visit($site_2,$connection);

// clean up
$maint_query = "DELETE FROM monitoring WHERE timestamp < ADDDATE( NOW( ) , -7 )";
$result = mysqli_query($connection,$maint_query);
mysqli_close($connection);
echo "Script completed\n";
?>

URL Performance Report

<?php
# Change as required
# Database
$db_host = 'localhost';
$db_user = '########';
$db_pass = '########';
$db_name = '########';

// connect to MySQL
$connection = @mysqli_connect($db_host,$db_user,$db_pass,$db_name);
?>
<html>
<head>
  <title>URL Report</title>
  <meta http-equiv="imagetoolbar" content="no">
  <link rel="stylesheet" type="text/css" href="monitoring.css"/>
<script>
function show(id)
 {
  var displayed = sessionStorage.displayed;
  if(!displayed)
   {
    sessionStorage.displayed = "yes";
    document.forms["stats"].submit();
   }
 }
</script>
</head>
<body onload='show();'>
<div id="container">
   <?php
   echo "<div id=\"top_box\">";
   if (isset($_POST['selurl'])) $selurl = $_POST['selurl'];
   else $selurl = "";

   $over_time = 2; // seconds

   $my_query = "SELECT DISTINCT url FROM monitoring order by url";
   $url_list = mysqli_query($connection,$my_query);

   echo "<br><form id='stats' name='stats' action=\"site_report.php\" method=post>\n";
   echo "<select name='selurl' onchange='this.form.submit()'>\n";
   while ($option = mysqli_fetch_array($url_list,MYSQLI_ASSOC))
    {
     $url = stripslashes($option['url']);
     echo "<option value='$url'";
     if ($selurl == $url) echo " selected";
     echo ">";
     echo "$url\n";
    }
   echo "</select>\n";
   echo "    <input type='submit' alt='Select URL' border=0 name='select' value='Report now'>\n";
   echo "</form>\n";

   if ($selurl)
    {
     $my_query  = "SELECT * FROM monitoring where url = '$selurl' ORDER BY load_time ASC  limit 0,1;";
     $my_result = mysqli_query($connection,$my_query);
     $my_row    = mysqli_fetch_array($my_result,MYSQLI_ASSOC);
     $fastest   = $my_row ['load_time'];
     $faststamp = $my_row ['timestamp'];

     $my_query  = "SELECT * FROM monitoring where url = '$selurl' ORDER BY load_time DESC limit 0,1;";
     $my_result = mysqli_query($connection,$my_query);
     $my_row    = mysqli_fetch_array($my_result,MYSQLI_ASSOC);
     $slowest   = $my_row ['load_time'];
     $slowstamp = $my_row ['timestamp'];

     $my_query  = "SELECT AVG(load_time) AS average, COUNT(recno) AS samples FROM monitoring where url = '$selurl';";
     $my_result = mysqli_query($connection,$my_query);
     $my_row    = mysqli_fetch_array($my_result,MYSQLI_ASSOC);
     $average   = round(($my_row ['average']),3);
     $samples   = $my_row ['samples'];

     $my_query  = "SELECT COUNT(recno) AS overs FROM monitoring where url = '$selurl' and load_time > '$over_time';";
     $my_result = mysqli_query($connection,$my_query);
     $my_row    = mysqli_fetch_array($my_result,MYSQLI_ASSOC);
     $overs     = $my_row ['overs'];

     $overpc    = round (($overs * 100 / $samples),1);

     echo "$selurl<br>\n";
     echo "Samples: $samples<br>\n";
     echo "Fastest load time: $fastest     $faststamp<br>\n";
     echo "Slowest load time: $slowest     $slowstamp<br>\n";
     echo "Average load time: $average<br>\n";
     echo "Load time > $over_time sec: $overs ($overpc%)<br>\n";
     echo "</div>\n";
     echo "<div id=\"slowest\">\n";
     echo "<h2>$selurl<br>Slowest load times</h2>\n";
     $my_query  = "SELECT * FROM monitoring WHERE url = '$selurl' order by load_time DESC LIMIT 0,30";
     $my_result = mysqli_query($connection,$my_query);
     echo "<table id=\"stats\">\n";
     while ($my_row = mysqli_fetch_array($my_result,MYSQLI_ASSOC))
      {
       $timestamp = $my_row ['timestamp'];
       $load_time = $my_row ['load_time'];
       $datasize  = $my_row ['datasize'];
       echo "<tr><td>$timestamp Datasize: $datasize Load time: $load_time</td></tr>\n";
      }
     echo "</table>\n";
     echo "</div>\n";
     echo "<div id=\"fastest\">\n";
     echo "<h2>$selurl<br>Fastest load times</h2>\n";
     $my_query  = "SELECT * FROM monitoring WHERE url = '$selurl' order by load_time ASC LIMIT 0,30";
     $my_result = mysqli_query($connection,$my_query);
     echo "<table id=\"stats\">\n";
     while ($my_row = mysqli_fetch_array($my_result,MYSQLI_ASSOC))
      {
       $timestamp = $my_row ['timestamp'];
       $load_time = $my_row ['load_time'];
       $datasize  = $my_row ['datasize'];
       echo "<tr><td>$timestamp Datasize: $datasize Load time: $load_time</td></tr>\n";
      }
     echo "</table>\n";
     echo "</div>\n";
    }
   ?>
</div>
</body>
</html>