-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmonitorTcpBuffers.server.ts
More file actions
57 lines (51 loc) · 1.83 KB
/
monitorTcpBuffers.server.ts
File metadata and controls
57 lines (51 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// monitorTcpBuffers.ts
import fs from "fs/promises";
import os from "os";
import { logger } from "./logger.server";
/**
* Parse /proc/net/sockstat and /proc/sys/net/* every `intervalMs`
* and log the numbers. You can pivot these logs into CloudWatch
* metrics with a filter pattern if you like.
*/
export function startTcpBufferMonitor(intervalMs = 5_000) {
async function sampleOnce() {
try {
const [sockstat, wmemMax, tcpMem] = await Promise.all([
fs.readFile("/proc/net/sockstat", "utf8"),
fs.readFile("/proc/sys/net/core/wmem_max", "utf8"),
fs.readFile("/proc/sys/net/ipv4/tcp_mem", "utf8"),
]);
logger.debug("tcp-buffer-monitor", {
sockstat,
wmemMax,
tcpMem,
});
// /proc/net/sockstat has lines like:
// TCP: inuse 5 orphan 0 tw 0 alloc 6 mem 409
const tcpLine = sockstat.split("\n").find((l) => l.startsWith("TCP:")) ?? "";
const fields = tcpLine.trim().split(/\s+/);
const inUse = Number(fields[2]); // open sockets
const alloc = Number(fields[8]); // total sockets with buffers
const memPages = Number(fields[10]); // pages (4 kB each)
const memBytes = memPages * 4096;
const wmemMaxBytes = Number(wmemMax.trim());
const [low, pressure, high] = tcpMem
.trim()
.split(/\s+/)
.map((n) => Number(n) * 4096); // pages → bytes
logger.debug("tcp-buffer-monitor", {
t: Date.now(),
host: os.hostname(),
sockets_in_use: inUse,
sockets_alloc: alloc,
tcp_mem_bytes: memBytes,
tcp_mem_high: high,
wmem_max: wmemMaxBytes,
});
} catch (err) {
// Log and keep going; most errors are “file disappeared for a moment”
console.error("tcp-buffer-monitor error", err);
}
}
return setInterval(sampleOnce, intervalMs);
}