Skip to content

Instantly share code, notes, and snippets.

@zr0n
Created August 30, 2017 06:02
Show Gist options
  • Save zr0n/2a51fcaa7cb8413cbdd6c19f3ccf8b9e to your computer and use it in GitHub Desktop.
Save zr0n/2a51fcaa7cb8413cbdd6c19f3ccf8b9e to your computer and use it in GitHub Desktop.
Verify cpu and memory usage and restart services to free memory/cpu
const os = require('os-utils');
const exec = require("child_process").exec;
const DEFAULT_INTERVAL_CHECK = 1000
const DEFAULT_MAX_CPU_USAGE = 80
const RESTART_COMMAND = "pm2 restart all"
const DEFAULT_MINIMUM_MEMORY = 200
let maxCpuUsage = DEFAULT_MAX_CPU_USAGE
let intervalCheck = DEFAULT_INTERVAL_CHECK
let minimumMemory = DEFAULT_MINIMUM_MEMORY
setInterval(() => {
os.cpuUsage(function(v){
const cpuUsage = v * 100
console.log("Cpu Usage: " + Math.round(cpuUsage) + "%")
if(cpuUsage >= maxCpuUsage){
exec(RESTART_COMMAND, function(err, stdout, stderr) {
console.log("Reseting server. Cpu Leak");
});
}
});
const freeMemory = os.freemem()
console.log("Free memory: ", freeMemory)
if(minimumMemory > freeMemory){
exec(RESTART_COMMAND, function(err, stdout, stderr) {
console.log("Reseting server. Memory leak");
});
}
}, intervalCheck)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment