Class: Riemann::Tools::Fd

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/fd.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

#initializeFd

Returns a new instance of Fd.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/riemann/tools/fd.rb', line 18

def initialize
  super

  @limits = {
    fd: { critical: opts[:fd_sys_critical], warning: opts[:fd_sys_warning] },
    process: { critical: opts[:fd_proc_critical], warning: opts[:fd_proc_warning] },
  }
  ostype = `uname -s`.chomp.downcase
  case ostype
  when 'freebsd'
    @fd = method :freebsd_fd
  else
    puts "WARNING: OS '#{ostype}' not explicitly supported. Falling back to Linux" unless ostype == 'linux'
    @fd = method :linux_fd
  end
end

Instance Method Details

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



35
36
37
38
39
40
41
42
# File 'lib/riemann/tools/fd.rb', line 35

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

#freebsd_fdObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/riemann/tools/fd.rb', line 44

def freebsd_fd
  sys_used = Integer(`sysctl -n kern.openfiles`)
  if sys_used > @limits[:fd][:critical]
    alert 'fd sys', :critical, sys_used, "system is using #{sys_used} fds"
  elsif sys_used > @limits[:fd][:warning]
    alert 'fd sys', :warning, sys_used, "system is using #{sys_used} fds"
  else
    alert 'fd sys', :ok, sys_used, "system is using #{sys_used} fds"
  end
end

#linux_fdObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/riemann/tools/fd.rb', line 55

def linux_fd
  sys_used = Integer(`lsof | wc -l`)
  if sys_used > @limits[:fd][:critical]
    alert 'fd sys', :critical, sys_used, "system is using #{sys_used} fds"
  elsif sys_used > @limits[:fd][:warning]
    alert 'fd sys', :warning, sys_used, "system is using #{sys_used} fds"
  else
    alert 'fd sys', :ok, sys_used, "system is using #{sys_used} fds"
  end

  opts[:processes]&.each do |process|
    used = Integer(`lsof -p #{process} | wc -l`)
    name, _pid = `ps axo comm,pid | grep -w #{process}`.split
    if used > @limits[:process][:critical]
      alert "fd #{name} #{process}", :critical, used, "process #{name} #{process} is using #{used} fds"
    elsif used > @limits[:process][:warning]
      alert "fd #{name} #{process}", :warning, used, "process #{name} #{process} is using #{used} fds"
    else
      alert "fd #{name} #{process}", :ok, used, "process #{name} #{process} is using #{used} fds"
    end
  end
end

#tickObject



78
79
80
# File 'lib/riemann/tools/fd.rb', line 78

def tick
  @fd.call
end