Skip to main content
added 532 characters in body
Source Link
RafH
  • 126
  • 3

You can simplify if status images are the same for all services.

var img = {
  "OK":"http://placehold.it/40/00cc33/ffffff",
  "WARNING":"http://placehold.it/40/ff6600/000000",
  "CRITICAL":"http://placehold.it/40/ff0000/000000",
  "UNKNOWN":"/path/to/unknow"
}
var services = ['http','ftp','mysql']

function refreshView (xml) {
  for (var i=0; i<services.length; i++) {
    $("." + services[i] +" img").attr("src",img[ $(xml).find(services[i]).text()] );
  }
}

You can simplify if status images are the same for all services.

var img = {
  "OK":"http://placehold.it/40/00cc33/ffffff",
  "WARNING":"http://placehold.it/40/ff6600/000000",
  "CRITICAL":"http://placehold.it/40/ff0000/000000",
  "UNKNOWN":"/path/to/unknow"
}
var services = ['http','ftp','mysql']

function refreshView (xml) {
  for (var i=0; i<services.length; i++) {
    $("." + services[i] +" img").attr("src",img[ $(xml).find(services[i]).text()] );
  }
}
Source Link
RafH
  • 126
  • 3

Here is a proposition. The idea is to define an object that maps the property (http, ftp...) with related images url indexed by possible property values (OK, WARNING,...).

With this approach :

  • get the img src for a service : object[service_name][service_status]
  • get the service status : $(xml).find(service_name).text()
  • get the img dom element $("." + service_name + " img")

Code :

    $(document).ready(function() {  
       var config = {
            "http":{
                "OK":"http://placehold.it/40/00cc33/ffffff",
                "WARNING":"http://placehold.it/40/ff6600/000000",
                "CRITICAL":"http://placehold.it/40/ff0000/000000",
                "UNKNOWN":"/path/to/unknow"
            },
            "ftp":{
                "OK":"http://placehold.it/40/00cc33/ffffff",
                "WARNING":"http://placehold.it/40/ff6600/000000",
                "CRITICAL":"http://placehold.it/40/ff0000/000000",
                "UNKNOWN":"/path/to/unknow"
            },
            "mysql":{
                "OK":"http://placehold.it/40/00cc33/ffffff",
                "WARNING":"http://placehold.it/40/ff6600/000000",
                "CRITICAL":"http://placehold.it/40/ff0000/000000",
                "UNKNOWN":"/path/to/unknow"
            },
            "load":{
                "OK":"http://placehold.it/40/00cc33/ffffff",
                "WARNING":"http://placehold.it/40/ff6600/000000",
                "CRITICAL":"http://placehold.it/40/ff0000/000000",
                "UNKNOWN":"/path/to/unknow"
            }
        };
       
       function refreshView (xml) {
            for (var name in config) {
                $("." + name +" img").attr("src",config[name][$(xml).find(name).text()] );
            }   
       }
      
        $.ajax({          
            type:  'GET',
            url:   'https://status.clook.net/xml/status/harvey.xml',
            dataType: 'xml',              
            success: refreshView
        });
    });