-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_daemon.js
executable file
·60 lines (53 loc) · 1.47 KB
/
check_daemon.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
/*jslint node: true */
"use strict";
var child_process = require('child_process');
var conf = require('./conf.js');
var mail = require('./mail.js');
function checkDaemon(daemon_name, handleResult){
child_process.exec('ps x', function(err, stdout, stderr){
if (err)
throw Error('ps x failed: '+err);
if (stderr)
throw Error('ps x stderr: '+stderr);
var bFound = false;
stdout.split('\n').forEach(function(line){
if (line.indexOf(daemon_name) >= 0){
bFound = true;
write(line);
}
});
handleResult(bFound);
});
}
function checkDaemonAndNotify(daemon_name){
checkDaemon(daemon_name, function(bFound){
if (!bFound)
notifyAdmin('daemon '+daemon_name+' is down');
});
}
function checkDaemonAndRestart(daemon_name, start_command){
checkDaemon(daemon_name, function(bFound){
if (bFound)
return;
notifyAdmin('daemon '+daemon_name+' is down, trying to restart '+start_command);
child_process.exec(start_command).unref();
process.exit();
});
}
function notifyAdmin(message){
write(message);
if (!conf.admin_email || !conf.from_email)
return write('cannot notify admin as admin_email or from_email are not defined');
mail.sendmail({
to: conf.admin_email,
from: conf.from_email,
subject: message,
body: 'Check daemon:\n'+message
});
}
function write(str){
console.log(Date().toString()+': '+str);
}
exports.checkDaemon = checkDaemon;
exports.checkDaemonAndNotify = checkDaemonAndNotify;
exports.checkDaemonAndRestart = checkDaemonAndRestart;