|
|
@@ -0,0 +1,114 @@
|
|
|
+import {
|
|
|
+ Controller,
|
|
|
+ Get,
|
|
|
+ Route
|
|
|
+} from "tsoa";
|
|
|
+import {CpuInfo, DriveInfo, LiveSystemInfo, NetworkInfo, SystemInfo} from "../models/systemInfo";
|
|
|
+import os from 'os';
|
|
|
+import dns from 'dns';
|
|
|
+import si from 'systeminformation';
|
|
|
+
|
|
|
+@Route("/api/sysinfo")
|
|
|
+export class SystemdInfoController extends Controller {
|
|
|
+ private readCpuInfo(): Array<CpuInfo> {
|
|
|
+ let cpus = os.cpus();
|
|
|
+ return cpus.map((x: any) => <CpuInfo> {
|
|
|
+ model: x.model
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private readNetworkInfo(): Array<NetworkInfo> {
|
|
|
+ let networkInfo = os.networkInterfaces();
|
|
|
+ let result = new Array<NetworkInfo>();
|
|
|
+ for (let i in networkInfo) {
|
|
|
+ for (let j of networkInfo[i]!) {
|
|
|
+ result.push(<NetworkInfo>{
|
|
|
+ iface: i,
|
|
|
+ address: j.address,
|
|
|
+ netmask: j.netmask,
|
|
|
+ family: j.family,
|
|
|
+ mac: j.mac
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async readDriveInfo(): Promise<Array<DriveInfo>> {
|
|
|
+ const mountFsInfo = await si.fsSize();
|
|
|
+ return (await si.blockDevices()).map((x) => {
|
|
|
+ const mountInfo = x.mount ? mountFsInfo.find(fsInfo => x.mount === fsInfo.mount) : null;
|
|
|
+ return <DriveInfo> {
|
|
|
+ name: x.name,
|
|
|
+ type: x.type,
|
|
|
+ fsType: x.fsType,
|
|
|
+ mount: x.mount,
|
|
|
+ size: x.size, // FIXME used size
|
|
|
+ usedSize: mountInfo? mountInfo.used : undefined,
|
|
|
+ rw: mountInfo ? mountInfo.rw : undefined,
|
|
|
+ physical: x.physical,
|
|
|
+ uuid: x.uuid,
|
|
|
+ label: x.label,
|
|
|
+ model: x.model,
|
|
|
+ serial: x.serial,
|
|
|
+ removable: x.removable,
|
|
|
+ protocol: x.protocol,
|
|
|
+ device: x.device
|
|
|
+ };
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Get("/")
|
|
|
+ public async getInfo(): Promise<SystemInfo> {
|
|
|
+ const system = await si.system();
|
|
|
+ return {
|
|
|
+ platform: os.platform(),
|
|
|
+ osVersion: os.release(),
|
|
|
+ arch: os.machine(),
|
|
|
+ distribution: (await si.osInfo()).distro,
|
|
|
+ nodeVersion: process.version,
|
|
|
+ uptime: os.uptime(),
|
|
|
+ hostname: os.hostname(),
|
|
|
+ memory: os.totalmem(),
|
|
|
+ memoryLayout: (await si.memLayout()).map(x => x.size),
|
|
|
+ cpuMaxSpeed: (await si.cpu()).speedMax,
|
|
|
+ cpus: this.readCpuInfo(),
|
|
|
+ network: this.readNetworkInfo(),
|
|
|
+ dnsServers: dns.getServers(),
|
|
|
+ manufacturer: system.manufacturer,
|
|
|
+ model: system.model,
|
|
|
+ drives: (await this.readDriveInfo())
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ private getCpuUsage(): Promise<Array<number>> {
|
|
|
+ return new Promise(ok => {
|
|
|
+ const stats1 = os.cpus();
|
|
|
+ const getTotalTime = (cpuInfo: any) => cpuInfo.user + cpuInfo.nice + cpuInfo.sys + cpuInfo.idle + cpuInfo.irq;
|
|
|
+
|
|
|
+ setTimeout(function() {
|
|
|
+ const stats2 = os.cpus();
|
|
|
+ let cores = [];
|
|
|
+ for (var i =0; i < stats1.length; ++i)
|
|
|
+ cores.push({
|
|
|
+ total: getTotalTime(stats2[i]!.times) - getTotalTime(stats1[i]!.times),
|
|
|
+ idle: stats2[i]!.times.idle - stats1[i]!.times.idle
|
|
|
+ });
|
|
|
+ ok(cores.map(x => 100-(100 * x.idle / x.total)).map(x => Math.round(x*100)/100));
|
|
|
+ }, 500 );
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Get("/live")
|
|
|
+ public async getLiveInfo(): Promise<LiveSystemInfo> {
|
|
|
+ const memInfo = await si.mem();
|
|
|
+ const cpuSpeed = await this.getCpuUsage();
|
|
|
+
|
|
|
+ return <LiveSystemInfo> {
|
|
|
+ totalMemoryUsed: memInfo.used,
|
|
|
+ activeMemory: memInfo.active,
|
|
|
+ currentCpuUsed: cpuSpeed.reduce((a, b) => a +b, 0) /cpuSpeed.length,
|
|
|
+ cpuByCore: cpuSpeed
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|