root / plugins / network / radio @ f14628ad
Historique | Voir | Annoter | Télécharger (5,59 ko)
| 1 |
#!/usr/bin/php |
|---|---|
| 2 |
<?php |
| 3 |
|
| 4 |
/* |
| 5 |
Plugin: radio |
| 6 |
Author: Philipp Giebel (spam@stimpyrama.org) |
| 7 |
Version: 0.1 |
| 8 |
|
| 9 |
Munin (http://munin.projects.linpro.no/) Plugin for counting |
| 10 |
listeners to both shout- and icecast streams. |
| 11 |
|
| 12 |
Requirements: * PHP (jap, I know that sucks.. ;) ) |
| 13 |
*/ |
| 14 |
|
| 15 |
// -------------- CONFIGURATION START --------------------------------------------------------------- |
| 16 |
|
| 17 |
$cfg = array( |
| 18 |
// SERVER #1 |
| 19 |
array( "type" => "ice", // server-type (ice/shout) |
| 20 |
"host" => "192.168.1.5", // server hostname or ip |
| 21 |
"port" => 8000, // server port |
| 22 |
"mountpoint" => "eclectix.mp3", // mountpoint to check (icecast only) |
| 23 |
"name" => "IceCast" // name for use in munin graphs |
| 24 |
), |
| 25 |
// SERVER #2 |
| 26 |
array( "type" => "shout", // server-type |
| 27 |
"host" => "radio.eclectix.de", // server hostname or ip |
| 28 |
"port" => 8000, // server port |
| 29 |
"name" => "ShoutCast" // name for use in munin graphs |
| 30 |
) |
| 31 |
); |
| 32 |
|
| 33 |
// -------------- CONFIGURATION END ---------------------------------------------------------------- |
| 34 |
|
| 35 |
function getIce( $host, $port, $mount, $name ) {
|
| 36 |
$error = false; |
| 37 |
$fp = fsockopen( $host, $port, $errno, $errstr, 10 ); |
| 38 |
if ( !$fp ) {
|
| 39 |
$error = $errstr ."(". $errno .")";
|
| 40 |
} else {
|
| 41 |
fputs( $fp, "GET /status HTTP/1.1\r\n" ); |
| 42 |
fputs( $fp, "Host: ". $host ."\r\n" ); |
| 43 |
fputs($fp, "User-Agent: Mozilla\r\n"); |
| 44 |
fputs( $fp, "Connection: close\r\n\r\n" ); |
| 45 |
|
| 46 |
$xml = ""; |
| 47 |
|
| 48 |
while ( !feof( $fp ) ) {
|
| 49 |
$xml .= fgets( $fp, 512 ); |
| 50 |
} |
| 51 |
|
| 52 |
fclose($fp); |
| 53 |
|
| 54 |
if ( stristr( $xml, "HTTP/1.0 200 OK" ) == true ) {
|
| 55 |
$xml = trim( substr( $xml, 42 ) ); |
| 56 |
} else {
|
| 57 |
$error = "Bad login"; |
| 58 |
} |
| 59 |
if ( !$error ) {
|
| 60 |
$res = array( "found" => true ); |
| 61 |
|
| 62 |
$mount = str_replace( ".", "\.", $mount ); |
| 63 |
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 );
|
| 64 |
|
| 65 |
$res["mount"] = $parser[1][0]; |
| 66 |
$res["listeners"] = $parser[2][0]; |
| 67 |
$res["listeners_peak"] = $parser[3][0]; |
| 68 |
$res["name"] = $name; |
| 69 |
} else {
|
| 70 |
$res = $error; |
| 71 |
} |
| 72 |
} |
| 73 |
return $res; |
| 74 |
} |
| 75 |
|
| 76 |
function getShout( $host, $port, $name ) {
|
| 77 |
$error = false; |
| 78 |
$fp = fsockopen( $host, $port, $errno, $errstr, 10 ); |
| 79 |
if ( !$fp ) {
|
| 80 |
$error = $errstr ."(". $errno .")";
|
| 81 |
} else {
|
| 82 |
fputs( $fp, "GET / HTTP/1.0\r\n" ); |
| 83 |
fputs($fp, "User-Agent: Mozilla\r\n"); |
| 84 |
fputs( $fp, "Connection: close\r\n\r\n" ); |
| 85 |
|
| 86 |
$xml = ""; |
| 87 |
|
| 88 |
while ( !feof( $fp ) ) {
|
| 89 |
$xml .= fgets($fp, 512); |
| 90 |
} |
| 91 |
fclose( $fp ); |
| 92 |
|
| 93 |
if ( stristr( $xml, "HTTP/1.0 200 OK" ) == true ) {
|
| 94 |
$xml = trim( substr( $xml, 42 ) ); |
| 95 |
} else {
|
| 96 |
$error = "Bad login"; |
| 97 |
} |
| 98 |
if ( !$error ) {
|
| 99 |
$res = array( "found" => true ); |
| 100 |
|
| 101 |
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 ); |
| 102 |
|
| 103 |
$res["listeners"] = $parser[1][0]; |
| 104 |
$res["name"] = $name; |
| 105 |
|
| 106 |
} else {
|
| 107 |
$res = $error; |
| 108 |
} |
| 109 |
} |
| 110 |
return $res; |
| 111 |
} |
| 112 |
|
| 113 |
switch( $argv[1] ) {
|
| 114 |
case "config": |
| 115 |
echo "graph_title Stream Listeners\n"; |
| 116 |
echo "graph_category Network\n"; |
| 117 |
echo "graph_vlabel listeners\n"; |
| 118 |
echo "graph_hlabel listeners\n"; |
| 119 |
echo "graph_args --base 1000 -l 0\n"; |
| 120 |
echo "graph_scale no\n"; |
| 121 |
echo "graph_order"; |
| 122 |
foreach ( $cfg as $c ) {
|
| 123 |
echo " ". strtolower( $c["name"] ); |
| 124 |
} |
| 125 |
echo " complete\n"; |
| 126 |
// echo "\n"; |
| 127 |
echo "graph_info Number of listeners to shout- and / or icecast streams\n"; |
| 128 |
foreach ( $cfg as $c ) {
|
| 129 |
echo strtolower( $c["name"] ) .".info ". $c["name"] ." listeners\n"; |
| 130 |
echo strtolower( $c["name"] ) .".label ". strtolower( $c["name"] ) ."\n"; |
| 131 |
} |
| 132 |
echo "complete.info Complete listeners\n"; |
| 133 |
echo "complete.label complete\n"; |
| 134 |
break; |
| 135 |
default: |
| 136 |
$complete = 0; |
| 137 |
|
| 138 |
foreach ( $cfg as $c ) {
|
| 139 |
switch ( $c["type"] ) {
|
| 140 |
case "ice": |
| 141 |
$res = getIce( $c["host"], $c["port"], $c["mountpoint"], $c["name"] ); |
| 142 |
$complete += $res["listeners"]; |
| 143 |
break; |
| 144 |
case "shout": |
| 145 |
$res = getShout( $c["host"], $c["port"], $c["name"] ); |
| 146 |
$complete += $res["listeners"]; |
| 147 |
break; |
| 148 |
} |
| 149 |
echo strtolower($c["name"]) .".value ". $res["listeners"] ."\n"; |
| 150 |
} |
| 151 |
|
| 152 |
echo "complete.value ". $complete ."\n"; |
| 153 |
break; |
| 154 |
} |
| 155 |
?> |
