lib/zabbix-ruby-client/plugins/memory.rb in zabbix-ruby-client-0.1.2 vs lib/zabbix-ruby-client/plugins/memory.rb in zabbix-ruby-client-0.1.3
- old
+ new
@@ -9,11 +9,10 @@
def collect(*args)
host = args[0]
info = get_info
if info
- time = Time.now.to_i
back = []
back << "#{host} memory[total] #{time} #{info['MemTotal']}"
back << "#{host} memory[free] #{time} #{info['MemFree']}"
back << "#{host} memory[used] #{time} #{info['MemUsed']}"
back << "#{host} memory[percent_used] #{time} #{info['MemPercent']}"
@@ -28,19 +27,42 @@
end
private
def get_info
- info = meminfo
- if info
- back = splitinfo(info)
- back["MemTotal"] = back["MemTotal"] * 1024
- back["MemFree"] = (back['MemFree'] + back['Buffers'] + back['Cached']) * 1024
- back["MemUsed"] = back["MemTotal"] - back["MemFree"]
- back["MemPercent"] = (back["MemUsed"] / back["MemTotal"].to_f * 100).to_i
- back['SwapTotal'] = back['SwapTotal'] * 1024
- back['SwapFree'] = back['SwapFree'] * 1024
- back['SwapUsed'] = back['SwapTotal'] - back['SwapFree']
+ case os
+ when :linux
+ info = meminfo
+ if info
+ back = splitinfo(info)
+ back["MemTotal"] = back["MemTotal"] * 1024
+ back["MemFree"] = (back['MemFree'] + back['Buffers'] + back['Cached']) * 1024
+ back["MemUsed"] = back["MemTotal"] - back["MemFree"]
+ back["MemPercent"] = (back["MemUsed"] / back["MemTotal"].to_f * 100).to_i
+ back['SwapTotal'] = back['SwapTotal'] * 1024
+ back['SwapFree'] = back['SwapFree'] * 1024
+ back['SwapUsed'] = back['SwapTotal'] - back['SwapFree']
+ back['SwapPercent'] = 0
+ unless back['SwapTotal'] == 0
+ back['SwapPercent'] = (back['SwapUsed'] / back['SwapTotal'].to_f * 100).to_i
+ end
+ back
+ else
+ false
+ end
+ when :unix
+ memtotal = `sysctl hw.realmem | cut -d' ' -f2`.chop
+ memused = `sysctl hw.usermem | cut -d' ' -f2`.chop
+ swap = `swapinfo -k | tail -n 1`
+ _, _, swapused, swaptotal = *swap.split(/\s+/)
+ back = {}
+ back["MemTotal"] = memtotal
+ back["MemFree"] = memtotal.to_i - memused.to_i
+ back["MemUsed"] = memused
+ back["MemPercent"] = (memused.to_i / memtotal.to_f * 100).to_i
+ back['SwapTotal'] = swaptotal.to_i * 1024
+ back['SwapUsed'] = swapused.to_i * 1024
+ back['SwapFree'] = back['SwapTotal'] - back['SwapUsed']
back['SwapPercent'] = 0
unless back['SwapTotal'] == 0
back['SwapPercent'] = (back['SwapUsed'] / back['SwapTotal'].to_f * 100).to_i
end
back