node-os-utils
Version:
Advanced cross-platform operating system monitoring utilities with TypeScript support
315 lines • 11.6 kB
JavaScript
"use strict";
/**
* node-os-utils v2.0.0
*
* 现代化的跨平台操作系统监控工具库
* TypeScript 重构版本,提供全面的系统监控功能
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.name = exports.version = exports.CacheManager = exports.AdapterFactory = exports.SystemMonitor = exports.ProcessMonitor = exports.NetworkMonitor = exports.DiskMonitor = exports.MemoryMonitor = exports.CPUMonitor = exports.createOSUtils = exports.OSUtils = void 0;
const adapter_factory_1 = require("./adapters/adapter-factory");
Object.defineProperty(exports, "AdapterFactory", { enumerable: true, get: function () { return adapter_factory_1.AdapterFactory; } });
const cache_manager_1 = require("./core/cache-manager");
Object.defineProperty(exports, "CacheManager", { enumerable: true, get: function () { return cache_manager_1.CacheManager; } });
const cpu_monitor_1 = require("./monitors/cpu-monitor");
Object.defineProperty(exports, "CPUMonitor", { enumerable: true, get: function () { return cpu_monitor_1.CPUMonitor; } });
const memory_monitor_1 = require("./monitors/memory-monitor");
Object.defineProperty(exports, "MemoryMonitor", { enumerable: true, get: function () { return memory_monitor_1.MemoryMonitor; } });
const disk_monitor_1 = require("./monitors/disk-monitor");
Object.defineProperty(exports, "DiskMonitor", { enumerable: true, get: function () { return disk_monitor_1.DiskMonitor; } });
const network_monitor_1 = require("./monitors/network-monitor");
Object.defineProperty(exports, "NetworkMonitor", { enumerable: true, get: function () { return network_monitor_1.NetworkMonitor; } });
const process_monitor_1 = require("./monitors/process-monitor");
Object.defineProperty(exports, "ProcessMonitor", { enumerable: true, get: function () { return process_monitor_1.ProcessMonitor; } });
const system_monitor_1 = require("./monitors/system-monitor");
Object.defineProperty(exports, "SystemMonitor", { enumerable: true, get: function () { return system_monitor_1.SystemMonitor; } });
const package_json_1 = __importDefault(require("../package.json"));
/**
* OSUtils 主类
*
* 提供统一的系统监控 API,自动处理平台适配和缓存管理
*/
class OSUtils {
constructor(config = {}) {
// 合并默认配置
this.config = {
platform: undefined, // 自动检测
cacheEnabled: true,
cacheTTL: 5000,
timeout: 10000,
maxCacheSize: 1000,
debug: false,
...config
};
// 创建平台适配器
this.adapter = adapter_factory_1.AdapterFactory.create(this.config.platform);
// 创建缓存管理器
this.cache = new cache_manager_1.CacheManager({
maxSize: this.config.maxCacheSize || 1000,
defaultTTL: this.config.cacheTTL || 5000,
enabled: this.config.cacheEnabled !== false
});
if (this.config.debug) {
console.log(`OSUtils initialized for platform: ${this.adapter.getPlatform()}`);
}
}
/**
* CPU 监控器
*/
get cpu() {
if (!this._cpu) {
this._cpu = new cpu_monitor_1.CPUMonitor(this.adapter, this.config.cpu, this.cache);
}
return this._cpu;
}
/**
* 内存监控器
*/
get memory() {
if (!this._memory) {
this._memory = new memory_monitor_1.MemoryMonitor(this.adapter, this.config.memory, this.cache);
}
return this._memory;
}
/**
* 磁盘监控器
*/
get disk() {
if (!this._disk) {
this._disk = new disk_monitor_1.DiskMonitor(this.adapter, this.config.disk, this.cache);
}
return this._disk;
}
/**
* 网络监控器
*/
get network() {
if (!this._network) {
this._network = new network_monitor_1.NetworkMonitor(this.adapter, this.config.network, this.cache);
}
return this._network;
}
/**
* 进程监控器
*/
get process() {
if (!this._process) {
this._process = new process_monitor_1.ProcessMonitor(this.adapter, this.config.process, this.cache);
}
return this._process;
}
/**
* 系统监控器
*/
get system() {
if (!this._system) {
this._system = new system_monitor_1.SystemMonitor(this.adapter, this.config.system, this.cache);
}
return this._system;
}
/**
* 获取平台信息
*/
getPlatformInfo() {
return adapter_factory_1.AdapterFactory.getCurrentPlatformInfo();
}
/**
* 获取支持的平台列表
*/
getSupportedPlatforms() {
return adapter_factory_1.AdapterFactory.getSupportedPlatforms();
}
/**
* 检查平台能力
*/
async checkPlatformCapabilities() {
return adapter_factory_1.AdapterFactory.checkPlatformCapabilities();
}
/**
* 获取缓存统计信息
*/
getCacheStats() {
return this.cache.getStats();
}
/**
* 清理缓存
*/
clearCache() {
this.cache.clear();
return this;
}
/**
* 配置缓存
*/
configureCache(options) {
const { enabled, maxSize, defaultTTL } = options;
const needsResize = maxSize !== undefined || defaultTTL !== undefined;
const previousEnabled = this.cache.isEnabled();
const targetEnabled = enabled !== undefined ? enabled : previousEnabled;
if (needsResize) {
const currentStats = this.cache.getStats();
const currentTTL = this.cache.getDefaultTTL();
const nextMaxSize = maxSize ?? currentStats.maxSize;
const nextTTL = defaultTTL ?? currentTTL;
this.cache.destroy();
this.resetMonitors();
this.cache = new cache_manager_1.CacheManager({
maxSize: nextMaxSize,
defaultTTL: nextTTL,
enabled: targetEnabled
});
this.config.maxCacheSize = nextMaxSize;
this.config.cacheTTL = nextTTL;
}
else if (enabled !== undefined) {
targetEnabled ? this.cache.enable() : this.cache.disable();
}
this.config.cacheEnabled = targetEnabled;
return this;
}
/**
* 配置调试模式
*/
setDebug(enabled) {
this.config.debug = enabled;
return this;
}
/**
* 获取系统总览信息
*/
async overview() {
const [systemInfo, cpuUsage, memoryInfo, diskOverview, networkOverview, processStats] = await Promise.allSettled([
this.system.info(),
this.cpu.usage(),
this.memory.summary(),
this.disk.spaceOverview(),
this.network.overview(),
this.process.stats()
]);
return {
platform: this.adapter.getPlatform(),
timestamp: Date.now(),
system: systemInfo.status === 'fulfilled' && systemInfo.value.success ?
systemInfo.value.data : null,
cpu: {
usage: cpuUsage.status === 'fulfilled' && cpuUsage.value.success ?
cpuUsage.value.data : null
},
memory: memoryInfo.status === 'fulfilled' && memoryInfo.value.success ?
memoryInfo.value.data : null,
disk: diskOverview.status === 'fulfilled' && diskOverview.value.success ?
diskOverview.value.data : null,
network: networkOverview.status === 'fulfilled' && networkOverview.value.success ?
networkOverview.value.data : null,
processes: processStats.status === 'fulfilled' && processStats.value.success ?
processStats.value.data : null
};
}
/**
* 健康检查
*/
async healthCheck() {
const [systemHealth, diskHealth, networkHealth] = await Promise.allSettled([
this.system.healthCheck(),
this.disk.healthCheck(),
this.network.healthCheck()
]);
const allIssues = [];
let overallStatus = 'healthy';
// 合并所有健康检查结果
[systemHealth, diskHealth, networkHealth].forEach((result) => {
if (result.status === 'fulfilled' && result.value.success && result.value.data) {
const health = result.value.data;
allIssues.push(...health.issues);
if (health.status === 'critical') {
overallStatus = 'critical';
}
else if (health.status === 'warning' && overallStatus !== 'critical') {
overallStatus = 'warning';
}
}
});
return {
status: overallStatus,
issues: allIssues,
timestamp: Date.now(),
details: {
system: systemHealth.status === 'fulfilled' && systemHealth.value.success ?
systemHealth.value.data : null,
disk: diskHealth.status === 'fulfilled' && diskHealth.value.success ?
diskHealth.value.data : null,
network: networkHealth.status === 'fulfilled' && networkHealth.value.success ?
networkHealth.value.data : null
}
};
}
/**
* 销毁实例,清理资源
*/
destroy() {
this.resetMonitors();
// 销毁缓存管理器(清理定时器)
if (this.cache) {
this.cache.destroy();
}
}
resetMonitors() {
if (this._cpu) {
this._cpu.destroy();
this._cpu = undefined;
}
if (this._memory) {
this._memory.destroy();
this._memory = undefined;
}
if (this._disk) {
this._disk.destroy();
this._disk = undefined;
}
if (this._network) {
this._network.destroy();
this._network = undefined;
}
if (this._process) {
this._process.destroy();
this._process = undefined;
}
if (this._system) {
this._system.destroy();
this._system = undefined;
}
}
}
exports.OSUtils = OSUtils;
/**
* 创建 OSUtils 实例的工厂函数
*/
function createOSUtils(config) {
return new OSUtils(config);
}
exports.createOSUtils = createOSUtils;
// 导出所有类型和接口
__exportStar(require("./types"), exports);
// 默认导出主类
exports.default = OSUtils;
// 库信息
exports.version = package_json_1.default.version;
exports.name = package_json_1.default.name;
//# sourceMappingURL=index.js.map