Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getStats() {
var procMem = process.memoryUsage();
var stats = {
mem: {
total: os.totalmem(),
free: os.freemem(),
heap: procMem.rss
},
cpu: os.loadavg()
};
for(var i=0; i < stats.cpu.length; i++) {
stats.cpu[i] = stats.cpu[i].toFixed(3);
}
stats.cpu = stats.cpu.join(",");
// We convert from bytes to mb
stats.mem.total = Math.ceil(stats.mem.total / 1000000) + "MB";
stats.mem.free = Math.ceil(stats.mem.free / 1000000) + "MB";
stats.mem.heap = (stats.mem.heap / 1000000).toFixed(3) + "MB";
return stats;
}
getUsage: function (request) {
const health = {
cpu_load: os.loadavg(),
mem_free: os.freemem(),
mem_free_percent: os.freemem() / os.totalmem(),
mem_total: os.totalmem(),
os_uptime: os.uptime()
}
if (typeof request.query.h !== 'undefined') {
// make it human friendly
health.mem_free_percent = health.mem_free_percent.toFixed(2) + '%'
health.mem_free = prettyBytes(health.mem_free)
health.mem_total = prettyBytes(health.mem_total)
health.os_uptime = humanize((health.os_uptime * 1000), {
delimiter: ', ',
language: opt.lang
})
}
return {
health: health
},
resident_set_size_in_bytes: get(hapiEvent, 'psmem.rss'),
},
event_loop_delay: get(hapiEvent, 'psdelay'),
pid: process.pid,
uptime_in_millis: process.uptime() * 1000,
},
os: {
load: {
'1m': get(hapiEvent, 'osload.0'),
'5m': get(hapiEvent, 'osload.1'),
'15m': get(hapiEvent, 'osload.2'),
},
memory: {
total_in_bytes: os.totalmem(),
free_in_bytes: os.freemem(),
used_in_bytes: get(hapiEvent, 'osmem.total') - get(hapiEvent, 'osmem.free'),
},
uptime_in_millis: os.uptime() * 1000,
...(await getOSInfo()),
},
response_times: {
avg_in_millis: isNaN(avgInMillis) ? undefined : avgInMillis, // convert NaN to undefined
max_in_millis: maxInMillis,
},
requests: {
...requestDefaults,
...keysToSnakeCaseShallow(get(hapiEvent, ['requests', port])),
},
concurrent_connections: hapiEvent.concurrent_connections,
};
}
getStats() {
// core stats
const stats: any = {
memory: {
total: (((os.totalmem() / 1024) / 1024) / 1024).toFixed(2),
used: ((((os.totalmem() - os.freemem()) / 1024) / 1024) / 1024).toFixed(2),
free: (((os.freemem() / 1024) / 1024) / 1024).toFixed(2)
}
};
stats.cpu = (os.platform() === 'win32') ? null : (os.loadavg()[0] * 100 / os.cpus().length).toFixed(2);
// server uptime
const uptime: any = {
delta: Math.floor(os.uptime())
};
uptime.days = Math.floor(uptime.delta / 86400);
uptime.delta -= uptime.days * 86400;
uptime.hours = Math.floor(uptime.delta / 3600) % 24;
uptime.delta -= uptime.hours * 3600;
uptime.minutes = Math.floor(uptime.delta / 60) % 60;
bytes(memoryUsage.rss),
bytes(memoryUsage.heapTotal),
bytes(memoryUsage.heapUsed),
bytes(memoryUsage.external || 0),
bytes(os.freemem()),
loads,
Math.floor(cpuUsage.user / 1000 / 1000),
Math.floor(cpuUsage.system / 1000 / 1000));
} else {
options.console.warn('[trace] %s 🏊 memory usage, rss: %s, heapTotal: %s, heapUsed: %s, external: %s; 💻 os free: %s, os load: %s',
ms(Date.now() - startTime),
bytes(memoryUsage.rss),
bytes(memoryUsage.heapTotal),
bytes(memoryUsage.heapUsed),
bytes(memoryUsage.external || 0),
bytes(os.freemem()),
loads);
}
};
traceTimer = setInterval(showTrace, 1000);
setInterval(function(){
var memoryUsage = process.memoryUsage();
var loadAvg = os.loadavg();
var msgs = [
'rss:' + format(memoryUsage.rss),
'heapTotal:' + format(memoryUsage.heapTotal),
'heapUsed:' + format(memoryUsage.heapUsed),
'freeMemory:' + format(os.freemem()),
'loadAvg:' + loadAvg[0].toFixed(1) + ',' + loadAvg[1].toFixed(1) + ',' + loadAvg[2].toFixed(2)
];
self.write(msgs);
}, C('log_memory_interval'))
},
start () {
this._startTime = Date.now()
this._startFreeMemory = os.freemem()
}
getSystemInfo() {
const arch = os.arch();
const cpus = os.cpus();
const freemem = os.freemem();
const type = os.type();
const hostname = os.hostname();
const release = os.release();
const totalmem = os.totalmem();
return {
arch,
cpus,
type,
freemem,
totalmem,
release,
hostname,
};
}
}
setInterval(function() {
//returns [0,0,0] on windows
var load = os.loadavg();
load1minGauge.set(load[0]);
load5minGauge.set(load[1]);
load15minGauge.set(load[2]);
var free = os.freemem();
var usedMem = os.totalmem() - free;
usedMemoryBytesGauge.set(usedMem);
freeMemoryBytesGauge.set(free);
}, 60000); //every 60 secs
}
.then(function(freebytes) {
return {
json: {
columns: namedNumbers,
disk: freebytes,
loadavg: os.loadavg(),
totalmem: os.totalmem(),
freemem: os.freemem()
}
};
});
});