Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: Ruby  |  size: 1.91 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env ruby
  2. require 'getoptlong'
  3. require 'net/telnet'
  4.  
  5. PLUGIN_NAME = 'teamspeak'
  6.  
  7. def usage
  8.   puts("#{$0} -h <host_id> [-i <sampling_interval>] [-a <address>] [-p <port>] [-s <serverid>]")
  9.   exit
  10. end
  11.  
  12. begin
  13.         # Sync stdout so that it will flush to collectd properly.
  14.         $stdout.sync = true
  15.  
  16.         hostname = nil
  17.         address = 'localhost'
  18.         port = 10011
  19.         serverid = 1
  20.         sampling_interval = 20 # sec, default
  21.        
  22.         opts = GetoptLong.new(
  23.                 [ '--hostid', '-h', GetoptLong::REQUIRED_ARGUMENT ],
  24.                 [ '--sampling-interval', '-i', GetoptLong::OPTIONAL_ARGUMENT ],
  25.                 [ '--address', '-a', GetoptLong::OPTIONAL_ARGUMENT ],
  26.                 [ '--port', '-p', GetoptLong::OPTIONAL_ARGUMENT ],
  27.                 [ '--serverid', '-s', GetoptLong::OPTIONAL_ARGUMENT ]
  28.         )
  29.         opts.each do |opt, arg|
  30.                 case opt
  31.                         when '--hostid'
  32.                                 hostname = arg
  33.                         when '--sampling-interval'
  34.                                 sampling_interval = arg.to_i
  35.                         when '--address'
  36.                                 address = arg
  37.                         when '--port'
  38.                                 port = arg.to_i
  39.                         when '--serverid'
  40.                                 serverid = arg.to_i
  41.                 end
  42.         end
  43.         usage if !hostname
  44.  
  45.         server = Net::Telnet::new(
  46.                 "Host" => address,
  47.                 "Port" => port,
  48.                 "Telnetmode" => false,
  49.                 "Prompt" => /\r/)
  50.        
  51.         server.cmd("use sid=" + serverid.to_s())
  52.  
  53.         # Collection loop
  54.         while true do
  55.                 start_run = Time.now.to_i
  56.                 next_run = start_run + sampling_interval
  57.  
  58.                 # collectd data and print the values
  59.                 #data = `uptime`[/load average: ([\d.]+)/, 1] # get 5-minute load average
  60.                 #puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-5_minute_load #{start_run}:#{data}")
  61.  
  62.                 response = server.cmd("serverinfo") #{ |c| print c }
  63.                 clients_regex = /virtualserver_clientsonline=(\d+) /
  64.                 clients = clients_regex.match(response)
  65.                 #print "found match? :"
  66.                 #puts clients[1]
  67.  
  68.                 puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-activity #{start_run}:#{clients[1]}")
  69.                
  70.                 # sleep to make the interval
  71.                 while((time_left = (next_run - Time.now.to_i)) > 0) do
  72.                         sleep(time_left)
  73.                 end
  74.         end
  75. end