lib/zabbix-ruby-client/plugins/cpu.rb in zabbix-ruby-client-0.0.17 vs lib/zabbix-ruby-client/plugins/cpu.rb in zabbix-ruby-client-0.0.18
- old
+ new
@@ -1,41 +1,50 @@
-# for more info check
+# for more info check
# http://www.linuxhowtos.org/System/procstat.htm
# http://juliano.info/en/Blog:Memory_Leak/Understanding_the_Linux_load_average
require "zabbix-ruby-client/logger"
module ZabbixRubyClient
module Plugins
module Cpu
extend self
+ extend ZabbixRubyClient::PluginBase
def collect(*args)
host = args[0]
- cpuinfo = `cat /proc/stat | grep "^cpu"`
- if $?.to_i == 0
- _, user, nice, sys, idle, wait, irq, soft, guest, steal = cpuinfo.split(/\s+/).map(&:to_i)
- else
- Log.warn "Oh you don't have a /proc ?"
- return []
+ info = get_info
+ back = []
+ if info
+ time = Time.now.to_i
+ info.each do |k,v|
+ back << "#{host} cpu[#{k}] #{time} #{v}"
+ end
end
- used = user + nice + sys
- total = used + idle
+ back
+ end
- time = Time.now.to_i
- back = []
- back << "#{host} cpu[user] #{time} #{user}"
- back << "#{host} cpu[nice] #{time} #{nice}"
- back << "#{host} cpu[system] #{time} #{sys}"
- back << "#{host} cpu[iowait] #{time} #{wait}"
- back << "#{host} cpu[irq] #{time} #{irq}"
- back << "#{host} cpu[soft] #{time} #{soft}"
- back << "#{host} cpu[steal] #{time} #{steal}"
- back << "#{host} cpu[guest] #{time} #{guest}"
- back << "#{host} cpu[idle] #{time} #{idle}"
- back << "#{host} cpu[used] #{time} #{used}"
- back << "#{host} cpu[total] #{time} #{total}"
- return back
+ private
+ def get_info
+ ret = {}
+ info = getline("/proc/stat", "^cpu ")
+ if info
+ back = info.split(/\s+/).map(&:to_i)
+ ret["user"] = back[1]
+ ret["nice"] = back[2]
+ ret["system"] = back[3]
+ ret["idle"] = back[4]
+ ret["iowait"] = back[5]
+ ret["irq"] = back[6]
+ ret["soft"] = back[7]
+ ret["steal"] = back[8] || 0
+ ret["guest"] = back[9] || 0
+ ret["used"] = back[1...4].reduce(&:+)
+ ret["total"] = back[1...9].reduce(&:+)
+ ret
+ else
+ false
+ end
end
end
end
end