Check the Webserver Downtime

Client Server Communication

In this project you will need the following components:

  • A Raspberry Pi
Raspberry Pi

Raspberry Pi, purchase here: English or German


You can have a look at the page of IoT Components in order to see how you can get a Raspberry Pi and other parts.

When you have a website it might be interesting to know it when your web server goes down. This can give you an idea about the reliability of the web server you are using for your website.

A web server consists of a hardware (one or more physical components) and some specific software components line an HTTP server and a database server. In most cases Apache is the HTTP server and MySQL is the database server software. They both receive requests from the Internet and send back requests that contain the contents of a webpage.

Here a simple image that shows the architecture of communication between a client (like a web browser on your computer) and a web server:

Client Server Communication

The Communication flow between an Internet browser (client) and a web server


How to observe the web server

The first question is: how can we know whether or not the web server is working?

The answer is quite easy: Just try to open a web page in your Internet browser! If you see the page contents, then the web server is working. Any other results mean there is a problem. We can easily create a web page with a very specific content that we can try to open from time to time. This can be a page written in HTML. HTML is very easy to learn and if you need to do so, I recommend this book in English or in German.

In case your website makes usa of the database, then you want to know if the database is up and running as well. In this case you need to write a program that can log into the database system on the server and issue a simple select query. Then this program sends a success of failure response back to your browser. We will use PHP to implement this program. If you need to learn PHP and SQL (the language that database systems usually speak), I recomment this book in English or in German.

Because we want to observe our website 24×7 we will use our Raspberry Po (Raspi) that will act as a client. It sends periodically requests to the web server and analyzes the response. The result can be written to a file that you can check from time to time. So let’s start with the practical part of this project!

Step 1: Database Preparation

In this example we assume that we use a database on the web server and so we want to observe its availability as well.

Create a new database on the server. We need the following data:

  • the database name
  • a database user
  • the password of the user

Step 2: Web page PHP code

Once the database and the user are created we can write the PHP program. This program will be copied to your server and will be used like a normal web page. I would recomment to use a weird file name so that it is not easy for others to read it.

Because we are going to open this page very frequently and because we know exactly what we are using this page for, its contents don’t need to be a correct and complete HTML code. It would be sufficient to receive only one word as response that tells us how the server is doing. Therefore this PHP file echos only short messages.

Here is the code:

I name this file “KdjfoUn4928.php“.

/* here we initialize some variables with the database data. */
$DB_HOST = "localhost";
$DB_NAME = "database_name";
$DB_USERNAME = "database_username";
$DB_PASSWORD = "user-password";

/* log into the database. */
$db = new mysqli($DB_HOST, $DB_USERNAME, $DB_PASSWORD, $DB_NAME);

if (mysqli_connect_errno()) {
/* login failed. Send back error message. */
} else {
/* issuing an easy select statement to the database is enough
to know whether or not it is working. */
$select = "SELECT 1";
$selectResult = $db->query($select);

if ($selectResult == false) {
/* The query failed. Send back error message. */
} else {
if ($selectResult->num_rows != 1) {
/* query worked but the database responded with
wrong result. Send back error message. */
} else {
/* query worked just fine. */

/* database logout */

Let’s have a look at this PHP program.

First we declare 4 variables (actually constants) that we use to store the database connection data:

$DB_HOST = "localhost";
$DB_NAME = "database_name";
$DB_USERNAME = "database_username";
$DB_PASSWORD = "user-password";

Because this program will run on the server and connect to the database system on the same server, we set “$DB_HOST” to “localhost”. If your database server is running on another server, then you must replace “localhost” with the IP address of the server where your database is running.

In most cases “localhost” is an alias for the IP address “”. I would not use this IP address here because there are cases where “localhost” stands for another IP address. In such cases this PHP program cannot connect or login to your database.

Return Values are as follows:

“DB_CONNECTION_ERROR”: This string is returned to the client in case the database system is not working and the PHP program cannot log into it.

DB_CONNECTED_SELECT_ERROR: This situation happens when the program has already logged to the database and has issued the select queiry. The database system sends back an error.

“DB_CONNECTED_SELECT_WRONG_RESULT”: The program tries to issue a simple select. If the database response is not correct, then this string is returned to the client. Basically this situation should never happen. But anyway…

“SERVER_OK”: Everything was OK. Server (HTTP and database servers are up an running).

Step 3: Client

The client is the program or app that tries to open the PHP page we implemented in step 2. Usually you use an Internet browser like Firefox in order to open a page on the web server. But in this case we want to carry out this job automatically. So, we develop another program that opens the web page on a periodic basis. Here I use the strength of Linux and develop a shell script that does the client job for me.

The Linux command “curl” takes a URL (this is an Internet address like, sends a request to the web server and outputs the response. This is exactly what we need in order to send a request to our web server (to the PHP web page we developed before) and to get the server response back and save it to a log file.

I use “curl” in my Linux shell script and I ask Linux to run this script periodically.

Here is the Linux shell script:




echo "$(date)" >> $logfile

myResponse=$(curl -s

if [[ -z "$myResponse" ]]
printf "\tServer is down!\n" >> $logfile
case "$myResponse" in
"$DB_CON_ERR") printf "\tDB connection error!" >> $logfile
"$DB_CON_SEL_NO_RES") printf "\tselection error!\n" >> $logfile
"$DB_CON_SEL_WRG_RES") printf "\tResult doesn't have only 1 row!\n" >> $logfile
"$SERVER_OK") printf "\t$myResponse\n" >> $logfile
*) printf "$myResponse\n" >> $logfile

Be carefull about the indentations in the shell script! They are all TABS.

Let’s have a look at some lines of our shell script:


This will be the file where we write the server responses to. You can use another path if you want. Otherwise you must create the appropriate subdirectories as follows:

   $ cd
   $ mkdir log-files
   $ cd log-files
   $ mkdir PHP-MONITOR

The next interesting line is this one:

echo "$(date)" >> $logfile

This will write date and time into the log file.

myResponse=$(curl -s

This is the magical curl that tries to open the PHP web page and receives its reponse.

The rest of the shell script is just about writing the correct response to the log file.

Now you just need to create a cron job that runs this shell script e.g. every 10 minutes.
From time to time you can have a look into the log file and see if there were any downtimes.

I hope this project was interesting and useful for you. Let me know how you think about this blog.