2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

Merge pull request #649 from stimpy23/master

Updated to v0.2
This commit is contained in:
Kenyon Ralph 2015-10-27 11:07:55 -07:00
commit 4c6dc3b0f1

View File

@ -1,56 +1,51 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/* /*
Plugin: radio Plugin: radio
Author: Philipp Giebel (spam@stimpyrama.org) Author: Philipp Giebel (spam@stimpyrama.org)
Version: 0.1 Version: 0.2
Munin (http://munin-monitoring.org/) Plugin for counting
Munin (http://munin.projects.linpro.no/) Plugin for counting
listeners to both shout- and icecast streams. listeners to both shout- and icecast streams.
Get the latest version at:
http://munin-monitoring.org/log/munin-contrib/plugins/network/radio
Requirements: * PHP (jap, I know that sucks.. ;) ) Requirements: * PHP (jap, I know that sucks.. ;) )
CHANGELOG
v0.1 - Quick & Dirty proof of concept
v0.2 - Updated to match output of current versions of ice- and shoutcast.
*/ */
// -------------- CONFIGURATION START ---------------------------------------
// -------------- CONFIGURATION START ---------------------------------------------------------------
$cfg = array( $cfg = array(
// SERVER #1 // SERVER #1
array( "type" => "ice", // server-type (ice/shout) array( "name" => "IceCast", // name for munin
"host" => "192.168.1.5", // server hostname or ip "type" => "ice", // server-type (ice/shout)
"host" => "ice.example.com", // server hostname or ip
"port" => 8000, // server port "port" => 8000, // server port
"mountpoint" => "eclectix.mp3", // mountpoint to check (icecast only) "mountpoint" => "live" // mountpoint to check
"name" => "IceCast" // name for use in munin graphs // (icecast only)
), ),
// SERVER #2 // SERVER #2
array( "type" => "shout", // server-type array( "name" => "ShoutCast", // name for munin
"host" => "radio.eclectix.de", // server hostname or ip "type" => "shout", // server-type
"port" => 8000, // server port "host" => "127.0.0.1", // server hostname or ip
"name" => "ShoutCast" // name for use in munin graphs "port" => 8000 // server port
) )
); );
// -------------- CONFIGURATION END -----------------------------------------
// -------------- CONFIGURATION END ---------------------------------------------------------------- error_reporting(E_ERROR);
function getIce( $host, $port, $mount, $name ) { function getIce( $host, $port, $mount, $name ) {
$error = false; $error = false;
$fp = fsockopen( $host, $port, $errno, $errstr, 10 ); if ( !$fp = fsockopen( $host, $port, $errno, $errstr, 10 ) ) {
if ( !$fp ) {
$error = $errstr ."(". $errno .")"; $error = $errstr ."(". $errno .")";
} else { } else {
fputs( $fp, "GET /status HTTP/1.1\r\n" ); fputs( $fp, "GET /status.xsl HTTP/1.1\r\n" );
fputs( $fp, "Host: ". $host ."\r\n" ); fputs( $fp, "Host: ". $host ."\r\n" );
fputs( $fp, "User-Agent: Mozilla\r\n" ); fputs( $fp, "User-Agent: Mozilla\r\n" );
fputs( $fp, "Connection: close\r\n\r\n" ); fputs( $fp, "Connection: close\r\n\r\n" );
$xml = ""; $xml = "";
while ( !feof( $fp ) ) { while ( !feof( $fp ) ) {
$xml .= fgets( $fp, 512 ); $xml .= fgets( $fp, 512 );
} }
fclose( $fp ); fclose( $fp );
if ( stristr( $xml, "HTTP/1.0 200 OK" ) == true ) { if ( stristr( $xml, "HTTP/1.0 200 OK" ) == true ) {
$xml = trim( substr( $xml, 42 ) ); $xml = trim( substr( $xml, 42 ) );
} else { } else {
@ -58,13 +53,11 @@
} }
if ( !$error ) { if ( !$error ) {
$res = array( "found" => true ); $res = array( "found" => true );
$mount = str_replace( ".", "\.", $mount ); $mount = str_replace( ".", "\.", $mount );
preg_match_all( "/Mount Point : \(\/(". $mount .")\).*?\<tr\>\<td\>Current Listeners:\<\/td\>\<td class=\"streamdata\"\>(\d*?)\<\/td\>\<\/tr\><tr>\<td\>Peak Listeners:\<\/td\>\<td class=\"streamdata\"\>(\d*?)\<\/td\>\<\/tr\>/s", $xml, $parser ); preg_match_all( "/Mount Point \/(". $mount .").*?\<tr\>\<td\>Current Listeners:\<\/td\>\<td class=\"streamdata\"\>(\d*?)\<\/td\>\<\/tr\><tr>\<td\>Peak Listeners:\<\/td\>\<td class=\"streamdata\"\>(\d*?)\<\/td\>\<\/tr\>/s", $xml, $parser );
$res["mount"] = $parser[1][0]; $res["mount"] = $parser[1][0];
$res["listeners"] = $parser[2][0]; $res["listeners"] = intval( $parser[2][0] );
$res["listeners_peak"] = $parser[3][0]; $res["listeners_peak"] = intval( $parser[3][0] );
$res["name"] = $name; $res["name"] = $name;
} else { } else {
$res = $error; $res = $error;
@ -72,44 +65,36 @@
} }
return $res; return $res;
} }
function getShout( $host, $port, $name ) { function getShout( $host, $port, $name ) {
$error = false; $error = false;
$fp = fsockopen( $host, $port, $errno, $errstr, 10 ); if ( !$fp = fsockopen( $host, $port, $errno, $errstr, 10 ) ) {
if ( !$fp ) {
$error = $errstr ."(". $errno .")"; $error = $errstr ."(". $errno .")";
} else { } else {
fputs( $fp, "GET / HTTP/1.0\r\n" ); fputs( $fp, "GET /index.html?sid=1 HTTP/1.0\r\n" );
fputs( $fp, "User-Agent: Mozilla\r\n" ); fputs( $fp, "User-Agent: Mozilla\r\n" );
fputs( $fp, "Connection: close\r\n\r\n" ); fputs( $fp, "Connection: close\r\n\r\n" );
$xml = ""; $xml = "";
while ( !feof( $fp ) ) { while ( !feof( $fp ) ) {
$xml .= fgets( $fp, 512 ); $xml .= fgets( $fp, 512 );
} }
fclose( $fp ); fclose( $fp );
if ( stristr( $xml, "HTTP/1.1 200 OK" ) == true ) {
if ( stristr( $xml, "HTTP/1.0 200 OK" ) == true ) {
$xml = trim( substr( $xml, 42 ) ); $xml = trim( substr( $xml, 42 ) );
} else { } else {
$error = "Bad login"; $error = "Bad login";
} }
if ( !$error ) { if ( !$error ) {
$res = array( "found" => true ); $res = array( "found" => true );
preg_match_all( "/.*?Stream Status: <\/td\>\<td\><b\>Stream is up at \d*? kbps with (\d*?) of \d*? listeners/s", $xml, $parser );
preg_match_all( "/.*?Stream Status: \<\/font\>\<\/td\>\<td\>\<font class=default\>\<b\>Stream is up at \d*? kbps with \<B\>(\d*?) of \d*? listeners \(\d*? unique\)\<\/b\>\<\/b\>/s", $xml, $parser ); $res["listeners"] = intval( $parser[1][0] );
$res["listeners"] = $parser[1][0];
$res["name"] = $name; $res["name"] = $name;
} else { } else {
$res = $error; $res = $error;
} }
} }
return $res; return $res;
} }
if ( !isset( $argv[1] ) ) $argv[1] = '';
switch( $argv[1] ) { switch( $argv[1] ) {
case "config": case "config":
echo "graph_title Stream Listeners\n"; echo "graph_title Stream Listeners\n";
@ -118,37 +103,37 @@
echo "graph_hlabel listeners\n"; echo "graph_hlabel listeners\n";
echo "graph_args --base 1000 -l 0\n"; echo "graph_args --base 1000 -l 0\n";
echo "graph_scale no\n"; echo "graph_scale no\n";
echo "graph_info Number of listeners to shout- and / or icecast streams\n";
echo "complete.info Complete listeners\n";
echo "complete.label complete\n";
echo "graph_order"; echo "graph_order";
foreach ( $cfg as $c ) { foreach ( $cfg as $c ) {
echo " ". strtolower( $c["name"] ); echo " ". strtolower( $c["name"] );
} }
echo " complete\n"; echo " complete\n";
// echo "\n";
echo "graph_info Number of listeners to shout- and / or icecast streams\n";
foreach ( $cfg as $c ) { foreach ( $cfg as $c ) {
echo strtolower( $c["name"] ) .".info ". $c["name"] ." listeners\n"; echo strtolower( $c["name"] ) .".info ". $c["name"] ." listeners\n";
echo strtolower( $c["name"] ) .".label ". strtolower( $c["name"] ) ."\n"; echo strtolower( $c["name"] ) .".label ". strtolower( $c["name"] ) ."\n";
} }
echo "complete.info Complete listeners\n";
echo "complete.label complete\n";
break; break;
default: default:
$complete = 0; $complete = 0;
foreach ( $cfg as $c ) { foreach ( $cfg as $c ) {
switch ( $c["type"] ) { switch ( $c["type"] ) {
case "ice": case "ice":
$res = getIce( $c["host"], $c["port"], $c["mountpoint"], $c["name"] ); $res = getIce( $c["host"], $c["port"], $c["mountpoint"], $c["name"] );
$complete += $res["listeners"];
break; break;
case "shout": case "shout":
$res = getShout( $c["host"], $c["port"], $c["name"] ); $res = getShout( $c["host"], $c["port"], $c["name"] );
$complete += $res["listeners"];
break; break;
} }
if ( is_array( $res ) ) {
echo strtolower($c["name"]) .".value ". $res["listeners"] ."\n"; echo strtolower($c["name"]) .".value ". $res["listeners"] ."\n";
$complete += $res["listeners"];
} else {
echo strtolower($c["name"]) .".value 0\n";
}
} }
echo "complete.value ". $complete ."\n"; echo "complete.value ". $complete ."\n";
break; break;
} }