I am using the below jQuery and HTML to read in and use an XML server status feed for a server widget. This is the code:
XML Feed:
<status>
<script id="tinyhippos-injected"/>
<serverinfo>
<hostname>harvey</hostname>
<external>
http://stats.pingdom.com/3tltrr03waho/1435422/history
</external>
</serverinfo>
<host>
<status>OK</status>
</host>
<service>
<http>OK</http>
<ftp>OK</ftp>
<mysql>OK</mysql>
<pop>OK</pop>
<imap>OK</imap>
<smtp>OK</smtp>
<load>OK</load>
</service>
</status>
jQuery:
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'https://status.clook.net/xml/status/harvey.xml',
dataType: 'xml',
success: function(xml){
var http = $(xml).find('http').text();
var ftp = $(xml).find('ftp').text();
var mysql = $(xml).find('mysql').text();
var load = $(xml).find('load').text();
if(http == 'OK') {
$(".http img").attr("src","http://placehold.it/40/00cc33/ffffff");
}
else if(http == 'WARNING') {
$(".http img").attr("src","http://placehold.it/40/ff6600/000000");
}
else if(http == 'CRITICAL') {
$(".http img").attr("src","http://placehold.it/40/ff0000/000000");
}
if(ftp == 'OK') {
$(".ftp img").attr("src","http://placehold.it/40/00cc33/ffffff");
}
else if(ftp == 'WARNING') {
$(".ftp img").attr("src","http://placehold.it/40/ff6600/000000");
}
else if(ftp == 'CRITICAL') {
$(".ftp img").attr("src","http://placehold.it/40/ff0000/000000");
}
if(mysql == 'OK') {
$(".mysql img").attr("src","http://placehold.it/40/00cc33/ffffff");
}
else if(mysql == 'WARNING') {
$(".mysql img").attr("src","http://placehold.it/40/ff6600/000000");
}
else if(mysql == 'CRITICAL') {
$(".mysql img").attr("src","http://placehold.it/40/ff0000/000000");
}
if(load == 'OK') {
$(".load img").attr("src","http://placehold.it/40/00cc33/ffffff");
}
else if(load == 'WARNING') {
$(".load img").attr("src","http://placehold.it/40/ff6600/000000");
}
else if(load == 'CRITICAL') {
$(".load img").attr("src","http://placehold.it/40/ff0000/000000");
}
}
});
});
HTML:
<div class="container">
<h3>Server Status Widget</h3>
<h4>Hosting Server</h4>
<p><strong>HTTP: </strong><span class="http"><img src="http://placehold.it/40" alt="" /></span></p>
<p><strong>FTP: </strong><span class="ftp"><img src="http://placehold.it/40" alt="" /></span></p>
<p><strong>MySQL: </strong><span class="mysql"><img src="http://placehold.it/40" alt="" /></span></p>
<p><strong>Load: </strong><span class="load"><img src="http://placehold.it/40" alt="" /></span></p>
</div>
The statuses can be either OK, WARNING, CRITICAL or UNKNOWN. I was going to use the default src
in the HTML as the UNKNOWN image.
I would appreciate any feedback on the jQuery - it is really wordy and I am sure can be improved. All works fine currently.