refactor: manage monitor via gateway hooks
This commit is contained in:
35
plugin/core/config.ts
Normal file
35
plugin/core/config.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { hostname } from 'os';
|
||||
|
||||
export interface HarborForgePluginConfig {
|
||||
enabled?: boolean;
|
||||
backendUrl?: string;
|
||||
identifier?: string;
|
||||
apiKey?: string;
|
||||
monitor_port?: number;
|
||||
reportIntervalSec?: number;
|
||||
httpFallbackIntervalSec?: number;
|
||||
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
||||
calendarEnabled?: boolean;
|
||||
calendarHeartbeatIntervalSec?: number;
|
||||
calendarApiKey?: string;
|
||||
managedMonitor?: string;
|
||||
}
|
||||
|
||||
interface PluginApiLike {
|
||||
pluginConfig?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function getPluginConfig(api: PluginApiLike): HarborForgePluginConfig {
|
||||
const cfg = (api.pluginConfig || {}) as HarborForgePluginConfig;
|
||||
return {
|
||||
enabled: true,
|
||||
backendUrl: 'https://monitor.hangman-lab.top',
|
||||
identifier: hostname(),
|
||||
reportIntervalSec: 30,
|
||||
httpFallbackIntervalSec: 60,
|
||||
logLevel: 'info',
|
||||
calendarEnabled: true,
|
||||
calendarHeartbeatIntervalSec: 60,
|
||||
...cfg,
|
||||
};
|
||||
}
|
||||
17
plugin/core/live-config.d.ts
vendored
17
plugin/core/live-config.d.ts
vendored
@@ -1,17 +0,0 @@
|
||||
export interface HarborForgeMonitorConfig {
|
||||
enabled?: boolean;
|
||||
backendUrl?: string;
|
||||
identifier?: string;
|
||||
apiKey?: string;
|
||||
monitor_port?: number;
|
||||
monitorPort?: number;
|
||||
reportIntervalSec?: number;
|
||||
httpFallbackIntervalSec?: number;
|
||||
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
||||
}
|
||||
interface OpenClawPluginApiLike {
|
||||
config?: Record<string, unknown>;
|
||||
}
|
||||
export declare function getLivePluginConfig(api: OpenClawPluginApiLike, fallback: HarborForgeMonitorConfig): HarborForgeMonitorConfig;
|
||||
export {};
|
||||
//# sourceMappingURL=live-config.d.ts.map
|
||||
@@ -1 +0,0 @@
|
||||
{"version":3,"file":"live-config.d.ts","sourceRoot":"","sources":["live-config.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,uBAAuB,CAAC,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;CAChD;AAED,UAAU,qBAAqB;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,qBAAqB,EAC1B,QAAQ,EAAE,wBAAwB,GACjC,wBAAwB,CAgC1B"}
|
||||
@@ -1,32 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getLivePluginConfig = getLivePluginConfig;
|
||||
function getLivePluginConfig(api, fallback) {
|
||||
const root = api.config || {};
|
||||
const plugins = root.plugins || {};
|
||||
const entries = plugins.entries || {};
|
||||
const entry = entries['harbor-forge'] || {};
|
||||
const cfg = entry.config || {};
|
||||
if (Object.keys(cfg).length > 0 || Object.keys(entry).length > 0) {
|
||||
const monitorPort = typeof cfg.monitor_port === 'number'
|
||||
? cfg.monitor_port
|
||||
: typeof cfg.monitorPort === 'number'
|
||||
? cfg.monitorPort
|
||||
: typeof fallback.monitor_port === 'number'
|
||||
? fallback.monitor_port
|
||||
: fallback.monitorPort;
|
||||
return {
|
||||
...fallback,
|
||||
...cfg,
|
||||
monitor_port: monitorPort,
|
||||
monitorPort,
|
||||
enabled: typeof cfg.enabled === 'boolean'
|
||||
? cfg.enabled
|
||||
: typeof entry.enabled === 'boolean'
|
||||
? entry.enabled
|
||||
: fallback.enabled,
|
||||
};
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
//# sourceMappingURL=live-config.js.map
|
||||
@@ -1 +0,0 @@
|
||||
{"version":3,"file":"live-config.js","sourceRoot":"","sources":["live-config.ts"],"names":[],"mappings":";;AAgBA,kDAmCC;AAnCD,SAAgB,mBAAmB,CACjC,GAA0B,EAC1B,QAAkC;IAElC,MAAM,IAAI,GAAI,GAAG,CAAC,MAAkC,IAAI,EAAE,CAAC;IAC3D,MAAM,OAAO,GAAI,IAAI,CAAC,OAAmC,IAAI,EAAE,CAAC;IAChE,MAAM,OAAO,GAAI,OAAO,CAAC,OAAmC,IAAI,EAAE,CAAC;IACnE,MAAM,KAAK,GAAI,OAAO,CAAC,cAAc,CAA6B,IAAI,EAAE,CAAC;IACzE,MAAM,GAAG,GAAI,KAAK,CAAC,MAAkC,IAAI,EAAE,CAAC;IAE5D,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACjE,MAAM,WAAW,GACf,OAAO,GAAG,CAAC,YAAY,KAAK,QAAQ;YAClC,CAAC,CAAC,GAAG,CAAC,YAAY;YAClB,CAAC,CAAC,OAAO,GAAG,CAAC,WAAW,KAAK,QAAQ;gBACnC,CAAC,CAAC,GAAG,CAAC,WAAW;gBACjB,CAAC,CAAC,OAAO,QAAQ,CAAC,YAAY,KAAK,QAAQ;oBACzC,CAAC,CAAC,QAAQ,CAAC,YAAY;oBACvB,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC;QAE/B,OAAO;YACL,GAAG,QAAQ;YACX,GAAG,GAAG;YACN,YAAY,EAAE,WAAW;YACzB,WAAW;YACX,OAAO,EACL,OAAO,GAAG,CAAC,OAAO,KAAK,SAAS;gBAC9B,CAAC,CAAC,GAAG,CAAC,OAAO;gBACb,CAAC,CAAC,OAAO,KAAK,CAAC,OAAO,KAAK,SAAS;oBAClC,CAAC,CAAC,KAAK,CAAC,OAAO;oBACf,CAAC,CAAC,QAAQ,CAAC,OAAO;SACG,CAAC;IAChC,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
||||
@@ -1,52 +0,0 @@
|
||||
export interface HarborForgeMonitorConfig {
|
||||
enabled?: boolean;
|
||||
backendUrl?: string;
|
||||
identifier?: string;
|
||||
apiKey?: string;
|
||||
monitor_port?: number;
|
||||
monitorPort?: number;
|
||||
reportIntervalSec?: number;
|
||||
httpFallbackIntervalSec?: number;
|
||||
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
||||
}
|
||||
|
||||
interface OpenClawPluginApiLike {
|
||||
config?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function getLivePluginConfig(
|
||||
api: OpenClawPluginApiLike,
|
||||
fallback: HarborForgeMonitorConfig
|
||||
): HarborForgeMonitorConfig {
|
||||
const root = (api.config as Record<string, unknown>) || {};
|
||||
const plugins = (root.plugins as Record<string, unknown>) || {};
|
||||
const entries = (plugins.entries as Record<string, unknown>) || {};
|
||||
const entry = (entries['harbor-forge'] as Record<string, unknown>) || {};
|
||||
const cfg = (entry.config as Record<string, unknown>) || {};
|
||||
|
||||
if (Object.keys(cfg).length > 0 || Object.keys(entry).length > 0) {
|
||||
const monitorPort =
|
||||
typeof cfg.monitor_port === 'number'
|
||||
? cfg.monitor_port
|
||||
: typeof cfg.monitorPort === 'number'
|
||||
? cfg.monitorPort
|
||||
: typeof fallback.monitor_port === 'number'
|
||||
? fallback.monitor_port
|
||||
: fallback.monitorPort;
|
||||
|
||||
return {
|
||||
...fallback,
|
||||
...cfg,
|
||||
monitor_port: monitorPort,
|
||||
monitorPort,
|
||||
enabled:
|
||||
typeof cfg.enabled === 'boolean'
|
||||
? cfg.enabled
|
||||
: typeof entry.enabled === 'boolean'
|
||||
? entry.enabled
|
||||
: fallback.enabled,
|
||||
} as HarborForgeMonitorConfig;
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
51
plugin/core/managed-monitor.ts
Normal file
51
plugin/core/managed-monitor.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { spawn, type ChildProcess } from 'child_process';
|
||||
import { existsSync } from 'fs';
|
||||
|
||||
export interface ManagedMonitorConfig {
|
||||
managedMonitor?: string;
|
||||
monitor_port?: number;
|
||||
logLevel?: string;
|
||||
}
|
||||
|
||||
let monitorProcess: ChildProcess | null = null;
|
||||
|
||||
export function startManagedMonitor(
|
||||
logger: { info: (...args: any[]) => void; warn: (...args: any[]) => void; error: (...args: any[]) => void },
|
||||
config: ManagedMonitorConfig,
|
||||
): void {
|
||||
if (!config.managedMonitor) return;
|
||||
if (monitorProcess) {
|
||||
logger.info('HarborForge managed monitor already running');
|
||||
return;
|
||||
}
|
||||
if (!existsSync(config.managedMonitor)) {
|
||||
logger.warn(`HarborForge managed monitor path not found: ${config.managedMonitor}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const args: string[] = [];
|
||||
if (config.monitor_port) args.push('--port', String(config.monitor_port));
|
||||
if (config.logLevel) args.push('--log-level', String(config.logLevel));
|
||||
|
||||
monitorProcess = spawn(config.managedMonitor, args, {
|
||||
stdio: 'inherit',
|
||||
detached: false,
|
||||
});
|
||||
|
||||
monitorProcess.on('exit', (code, signal) => {
|
||||
logger.warn(`HarborForge managed monitor exited code=${code ?? 'null'} signal=${signal ?? 'null'}`);
|
||||
monitorProcess = null;
|
||||
});
|
||||
|
||||
logger.info(`HarborForge managed monitor started: ${config.managedMonitor}`);
|
||||
}
|
||||
|
||||
export function stopManagedMonitor(
|
||||
logger: { info: (...args: any[]) => void; warn: (...args: any[]) => void },
|
||||
): void {
|
||||
if (!monitorProcess) return;
|
||||
const pid = monitorProcess.pid;
|
||||
monitorProcess.kill('SIGTERM');
|
||||
monitorProcess = null;
|
||||
logger.info(`HarborForge managed monitor stopped pid=${pid ?? 'unknown'}`);
|
||||
}
|
||||
55
plugin/core/monitor-bridge.d.ts
vendored
55
plugin/core/monitor-bridge.d.ts
vendored
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
* Monitor Bridge Client
|
||||
*
|
||||
* Queries the local HarborForge.Monitor bridge endpoint on MONITOR_PORT
|
||||
* to enrich plugin telemetry with host/hardware data.
|
||||
*
|
||||
* If the bridge is unreachable, all methods return null gracefully —
|
||||
* the plugin continues to function without Monitor data.
|
||||
*/
|
||||
export interface MonitorHealth {
|
||||
status: string;
|
||||
monitor_version: string;
|
||||
identifier: string;
|
||||
}
|
||||
export interface MonitorTelemetryResponse {
|
||||
status: string;
|
||||
monitor_version: string;
|
||||
identifier: string;
|
||||
telemetry?: {
|
||||
identifier: string;
|
||||
plugin_version: string;
|
||||
cpu_pct: number;
|
||||
mem_pct: number;
|
||||
disk_pct: number;
|
||||
swap_pct: number;
|
||||
load_avg: number[];
|
||||
uptime_seconds: number;
|
||||
nginx_installed: boolean;
|
||||
nginx_sites: string[];
|
||||
};
|
||||
last_updated?: string;
|
||||
}
|
||||
export declare class MonitorBridgeClient {
|
||||
private baseUrl;
|
||||
private timeoutMs;
|
||||
constructor(port: number, timeoutMs?: number);
|
||||
health(): Promise<MonitorHealth | null>;
|
||||
telemetry(): Promise<MonitorTelemetryResponse | null>;
|
||||
/**
|
||||
* POST OpenClaw metadata to the Monitor bridge so it can enrich
|
||||
* its heartbeat uploads with OpenClaw version, plugin version,
|
||||
* and agent information.
|
||||
*/
|
||||
pushOpenClawMeta(meta: OpenClawMeta): Promise<boolean>;
|
||||
private fetchJson;
|
||||
}
|
||||
/**
|
||||
* OpenClaw metadata payload sent to the Monitor bridge.
|
||||
*/
|
||||
export interface OpenClawMeta {
|
||||
version: string;
|
||||
plugin_version: string;
|
||||
agents?: any[];
|
||||
}
|
||||
//# sourceMappingURL=monitor-bridge.d.ts.map
|
||||
@@ -1 +0,0 @@
|
||||
{"version":3,"file":"monitor-bridge.d.ts","sourceRoot":"","sources":["monitor-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE;QACV,UAAU,EAAE,MAAM,CAAC;QACnB,cAAc,EAAE,MAAM,CAAC;QACvB,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,EAAE,CAAC;QACnB,cAAc,EAAE,MAAM,CAAC;QACvB,eAAe,EAAE,OAAO,CAAC;QACzB,WAAW,EAAE,MAAM,EAAE,CAAC;KACvB,CAAC;IACF,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAS;gBAEd,IAAI,EAAE,MAAM,EAAE,SAAS,SAAO;IAKpC,MAAM,IAAI,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAIvC,SAAS,IAAI,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC;IAI3D;;;;OAIG;IACG,gBAAgB,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC;YAmB9C,SAAS;CAgBxB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,EAAE,MAAM,CAAC;IACvB,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC;CAChB"}
|
||||
@@ -1,66 +0,0 @@
|
||||
"use strict";
|
||||
/**
|
||||
* Monitor Bridge Client
|
||||
*
|
||||
* Queries the local HarborForge.Monitor bridge endpoint on MONITOR_PORT
|
||||
* to enrich plugin telemetry with host/hardware data.
|
||||
*
|
||||
* If the bridge is unreachable, all methods return null gracefully —
|
||||
* the plugin continues to function without Monitor data.
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.MonitorBridgeClient = void 0;
|
||||
class MonitorBridgeClient {
|
||||
baseUrl;
|
||||
timeoutMs;
|
||||
constructor(port, timeoutMs = 3000) {
|
||||
this.baseUrl = `http://127.0.0.1:${port}`;
|
||||
this.timeoutMs = timeoutMs;
|
||||
}
|
||||
async health() {
|
||||
return this.fetchJson('/health');
|
||||
}
|
||||
async telemetry() {
|
||||
return this.fetchJson('/telemetry');
|
||||
}
|
||||
/**
|
||||
* POST OpenClaw metadata to the Monitor bridge so it can enrich
|
||||
* its heartbeat uploads with OpenClaw version, plugin version,
|
||||
* and agent information.
|
||||
*/
|
||||
async pushOpenClawMeta(meta) {
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
|
||||
const response = await fetch(`${this.baseUrl}/openclaw`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(meta),
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
return response.ok;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
async fetchJson(path) {
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
|
||||
const response = await fetch(`${this.baseUrl}${path}`, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
if (!response.ok)
|
||||
return null;
|
||||
return (await response.json());
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.MonitorBridgeClient = MonitorBridgeClient;
|
||||
//# sourceMappingURL=monitor-bridge.js.map
|
||||
@@ -1 +0,0 @@
|
||||
{"version":3,"file":"monitor-bridge.js","sourceRoot":"","sources":["monitor-bridge.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AA2BH,MAAa,mBAAmB;IACtB,OAAO,CAAS;IAChB,SAAS,CAAS;IAE1B,YAAY,IAAY,EAAE,SAAS,GAAG,IAAI;QACxC,IAAI,CAAC,OAAO,GAAG,oBAAoB,IAAI,EAAE,CAAC;QAC1C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED,KAAK,CAAC,MAAM;QACV,OAAO,IAAI,CAAC,SAAS,CAAgB,SAAS,CAAC,CAAC;IAClD,CAAC;IAED,KAAK,CAAC,SAAS;QACb,OAAO,IAAI,CAAC,SAAS,CAA2B,YAAY,CAAC,CAAC;IAChE,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,gBAAgB,CAAC,IAAkB;QACvC,IAAI,CAAC;YACH,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;YACzC,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;YAErE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,WAAW,EAAE;gBACvD,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;gBAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;gBAC1B,MAAM,EAAE,UAAU,CAAC,MAAM;aAC1B,CAAC,CAAC;YACH,YAAY,CAAC,OAAO,CAAC,CAAC;YAEtB,OAAO,QAAQ,CAAC,EAAE,CAAC;QACrB,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,SAAS,CAAI,IAAY;QACrC,IAAI,CAAC;YACH,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;YACzC,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;YAErE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI,EAAE,EAAE;gBACrD,MAAM,EAAE,UAAU,CAAC,MAAM;aAC1B,CAAC,CAAC;YACH,YAAY,CAAC,OAAO,CAAC,CAAC;YAEtB,IAAI,CAAC,QAAQ,CAAC,EAAE;gBAAE,OAAO,IAAI,CAAC;YAC9B,OAAO,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAM,CAAC;QACtC,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;CACF;AAzDD,kDAyDC"}
|
||||
Reference in New Issue
Block a user