Fetching Nodeping check data for use on your website.

NodePing is a monitoring service used by businesses and IT professionals to stay in the know about the state of their internet facing services.
You can check out NodePing here.

I wanted to see if using PHP I could interrogate the API and display a summary of failed checks on a webpage without the need for logging in to NodePing.

It actually turned out to be quite easy and with just a few lines of PHP I was able to display whether all checks were successful or list any that are failing.

The first thing needed was an API token from NodePing. Once you have an account you can see your token on the API documentation page.

After that it was just a case of writing a very quick (and in my case probably dirty) PHP script which does the following;

  • Calls the NodePing checks API using the token retrieved above.
  • Grabs the JSON string returned containing all the checks.
  • Convert the JSON to a PHP array.
$json_string = 'https://api.nodeping.com/api/1/checks?token=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX;
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
  • Then we iterate each check within the array and the state == 0 then the check is failing and we add the label to a new array called failedChecks as well as the timestamp it started failing.
 $i = 0;
$failedChecks = array();

foreach ($obj as $check) {

if ($check['state'] == 0) {

$failedChecks[$i]['label'] = $check['label'];
$failedChecks[$i]['firstdown'] = $check['firstdown'];
$state = "Down";

}

$i++;

}

  • We then check if there are any items in the failedChecks array. If there are then we list them. If not then i’m simply displaying a message saying everything is good.

if (count($failedChecks) > 0) {

foreach ($failedChecks as $failedCheck) {

$timestamp = $failedCheck['firstdown']/1000;

echo "

<h2><img class='alert' src='/img/red_dot.png'>Down Since  " . date("d/m/Y H:i:s", $timestamp) . " 
<span class='padding'></span>" . $failedCheck['label'] . "</h2>


";


}


}

else {

echo "

<h1 align='center'>All Services Up</h1>


";
echo "<img class='status' src='/img/check_box_inv.png'>";

}

This works great, however it will only do this check once (when the page loads). In order to have this information update automatically, I needed to use a bit of jQuery to do a quick ajax call.

Firstly I saved the below code into a file called getNodeping.php in the root of my website.


$json_string = 'https://api.nodeping.com/api/1/checks?token=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX;
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);

print '

<h1 align="center">Service Status (' . count($obj) . ' Checks)</h2>


';

$i = 0;
$failedChecks = array();

foreach ($obj as $check) {

if ($check['state'] == 0) {

$failedChecks[$i]['label'] = $check['label'];
$failedChecks[$i]['firstdown'] = $check['firstdown'];
$state = "Down";

}

$i++;

}

if (count($failedChecks) > 0) {

foreach ($failedChecks as $failedCheck) {

$timestamp = $failedCheck['firstdown']/1000;

echo "

<h2><img class='alert' src='/img/red_dot.png'>Down Since  " . date("d/m/Y H:i:s", $timestamp) . " 
<span class='padding'></span>" . $failedCheck['label'] . "</h2>


";


}


}

else {

echo "

<h1 align='center'>All Services Up</h1>


";
echo "<img class='status' src='/img/check_box_inv.png'>";

}

Then from the html document I want to display the content on I include the following, between the <head></head> tags.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<script>

(function updateNodeping() {
$.ajax({
type : 'GET',
url : 'getNodeping.php',
success : function(data){
$('#nodePingChecks').html(data);
},
// pass existing options
}).then(function() {           // on completion, restart
setTimeout(updateNodeping, 30000);  // function refers to itself
});
})();

</script>

This includes the jQuery library and a function which fires when the page loads. This function loads the php file created earlier and displays the output in a DIV with ID nodePingChecks.




<div id='nodePingChecks'></div>



Hopefully this helps anyone that wants to display a very basic summary of their nodeping checks on their website.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *