public
Created

metrics

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
require 'json'
 
def metrics(values, keys)
Hash[keys.zip(values)]
end
 
values = %x[free -m].match(/cache:\s+(\d+)\s+(\d+)\n/).captures
keys = ["memory-buffers", "memory-cached"]
 
data = metrics(values, keys)
 
values = File.read('/proc/loadavg').match(/^(\d+\.\d{2}) (\d+\.\d{2}) (\d+\.\d{2})/).captures
keys = ["load-1", "load-5", "load-15"]
data.update(metrics(values, keys))
 
filesystems = %x[df -P].split("\n").reject { |lines| lines =~ /1024-blocks|tmpfs/ }
filesystems.each do |fs|
values = fs.match(/^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+(.+)$/).captures
name = values.shift.gsub('/', '-')
keys = ["fs#{name}-size", "fs#{name}-used", "fs#{name}-free", "fs#{name}-%-used", "fs#{name}-mountpoint"]
data.update(metrics(values, keys))
end
 
puts data.to_json

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.