This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
forked from iloire/watchmen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (78 loc) · 2.95 KB
/
server.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
var config = require('./config/general');
var email_service = require ('./lib/notifications/email/email');
var services = require('./lib/services').load_services();
//----------------------------------------------------
// Fetch storage
//----------------------------------------------------
var storage_factory = require('./lib/storage/storage_factory');
var storage = storage_factory.get_storage_instance();
//----------------------------------------------------
// Create watchmen instance
//----------------------------------------------------
var WatchMen = require('./lib/watchmen');
var watchmen = new WatchMen(services, storage);
//----------------------------------------------------
// Subscribe to service events
//----------------------------------------------------
watchmen.on('service_error', function(service, state) {
/*
//Do here any additional stuff when you get an error
*/
var info = service.url_info + ' down!. Error: ' + state.error + '. Retrying in ' +
(parseInt(state.next_attempt_secs, 10) / 60) + ' minute(s)..';
console.log (info);
if (state.prev_state.status === 'success' && config.notifications.enabled) {
email_service.sendEmail(
service.alert_to,
service.url_info + ' is down!',
service.url_info + ' is down!. Reason: ' + state.error
);
}
});
watchmen.on('service_warning', function(service, state) {
/*
//Do here any additional stuff when you get a warning
console.log (service.url_info + ' WARNING (' + state.elapsed_time + ' ms, avg: '
+ state.avg_response_time + ') ## ' + state.warnings + ' warnings');
*/
});
watchmen.on('service_back', function(service, state) {
if (config.notifications.enabled){
email_service.sendEmail(
service.alert_to,
service.url_info + ' is back!',
service.url_info + ' ' + service.msg
);
}
});
watchmen.on('service_ok', function(service, state) {
/*
//Do here any additional stuff when you get a successful response
console.log (service.url_info + ' responded OK! (' + state.elapsed_time + ' milliseconds, avg: '
+ state.avg_response_time + ')');
*/
});
//----------------------------------------------------
// Start watchmen
//----------------------------------------------------
watchmen.start();
//----------------------------------------------------
// Web server
//----------------------------------------------------
// You can launch the webserver in a separate process by doing: node webserver/app.js
// - or -
// you can just uncomment the following line to launch both the monitor and the web server
// in the same process:
// require('./webserver/app');
//----------------------------------------------------
// Error handling
//----------------------------------------------------
process.on('uncaughtException', function(err) {
console.error('uncaughtException:');
console.error(err);
});
process.on('SIGINT', function () {
console.log('stopping watchmen..');
storage.quit();
process.exit(0);
});