telex-server-monitor-sdk
Version:
SDK for Telex Server Monitor - A lightweight agent that collects and reports server CPU metrics to the Telex monitoring platform
57 lines (56 loc) • 1.79 kB
JavaScript
import { logger } from "../utils/logger.js";
import { getProcessMetrics } from "./processes.js";
import { getDiskMetrics } from "./disk.js";
import { getCpuMetrics, getCpuUsagePerCoreMetrics } from "./cpu.js";
import { getNetworkMetrics } from "./network.js";
import { getMemoryMetrics } from "./memory.js";
import { getSecurityMetrics } from "./security.js";
// get the formatted cpu metrics
async function getFormattedCpuMetrics() {
try {
const { cpu: cpuMetrics } = await getCpuMetrics();
if (!cpuMetrics) {
return "Error: Could not retrieve CPU metrics";
}
return `
CPU Usage: ${cpuMetrics.usage.toFixed(2)}%
CPU Cores: ${cpuMetrics.cores || "N/A"}
Load Average: ${cpuMetrics.load_avg?.[0]?.toFixed(2) || "N/A"}
`.trim();
}
catch (error) {
logger.error(`Failed to format CPU metrics: ${error.message}`);
return "Error fetching CPU metrics";
}
}
// get all metrics
const getMetrics = async () => {
const { cpu, cpuLoadAvgs } = await getCpuMetrics();
const { cpuUsagePerCore } = await getCpuUsagePerCoreMetrics();
const { memory } = await getMemoryMetrics();
const { disk } = await getDiskMetrics();
const { processes } = await getProcessMetrics();
const { networkMetrics } = await getNetworkMetrics();
const { security } = await getSecurityMetrics();
const allMetrics = {
cpu,
cpuLoadAvgs,
memory,
cpuUsagePerCore,
disk,
processes,
networkMetrics,
security,
};
return allMetrics;
};
export const CollectorService = {
getMetrics,
getFormattedCpuMetrics,
getCpuUsagePerCoreMetrics,
getDiskMetrics,
getProcessMetrics,
getNetworkMetrics,
getMemoryMetrics,
getSecurityMetrics,
};