bin/riemann-health in riemann-tools-0.2.14 vs bin/riemann-health in riemann-tools-1.0.0

- old
+ new

@@ -1,13 +1,16 @@ #!/usr/bin/env ruby +Process.setproctitle($0) # Reports current CPU, disk, load average, and memory use to riemann. require File.expand_path('../../lib/riemann/tools', __FILE__) +require File.expand_path('../../lib/riemann/tools/utils', __FILE__) class Riemann::Tools::Health include Riemann::Tools + include Riemann::Tools::Utils opt :cpu_warning, "CPU warning threshold (fraction of total jiffies)", :default => 0.9 opt :cpu_critical, "CPU critical threshold (fraction of total jiffies)", :default => 0.95 opt :disk_warning, "Disk warning threshold (fraction of space used)", :default => 0.9 opt :disk_critical, "Disk critical threshold (fraction of space used)", :default => 0.95 @@ -55,10 +58,11 @@ puts "WARNING: OS '#{@ostype}' not explicitly supported. Falling back to Linux" unless @ostype == "linux" @cpu = method :linux_cpu @disk = method :disk @load = method :linux_load @memory = method :linux_memory + @supports_exclude_type = `df --help 2>&1 | grep -e "--exclude-type"` != "" end opts[:checks].each do |check| case check when "disk" @@ -107,11 +111,11 @@ used = (u2+n2+s2) - (u1+n1+s1) total = used + i2-i1 fraction = used.to_f / total - report_pct :cpu, fraction, "user+nice+system\n\n#{`ps -eo pcpu,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :cpu, fraction, "user+nice+system\n\n#{reverse_numeric_sort_with_header(`ps -eo pcpu,pid,comm`)}" end @old_cpu = [u2, n2, s2, i2] end @@ -136,11 +140,11 @@ free = m['MemFree'].to_i + m['Buffers'].to_i + m['Cached'].to_i total = m['MemTotal'].to_i fraction = 1 - (free.to_f / total) - report_pct :memory, fraction, "used\n\n#{`ps -eo pmem,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :memory, fraction, "used\n\n#{reverse_numeric_sort_with_header(`ps -eo pmem,pid,comm`)}" end def freebsd_cpu u2, n2, s2, t2, i2 = `sysctl -n kern.cp_time 2>/dev/null`.split.map{ |e| e.to_i } #FreeBSD has 5 cpu stats @@ -149,11 +153,11 @@ used = (u2+n2+s2+t2) - (u1+n1+s1+t1) total = used + i2-i1 fraction = used.to_f / total - report_pct :cpu, fraction, "user+nice+sytem+interrupt\n\n#{`ps -axo pcpu,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :cpu, fraction, "user+nice+sytem+interrupt\n\n#{reverse_numeric_sort_with_header(`ps -axo pcpu,pid,comm`)}" end @old_cpu = [u2, n2, s2, t2, i2] end @@ -165,11 +169,11 @@ used = (u2+n2+s2+t2) - (u1+n1+s1+t1) total = used + i2-i1 fraction = used.to_f / total - report_pct :cpu, fraction, "user+nice+sytem+interrupt\n\n#{`ps -axo pcpu,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :cpu, fraction, "user+nice+sytem+interrupt\n\n#{reverse_numeric_sort_with_header(`ps -axo pcpu,pid,comm`)}" end @old_cpu = [u2, n2, s2, t2, i2] end @@ -189,11 +193,11 @@ fraction = 0 else fraction = used.to_f / total end - report_pct :cpu, fraction, "user+sytem+interrupt\n\n#{`ps -ao pcpu,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :cpu, fraction, "user+sytem+interrupt\n\n#{reverse_numeric_sort_with_header(`ps -ao pcpu,pid,comm`)}" end @old_cpu = [u2, s2, t2, i2] end @@ -211,26 +215,26 @@ def freebsd_memory meminfo = `sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_wire_count vm.stats.vm.v_active_count 2>/dev/null`.chomp.split fraction = (meminfo[1].to_f + meminfo[2].to_f) / meminfo[0].to_f - report_pct :memory, fraction, "used\n\n#{`ps -axo pmem,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :memory, fraction, "used\n\n#{reverse_numeric_sort_with_header(`ps -axo pmem,pid,comm`)}" end def openbsd_memory meminfo = `vmstat 2>/dev/null`.chomp.split fraction = meminfo[28].to_f / meminfo[29].to_f #The ratio of active to free memory unlike the others :( - report_pct :memory, fraction, "used\n\n#{`ps -axo pmem,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :memory, fraction, "used\n\n#{reverse_numeric_sort_with_header(`ps -axo pmem,pid,comm`)}" end def sunos_memory meminfo = `vmstat 2>/dev/null`.chomp.split total_mem = `prtconf | grep Memory`.split[2].to_f * 1024 # reports in GB but vmstat is in MB fraction = ( total_mem - meminfo[32].to_f ) / total_mem - report_pct :memory, fraction, "used\n\n#{`ps -ao pmem,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :memory, fraction, "used\n\n#{reverse_numeric_sort_with_header(`ps -ao pmem,pid,comm`)}" end def darwin_top raw = `top -l 1 | grep -i "^\\(cpu\\|physmem\\|load\\)"`.chomp @topdata = {:stamp => Time.now.to_i } @@ -262,11 +266,11 @@ darwin_top unless (Time.now.to_i - @topdata[:stamp]) < opts[:interval] unless @topdata[:cpu] alert 'cpu', :unknown, nil, "unable to get CPU stats from top" return false end - report_pct :cpu, @topdata[:cpu], "usage\n\n#{`ps -eo pcpu,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :cpu, @topdata[:cpu], "usage\n\n#{reverse_numeric_sort_with_header(`ps -eo pcpu,pid,comm`)}" end def darwin_load darwin_top unless (Time.now.to_i - @topdata[:stamp]) < opts[:interval] unless @topdata[:load] @@ -287,20 +291,24 @@ darwin_top unless (Time.now.to_i - @topdata[:stamp]) < opts[:interval] unless @topdata[:memory] alert 'memory', :unknown, nil, "unable to get memory data from top" return false end - report_pct :memory, @topdata[:memory], "usage\n\n#{`ps -eo pmem,pid,comm | sort -nrb -k1 | head -10`.chomp}" + report_pct :memory, @topdata[:memory], "usage\n\n#{reverse_numeric_sort_with_header(`ps -eo pmem,pid,comm`)}" end def df case @ostype when 'darwin', 'freebsd', 'openbsd' `df -P -t noiso9660` when 'sunos' `df -P` # Is there a good way to exlude iso9660 here? else - `df -P --exclude-type=iso9660` + if @supports_exclude_type + `df -P --exclude-type=iso9660 --exclude-type=nfs` + else + `df -P` + end end end def disk df.split(/\n/).each do |r|