Roland Ulbricht

A simple script for displaying server's status on a website

2012-12-07

My file server's internet connection was unstable for a few weeks. Worrying about the issue all the time, I wrote a small script to monitor the server.

The server's connection failed without reason or pattern and it was very hard to track down the problem. Several times, I thought that I had found the solution and the server worked again, but then failed again after a few minutes, hours or days. So I started to check whether the server was still online every few minutes. This made me very nervous and I wanted a better solution.

The only incoming transmission the firewall would allow was SSH on a non-standard port. So I couldn't use any of the tools that check for website availability or ping. Of course, I could have used a full-fledged monitoring tool such as Nagios, but that seemed to be overkill.

Therefore I decided to write a little script which would be placed on the file server and call our web server every few minutes. This works quite well and allows all users to check whether the server is down.

Part #1 Sending script (Linux Shell)

The sending script is placed on the server you want to monitor. It calls a special website using lynx, an open text-based browser.

This part of the script is quite simple, the website is called, but we don't actually want to see the output, instead redirect it to nowhere.

I set this script to execute every five minutes using crontab.

#!/bin/sh   
lynx -dump http://www.my-server/alive.php?who=centauri > /dev/null

"Centauri" is the name of my file server.

Part #2 Receiving script (HTML / PHP)

The receiving script is placed on the web server. It is also quite simple, written in PHP. When the page is loaded with the argument "centauri", the current time is written to a file.

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Counter</title>
</head>

<body>

<?php

# Put time of last contact into a file

if ($_REQUEST['who'] == "centauri") {
    file_put_contents("centauri.txt", time());
} else if ($_REQUEST['who'] == "some other server") {
    file_put_contents("some other server.txt", time());
}

?>

Counter done!

</body>

</html>

Part #3 Displaying script (HTML / PHP)

The displaying script is another PHP-script. I placed it directly in one of the organisation's Drupal websites using the PHP-plugin (therefore I don't need the html-header here).

<?php
# get contents of the file
$centauri = file_get_contents("/www/alive/centauri.txt");

# calculate the time since last contact
$secs_centauri = time() - $centauri;
?>

<table>
<thead>
    <tr>
        <th>Server</th>
        <th>Status</th>
        <th>Last Seen</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Centauri</td>
        <td>

<?php

if ($secs_centauri < 60*10) { 
    # if last contact was less than 10 minutes ago, say ok
    echo "Ok";
} else {
    # if last contact was more than 10 minutes ago, not ok.
    echo round(($secs_centauri / 60)-10) . " minutes overdue.";
}

?>

        </td>
        <td>

<?php
# display the some of last contact with centauri
echo date("Y-m-d H:i", $centauri);
?>

        </td>
    </tr>
</tbody>
</table>

What I like about the script

  • Very easy
  • Don't need root access on the web server, no need to install anything.

Things to consider

  • The script does not collect any information other than the time of the last contact (of course, this could be built in)
  • Consider traffic: If a script runs every 5 minutes, this means it will run 8640 times in a 30-day month! So the receiving script should really be kept small.

So, what was the problem?

After two weeks I finally found out what the problem was: One of the network cables between the router and the service provider seems to have had a loose contact. This made the router to hang up in changing time intervals.

Disclaimer

I take no responsibility for anything that happens if you use these scripts. Please only use them if you know what you are doing. Unfortunately, I am not able to provide any support but I would be glad about any feedback.

Posted: 2012-12-07; Tags: IT, webserver