Add based query response system, info broker
The new InfoBroker class' job is to fetch system information and return it in a standardised format.
This commit is contained in:
parent
bee1ffc2de
commit
e63111e64f
2 changed files with 43 additions and 0 deletions
|
@ -3,10 +3,12 @@
|
|||
import sysinfo from 'systeminformation';
|
||||
|
||||
import Agent from './agent/Agent.mjs';
|
||||
import InfoBroker from './core/InfoBroker.mjs';
|
||||
|
||||
class SystemQuery {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
this.info = new InfoBroker();
|
||||
}
|
||||
|
||||
async init() {
|
||||
|
@ -24,7 +26,16 @@ class SystemQuery {
|
|||
}
|
||||
|
||||
async handle_query(peer, msg) {
|
||||
// 1: Validate input
|
||||
if(typeof msg.table !== "string"
|
||||
|| !this.info.is_valid_table(msg.table)) return;
|
||||
|
||||
// 2: Fetch system info
|
||||
let table = await this.info.fetch_table(msg.table);
|
||||
if(table === null) return;
|
||||
|
||||
// 3: Return to requester
|
||||
await peer.send("query-response", { result: table });
|
||||
}
|
||||
async handle_query_response(peer, msg) {
|
||||
|
||||
|
|
32
src/lib/core/InfoBroker.mjs
Normal file
32
src/lib/core/InfoBroker.mjs
Normal file
|
@ -0,0 +1,32 @@
|
|||
"use strict";
|
||||
|
||||
import sysinfo from 'systeminformation';
|
||||
|
||||
import log from '../../lib/io/NamespacedLog.mjs'; const l = log("infobroker");
|
||||
|
||||
class InfoBroker {
|
||||
constructor() {
|
||||
this.allowed_tables = {
|
||||
// name → sysinfo name
|
||||
cpu: "cpu",
|
||||
};
|
||||
}
|
||||
|
||||
is_valid_table(name) {
|
||||
return Object.keys(this.allowed_tables).includes(name);
|
||||
}
|
||||
|
||||
async fetch_table(name) {
|
||||
if(!(typeof name === "string"))
|
||||
throw new Exception(`Error: Expected name to be of type string, but received value of type ${typeof name} instead.`);
|
||||
|
||||
if(!this.is_valid_table(name)) {
|
||||
l.warn(`Unrecognised table '${name}' requested, returning null`);
|
||||
return null;
|
||||
}
|
||||
|
||||
return await sysinfo[this.allowed_tables[name]]();
|
||||
}
|
||||
}
|
||||
|
||||
export default InfoBroker;
|
Loading…
Reference in a new issue