b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | let f = fs.open("/proc/stat"); |
| 2 | |
| 3 | if (!f) |
| 4 | return false; |
| 5 | |
| 6 | const desc = [ |
| 7 | null, |
| 8 | "user", |
| 9 | "nice", |
| 10 | "system", |
| 11 | "idle", |
| 12 | "iowait", |
| 13 | "irq", |
| 14 | "softirq", |
| 15 | "steal", |
| 16 | "guest", |
| 17 | "guest_nice", |
| 18 | ]; |
| 19 | const m_cpu = counter("node_cpu_seconds_total"); |
| 20 | |
| 21 | let line; |
| 22 | while (line = nextline(f)) { |
| 23 | const x = wsplit(line); |
| 24 | |
| 25 | if (length(x) < 2) |
| 26 | continue; |
| 27 | |
| 28 | if (match(x[0], /^cpu\d+/)) { |
| 29 | const count = min(length(x), length(desc)); |
| 30 | for (let i = 1; i < count; i++) |
| 31 | m_cpu({ cpu: x[0], mode: desc[i] }, x[i] / 100.0); |
| 32 | } else if (x[0] == "intr") |
| 33 | counter("node_intr_total")(null, x[1]); |
| 34 | else if (x[0] == "ctxt") |
| 35 | counter("node_context_switches_total")(null, x[1]); |
| 36 | else if (x[0] == "btime") |
| 37 | gauge("node_boot_time_seconds")(null, x[1]); |
| 38 | else if (x[0] == "processes") |
| 39 | counter("node_forks_total")(null, x[1]); |
| 40 | else if (x[0] == "procs_running") |
| 41 | gauge("node_procs_running_total")(null, x[1]); |
| 42 | else if (x[0] == "procs_blocked") |
| 43 | gauge("node_procs_blocked_total")(null, x[1]); |
| 44 | } |