This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
quickstatus/system.js

89 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-03-01 19:44:08 +00:00
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 () {
2019-03-01 20:00:53 +00:00
let cpus = os.cpus()
2019-03-01 19:44:08 +00:00
return {
2019-03-01 20:00:53 +00:00
model: cpus[0].model,
2019-03-01 19:44:08 +00:00
usage: await osu.cpu.usage(),
loadAverage: os.loadavg(),
2019-03-01 20:00:53 +00:00
count: cpus.length,
2019-03-01 19:44:08 +00:00
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)))