You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.1 KiB
JavaScript
92 lines
3.1 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
function ponyReadableToBytes(p) {
|
|
if (p.endsWith("B")) {
|
|
return parseInt(p.split("B")[0])
|
|
}
|
|
|
|
if (p.endsWith("K")) {
|
|
return parseInt(p.split("K")[0]) * 1024
|
|
}
|
|
|
|
if (p.endsWith("M")) {
|
|
return parseInt(p.split("M")[0]) * 1024**2
|
|
}
|
|
|
|
if (p.endsWith("G")) {
|
|
return parseInt(p.split("G")[0]) * 1024**3
|
|
}
|
|
|
|
if (p.endsWith("T")) {
|
|
return parseInt(p.split("T")[0]) * 1024**4
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
let parts;
|
|
try {
|
|
parts = [...new Set(require('child_process').execSync("lsblk", { stdio: ["ignore", "pipe", "ignore"] }).toString().trim().split("\n").map(i => i.trim().replace(/( )+/gm, " ").split(" ")).filter(i => i.length > 1).map(i => i[i.length - 1]).filter(i => i.startsWith("/")))].join(" ");
|
|
} catch (e) {
|
|
parts = "";
|
|
}
|
|
|
|
let disk;
|
|
try {
|
|
disk = require('child_process').execSync(`df -x tmpfs -x squashfs -x devtmpfs --total --output=source,fstype,size,used,avail,pcent,ipcent,iused,itotal,target ${parts}`, { stdio: ["ignore", "pipe", "ignore"] }).toString().trim().replace(/( )+/gm, " ").split("\n").map((i) => i.trim());
|
|
totalMem = require('os').totalmem();
|
|
freeMem = require('os').freemem();
|
|
} catch (e) {
|
|
try {
|
|
disk = require('child_process').execSync(`gdf -x tmpfs -x squashfs -x devtmpfs --total --output=source,fstype,size,used,avail,pcent,ipcent,iused,itotal,target ${parts}`, { stdio: ["ignore", "pipe", "ignore"] }).toString().trim().replace(/( )+/gm, " ").split("\n").map((i) => i.trim());
|
|
totalMem = require('os').totalmem();
|
|
freeMem = require('os').freemem();
|
|
} catch (e) {
|
|
disk = require('child_process').execSync(`df -c`, { stdio: ["ignore", "pipe", "ignore"] }).toString().trim().replace(/( )+/gm, " ").split("\n").map((i) => i.trim());
|
|
memParts = require('child_process').execSync("top -d1 | grep Mem:").toString().trim().split(" ").filter(i => i.endsWith("M")).map(i => ponyReadableToBytes(i));
|
|
totalMem = memParts[0] + memParts[1] + memParts[2] + memParts[3] + memParts[5];
|
|
freeMem = memParts[1] + memParts[2] + memParts[5]
|
|
}
|
|
}
|
|
|
|
let top;
|
|
try {
|
|
top = require('child_process').execSync("top -bn1 | grep \"Cpu(s)\" | sed \"s/.*, *\\([0-9.]*\\)%* id.*/\\1/\" | awk '{print 100 - $1\"%\"}'", { stdio: ["ignore", "pipe", "ignore"] }).toString().split("%")[0];
|
|
|
|
if (top === "") throw new Error();
|
|
} catch (e) {
|
|
top = require('child_process').execSync("top -b | grep \"CPU\" | sed \"s/.*, *\\([0-9.]*\\)%* id.*/\\1/\" | awk '{print 100 - $1\"%\"}'", { stdio: ["ignore", "pipe", "ignore"] }).toString().split("%")[0];
|
|
}
|
|
|
|
let info = disk[disk.length - 1].split(" ");
|
|
|
|
if (info.length === 5) {
|
|
info = [
|
|
info[0],
|
|
null,
|
|
info[1],
|
|
info[2],
|
|
info[3],
|
|
info[4],
|
|
null,
|
|
null,
|
|
null,
|
|
null
|
|
];
|
|
}
|
|
|
|
let data = {
|
|
ram: {
|
|
used: totalMem - freeMem,
|
|
total: totalMem
|
|
},
|
|
cpu: {
|
|
usage: parseFloat(top)
|
|
},
|
|
disk: {
|
|
used: parseInt(info[3]),
|
|
total: parseInt(info[2])
|
|
}
|
|
}
|
|
|
|
process.stdout.write(JSON.stringify(data)); |