Browse Source

Add hostname and slave support

isundil 3 months ago
parent
commit
0b71b01466

+ 11 - 1
src/config.ts

@@ -1,17 +1,27 @@
 import path from "path";
+import os from 'os';
 
 export interface Configuration {
     port: number;
     slave: boolean;
+    slaves: Array<string>;
+    hostname: string;
 }
 
 class ConfigurationManagerLoader {
     public static Load(fileName: string): Configuration {
         let config = require(fileName);
 
+        if (config.slave && config.slaves?.length) {
+            throw new Error("Cannot have slaves while beeing slave");
+        }
+
         return {
             port: config.port || 9090,
-            slave: config.slave || false
+            slave: config.slave || false,
+            slaves: config.slaves || new Array(),
+
+            hostname: os.hostname()
         };
     }
 }

+ 9 - 0
src/models/service.ts

@@ -17,3 +17,12 @@ export interface Service {
     statusText: string;
     description: string;
 }
+
+export interface HostnameServiceDescription {
+    [key: string]: Array<Service>
+}
+
+export interface ServiceDescription {
+    services: HostnameServiceDescription
+}
+

+ 17 - 0
src/models/systemInfo.ts

@@ -1,3 +1,4 @@
+import {HostnameData} from "./service";
 
 export interface CpuInfo {
     model: string;
@@ -65,3 +66,19 @@ export interface LiveSystemInfo {
     // FIXME temperature
 }
 
+export interface HostnameSystemInfoDescription {
+    [key: string]: SystemInfo
+}
+
+export interface SystemInfoDescription {
+    systemInfo: HostnameSystemInfoDescription
+}
+
+export interface HostnameLiveSystemInfoDescription {
+    [key: string]: LiveSystemInfo
+}
+
+export interface LiveSystemInfoDescription {
+    liveSystemInfo: HostnameLiveSystemInfoDescription
+}
+

+ 13 - 9
src/routes/api_sysInfoController.ts

@@ -3,10 +3,11 @@ import {
     Get,
     Route
 } from "tsoa";
-import {CpuInfo, DriveInfo, LiveSystemInfo, NetworkInfo, SystemInfo} from "../models/systemInfo";
+import {CpuInfo, DriveInfo, LiveSystemInfoDescription, NetworkInfo, SystemInfoDescription} from "../models/systemInfo";
 import os from 'os';
 import dns from 'dns';
 import si from 'systeminformation';
+import ConfigurationManager from "../config";
 
 @Route("/api/sysinfo")
 export class SystemdInfoController extends Controller {
@@ -59,9 +60,10 @@ export class SystemdInfoController extends Controller {
     }
 
     @Get("/")
-    public async getInfo(): Promise<SystemInfo> {
+    public async getInfo(): Promise<SystemInfoDescription> {
         const system = await si.system();
-        return {
+        let result: SystemInfoDescription = { systemInfo: {}};
+        result.systemInfo[ConfigurationManager.hostname] = {
             platform: os.platform(),
             osVersion: os.release(),
             arch: os.machine(),
@@ -78,8 +80,9 @@ export class SystemdInfoController extends Controller {
             manufacturer: system.manufacturer,
             model: system.model,
             drives: (await this.readDriveInfo())
-        }
-    };
+        };
+        return result;
+    }
 
     private getCpuUsage(): Promise<Array<number>> {
         return new Promise(ok => {
@@ -100,15 +103,16 @@ export class SystemdInfoController extends Controller {
     }
 
     @Get("/live")
-    public async getLiveInfo(): Promise<LiveSystemInfo> {
+    public async getLiveInfo(): Promise<LiveSystemInfoDescription> {
         const memInfo = await si.mem();
         const cpuSpeed = await this.getCpuUsage();
-
-        return <LiveSystemInfo> {
+        let result: LiveSystemInfoDescription = { liveSystemInfo: {}};
+        result.liveSystemInfo[ConfigurationManager.hostname] = {
             totalMemoryUsed: memInfo.used,
             activeMemory: memInfo.active,
             currentCpuUsed: cpuSpeed.reduce((a, b) => a +b, 0) /cpuSpeed.length,
             cpuByCore: cpuSpeed
-        }
+        };
+        return result;
     }
 }

+ 11 - 8
src/routes/api_systemdController.ts

@@ -1,16 +1,19 @@
 import {
-  Controller,
-  Get,
-  Route
+    Controller,
+    Get,
+    Route
 } from "tsoa";
-import {Service} from "../models/service";
 import systemdService from "../services/systemdService";
+import ConfigurationManager from "../config";
+import {ServiceDescription} from "../models/service";
 
 @Route("/api/services")
 export class SystemdServiceController extends Controller {
 
-  @Get("/list")
-  public async getAllServices(): Promise<Array<Service>> {
-    return systemdService.getAllServices()
-  };
+    @Get("/list")
+    public async getAllServices(): Promise<ServiceDescription> {
+        let result: ServiceDescription = { services: {}};
+        result.services[ConfigurationManager.hostname] = (await systemdService.getAllServices());
+        return result;
+    }
 }