Compare commits

..

2 Commits

Author SHA1 Message Date
zhi
6d6d00437d chore: add .gitignore for build artifacts 2026-03-19 18:20:43 +00:00
zhi
0dc824549a feat: fix API Key authentication and payload alignment
- Update openclaw.plugin.json: replace challengeUuid with apiKey (optional)
- Fix tsconfig: use CommonJS module to avoid import.meta.url issues
- Fix plugin/index.ts: remove ESM-specific code, use __dirname
- Fix telemetry.mjs:
  - Add loadavg to os imports, remove require() call
  - Replace challengeUuid with apiKey in config
  - Update endpoint to heartbeat-v2
  - Add X-API-Key header when apiKey is configured
  - Fix payload field names: agents, load_avg (array), uptime_seconds
  - Change missing apiKey from error to warning
2026-03-19 18:20:29 +00:00
6 changed files with 97 additions and 40 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
plugin/node_modules/
plugin/*.js
plugin/*.js.map
plugin/*.d.ts
plugin/*.d.ts.map

View File

@@ -1,21 +1,17 @@
/**
* HarborForge Monitor Plugin for OpenClaw
*
*
* Manages sidecar lifecycle and provides monitor-related tools.
*/
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { join } from 'path';
import { existsSync } from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
interface PluginConfig {
enabled?: boolean;
backendUrl?: string;
identifier?: string;
challengeUuid?: string;
apiKey?: string;
reportIntervalSec?: number;
httpFallbackIntervalSec?: number;
logLevel?: 'debug' | 'info' | 'warn' | 'error';
@@ -35,7 +31,7 @@ interface PluginAPI {
}
export default function register(api: PluginAPI, config: PluginConfig) {
const logger = api.logger || {
const logger = api.logger || {
info: (...args: any[]) => console.log('[HF-Monitor]', ...args),
error: (...args: any[]) => console.error('[HF-Monitor]', ...args),
debug: (...args: any[]) => console.debug('[HF-Monitor]', ...args),
@@ -47,14 +43,13 @@ export default function register(api: PluginAPI, config: PluginConfig) {
return;
}
if (!config.challengeUuid) {
logger.error('Missing required config: challengeUuid');
logger.error('Please register server in HarborForge Monitor first');
return;
if (!config.apiKey) {
logger.warn('Missing config: apiKey');
logger.warn('API authentication will fail. Generate apiKey from HarborForge Monitor admin.');
}
const serverPath = join(__dirname, '..', 'server', 'telemetry.mjs');
if (!existsSync(serverPath)) {
logger.error('Telemetry server not found:', serverPath);
return;
@@ -69,12 +64,12 @@ export default function register(api: PluginAPI, config: PluginConfig) {
}
logger.info('Starting HarborForge Monitor telemetry server...');
const env = {
...process.env,
HF_MONITOR_BACKEND_URL: config.backendUrl || 'https://monitor.hangman-lab.top',
HF_MONITOR_IDENTIFIER: config.identifier || '',
HF_MONITOR_CHALLENGE_UUID: config.challengeUuid,
HF_MONITOR_API_KEY: config.apiKey || '',
HF_MONITOR_REPORT_INTERVAL: String(config.reportIntervalSec || 30),
HF_MONITOR_HTTP_FALLBACK_INTERVAL: String(config.httpFallbackIntervalSec || 60),
HF_MONITOR_LOG_LEVEL: config.logLevel || 'info',
@@ -117,7 +112,7 @@ export default function register(api: PluginAPI, config: PluginConfig) {
logger.info('Stopping HarborForge Monitor telemetry server...');
sidecar.kill('SIGTERM');
const timeout = setTimeout(() => {
if (sidecar && !sidecar.killed) {
logger.warn('Telemetry server did not exit gracefully, forcing kill');

View File

@@ -22,9 +22,9 @@
"type": "string",
"description": "Server identifier (auto-detected from hostname if not set)"
},
"challengeUuid": {
"apiKey": {
"type": "string",
"description": "Registration challenge UUID from Monitor"
"description": "API Key from HarborForge Monitor admin panel (optional but required for authentication)"
},
"reportIntervalSec": {
"type": "number",
@@ -42,7 +42,6 @@
"default": "info",
"description": "Logging level"
}
},
"required": ["challengeUuid"]
}
}
}

48
plugin/package-lock.json generated Normal file
View File

@@ -0,0 +1,48 @@
{
"name": "harborforge-monitor-plugin",
"version": "0.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "harborforge-monitor-plugin",
"version": "0.1.0",
"license": "MIT",
"devDependencies": {
"@types/node": "^20.0.0",
"typescript": "^5.0.0"
}
},
"node_modules/@types/node": {
"version": "20.19.37",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.37.tgz",
"integrity": "sha512-8kzdPJ3FsNsVIurqBs7oodNnCEVbni9yUEkaHbgptDACOPW04jimGagZ51E6+lXUwJjgnBw+hyko/lkFWCldqw==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.21.0"
}
},
"node_modules/typescript": {
"version": "5.9.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/undici-types": {
"version": "6.21.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
"dev": true,
"license": "MIT"
}
}
}

View File

@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"module": "CommonJS",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,

View File

@@ -8,20 +8,23 @@ import { readFile, access } from 'fs/promises';
import { constants } from 'fs';
import { exec } from 'child_process';
import { promisify } from 'util';
import { platform, hostname, freemem, totalmem, uptime } from 'os';
import { platform, hostname, freemem, totalmem, uptime, loadavg } from 'os';
const execAsync = promisify(exec);
// Config from environment (set by plugin)
const openclawPath = process.env.OPENCLAW_PATH || `${process.env.HOME}/.openclaw`;
const CONFIG = {
backendUrl: process.env.HF_MONITOR_BACKEND_URL || 'https://monitor.hangman-lab.top',
identifier: process.env.HF_MONITOR_IDENTIFIER || hostname(),
challengeUuid: process.env.HF_MONITOR_CHALLENGE_UUID,
apiKey: process.env.HF_MONITOR_API_KEY,
reportIntervalSec: parseInt(process.env.HF_MONITOR_REPORT_INTERVAL || '30', 10),
httpFallbackIntervalSec: parseInt(process.env.HF_MONITOR_HTTP_FALLBACK_INTERVAL || '60', 10),
logLevel: process.env.HF_MONITOR_LOG_LEVEL || 'info',
openclawPath: process.env.OPENCLAW_PATH || `${process.env.HOME}/.openclaw`,
openclawPath,
openclawVersion: process.env.OPENCLAW_VERSION || 'unknown',
cachePath: process.env.HF_MONITOR_CACHE_PATH || `${openclawPath}/telemetry_cache.json`,
maxCacheSize: parseInt(process.env.HF_MONITOR_MAX_CACHE_SIZE || '100', 10),
};
// Logging
@@ -48,7 +51,7 @@ async function collectSystemMetrics() {
const memFree = freemem();
const memUsed = memTotal - memFree;
const diskInfo = await getDiskUsage();
const loadAvg = platform() !== 'win32' ? require('os').loadavg() : [0, 0, 0];
const loadAvg = platform() !== 'win32' ? loadavg() : [0, 0, 0];
return {
cpu_pct: cpuUsage,
@@ -59,8 +62,12 @@ async function collectSystemMetrics() {
disk_used_gb: Math.round(diskInfo.usedGB * 10) / 10,
disk_total_gb: Math.round(diskInfo.totalGB * 10) / 10,
swap_pct: diskInfo.swapUsedPct || 0,
uptime_sec: Math.floor(uptime()),
load_avg_1m: Math.round(loadAvg[0] * 100) / 100,
uptime_seconds: Math.floor(uptime()),
load_avg: [
Math.round(loadAvg[0] * 100) / 100,
Math.round(loadAvg[1] * 100) / 100,
Math.round(loadAvg[2] * 100) / 100,
],
platform: platform(),
hostname: hostname(),
};
@@ -177,12 +184,10 @@ async function buildPayload() {
return {
identifier: CONFIG.identifier,
challenge_uuid: CONFIG.challengeUuid,
timestamp: new Date().toISOString(),
...system,
openclaw_version: openclaw.version,
openclaw_agents: openclaw.agents,
openclaw_agent_count: openclaw.agent_count,
agents: openclaw.agents,
};
}
@@ -194,13 +199,18 @@ async function sendHttpHeartbeat() {
const payload = await buildPayload();
log.debug('Sending HTTP heartbeat...');
const response = await fetch(`${CONFIG.backendUrl}/monitor/server/heartbeat`, {
const headers = {
'Content-Type': 'application/json',
'X-Server-Identifier': CONFIG.identifier,
};
if (CONFIG.apiKey) {
headers['X-API-Key'] = CONFIG.apiKey;
}
const response = await fetch(`${CONFIG.backendUrl}/monitor/server/heartbeat-v2`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Server-Identifier': CONFIG.identifier,
'X-Challenge-UUID': CONFIG.challengeUuid,
},
headers,
body: JSON.stringify(payload),
});
@@ -268,11 +278,11 @@ log.info('Config:', {
identifier: CONFIG.identifier,
backendUrl: CONFIG.backendUrl,
reportIntervalSec: CONFIG.reportIntervalSec,
hasApiKey: !!CONFIG.apiKey,
});
if (!CONFIG.challengeUuid) {
log.error('Missing HF_MONITOR_CHALLENGE_UUID environment variable');
process.exit(1);
if (!CONFIG.apiKey) {
log.warn('Missing HF_MONITOR_API_KEY environment variable - API authentication will fail');
}
reportingLoop();