Skip to content

Instantly share code, notes, and snippets.

@darron
Last active October 14, 2016 23:38
Show Gist options
  • Save darron/dfcaa505ae078a76a08f to your computer and use it in GitHub Desktop.
Save darron/dfcaa505ae078a76a08f to your computer and use it in GitHub Desktop.
Combine RSS memory usage and Apache 2 /server-status for heatmap with Datadog.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'pp'
require 'statsd'
statsd = Statsd.new
def get_memory_usage
memory_usage = {}
ps = `ps -C apache2 -o pid,rss --no-header`
memory = ps.split("\n")
memory = memory.collect{|mem| mem.strip}
memory.each do |mem|
usage = mem.split(" ")
memory_usage[usage[0]] = usage[1]
end
memory_usage
end
while true do
memory = get_memory_usage
puts "#{memory.count} processes Memory Usage: #{memory.inspect}"
doc = Nokogiri::HTML(open("http://127.0.0.1/server-status"))
rows = doc.xpath("//tr")
running = 1
rows.each do |row|
if row.children[0].name == 'td'
array = row.children.to_a
if array[1].to_s != "<td>-</td>\n"
pid = array[1].to_s.scan(/[0-9]*/)
site = array[11].to_s.scan(/<td nowrap>(.*)<\/td>/)
puts "We have a running process number #{running}"
puts "Process: #{pid}"
puts "Memory: " + memory["#{pid}"] unless memory["#{pid}"].nil?
puts "Site: '#{site}' #{site.class}"
running += 1
if site.to_s =~ /\*\.bam/
puts "Not sending stats for bam - not useful"
elsif site.to_s =~ /\?/
puts "NOT sending stats for unknown site."
else
statsd.histogram('apache.rss_memory_tagged', memory["#{pid}"], :tags => ["site:#{site}"])
puts "Sent stats for #{site}"
end
puts "\n\n"
end
end
end
sleep(5)
end
{
"viz": "heatmap",
"requests": [
{
"q": "avg:apache.rss_memory_tagged.max{host:bam} by {site}",
"type": "area"
}
],
"events": []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment