Skip to content

Instantly share code, notes, and snippets.

@maguec
Created March 16, 2013 17:36
Show Gist options
  • Save maguec/5177424 to your computer and use it in GitHub Desktop.
Save maguec/5177424 to your computer and use it in GitHub Desktop.
A sample plugin to collect data from squid
#!/usr/bin/env ruby
require 'getoptlong'
require 'yaml'
#need to sync stdout with collectd execs!
$stdout.sync = true
if File.readable? '/etc/mcollective/facts.yaml'
facts=YAML::load(File.open('/etc/mcollective/facts.yaml'))
else
exit! 1
end
args = { :interval => 30,
:hostname => facts['hostname'],
:path => '/usr/local/squid/bin/squidclient',
:port => 3128 }
opts = GetoptLong.new(
[ '--interval', '-i', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--port', '-p', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--host', '-h', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--path', '-u', GetoptLong::OPTIONAL_ARGUMENT ],
)
opts.each do |opt, arg|
case opt
when '--interval'
args[:interval] = arg.to_i
when '--port'
args[:port] = arg.to_i
when '--host'
args[:host] = arg
when '--path'
args[:path] = arg
end
end
@@regexps = {
:all_requests_service_median => { :exp => Regexp.compile(/HTTP Requests \(All\): ([0-9\.]+)/), :type => 'gauge' },
:all_cache_hit_rate => { :exp => Regexp.compile(/Hits as \% of all requests:\s+5min:\s+([0-9\.]+)/), :type => 'gauge' },
:cache_memory_hit_rate => { :exp => Regexp.compile(/Memory hits as \% of hit requests:\s+5min:\s+([0-9\.]+)/), :type => 'gauge' },
:cache_disk_hit_rate => { :exp => Regexp.compile(/Disk hits as \% of hit requests:\s+5min:\s+([0-9\.]+)/), :type => 'gauge' },
:storage_swap_capcity => { :exp => Regexp.compile(/Storage Swap capacity:\s+([0-9\.]+)\% used/), :type => 'gauge' },
:cache_miss_service_median => { :exp => Regexp.compile(/Cache Misses:\s+([0-9\.]+)/), :type => 'gauge' },
:cache_hits_service_median => { :exp => Regexp.compile(/Cache Hits:\s+([0-9\.]+)/), :type => 'gauge' },
:cache_dns_service_median => { :exp => Regexp.compile(/DNS Lookups:\s+([0-9\.]+)/), :type => 'gauge' },
:all_requests => { :exp => Regexp.compile(/Number of HTTP requests received:\s+(\d+)/), :type => 'counter' },
:all_clients => { :exp => Regexp.compile(/Number of clients accessing cache:\s+(\d+)/), :type => 'gauge' },
}
def collect_data(start_run, args)
cmdrun = IO.popen("#{args[:path]} -p #{args[:port]} mgr:info")
stats = cmdrun.read.split("\n")
cmdrun.close
stats.each do |line|
@@regexps.each do |k, v|
if line =~ v[:exp]
puts "PUTVAL #{args[:hostname]}/squid/#{v[:type]}-#{k}_#{args[:port]} #{start_run}:#{$1}"
end
end
end
end
while true do
start_run = Time.now.to_i
next_run = start_run + args[:interval]
collect_data(start_run, args)
while((time_left = (next_run - Time.now.to_i)) > 0) do
sleep(time_left)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment