function render_status_page() { console.info('updating status page'); req = new XMLHttpRequest(); req.open('GET', '/status.json'); req.setRequestHeader('Accept', 'application/json'); req.addEventListener('load', function(event) { result = JSON.parse(req.responseText)['results']; result.sort(function(a, b) { aname = a['attrs']['host_name'] + a['attrs']['display_name']; bname = b['attrs']['host_name'] + b['attrs']['display_name']; if (aname < bname) return -1; if (aname > bname) return 1; return 0; }); out = ''; last_hostname = ''; result.forEach(function(item) { if (last_hostname != item['attrs']['host_name']) { out += '

' + item['attrs']['host_name'] + '

'; last_hostname = item['attrs']['host_name'] } out += '

'; out += downtime_or_ack_to_string(item['attrs']['downtime_depth'], item['attrs']['acknowledgement']) + ' '; out += state_to_string(item['attrs']['state'], item['attrs']['state_type']) + ' '; out += escape_html(item['attrs']['display_name']); out += '

'; if (item['attrs']['state'] != 0) { out += '
' + escape_html(item['attrs']['last_check_result']['output']) + '
'; out += '

'; out += 'Last checked: ' + item['attrs']['__custom']['last_check'] + '
'; out += 'Last state change: ' + item['attrs']['__custom']['last_state_change']; out += '

'; } out += ''; }); if (out.length == 0) { out += '
'; } document.getElementById('output').innerHTML = out; console.info('updated status page'); }); req.send(); } function state_to_string(state, type) { if (type != 1) { maybe = ' (pending)'; } else { maybe = ''; } if (state == 0) { return 'OK' + maybe + ''; } else if (state == 1) { return 'WARNING' + maybe + ''; } else if (state == 2) { return 'CRITICAL' + maybe + ''; } else { return 'UNKNOWN' + maybe + ''; } } function downtime_or_ack_to_string(dt, ack) { if ((dt + ack) > 0) { return 'Work in Progress'; } return ''; } function escape_html(text) { return text .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }