forked from abbr/nagios-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
70 lines (67 loc) · 2.29 KB
/
test.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
'use strict';
// add a command line parser;
// you can substitute with your own favorite npm module
var getOpt = require('node-getopt')
.create([ [ 'm', 'match=<STRING>', 'String response body must match' ]
, [ 'w', 'warning=<STRING>', 'Warning threshold' ]
, [ 'c', 'critical=<STRING>', 'Critical threshold' ]
, [ 'h', 'help', 'display this help' ] ])
.bindHelp();
getOpt.setHelp('Usage: node test.js [Options] -- '
+ '<arguments passed to wget>\nOptions:\n[[OPTIONS]]');
var args = getOpt.parseSystem();
// validate mandatory arguments
if (args.argv.length == 0) {
console.log('missing arguments passed to wget');
getOpt.showHelp();
process.exit(3);
}
var Plugin = require('./lib/index.js');
// create a new plugin object with optional initialization parameters
var o = new Plugin({
// shortName is used in output
shortName : 'wget_http'
});
// set monitor thresholds
o.setThresholds({
'critical' : args.options.critical || 2,
'warning' : args.options.warning || 0.2
});
// run the check - replace with your own business logic
var exec = require('child_process').exec;
var before = new Date().getTime();
exec('wget -qO- ' + args.argv.join(' '), function(error, stdout, stderr) {
var after = new Date().getTime();
var diff = (after - before) / 1000;
// check actual data against predefined threshold
// and returns state: OK, WARNING or CRITICAL
var state = o.checkThreshold(diff);
// Add message for later output. Multiple messages
// in the same state are concatenated at output
o.addMessage(state, stdout.length + ' bytes in ' + diff
+ ' seconds response time');
// use get() method to retrieved parsed program arguments
if (args.options.match && stdout.indexOf(args.options.match) === -1) {
o.addMessage(o.states.CRITICAL, args.options.match + ' not found');
}
// Add performance data
o.addPerfData({
label : "time",
value : diff,
uom : "s",
threshold : o.threshold,
min : 0
});
o.addPerfData({
label : "size",
value : stdout.length,
uom : "B",
min : 0
});
// check messages added earlier and return the most severe set:
// CRITICAL; otherwise WARNING; otherwise OK
var messageObj = o.checkMessages();
// output the short name, state, message and perf data
// exit the program with state as return code
o.nagiosExit(messageObj.state, messageObj.message);
});