Skip to content

Instantly share code, notes, and snippets.

@Atalanta
Created October 6, 2012 05:14
Show Gist options
  • Save Atalanta/3843980 to your computer and use it in GitHub Desktop.
Save Atalanta/3843980 to your computer and use it in GitHub Desktop.
metrics
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment