Last active
December 18, 2015 06:08
-
-
Save mudkipme/5737462 to your computer and use it in GitHub Desktop.
Monitors mutiple services on 52Poké server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exec = require('child_process').exec, | |
fs = require('fs'), | |
async = require('async'); | |
var services = [ 'php-fpm', 'nginx', 'mysqld', ['mongod', 'mongodb'], ['redis-server', 'redis'] ]; | |
var logFile = '/home/52poke/logs/monitor.log'; | |
var actions = []; | |
var addLog = function(str){ | |
fs.appendFileSync(logFile, new Date().toString()+ str + '\n'); | |
}; | |
services.forEach(function(service){ | |
var serv = service; | |
if (!Array.isArray(service)) { | |
serv = [service, service]; | |
} | |
actions.push(function(callback){ | |
exec('pgrep ' + serv[0], function(err, stdout, stderr){ | |
if (!stdout || !stdout.trim()) { | |
addLog(serv[1] + ' is not working!!! Restarting...'); | |
exec('systemctl restart ' + serv[1], function(){ | |
callback(); | |
}); | |
} else { | |
callback(); | |
} | |
}); | |
}); | |
}); | |
var monitor = function(){ | |
async.series(actions, function(){ | |
setTimeout(monitor, 10000); | |
}); | |
}; | |
monitor(); | |
fs.appendFileSync(logFile, new Date().toString()+' Monitor started.\n'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment