-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.js
executable file
·119 lines (99 loc) · 3.58 KB
/
notifier.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env node
/*
Incoming arguments:
-A Contact information (API keys, device ID's, that kind of stuff)
-H Hostname
-T Type (host or service)
-D HOST_OBJECT / SERVICEDESC
-S Host/Service state
-O Host/Service output
Make the call:
notifier_script -A "pushalot:API_KEY;pushbullet:API_KEY,deviceId;;;;" -T <host/service> -H <hostname> -D <description> -S <state> -O <output>
Host config in OP5/Nagios
/opt/nagios-notifications/bin/notify.js -A "$CONTACTADDRESS1$\;$CONTACTADDRESS2$\;$CONTACTADDRESS3$\;$CONTACTADDRESS4$\;$CONTACTADDRESS5$\;$CONTACTADDRESS6$" -H "$HOSTNAME$" -T host -D "HOST_OBJECT" -S "$HOSTSTATE" -O "$HOSTOUTPUT$"
Service config in OP5/Nagios
/opt/nagios-notifications/bin/notify.js -A "$CONTACTADDRESS1$\;$CONTACTADDRESS2$\;$CONTACTADDRESS3$\;$CONTACTADDRESS4$\;$CONTACTADDRESS5$\;$CONTACTADDRESS6$" -H "$HOSTNAME$" -T service -D "$SERVICEDESC$" -S "$SERVICESTATE" -O "$SERVICEOUTPUT$"
Output Text:
Service text: "<hostname> changed into state: <state> for service: <description>: <output>"
Host text: "<hostname> changed into state: <state> : <output>"
*/
// Load modules
// load pushbullet library
var PushBullet = require('pushbullet');
var Pushalot = require('pushalot-node');
// Load optimist, read/parse command line arguments
var argv = require('optimist')
.usage('Sends notifications to push notification services.\nUsage: $0 -A [address string] -H [hostname] -T [type] -D [description] -S [state] -O [output]')
.demand(['A', 'H', 'T', 'D', 'S', 'O'])
.argv;
/*
# TODO
* contact: extract adresses
* message: distinct between host and service
* message: send
*/
// Handle input parameters
var addresses = argv.A.split(",");
var host = argv.H;
var type = argv.T;
var description = argv.D;
var state = argv.S;
var output = argv.O;
// prepare the message
var noteTitle;
var noteBody;
if (type == 'host') {
noteTitle = 'Host status update';
noteBody = host.concat(' changed into state: ', state, ' : ', output);
} else if (type == 'service') {
noteTitle = 'Service status update';
noteBody = host.concat(' changed into state: ', state, ' for service: ', description, ': ', output);
} else {
noteTitle = 'unknown type';
noteBody = host.concat(' changed into state: ', state, ' for : ', description, ': ', output);
}
// console.log("title: %s", noteTitle);
// console.log("body: %s", noteBody);
// Iterate over addresses
addresses.map( function(address) {
// scanning the addresses one by one
if (address) {
// console.log("we've got an address");
// console.log("address: %s", address);
// servicename is in the part before :
var parts = address.split(":");
var service = parts[0];
var destination = parts[1];
console.log("service: %s", service);
console.log("destination: %s", destination);
if (service == 'pushbullet') {
// we've got a winner
var s = destination.split("!");
var apikey = s[0];
var deviceId = s[1];
console.log("apikey: %s\ndevice: %s\n", apikey, deviceId);
// we should push the message now
var pusher = new PushBullet(apikey);
// push message to given device
pusher.note(deviceId, noteTitle, noteBody, function(error, response) {
if (error) {
console.log("error: %j", error);
} else {
console.dir(response);
}
});
} else if (service == 'pushalot') {
var apikey = destination;
var pushalotClient = new Pushalot(apikey);
pushalotClient.push(noteBody,noteTitle)
.on('succes', function ( code , res) {
console.log("code: %j", code);
console.log("res : %j", res);
})
.on('error', function (code,res) {
console.log("code: %j", code);
console.log("res : %j", res);
});
}
}
});