What is rong with this script?

Links for php scripts

Moderators: macek, egami, gesf

cuculetea
New php-forum User
New php-forum User
Posts: 1
Joined: Tue Mar 20, 2012 1:27 pm

What is rong with this script?

Postby cuculetea » Tue Mar 20, 2012 1:37 pm

I use a script tu query users and bandwidth from teamspeak 3
This mean the script open a socket, send some commands, execute some explodes and then print the output. I use this to make grapfics with cacti. The ideea is i have 4 virtual servers.

this is the script

#!/usr/bin/php
<?php
define("EOL" , "\n");
// enable server/client debug messages
define("DEBUG" , false);

/**
* Open a server connection to TS3 server
*
* @param string $host
* @param int $port
* @param string $errno
* @param string $errstr
* @param int $timeout
*/
function Open($host, $port, &$errno, &$errstr, $timeout)
{
$socket = fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$socket or fread($socket, 4) != "TS3\n") {
die("ERR: Teamspeak server not running!" . EOL);
return false;
}
return $socket;
}

/**
* Select the virtualserver on opened TS3 server connection
*
* @param handle $socket
* @param int $serverid
*/
function selectServerId($socket, $serverid)
{
$command = "use sid={$serverid}";
if( DEBUG ) echo ' >> ' . $command . EOL;
fputs($socket, $command . EOL);
if ( ($msg = Expected($socket,'ok')) !== TRUE ) {
die('ERR: ' . ErrMessage($msg) . EOL);
}
}

/**
* Returns and array containing serverinfo variables
* note: that variables are different if you connect to localhost or a worldwide server
*
* @param handle $socket
*/
function serverInfo($socket)
{
$command = 'serverinfo';
if( DEBUG ) echo ' >> ' . $command . EOL;
fputs($socket, $command . EOL);
return serverInfo2Array(Read($socket));
}
// private use is easy to debug
function serverInfo2Array($sinfo)
{
$out = explode(" ", $sinfo);
$result = array();
foreach($out as $line){
if( DEBUG ) echo ' s: ' . $line . EOL;
@list($k,$v) = explode("=", $line);
$result[$k] = str_replace('\s'," ", $v);
}
return $result;
}

/**
* Perform login on remote TS3 Server
*
* @param handle $socket
* @param handle $user
* @param handle $password
*/
function Login($socket, $user, $password)
{
$command = "login client_login_name={$user} client_login_password={$password}";
if( DEBUG ) echo ' >> ' . $command . EOL;
fputs($socket, $command . EOL);
if ( ($msg = Expected($socket,'ok')) !== TRUE ) {
die('ERR: ' . ErrMessage($msg) . EOL);
}
}

/**
* Check if the string contains at its end a message
*
* @param handle $socket
* @param string $msg
*/
function Expected($socket, $msg )
{
$out = trim(fread($socket, 10000));
if( DEBUG ) echo ' << ' . $out . EOL;
$pattern = '/' . $msg .'$/';
if ( preg_match('/' . $msg .'$/', $out ) ) {
return TRUE;
} else {
return $out;
}
return $out;
}

/**
* Simply read from socket
*
* @param handle $socket
*/
function Read($socket)
{
$out = trim(fread($socket, 10000));
if( DEBUG ) echo ' << ' . $out . EOL;
return $out;
}

/**
* Close a server connection to TS3 server
*
* @param handle $socket
*/
function Close($socket)
{
$command = 'quit';
if( DEBUG ) echo ' >> ' . $out . EOL;
fputs($socket, $command . EOL);
fclose($socket);
}
/**
* Parse message from server, ONLY message not even extra_msg
*
* @param string $string
*/
function ErrMessage($string)
{
$err = preg_match('/msg=(.*) extra_msg=(.*)/', $string, $out);
return str_replace('\s'," ", $out[1]);
}

// usage
if ( count($argv) < 6 ) {
echo "Usage: {$argv[0]} <ip> <sid> <user> <pass> < clients | bandwidth >" . EOL;
exit;
}
// open
$errstr = $errno = '';
$socket = Open($argv[1], 10011, $errno, $errstr, 1);
if($errno != 0) exit(1);
// select server id
selectServerId($socket, $argv[2]);
// login
Login($socket, $argv[3], $argv[4]);
// fetch server infoes
$out = serverInfo($socket);
// parse argv[5]
switch($argv[5]) {
case 'clients':
$max = intval(@$out['virtualserver_maxclients']);
$now = intval(@$out['virtualserver_clientsonline']) - 1; // server count us as a client so -1
echo "max:{$max} now:{$now}";
break;
case 'bandwidth':
$in = intval(@$out['connection_bandwidth_received_last_minute_total']) ;
$out = intval(@$out['connection_bandwidth_sent_last_minute_total']) ;
echo "band_out:{$out} band_in:{$in}";
break;
}


The problem is when i run this for multiple times it`s get freeze for 1-2 min. On cacti a crontab is run on every 5 min. Because i have 4 virtual servers on team speak, this script run 8 times. 905 of time is get more then 5 min to run 8 times and all my grapfic from cacti will go empty.

I belife is a problem with open socket from php because if i enable debug and start to run from putty
"php script.php address server-id username password clients|bandwidth" and use up arrow and enter multiple time....the script get freeze after i run him 8 times, other time after i run the script 50 times. If i let him allone after 1-2 min i will get the results. But i don`t understande why he get freeze.

If anyone can help me....

P.S. sorry for my poor english
Thanks in advance,
Andrei

Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest

cron