forked from notatestuser/node-nagios-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagiosparser.js
executable file
·91 lines (76 loc) · 3.54 KB
/
nagiosparser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
var jsdom = require('jsdom')
jQuery = require('jquery');
var parseDocument = function(textData, callback) {
var headings = [],
parsedHash = {};
jsdom.env(textData, [],
function(errors, window) {
var $ = jQuery.create(window);
var count = 0,
lastServerObj = null;
$('table.status > tr').each(function(index) {
var $this = $(this);
if (index == 0) {
// heading row of the stats table -- build headings
$this.children('th').each(function(thIdx) {
if (thIdx > 1) {
// add the current heading text to the array (e.g. "Status Information")
headings.push($.trim($(this).text()));
}
});
} else {
// just another ordinary row
var $name = $this.find('a');
if ($name.length) {
var serverAddr = $name.attr('title');
if (serverAddr) {
var serverName = $name.html();
//console.info(' + ' + $name.html());
lastServerObj = parsedHash[serverName] = {};
count++;
// get the first service (same line than serverName)
$name = $name.slice(1);
// avoid getting icons
if ($name.find('img').length) {
$name = $name.slice(1);
}
}
var serviceName = $name.html();
//console.info('\t' + serviceName);
lastServerObj[serviceName] = {};
$this.children('td').each(function(tdIdx) {
if (tdIdx > 1) {
//console.info('\t\t' + headings[tdIdx - 2] + ' ' + $(this).text());
lastServerObj[serviceName][headings[tdIdx - 2]] = $.trim($(this).text());
}
});
}
}
});
callback(parsedHash, count);
});
}
exports.parser = function(){
return {
parse: function(nagiosUrlObject, callback, errorCallback) {
nagiosUrlObject.pathname = nagiosUrlObject.pathname ? nagiosUrlObject.pathname : '';
nagiosUrlObject.path = nagiosUrlObject.pathname += '/cgi-bin/status.cgi?hostgroup=all&style=detail';
var data = '';
var http = nagiosUrlObject.protocol.indexOf('https') === 0 ? require('https') : require('http');
var req = http.request(nagiosUrlObject,
function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
}).on('end', function() {
parseDocument(data, callback);
});
}).on('error', function(e) {
if (errorCallback != undefined)
errorCallback(e);
});
req.end();
}
}
}();