aboutsummaryrefslogtreecommitdiff
path: root/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/cpu.lua
blob: 17e35bf7842e6418b77394eab6037c1f3ba7ca66 (plain)
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
-- stat/cpu collector
local function scrape()
  local stat = get_contents("/proc/stat")

  -- system boot time, seconds since epoch
  metric("node_boot_time_seconds", "gauge", nil,
    string.match(stat, "btime ([0-9]+)"))

  -- context switches since boot (all CPUs)
  metric("node_context_switches_total", "counter", nil,
    string.match(stat, "ctxt ([0-9]+)"))

  -- cpu times, per CPU, per mode
  local cpu_mode = {"user", "nice", "system", "idle", "iowait", "irq",
                    "softirq", "steal", "guest", "guest_nice"}
  local i = 0
  local cpu_metric = metric("node_cpu_seconds_total", "counter")
  while true do
    local cpu = {string.match(stat,
      "cpu"..i.." (%d+) (%d+) (%d+) (%d+) (%d+) (%d+) (%d+) (%d+) (%d+) (%d+)")}
    if #cpu ~= 10 then
      break
    end
    for ii, mode in ipairs(cpu_mode) do
      cpu_metric({cpu="cpu"..i, mode=mode}, cpu[ii] / 100)
    end
    i = i + 1
  end

  -- interrupts served
  metric("node_intr_total", "counter", nil,
    string.match(stat, "intr ([0-9]+)"))

  -- processes forked
  metric("node_forks_total", "counter", nil,
    string.match(stat, "processes ([0-9]+)"))

  -- processes running
  metric("node_procs_running_total", "gauge", nil,
    string.match(stat, "procs_running ([0-9]+)"))

  -- processes blocked for I/O
  metric("node_procs_blocked_total", "gauge", nil,
    string.match(stat, "procs_blocked ([0-9]+)"))
end

return { scrape = scrape }