Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / network / radio @ 17f78427

Historique | Voir | Annoter | Télécharger (5,02 ko)

1
#!/usr/bin/php
2
<?php
3
/*
4
  Plugin: radio
5
  Author: Philipp Giebel (spam@stimpyrama.org)
6
  Version: 0.2
7
  Munin (http://munin-monitoring.org/) Plugin for counting
8
  listeners to both shout- and icecast streams.
9
  Get the latest version at:
10
  http://munin-monitoring.org/log/munin-contrib/plugins/network/radio
11
  Requirements: * PHP (jap, I know that sucks.. ;) )
12
  CHANGELOG
13
  v0.1 - Quick & Dirty proof of concept
14
  v0.2 - Updated to match output of current versions of ice- and shoutcast.
15
*/
16
// -------------- CONFIGURATION START ---------------------------------------
17
  $cfg = array(
18
    // SERVER #1
19
    array(  "name"        => "IceCast",         // name for munin
20
            "type"        => "ice",             // server-type (ice/shout)
21
            "host"        => "ice.example.com", // server hostname or ip
22
            "port"        => 8000,              // server port
23
            "mountpoint"  => "live"             // mountpoint to check
24
                                                // (icecast only)
25
    ),
26
    // SERVER #2
27
    array(  "name"  => "ShoutCast",             // name for munin
28
            "type"  => "shout",                 // server-type
29
            "host"  => "127.0.0.1",             // server hostname or ip
30
            "port"  => 8000                     // server port
31
    )
32
  );
33
// -------------- CONFIGURATION END -----------------------------------------
34
  error_reporting(E_ERROR);
35
  function getIce( $host, $port, $mount, $name ) {
36
    $error = false;
37
    if ( !$fp = fsockopen( $host, $port, $errno, $errstr, 10 ) ) {
38
      $error = $errstr ."(". $errno .")";
39
    } else {
40
      fputs( $fp, "GET /status.xsl HTTP/1.1\r\n" );
41
      fputs( $fp, "Host: ". $host ."\r\n" );
42
      fputs( $fp, "User-Agent: Mozilla\r\n" );
43
      fputs( $fp, "Connection: close\r\n\r\n" );
44
      $xml = "";
45
      while ( !feof( $fp ) ) {
46
        $xml .= fgets( $fp, 512 );
47
      }
48
      fclose( $fp );
49
      if ( stristr( $xml, "HTTP/1.0 200 OK" ) == true ) {
50
        $xml = trim( substr( $xml, 42 ) );
51
      } else {
52
        $error = "Bad login";
53
      }
54
      if ( !$error ) {
55
        $res = array( "found" => true );
56
        $mount = str_replace( ".", "\.", $mount );
57
        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 );
58
        $res["mount"] = $parser[1][0];
59
        $res["listeners"] = intval( $parser[2][0] );
60
        $res["listeners_peak"] = intval( $parser[3][0] );
61
        $res["name"] = $name;
62
      } else {
63
        $res = $error;
64
      }
65
    }
66
    return $res;
67
  }
68
  function getShout( $host, $port, $name ) {
69
    $error = false;
70
    if ( !$fp = fsockopen( $host, $port, $errno, $errstr, 10 ) ) {
71
      $error = $errstr ."(". $errno .")";
72
    } else {
73
      fputs( $fp, "GET /index.html?sid=1 HTTP/1.0\r\n" );
74
      fputs( $fp, "User-Agent: Mozilla\r\n" );
75
      fputs( $fp, "Connection: close\r\n\r\n" );
76
      $xml = "";
77
      while ( !feof( $fp ) ) {
78
        $xml .= fgets( $fp, 512 );
79
      }
80
      fclose( $fp );
81
      if ( stristr( $xml, "HTTP/1.1 200 OK" ) == true ) {
82
        $xml = trim( substr( $xml, 42 ) );
83
      } else {
84
        $error = "Bad login";
85
      }
86
      if ( !$error ) {
87
        $res = array( "found" => true );
88
        preg_match_all( "/.*?Stream Status: <\/td\>\<td\><b\>Stream is up at \d*? kbps with (\d*?) of \d*? listeners/s", $xml, $parser );
89
        $res["listeners"] = intval( $parser[1][0] );
90
        $res["name"] = $name;
91
      } else {
92
        $res = $error;
93
      }
94
    }
95
    return $res;
96
  }
97
  if ( !isset( $argv[1] ) ) $argv[1] = '';
98
  switch( $argv[1] ) {
99
    case "config":
100
      echo "graph_title Stream Listeners\n";
101
      echo "graph_category streaming\n";
102
      echo "graph_vlabel listeners\n";
103
      echo "graph_hlabel listeners\n";
104
      echo "graph_args --base 1000 -l 0\n";
105
      echo "graph_scale no\n";
106
      echo "graph_info Number of listeners to shout- and / or icecast streams\n";
107
      echo "complete.info Complete listeners\n";
108
      echo "complete.label complete\n";
109
      echo "graph_order";
110
      foreach ( $cfg as $c ) {
111
        echo " ". strtolower( $c["name"] );
112
      }
113
      echo " complete\n";
114
      foreach ( $cfg as $c ) {
115
        echo strtolower( $c["name"] ) .".info ". $c["name"] ." listeners\n";
116
        echo strtolower( $c["name"] ) .".label ". strtolower( $c["name"] ) ."\n";
117
      }
118
    break;
119
    default:
120
      $complete = 0;
121
      foreach ( $cfg as $c ) {
122
        switch ( $c["type"] ) {
123
          case "ice":
124
            $res = getIce( $c["host"], $c["port"], $c["mountpoint"], $c["name"] );
125
          break;
126
          case "shout":
127
            $res = getShout( $c["host"], $c["port"], $c["name"] );
128
          break;
129
        }
130
        if ( is_array( $res ) ) {
131
          echo strtolower($c["name"]) .".value ". $res["listeners"] ."\n";
132
          $complete += $res["listeners"];
133
        } else {
134
          echo strtolower($c["name"]) .".value 0\n";
135
        }
136
      }
137
      echo "complete.value ". $complete ."\n";
138
    break;
139
  }
140
?>