Forum


Notice to all users

We are migrating towards a new forum system located at community.teamspeak.com, as such this forum will become read-only on January 29, 2020

Results 1 to 3 of 3

Thread: MRTG TeamSpeak3

  1. #1
    Join Date
    February 2011
    Location
    Russia
    Posts
    23

    MRTG TeamSpeak3

    Hello.

    Can anyone help me with script for mrtg?

    Need to print total online clients on host.

    hostinfo
    virtualservers_total_clients_online=150 <--- This

  2. #2
    Join Date
    February 2012
    Location
    Germany
    Posts
    577
    I monitor my Teamspeak server with cacti, which is a bit more sophisticated wrapper around rrdtool than mrtg, but somewhat related. For cacti, I wrote this php command line script to get some information from the Teamspeak server. With a few tweaks, you might probably adapt it for mrtg:
    (the global.php is probably not needed - try to comment it away if you know what you are doing)

    Code:
    <?php
    
    /* do NOT run this script through a web browser */
    if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD'])  || isset($_SERVER['REMOTE_ADDR'])) {
      die("<br><strong>This script is only meant to run at the command line.</strong>");
    }
    
    $no_http_headers = true;
    
    /* display no errors */
    error_reporting(0);
    
    if (!isset($called_by_script_server)) {
      include_once(dirname(__FILE__) . "/../include/global.php");
      array_shift($_SERVER["argv"]);
      print call_user_func_array("ss_teamspeak3", $_SERVER["argv"]);
    }
    
    
    # decode \? sequences in given scalar string
    function teamspeak3_decode($s) {
    
      return str_replace(
        array('\\',   '\/', '\s', '\p', '\a',  '\b',  '\f',   '\n',   '\r',   '\t',  '\v'),
        array("\x92", '/',  ' ',  '|',  "\x7", "\x8", "\x12", "\x10", "\x13", "\x9", "\x11"),
        $s);
    }
    
    # explode "kw1=value1 kw2=value2 ..." into array[kwX]=valueX
    function teamspeak3_str2array($line) {
    
      $arr = array();
      foreach (explode(" ", $line) as $pair) {
        @list($key, $value) = explode("=", $pair, 2);
        $arr[$key] = teamspeak3_decode($value);
      }
      return $arr;
    }
    
    
    # connect to Teamspeak 3 server
    function teamspeak3_connect($host, $port) {
    
      # 5 seconds connect timeout
      $sock = fsockopen($host, $port, $errno, $errstr, 5);
      if (!$sock)
        return "ERROR: unable to connect to Teamspeak 3 server '$host' on port $port: $errstr\n";
    
      # set timeout of 2 seconds for stream reading
      stream_set_timeout($sock, 2, 0);
    
      if (fgets($sock) != "TS3\n")
        return "ERROR: Teamspeak 3 server didn't respond properly with 'TS3'";
    
      # skip welcome message
      fread($sock, 1000);
    
      return $sock;
    }
    
    
    # disconnect from Teamspeak 3 server
    function teamspeak3_disconnect($sock) {
    
      teamspeak3_command($sock, 'quit');
      fclose($sock);
    }
    
    
    # send a command to the currently connected teamspeak server
    # return array:
    # $result['cmd']=<command>
    # $result['raw'][1..n]=<response line>
    # $result['error']['id']=<number>
    # $result['error']['msg']=message
    # $result['result'][<kw>]=<value>
    function teamspeak3_command($sock, $cmd) {
    
      $result = array();
      $result['cmd'] = $cmd;
      fputs($sock, "$cmd\n");
    #  echo "Sent:------'$cmd'---\n";
    
      # a command responds with:
      # 1 line with command-specific response, usually keyword=value ... pairs
      # error id=0 msg=ok
      # or with error:
      # error id=256 msg=command\snot\sfound
      while (1) {
        $line = ltrim(rtrim(fgets($sock)), "\r");
        $result['raw'][] = $line;
    #    echo "Received:--'" . bin2hex($line) . "'---\n";
        $info = stream_get_meta_data($sock);
        if ($info['timed_out']) {
          $result['error']['id'] = -1;
          $result['error']['msg'] = 'Connection timed out';
          break;
        }
    #    echo "checking substr(line, 0, 5)='" . substr($line, 0, 5) . "'\n";
        if (substr($line, 0, 5) == 'error') {
          $result['error'] = teamspeak3_str2array($line);
          break;
        }
      }
      if (count($result['raw']) == 2) {
        $result['result'] = teamspeak3_str2array($result['raw'][0]);
      }
      return $result;
    }
    
    
    # select given virtual server
    function teamspeak3_select_virtualserver($sock, $sid) {
    
      return teamspeak3_command($sock, "use sid=$sid");
    }
    
    
    # login to the server
    function teamspeak3_login($sock, $username, $password) {
    
      return teamspeak3_command($sock, "login client_login_name=$username client_login_password=$password");
    }
    
    
    # get server info
    function teamspeak3_serverinfo($sock) {
    
      return teamspeak3_command($sock, "serverinfo");
    }
    
    function ss_teamspeak3($host=NULL, $sid=NULL, $username=NULL, $password=NULL) {
    
      if (!isset($password)) {
        echo "host=$host\n";
        echo "Usage: ss_teamspeak3.php <hostname> <sid> <username> <password>\n";
        return "ERROR: bad commandline arguments";
      }
    
      $sock = teamspeak3_connect($host, 10011);
      if (!is_resource($sock))
        return $sock;
    
      $result = teamspeak3_login($sock, $username, $password);
      if ($result['error']['id']!=0)
        return "ERROR: unable to login to the Teamspeak server: {$result['error']['msg']}";
    
      $result = teamspeak3_select_virtualserver($sock, $sid);
      if ($result['error']['id']!=0)
        return "ERROR: unable to select virtual server '$sid': {$result['error']['msg']}";
    
      $result = teamspeak3_serverinfo($sock);
      if ($result['error']['id']!=0)
        return "ERROR: unable to get server info: {$result['error']['msg']}";
    
      teamspeak3_disconnect($sock);
      $r = $result['result'];
      #     1234567890123456789
      $clientsonline = $r['virtualserver_clientsonline'] - $r['virtualserver_queryclientsonline'];
      $s = "maxclients:{$r['virtualserver_maxclients']} " .
           "clientsonline:{$clientsonline} " .
           "pktloss_speech:{$r['virtualserver_total_packetloss_speech']} " .
           "pktloss_keepalive:{$r['virtualserver_total_packetloss_keepalive']} " .
           "pktloss_control:{$r['virtualserver_total_packetloss_control']} " .
           "pktloss_total:{$r['virtualserver_total_packetloss_total']} " .
           "ping:{$r['virtualserver_total_ping']} " .
           "bytes_sent_file:{$r['connection_filetransfer_bytes_sent_total']} " .
           "bytes_recv_file:{$r['connection_filetransfer_bytes_received_total']} " .
           "bytes_sent_speech:{$r['connection_bytes_sent_speech']} " .
           "bytes_recv_speech:{$r['connection_bytes_received_speech']} " .
           "bytes_sent_keepaliv:{$r['connection_bytes_sent_keepalive']} " .
           "bytes_recv_keepaliv:{$r['connection_bytes_received_keepalive']} " .
           "bytes_sent_control:{$r['connection_bytes_sent_control']} " .
           "bytes_recv_control:{$r['connection_bytes_received_control']} " .
           "bytes_sent_total:{$r['connection_bytes_sent_total']} " .
           "bytes_recv_total:{$r['connection_bytes_received_total']}";
      #     1234567890123456789
      return $s;
    }
    
    ?>

  3. #3
    Join Date
    February 2011
    Location
    Russia
    Posts
    23
    Dont know php.
    So can anyone help with edit code above?
    After exec it, it must show:
    7415 //*1st stroke: virtualservers_total_maxclients
    2334 //*2nd stroke: virtualservers_total_clients_online
    Thx

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •