Created
April 16, 2017 04:30
-
-
Save unak/58f93c8ed4979589a0541992bdb8f452 to your computer and use it in GitHub Desktop.
munin plugin to check process existence for NetBSD, implemented with Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
Settings example: