Skip to content

Instantly share code, notes, and snippets.

@marvin
Created March 18, 2017 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvin/6072729aa454fb8a797d2c2a04c61b3c to your computer and use it in GitHub Desktop.
Save marvin/6072729aa454fb8a797d2c2a04c61b3c to your computer and use it in GitHub Desktop.
batman clients counter for sending data to elasticsearch / logstash
#!/usr/bin/env ruby
require 'getoptlong'
# The name of the collectd plugin, something like apache, memory, mysql, interface, ...
PLUGIN_NAME = 'batmanclients'
def usage
puts("#{$0} -h <host_id> [-i <sampling_interval>]")
exit
end
# Main
begin
# Sync stdout so that it will flush to collectd properly.
$stdout.sync = true
# Parse command line options
hostname = nil
sampling_interval = 60 # in seconds, Default value
opts = GetoptLong.new(
['--hostid', '-h', GetoptLong::REQUIRED_ARGUMENT],
['--sampling-interval', '-i', GetoptLong::OPTIONAL_ARGUMENT]
)
opts.each do |opt, arg|
case opt
when '--hostid'
hostname = arg
when '--sampling-interval'
sampling_interval = arg.to_i
end
end
usage if !hostname
# Collection loop
while true do
start_run = Time.now.to_i
next_run = start_run + sampling_interval
# collectd data and print the values
data = `/usr/bin/alfred-json -r 159 -z|/bin/grep clients -A 3|/bin/grep 'total'|/usr/bin/cut -d ' ' -f 8|sed 's/,$//'|/usr/bin/awk '{s+=$1} END {print s}'`
puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-clients_total #{start_run}:#{data}")
# sleep to make the interval
while((time_left = (next_run - Time.now.to_i)) > 0) do
sleep(time_left)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment