Skip to content

Instantly share code, notes, and snippets.

@unak
Created April 16, 2017 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unak/58f93c8ed4979589a0541992bdb8f452 to your computer and use it in GitHub Desktop.
Save unak/58f93c8ed4979589a0541992bdb8f452 to your computer and use it in GitHub Desktop.
munin plugin to check process existence for NetBSD, implemented with Ruby
#!/usr/local/bin/ruby
PS = "/bin/ps"
keys = ENV.keys.grep(/^process_\d+$/).sort_by{|e| e.split(/_/, 2).last.to_i}
arg = ARGV.shift
if arg == "autoconf"
if keys.empty?
puts "no"
exit 1
else
puts "yes"
exit 0
end
end
if arg == "config"
puts <<-EOC
graph_title Process memory usage
graph_args --base 1000 -l 0 --vertical-label MB
graph_scale no
graph_vlabel process memory (MB)
graph_category healthcheck
EOC
keys.each do |key|
puts <<-EOC
#{key}.label #{ENV[key]}
#{key}.draw LINE2
#{key}.min -10
#{key}.critical 0:
EOC
end
exit 0
end
# run
def process_name(cmd)
args = cmd.split(/\s+/)
if /^\w+:$/ =~ args[0]
args[0, 2].join(' ')
elsif /^\(([^\)]+)\)$/ =~ args[0]
$1
else
args[0]
end
end
ps = `#{PS} awwx -o user,vsz,command`.lines.to_a[1..-1].map{|line| user, sz, cmd = line.strip.split(/ +/, 3); [sz.to_i, user, process_name(cmd)]}
keys.each do |key|
by_user = ENV[key + "_by"] == "user"
data = ps.select{|sz, user, cmd| by_user ? user == ENV[key] : Regexp.compile("\\b#{Regexp.quote(ENV[key])}\\b") =~ cmd}.map(&:first)
if data.empty?
puts "#{key}.value -10"
puts "#{key}.extinfo the process is down"
else
puts "#{key}.value #{'%.2f' % [data.inject(0, &:+) / 1024.0]}"
end
end
@unak
Copy link
Author

unak commented Apr 16, 2017

Settings example:

[healthcheck_process]
env.process_1 postfix
env.process_1_by user
env.process_2 postgrey
env.process_3 amavisd
env.process_4 clamd
env.process_5 named
env.process_6 httpd
env.process_7 squid

@unak
Copy link
Author

unak commented Apr 16, 2017

This is a clone of "healthcheck_process" at http://d.hatena.ne.jp/rti7743/20110604/1307219520 .
The original was written for bash and GNU ps, therefore I've re-implemented with Ruby and NetBSD ps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment