add cpu state metric (/proc/state)

phoen
Daniel Barlow 2022-03-27 16:20:48 +01:00
parent 7e81c7c5ad
commit 028d5920a2
1 changed files with 25 additions and 1 deletions

View File

@ -15,4 +15,28 @@
(tset fields (: (name:gsub "_" "-") :lower) value)))
fields))))
{ : loadavg : battery }
(fn parse-cpu-stat-line [line]
(let [labels [:user :nice :system :idle :iowait
:irq :softirq :steal :guest :guest_nice]
vals (icollect [field (line:gmatch "([%d.]+)")]
(tonumber field))]
(collect [i label (ipairs labels)]
label (. vals i))))
(var proc-stat-handle nil)
(fn cpustat [path]
(if proc-stat-handle
(proc-stat-handle:seek :set 0)
(set proc-stat-handle (io.open "/proc/stat" :r)))
(let [f proc-stat-handle]
(accumulate [ret nil
line #(f:read "*l") ]
(if (= (string.sub line 1 (# "cpu ")) "cpu ")
(parse-cpu-stat-line line)
ret))))
{: loadavg
: battery
: cpustat
}