From 38c5a08b021817e0191d42f05bee58708ae9e888 Mon Sep 17 00:00:00 2001 From: gxkl Date: Thu, 20 Nov 2025 20:38:20 +0800 Subject: [PATCH] feat: support ignore fs --- index.d.ts | 3 ++- lib/agent.js | 1 + orders/system_log.js | 7 ++++--- test/config.test.js | 8 ++++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8829e4f..b365486 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,13 +8,14 @@ export interface XtransitConfig { // II. 比较重要的可选配置(不知道怎么配置的别传任何值,key 也别传,整个配置留空!!) disks?: string[], // 数组,每一项为配置需要监控的 disk 目录全路径 + ignoredFileSystems?: string[], // 数组,每一项为配置监控磁盘需要忽略的文件系统名 errors?: string[], // 数组,每一项为配置需要监控的 error 日志文件全路径 packages?: string[], // 数组,每一项为配置需要监控的 package.json 文件全路径,并且要保证存在平级的 lock 文件(package-lock.json 或者 yarn.lock) // III. 不是很重要的可选的配置(不知道怎么配置的别传任何值,key 也别传,整个配置留空!!) logDir?: string, // xprofiler 插件生成性能日志文件的目录,默认两者均为 os.tmpdir() 如:'/path/to/xprofiler_output' docker?: boolean, // 默认 false,系统数据采集会依赖当前是否是 docker 环境而进行一些特殊处理,可以手动强制指定当前实例是否为 docker 环境 - ipMode?: boolean, // 默认 false,此时仅使用 hostname 作为 agentId;设置为 true 后 agentId 组装形式为 ${ip}_${hostname} + ipMode?: boolean, // 默认 false,此时仅使用 hostname 作为 agentId;设置为 true 后 agentId 组装形式为 ${ip}_${hostname} libMode?: boolean, // 默认 false,此时采集如果收到 shutdown 事件会退出当前进程;如果是以第三方库的形式引用接入应用内,请将此属性设置为 true errexp?: RegExp, // 匹配错误日志起始的正则,默认为匹配到 YYYY-MM-DD HH:mm:ss 时间戳即认为是一条错误日志的起始 /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/i logger?: any, // 可以传入应用日志句柄方便日志统一管理,注意需要实现 error, info, warn 和 debug 四个方法 diff --git a/lib/agent.js b/lib/agent.js index 6780a87..1fe7245 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -36,6 +36,7 @@ class XtransitAgent extends EventEmitter { this.libMode = utils.isBoolean(config.libMode) ? config.libMode : false; this.cleanAfterUpload = utils.isBoolean(config.cleanAfterUpload) ? config.cleanAfterUpload : false; this.disks = Array.from(new Set(config.disks || [])); + this.ignoredFileSystems = Array.from(new Set(config.ignoredFileSystems || [])); this.errors = Array.from(new Set(config.errors || [])); this.packages = Array.from(new Set(config.packages || [])); this.coredirs = Array.from(new Set(config.coredirs || [])); diff --git a/orders/system_log.js b/orders/system_log.js index 6f5a153..dc7a42e 100644 --- a/orders/system_log.js +++ b/orders/system_log.js @@ -488,7 +488,7 @@ async function getLoadAvg() { return os.loadavg(); } -async function getDiskUsage(disks) { +async function getDiskUsage(disks, ignoredFileSystems) { if (isWindows) { return {}; } @@ -502,7 +502,8 @@ async function getDiskUsage(disks) { existsDisks.push(disk); } const params = existsDisks.length ? ` ${existsDisks.join(' ')}` : ''; - const command = `df -P${params}`; + const xfs = ignoredFileSystems.length ? ` ${ignoredFileSystems.map(fs => `-x ${fs}`).join(' ')}` : ''; + const command = `df -P${xfs}${params}`; logger.debug(`[system_log] get disks info: ${command}`); const { stdout } = await exec(command); @@ -560,7 +561,7 @@ exports = module.exports = async function() { tasks.push(getCpuUsage()); tasks.push(getFreeMemory()); tasks.push(getLoadAvg()); - tasks.push(getDiskUsage(this.disks)); + tasks.push(getDiskUsage(this.disks, this.ignoredFileSystems)); tasks.push(getNodeCount(this.titles)); const [ diff --git a/test/config.test.js b/test/config.test.js index 0ac1f68..afdc6b0 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -51,6 +51,8 @@ describe('get config', function() { expect(agent.packages.length).to.be(0); expect(Array.isArray(agent.titles)).to.be(true); expect(agent.titles.length).to.be(0); + expect(Array.isArray(agent.ignoredFileSystems)).to.be(true); + expect(agent.ignoredFileSystems.length).to.be(0); }); it('shoule merge user config', function() { @@ -67,6 +69,7 @@ describe('get config', function() { const errors = ['/error.log', '/error.log', '/error.log']; const packages = ['/package.json', '/package.json', '/package.json']; const titles = ['mock-node']; + const ignoredFileSystems = ['tmpfs', 'tmpfs', 'devtmpfs']; const agent = new Agent({ server: 'ws://127.0.0.1', appId: 1, @@ -84,6 +87,7 @@ describe('get config', function() { errors, packages, titles, + ignoredFileSystems, }); expect(agent.logdir).to.be(logdir); expect(agent.logLevel).to.be(logLevel); @@ -102,6 +106,8 @@ describe('get config', function() { expect(agent.packages.length).to.be(Array.from(new Set(packages)).length); expect(Array.isArray(agent.titles)).to.be(true); expect(agent.titles.length).to.be(Array.from(new Set(titles)).length); + expect(Array.isArray(agent.ignoredFileSystems)).to.be(true); + expect(agent.ignoredFileSystems.length).to.be(Array.from(new Set(ignoredFileSystems)).length); }); it('should config logDir', function() { @@ -129,6 +135,7 @@ describe('get config', function() { const errors = ['/error.log', '/error.log', '/error.log']; const packages = ['/package.json', '/package.json', '/package.json']; const titles = ['mock-node']; + const ignoredFileSystems = ['tmpfs', 'tmpfs', 'devtmpfs']; const agent = new Agent({ nodeExe: 'foo', server: 'ws://127.0.0.1', @@ -147,6 +154,7 @@ describe('get config', function() { errors, packages, titles, + ignoredFileSystems, }); let execOptions; let exeFile;