Skip to content

Instantly share code, notes, and snippets.

@lafka
Last active June 15, 2016 12:36
Show Gist options
  • Save lafka/851a7371722b0868263cdd0eb974843a to your computer and use it in GitHub Desktop.
Save lafka/851a7371722b0868263cdd0eb974843a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Reports some basic LXC container stats to Riemann
require File.expand_path('../../lib/riemann/tools', __FILE__)
class Riemann::Tools::LXC
include Riemann::Tools
def initialize
@hostname = `hostname`.chomp
end
def tick
File.open("/sys/fs/cgroup/memory/lxc/" + @hostname + "/memory.stat", "r") do |file|
file.each_line do |stat|
stat = stat.split
send("mem #{stat[0]}", stat[1].to_i)
end
end
usage = File.readlines("/sys/fs/cgroup/memory/lxc/" + @hostname + "/memory.usage_in_bytes", "r")[0]
send("mem usage bytes", usage.to_i)
File.open("/sys/fs/cgroup/cpuacct/lxc/" + @hostname + "/cpuacct.stat", "r") do |file|
file.each_line do |stat|
stat = stat.split
send("cpu #{stat[0]}", stat[1].to_i)
end
end
end
def send(type, metric)
report(
:host => @hostname,
:service => "cgroup #{type}",
:metric => metric,
:state => "ok",
:description => nil
)
end
end
Riemann::Tools::LXC.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment