lib/zabbix-ruby-client/plugins/disk.rb in zabbix-ruby-client-0.0.17 vs lib/zabbix-ruby-client/plugins/disk.rb in zabbix-ruby-client-0.0.18

- old
+ new

@@ -1,59 +1,78 @@ # some doc on # http://www.xaprb.com/blog/2010/01/09/how-linux-iostat-computes-its-results/ # http://www.mjmwired.net/kernel/Documentation/iostats.txt require "zabbix-ruby-client/logger" +require "zabbix-ruby-client/plugin_base" module ZabbixRubyClient module Plugins module Disk extend self + extend ZabbixRubyClient::PluginBase def collect(*args) host = args[0] dev = args[1] mapped = args[3] || dev - diskspace = `df | grep "#{mapped}"` - if $?.to_i == 0 - _, size, used, available, percent_used, mount = diskspace.split(/\s+/) - else - Log.error "df is not working... ouchie." - return [] - end - time = Time.now.to_i - back = [] - back << "#{host} disk.space[#{mapped},size] #{time} #{size.to_i * 1000}" - back << "#{host} disk.space[#{mapped},used] #{time} #{used.to_i * 1000}" - back << "#{host} disk.space[#{mapped},available] #{time} #{available.to_i * 1000}" - back << "#{host} disk.space[#{mapped},percent_used] #{time} #{percent_used.gsub(/%/,'')}" - if dev != "" - diskinfo = `cat /proc/diskstats | grep " #{dev} "` - if $?.to_i == 0 - _, _, _, _, read_ok, read_merged, read_sector, read_time, write_ok, write_merged, write_sector, write_time, io_current, io_time, io_weighted = diskinfo.split(/\s+/) - else - logger.warn "Oh there is no such device." - return [] - end - back << "#{host} disk.io[#{mapped},read_ok] #{time} #{read_ok}" - back << "#{host} disk.io[#{mapped},read_merged] #{time} #{read_merged}" - back << "#{host} disk.io[#{mapped},read_sector] #{time} #{read_sector}" - back << "#{host} disk.io[#{mapped},read_time] #{time} #{read_time}" - back << "#{host} disk.io[#{mapped},write_ok] #{time} #{write_ok}" - back << "#{host} disk.io[#{mapped},write_merged] #{time} #{write_merged}" - back << "#{host} disk.io[#{mapped},write_sector] #{time} #{write_sector}" - back << "#{host} disk.io[#{mapped},write_time] #{time} #{write_time}" - back << "#{host} disk.io[#{mapped},io_time] #{time} #{io_time}" - back << "#{host} disk.io[#{mapped},io_weighted] #{time} #{io_weighted}" + info = get_info(dev, mapped) + if info + time = Time.now.to_i + back = [] + back << "#{host} disk.space[#{mapped},size] #{time} #{info[1].to_i * 1000}" + back << "#{host} disk.space[#{mapped},used] #{time} #{info[2].to_i * 1000}" + back << "#{host} disk.space[#{mapped},available] #{time} #{info[3].to_i * 1000}" + back << "#{host} disk.space[#{mapped},percent_used] #{time} #{info[4].gsub(/%/,'')}" + back << "#{host} disk.io[#{mapped},read_ok] #{time} #{info[9]}" + back << "#{host} disk.io[#{mapped},read_merged] #{time} #{info[10]}" + back << "#{host} disk.io[#{mapped},read_sector] #{time} #{info[11]}" + back << "#{host} disk.io[#{mapped},read_time] #{time} #{info[12]}" + back << "#{host} disk.io[#{mapped},write_ok] #{time} #{info[13]}" + back << "#{host} disk.io[#{mapped},write_merged] #{time} #{info[14]}" + back << "#{host} disk.io[#{mapped},write_sector] #{time} #{info[15]}" + back << "#{host} disk.io[#{mapped},write_time] #{time} #{info[16]}" + back << "#{host} disk.io[#{mapped},io_time] #{time} #{info[17]}" + back << "#{host} disk.io[#{mapped},io_weighted] #{time} #{info[18]}" end return back end def discover(*args) device = args[0] mount = args[1] mapped = args[2] || device - [ "disk.dev.discovery", "{\"{#DISK_DEVICE}\": \"#{mapped}\", \"{#DISK_MOUNT}\": \"#{mount}\"}" ] + [ "disk.dev.discovery", + "{\"{#DISK_DEVICE}\": \"#{mapped}\", \"{#DISK_MOUNT}\": \"#{mount}\"}" + ] + end + + private + + def get_info(disk, device) + info = diskinfo(disk) + if info + back = info.split(/\s+/) + io = getline("/proc/diskstats", " #{device} ") + if io + back += io.split(/\s+/) + end + back + else + false + end + end + + def diskinfo(disk) + output = `df | grep "#{disk}"` + if $?.to_i == 0 + Log.debug self + Log.debug output + output + else + Log.warn "Oh there is no such device." + false + end end end end end