99 lines
4.3 KiB
JavaScript
Executable File
99 lines
4.3 KiB
JavaScript
Executable File
const route = require("express").Router();
|
|
const drivelist = require("drivelist");
|
|
const diskuse = require("diskusage");
|
|
// const processList = require("process-list");
|
|
const os = require("os");
|
|
const ostype = os.type();
|
|
const {exec} = require("child_process");
|
|
const lstdrvs = ()=>new Promise((res, rej)=>{
|
|
drivelist.list().then((list)=>{
|
|
res(list.map((item)=>{
|
|
item.mountpoints = item.mountpoints.map((mountpoint)=>{
|
|
mountpoint.usage = diskuse.checkSync(mountpoint.path)
|
|
return mountpoint
|
|
})
|
|
return item
|
|
}))
|
|
}, ()=>{
|
|
rej({error: 500, message:"failed to list drives"})
|
|
})
|
|
})
|
|
|
|
route.get("/driveinfo", (erq, res)=>{
|
|
if(ostype=="Linux"){
|
|
exec(`lsblk -l -o name,rota,mountpoint`, (err, out, serr)=>{
|
|
if(err){
|
|
res.status(500).json("couldn't get drive types, make sure your machine can execute lsblk")
|
|
}else{
|
|
const drives = out.split("\n").filter((line)=>!line.includes("loop")).slice(1).map((line)=>line.split(" ").filter((seg)=>seg!=""))
|
|
lstdrvs().then((drvs)=>{
|
|
res.json(drvs.map((drive)=>{
|
|
const drv = drives.find((drv)=>drive.device.match(drv[0]));
|
|
drive.isSSD = drv?drv[1]=="0":undefined
|
|
const parts = drives.filter((drv)=>drv[0]&&drv[0].includes(drive.device.replace("/dev/", ""))&&drv[0]!=drive.device.replace("/dev/", ""));
|
|
drive.mountpoints = drive.mountpoints.map((mountpoint)=>{
|
|
// console.log(mountpoint, parts)
|
|
const part = parts.find((part)=>part[2]&&part[2]==mountpoint.path);
|
|
mountpoint.dev = part[0]
|
|
return mountpoint
|
|
})
|
|
drive.mountpoints.push(...parts.filter((part)=>!part[2]).map((part)=>({path:"", dev:part[0], usage:{total:0, available:0, free:0}})))
|
|
return drive
|
|
}))
|
|
})
|
|
}
|
|
})
|
|
|
|
}else if (ostype=="Windows_NT"){
|
|
exec("Get-PhysicalDisk", {shell:"powershell.exe"}, (err, out, serr)=>{
|
|
if(err){
|
|
res.status(500).json("couldn't get drive types, make sure your machine can execute lsblk")
|
|
}else{
|
|
const drives = out.split("\n").slice(2).map((line)=>line.split(" ").filter((seg)=>seg!=""))
|
|
lstdrvs().then((drvs)=>{
|
|
res.json(drvs.map((drive)=>{
|
|
const drv = drives.find((drv)=>drive.description.match(drv[1]));
|
|
// console.log(drive.description, drv[6])
|
|
drive.isSSD = drv?drv.includes("SSD"):undefined
|
|
return drive
|
|
}))
|
|
})
|
|
}
|
|
})
|
|
}
|
|
route.get("/system", (req, res)=>{
|
|
const sendobj = {};
|
|
sendobj.cpus = os.cpus();
|
|
sendobj.net = os.networkInterfaces();
|
|
sendobj.memory = {total: os.totalmem(), free: os.freemem()}
|
|
sendobj.load = os.loadavg()
|
|
sendobj.uptime = os.uptime();
|
|
exec(ostype=="Linux"?"cat /sys/class/thermal/thermal_zone0/temp":'(Get-WMIObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2").HighPrecisionTemperature', {shell:ostype=="Linux"?"/bin/bash":"powershell.exe"}, (err, out, serr)=>{
|
|
if(!err){
|
|
sendobj.temp = parseInt(out.trim())
|
|
}
|
|
res.json(sendobj)
|
|
})
|
|
})
|
|
// drivelist.list().then((list)=>{
|
|
// res.json(list.map((item)=>{
|
|
// // console.log(item.mountpoints[0].path)
|
|
// const used = diskuse.checkSync(item.mountpoints[0].path);
|
|
// // console.log(used);
|
|
// item.used = used
|
|
// return item
|
|
// }));
|
|
// }, (error)=>{
|
|
// console.log(error);
|
|
// res.status(500).json("can't get drive list")
|
|
// })
|
|
})
|
|
// route.get("/processes", (req, res)=>{
|
|
// processList.snapshot("pid", "name", "path", "owner", "starttime", "pmem", "cpu").then((list)=>{
|
|
// res.json(list)
|
|
// }, (error)=>{
|
|
// console.log(error);
|
|
// res.status(500).json("can't get process list")
|
|
// })
|
|
// })
|
|
module.exports = route |