89 lines
1.7 KiB
JavaScript
89 lines
1.7 KiB
JavaScript
const fs = require('fs')
|
|
const util = require('util')
|
|
const os = require('os')
|
|
const osu = require('node-os-utils')
|
|
|
|
let disks = ['/']
|
|
|
|
// Get disks from environment variable
|
|
let denv = process.env.SHOW_DISKS
|
|
if (denv) {
|
|
try {
|
|
disks = JSON.parse(denv)
|
|
} catch (e) {
|
|
console.warn(e)
|
|
}
|
|
}
|
|
|
|
async function diskInfo () {
|
|
let result = {}
|
|
for (let i in disks) {
|
|
let info = await osu.drive.info(disks[i])
|
|
result[disks[i]] = info
|
|
}
|
|
return result
|
|
}
|
|
|
|
async function networkInfo () {
|
|
let interfaces = {}
|
|
let all = os.networkInterfaces()
|
|
for (let iface in all) {
|
|
let ips = all[iface]
|
|
interfaces[iface] = {
|
|
addresses: [],
|
|
upload: null,
|
|
download: null,
|
|
}
|
|
for (let i in ips) {
|
|
interfaces[iface].addresses.push(ips[i].address)
|
|
}
|
|
}
|
|
|
|
// Interface usage statistics
|
|
let data = await osu.netstat.stats()
|
|
for (let p in data) {
|
|
let ifa = data[p]
|
|
let dt = interfaces[ifa.interface]
|
|
if (!dt) continue
|
|
dt.upload = ifa.outputBytes
|
|
dt.download = ifa.inputBytes
|
|
}
|
|
|
|
return interfaces
|
|
}
|
|
|
|
async function cpuInfo () {
|
|
let cpus = os.cpus()
|
|
return {
|
|
model: cpus[0].model,
|
|
usage: await osu.cpu.usage(),
|
|
loadAverage: os.loadavg(),
|
|
count: cpus.length,
|
|
processes: await osu.proc.totalProcesses()
|
|
}
|
|
}
|
|
|
|
async function memInfo () {
|
|
let free = os.freemem()
|
|
let total = os.totalmem()
|
|
return {
|
|
free: free,
|
|
total: total,
|
|
usage: (total - free) / total * 100
|
|
}
|
|
}
|
|
|
|
async function all () {
|
|
return {
|
|
hostname: os.hostname(),
|
|
kernel: os.release(),
|
|
uptime: os.uptime(),
|
|
cpu: await cpuInfo(),
|
|
memory: await memInfo(),
|
|
network: await networkInfo(),
|
|
disk: await diskInfo()
|
|
}
|
|
}
|
|
|
|
all().then((a) => console.log(JSON.stringify(a)))
|