Get Today’s Bandwith Usage from a Shoutcast Log

By | June 29, 2014

Shoutcast logs can be pretty big, if you don’t read them in line by line there is a fair chance of getting an out of memory error. The following function getShoutcastBW(logfile) simply returns the number of bytes streamed today out of the log. Good for calculating bandwidth.

function my_strip($start,$end,$total){
	$total = stristr($total,$start);
	$f2 = stristr($total,$end);
	return substr($total,strlen($start),-strlen($f2));
}

function getShoutcastBW($logfile) {
	$bytes=0;
	$newbytes=0;
	$file_handle = fopen($logfile, "r");
	while (!feof($file_handle)) {
	    $line = fgets($file_handle);   
		$linedate = my_strip("<","@",$line);
		$new_date = date( 'm-d-Y', strtotime( $linedate ) );
		$today = date("m-d-Y");
		if ($today == $new_date) {
			$newbytes = my_strip("{Bytes: ","}",$line);
			$bytes+=$newbytes;
		}
	}
	fclose($file_handle);
	return $bytes;
}

echo "Total bytes streamed today is: " . getShoutcastBW("/etc/shoutcast/sc_serv.log");

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *