Last active
December 17, 2015 08:59
-
-
Save mudkipme/5583702 to your computer and use it in GitHub Desktop.
Monitors memcached on 52Poké server, restart memcached when it's using too much CPU resource.
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'); | |
var errorCount = 0; | |
function getCPU(callback){ | |
exec('top -b -n 1 -p `pgrep memcached`', function(error, stdout, stderr){ | |
var lines = stdout ? stdout.split('\n') : [''], | |
str = lines[lines.length - 1].substring(47, 52); | |
if (str) { | |
callback(parseFloat(str)); | |
} else { | |
callback(-1); | |
} | |
}); | |
} | |
var monitor = function(){ | |
getCPU(function(result){ | |
if (result == -1) { | |
fs.appendFileSync('/home/52poke/logs/monitor.log', new Date().toString()+' Memcached error.\n'); | |
} if (result < 20) { | |
errorCount = 0; | |
} if (result > 35) { | |
fs.appendFileSync('/home/52poke/logs/monitor.log', new Date().toString()+' Memcached warning, CPU ' + result +'%.\n'); | |
errorCount++; | |
if (errorCount >= 5) { | |
fs.appendFileSync('/home/52poke/logs/monitor.log', new Date().toString()+' Memcached killed.\n'); | |
exec('killall memcached', function(error, stdout, stderr){ | |
setTimeout(monitor, 10000); | |
}); | |
return; | |
} | |
setTimeout(monitor, 5000); | |
return; | |
} | |
setTimeout(monitor, 10000); | |
}); | |
}; | |
fs.appendFileSync('/home/52poke/logs/monitor.log', new Date().toString()+' Monitor started.\n'); | |
monitor(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment