Server Up/Down Dashboard

This project creates a dashboard that shows whether servers are responding to pings. The dashboard refreshes every 10 seconds and the ping output is given a background color based on the response. (Green – UP, Red – DOWN, Grey – BAD VALUE).

We create an HTML form to accept up to 4 website/server names of IP addresses. These values are then saved as SESSION variables. SESSION variables allow you to save variable values without writing to a file or database. This creates variables that can be called up until you close the webpage, and the variable values stay set even when the page is refreshed.

The servers are then pinged using shell_exec() to run PING from the command line, and the results are tested with strpos() to determine if a server responded to the ping. Based on this the DIV background color is changed for each ping.

This whole page is auto refreshed every 10 seconds using http-equiv=”refresh” at the top of the page.

Potential Quirks/ Issues:

We set the auto refresh time to 10 seconds to show the output quickly. You may want to set this to 30-60 seconds normally. If you try to change the IP Addresses/ Server names during the update process the edits will be lost. (I edit one value and then hit submit to lock in the change). Also, shell_exec() waits for a response from the operating system, if for some reason your server is slow the page could lock up with a fast refresh rate.

Code Explanation:


Line 1 – This HTML code auto refreshes the page every 10 seconds. You probably want to increase this to 30 seconds.

Line 3 – App Title

Line 5 – Open PHP script

Line 7 – session_start() starts the SESSION so that you can have SESSION variables. This should be as high as possible in a PHP script.

Lines 9 – 20 – These 4 IF Statements determine whether a POST value from the form later in the script are not blank. If there is a value in the POST Variable the it is saved to the SESSION Variable.

Lines 22-25 – Creates Variables for 1st-4th sites and sets them to the values of the SESSION variables. We do this because SESSION variables can be difficult to deal with in PHP code, so by creating standard variables and setting them to the values of the SESSION variables we can do things like use the values as the default values in the form.

Line 27 – Create the command that will be run in shell_exec(). Notice we set the PING to a count of 1 so it only pings once. Linux by default pings continuously so without this the page would lock up.

Lines 29 – 35 – We dynamically write an HTML form. By using PHP we can set the default values for the text boxes to what has been entered before. value=’first’ makes the textbox prefilled with the value of $first, which we set as the the value of $_SESSION[‘first’].

Line 37 – We create an array for the 4 servers so that we can loop through them for the out put using foreach().

Line 39 – Open the foreach() loop.

Line 40 – Create a variable $result that will be set by running shell_exec() using the $command with the current $site variable.

Line 41 – Open if/else statement, and use strpos() to determine if “1 received” is within $result. If so we set the $color variable to green

Line 43 – We use an elseif to see if $result contains “0 received” which means the server did not respond, and then we set $color to red.

Line 45 – The final else says that if we get any other response set the $color value to grey. Generally the other response would be for ping to fail because the domain name can’t be resolved, or if the IP address isn’t correct. Such as .

Line 48 – We create an HTML DIV and set the background color to the current value of $color.

Line 49 – We print out the current server we are pinging

Line 50 – We print out the full value for $result.

Lines 51-52 – We close the DIV and then add a <br> to add a space between the print out of each result.

<meta http-equiv="refresh" content="10"> <h1>Up Down Tester</h1> <?php session_start(); if ($_POST['first'] !=""){ $_SESSION['first'] = $_POST['first']; } if ($_POST['second']!=""){ $_SESSION['second'] = $_POST['second']; } if ($_POST['third'] !=""){ $_SESSION['third'] = $_POST['third']; } if ($_POST['fourth'] !=""){ $_SESSION['fourth'] = $_POST['fourth']; } $first = $_SESSION['first']; $second = $_SESSION['second']; $third = $_SESSION['third']; $fourth = $_SESSION['fourth']; $command = "ping -c 1"; echo "<form action='tester.php' method='post'>"; echo "<input type='text' name='first' value=$first>"; echo "<input type='text' name='second' value=$second>"; echo "<input type='text' name='third' value=$third>"; echo "<input type='text' name='fourth' value=$fourth>"; echo "<input type='submit'>"; echo "</form>"; $site_array = array($first, $second, $third, $fourth); foreach($site_array as $site){ $result = shell_exec("$command $site"); if(strpos($result,"1 received")){ $color="green"; } elseif(strpos($result,"0 received")){ $color="red"; }else { $color="grey"; } echo "<div style='background-color:$color;'>"; echo "<h2>$site</h2>"; echo $result; echo "</div>"; echo "<br>"; } ?>
Code language: HTML, XML (xml)