
Untitled
By: a guest on
Jul 24th, 2012 | syntax:
Ruby | size: 1.91 KB | hits: 18 | expires: Never
#!/usr/bin/env ruby
require 'getoptlong'
require 'net/telnet'
PLUGIN_NAME = 'teamspeak'
def usage
puts("#{$0} -h <host_id> [-i <sampling_interval>] [-a <address>] [-p <port>] [-s <serverid>]")
exit
end
begin
# Sync stdout so that it will flush to collectd properly.
$stdout.sync = true
hostname = nil
address = 'localhost'
port = 10011
serverid = 1
sampling_interval = 20 # sec, default
opts = GetoptLong.new(
[ '--hostid', '-h', GetoptLong::REQUIRED_ARGUMENT ],
[ '--sampling-interval', '-i', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--address', '-a', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--port', '-p', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--serverid', '-s', GetoptLong::OPTIONAL_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when '--hostid'
hostname = arg
when '--sampling-interval'
sampling_interval = arg.to_i
when '--address'
address = arg
when '--port'
port = arg.to_i
when '--serverid'
serverid = arg.to_i
end
end
usage if !hostname
server = Net::Telnet::new(
"Host" => address,
"Port" => port,
"Telnetmode" => false,
"Prompt" => /\r/)
server.cmd("use sid=" + serverid.to_s())
# Collection loop
while true do
start_run = Time.now.to_i
next_run = start_run + sampling_interval
# collectd data and print the values
#data = `uptime`[/load average: ([\d.]+)/, 1] # get 5-minute load average
#puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-5_minute_load #{start_run}:#{data}")
response = server.cmd("serverinfo") #{ |c| print c }
clients_regex = /virtualserver_clientsonline=(\d+) /
clients = clients_regex.match(response)
#print "found match? :"
#puts clients[1]
puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-activity #{start_run}:#{clients[1]}")
# sleep to make the interval
while((time_left = (next_run - Time.now.to_i)) > 0) do
sleep(time_left)
end
end
end