@aidalinfo/aegis-agent 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin.js +34 -7
- package/dist/index.d.ts +2 -2
- package/dist/index.js +30 -3
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -7,14 +7,25 @@ import { execSync as execSync2 } from "child_process";
|
|
|
7
7
|
// src/config.ts
|
|
8
8
|
import { z } from "zod";
|
|
9
9
|
import { readFileSync } from "fs";
|
|
10
|
+
import { join } from "path";
|
|
10
11
|
import { hostname } from "os";
|
|
11
12
|
var ConfigSchema = z.object({
|
|
12
13
|
interval: z.coerce.number().int().min(5).default(30),
|
|
13
14
|
endpoint: z.string().url(),
|
|
14
15
|
apiKey: z.string().min(1),
|
|
15
16
|
mode: z.enum(["core", "full"]).default("core"),
|
|
16
|
-
hostname: z.string().
|
|
17
|
+
hostname: z.string().min(1)
|
|
17
18
|
});
|
|
19
|
+
function resolveHostname() {
|
|
20
|
+
const hostRoot = process.env.AEGIS_HOST_ROOT;
|
|
21
|
+
if (hostRoot) {
|
|
22
|
+
try {
|
|
23
|
+
return readFileSync(join(hostRoot, "/etc/hostname"), "utf8").trim();
|
|
24
|
+
} catch {
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return hostname();
|
|
28
|
+
}
|
|
18
29
|
function loadConfig(configPath = "/etc/aegis-agent/config.json") {
|
|
19
30
|
let fileConfig = {};
|
|
20
31
|
try {
|
|
@@ -27,24 +38,24 @@ function loadConfig(configPath = "/etc/aegis-agent/config.json") {
|
|
|
27
38
|
...process.env.AEGIS_ENDPOINT !== void 0 && { endpoint: process.env.AEGIS_ENDPOINT },
|
|
28
39
|
...process.env.AEGIS_API_KEY !== void 0 && { apiKey: process.env.AEGIS_API_KEY },
|
|
29
40
|
...process.env.AEGIS_MODE !== void 0 && { mode: process.env.AEGIS_MODE },
|
|
30
|
-
|
|
41
|
+
hostname: process.env.AEGIS_HOSTNAME ?? fileConfig.hostname ?? resolveHostname()
|
|
31
42
|
});
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
// src/metrics/hostpaths.ts
|
|
35
46
|
import { readFile, statfs } from "fs/promises";
|
|
36
|
-
import { join } from "path";
|
|
47
|
+
import { join as join2 } from "path";
|
|
37
48
|
var HOST_PROC = process.env.AEGIS_HOST_PROC || "/proc";
|
|
38
49
|
var HOST_SYS = process.env.AEGIS_HOST_SYS || "/sys";
|
|
39
50
|
var HOST_ROOT = process.env.AEGIS_HOST_ROOT || "";
|
|
40
51
|
function readProc(rel) {
|
|
41
|
-
return readFile(
|
|
52
|
+
return readFile(join2(HOST_PROC, rel), "utf8");
|
|
42
53
|
}
|
|
43
54
|
function readHostFile(absPath) {
|
|
44
|
-
return readFile(HOST_ROOT ?
|
|
55
|
+
return readFile(HOST_ROOT ? join2(HOST_ROOT, absPath) : absPath, "utf8");
|
|
45
56
|
}
|
|
46
57
|
function statHostFs(mount) {
|
|
47
|
-
return statfs(HOST_ROOT ?
|
|
58
|
+
return statfs(HOST_ROOT ? join2(HOST_ROOT, mount) : mount);
|
|
48
59
|
}
|
|
49
60
|
|
|
50
61
|
// src/metrics/proc-parse.ts
|
|
@@ -146,6 +157,7 @@ var PSEUDO_FS = /* @__PURE__ */ new Set([
|
|
|
146
157
|
"binfmt_misc",
|
|
147
158
|
"efivarfs"
|
|
148
159
|
]);
|
|
160
|
+
var DOCKER_FILE_MOUNTS = /* @__PURE__ */ new Set(["/etc/hostname", "/etc/resolv.conf", "/etc/hosts"]);
|
|
149
161
|
function parseMounts(content) {
|
|
150
162
|
const seen = /* @__PURE__ */ new Set();
|
|
151
163
|
const out = [];
|
|
@@ -154,6 +166,7 @@ function parseMounts(content) {
|
|
|
154
166
|
if (parts.length < 3) continue;
|
|
155
167
|
const [, mount, fstype] = parts;
|
|
156
168
|
if (PSEUDO_FS.has(fstype)) continue;
|
|
169
|
+
if (DOCKER_FILE_MOUNTS.has(mount)) continue;
|
|
157
170
|
if (seen.has(mount)) continue;
|
|
158
171
|
seen.add(mount);
|
|
159
172
|
out.push(mount);
|
|
@@ -185,7 +198,15 @@ function diskIoRates(prev3, cur, dtSec) {
|
|
|
185
198
|
var prev = null;
|
|
186
199
|
async function collectCpu() {
|
|
187
200
|
const cur = parseProcStat(await readProc("stat"));
|
|
188
|
-
|
|
201
|
+
if (!prev) {
|
|
202
|
+
prev = cur;
|
|
203
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
204
|
+
const warm = parseProcStat(await readProc("stat"));
|
|
205
|
+
const usagePercent2 = cpuPercent(prev, warm);
|
|
206
|
+
prev = warm;
|
|
207
|
+
return { usagePercent: usagePercent2 };
|
|
208
|
+
}
|
|
209
|
+
const usagePercent = cpuPercent(prev, cur);
|
|
189
210
|
prev = cur;
|
|
190
211
|
return { usagePercent };
|
|
191
212
|
}
|
|
@@ -268,7 +289,13 @@ async function collectSwap() {
|
|
|
268
289
|
|
|
269
290
|
// src/metrics/processes.ts
|
|
270
291
|
import si from "systeminformation";
|
|
292
|
+
var initialized = false;
|
|
271
293
|
async function collectProcesses() {
|
|
294
|
+
if (!initialized) {
|
|
295
|
+
await si.processes();
|
|
296
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
297
|
+
initialized = true;
|
|
298
|
+
}
|
|
272
299
|
const { list } = await si.processes();
|
|
273
300
|
return list.sort((a, b) => b.cpu - a.cpu).slice(0, 5).map((p) => ({
|
|
274
301
|
pid: p.pid,
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ declare const ConfigSchema: z.ZodObject<{
|
|
|
5
5
|
endpoint: z.ZodString;
|
|
6
6
|
apiKey: z.ZodString;
|
|
7
7
|
mode: z.ZodDefault<z.ZodEnum<["core", "full"]>>;
|
|
8
|
-
hostname: z.
|
|
8
|
+
hostname: z.ZodString;
|
|
9
9
|
}, "strip", z.ZodTypeAny, {
|
|
10
10
|
interval: number;
|
|
11
11
|
endpoint: string;
|
|
@@ -15,9 +15,9 @@ declare const ConfigSchema: z.ZodObject<{
|
|
|
15
15
|
}, {
|
|
16
16
|
endpoint: string;
|
|
17
17
|
apiKey: string;
|
|
18
|
+
hostname: string;
|
|
18
19
|
interval?: number | undefined;
|
|
19
20
|
mode?: "core" | "full" | undefined;
|
|
20
|
-
hostname?: string | undefined;
|
|
21
21
|
}>;
|
|
22
22
|
type Config = z.infer<typeof ConfigSchema>;
|
|
23
23
|
declare function loadConfig(configPath?: string): Config;
|
package/dist/index.js
CHANGED
|
@@ -113,6 +113,7 @@ var PSEUDO_FS = /* @__PURE__ */ new Set([
|
|
|
113
113
|
"binfmt_misc",
|
|
114
114
|
"efivarfs"
|
|
115
115
|
]);
|
|
116
|
+
var DOCKER_FILE_MOUNTS = /* @__PURE__ */ new Set(["/etc/hostname", "/etc/resolv.conf", "/etc/hosts"]);
|
|
116
117
|
function parseMounts(content) {
|
|
117
118
|
const seen = /* @__PURE__ */ new Set();
|
|
118
119
|
const out = [];
|
|
@@ -121,6 +122,7 @@ function parseMounts(content) {
|
|
|
121
122
|
if (parts.length < 3) continue;
|
|
122
123
|
const [, mount, fstype] = parts;
|
|
123
124
|
if (PSEUDO_FS.has(fstype)) continue;
|
|
125
|
+
if (DOCKER_FILE_MOUNTS.has(mount)) continue;
|
|
124
126
|
if (seen.has(mount)) continue;
|
|
125
127
|
seen.add(mount);
|
|
126
128
|
out.push(mount);
|
|
@@ -152,7 +154,15 @@ function diskIoRates(prev3, cur, dtSec) {
|
|
|
152
154
|
var prev = null;
|
|
153
155
|
async function collectCpu() {
|
|
154
156
|
const cur = parseProcStat(await readProc("stat"));
|
|
155
|
-
|
|
157
|
+
if (!prev) {
|
|
158
|
+
prev = cur;
|
|
159
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
160
|
+
const warm = parseProcStat(await readProc("stat"));
|
|
161
|
+
const usagePercent2 = cpuPercent(prev, warm);
|
|
162
|
+
prev = warm;
|
|
163
|
+
return { usagePercent: usagePercent2 };
|
|
164
|
+
}
|
|
165
|
+
const usagePercent = cpuPercent(prev, cur);
|
|
156
166
|
prev = cur;
|
|
157
167
|
return { usagePercent };
|
|
158
168
|
}
|
|
@@ -235,7 +245,13 @@ async function collectSwap() {
|
|
|
235
245
|
|
|
236
246
|
// src/metrics/processes.ts
|
|
237
247
|
import si from "systeminformation";
|
|
248
|
+
var initialized = false;
|
|
238
249
|
async function collectProcesses() {
|
|
250
|
+
if (!initialized) {
|
|
251
|
+
await si.processes();
|
|
252
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
253
|
+
initialized = true;
|
|
254
|
+
}
|
|
239
255
|
const { list } = await si.processes();
|
|
240
256
|
return list.sort((a, b) => b.cpu - a.cpu).slice(0, 5).map((p) => ({
|
|
241
257
|
pid: p.pid,
|
|
@@ -303,14 +319,25 @@ async function collectMetrics(config) {
|
|
|
303
319
|
// src/config.ts
|
|
304
320
|
import { z } from "zod";
|
|
305
321
|
import { readFileSync } from "fs";
|
|
322
|
+
import { join as join2 } from "path";
|
|
306
323
|
import { hostname } from "os";
|
|
307
324
|
var ConfigSchema = z.object({
|
|
308
325
|
interval: z.coerce.number().int().min(5).default(30),
|
|
309
326
|
endpoint: z.string().url(),
|
|
310
327
|
apiKey: z.string().min(1),
|
|
311
328
|
mode: z.enum(["core", "full"]).default("core"),
|
|
312
|
-
hostname: z.string().
|
|
329
|
+
hostname: z.string().min(1)
|
|
313
330
|
});
|
|
331
|
+
function resolveHostname() {
|
|
332
|
+
const hostRoot = process.env.AEGIS_HOST_ROOT;
|
|
333
|
+
if (hostRoot) {
|
|
334
|
+
try {
|
|
335
|
+
return readFileSync(join2(hostRoot, "/etc/hostname"), "utf8").trim();
|
|
336
|
+
} catch {
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return hostname();
|
|
340
|
+
}
|
|
314
341
|
function loadConfig(configPath = "/etc/aegis-agent/config.json") {
|
|
315
342
|
let fileConfig = {};
|
|
316
343
|
try {
|
|
@@ -323,7 +350,7 @@ function loadConfig(configPath = "/etc/aegis-agent/config.json") {
|
|
|
323
350
|
...process.env.AEGIS_ENDPOINT !== void 0 && { endpoint: process.env.AEGIS_ENDPOINT },
|
|
324
351
|
...process.env.AEGIS_API_KEY !== void 0 && { apiKey: process.env.AEGIS_API_KEY },
|
|
325
352
|
...process.env.AEGIS_MODE !== void 0 && { mode: process.env.AEGIS_MODE },
|
|
326
|
-
|
|
353
|
+
hostname: process.env.AEGIS_HOSTNAME ?? fileConfig.hostname ?? resolveHostname()
|
|
327
354
|
});
|
|
328
355
|
}
|
|
329
356
|
export {
|