Class: Riemann::Tools::Proc

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/proc.rb

Constant Summary

Constants included from Riemann::Tools

VERSION

Instance Attribute Summary

Attributes included from Riemann::Tools

#argv

Instance Method Summary collapse

Methods included from Riemann::Tools

#attributes, #endpoint_name, included, #options, #report, #riemann, #run

Constructor Details

#initializeProc

Returns a new instance of Proc.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/riemann/tools/proc.rb', line 15

def initialize
  super

  @limits = { critical: { min: opts[:proc_min_critical], max: opts[:proc_max_critical] } }

  abort 'FATAL: specify a process regular expression, see --help for usage' unless opts[:proc_regex]

  ostype = `uname -s`.chomp.downcase
  puts "WARNING: OS '#{ostype}' not explicitly supported. Falling back to Linux" unless ostype == 'linux'
  @check = method :linux_proc
end

Instance Method Details

#alert(service, state, metric, description) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/riemann/tools/proc.rb', line 27

def alert(service, state, metric, description)
  report(
    service: service.to_s,
    state: state.to_s,
    metric: metric.to_f,
    description: description,
  )
end

#linux_procObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/riemann/tools/proc.rb', line 36

def linux_proc
  process = opts[:proc_regex]
  found = `ps axo pid=,rss=,vsize=,state=,cputime=,lstart=,command= | grep '#{process}' | grep -v grep | grep -v riemann-proc`
  running = found.count("\n")
  if (running > @limits[:critical][:max]) || (running < @limits[:critical][:min])
    alert "proc count/#{process}", :critical, running, "process #{process} is running #{running} instances.\n"
  else
    alert "proc count/#{process}", :ok, running, "process #{process} is running #{running} instances.\n"
  end
  # Iterate on all the lines and create an entry for the following metrics:
  #
  # process/<pid>-<start-time>/rss
  # process/<pid>-<start-time>/vsize
  # process/<pid>-<start-time>/running
  # process/<pid>-<start-time>/cputime
  #
  # description should contain the command itself.
  # value should be either process RSS, VSIZE, or 1 if running
  # state is always unknown for the moment
  #
  ps_regex = /([0-9]+) +([0-9]+) +([0-9]+) +([A-Z]) +([0-9:.]+) +[A-Za-z]{3} +([A-Za-z]{3} {1,2}[0-9]+ [0-9:]+ [0-9]+) +(.*)/
  found.each_line do |line|
    m = ps_regex.match(line)
    next if m.nil?

    pid, rss, vsize, state, cputime, start, command = m.captures
    start_s = DateTime.parse(start, 'Mmm DD HH:MM:ss YYYY').to_time.to_i
    cputime_s = DateTime.parse(cputime, '%H:%M:%S')
    cputime_seconds = (cputime_s.hour * 3600) + (cputime_s.minute * 60) + cputime_s.second
    running = 0
    case state[0]
    when 'R'
      state_s = 'ok'
      running = 1
    when 'S'
      state_s = 'ok'
    when 'I'
      state_s = 'warning'
    when 'T', 'U', 'Z'
      state_s = 'critical'
    else
      state_s = 'unknown'
    end
    report(
      service: "proc #{pid}-#{start_s}/rss",
      state: state_s.to_s,
      metric: rss.to_f,
      description: command,
    )
    report(
      service: "proc #{pid}-#{start_s}/vsize",
      state: state_s.to_s,
      metric: vsize.to_f,
      description: command,
    )
    report(
      service: "proc #{pid}-#{start_s}/running",
      state: state_s.to_s,
      metric: running.to_f,
      description: command,
    )
    report(
      service: "proc #{pid}-#{start_s}/cputime",
      state: state_s.to_s,
      metric: cputime_seconds,
      description: command,
    )
  end
end

#tickObject



106
107
108
# File 'lib/riemann/tools/proc.rb', line 106

def tick
  @check.call
end