2015-09-16 45 views
1

因此,我試圖更新下面的代碼,它監視icecast和shoutcast監聽器,因爲當我升級到shoutcast 2時,它停止工作,作者是AWOL或至少不選擇回答。更新munin shoutcast監控插件代碼從版本1到2

現在我認爲我已經解決了正則表達式的問題,但它仍然無法正常工作。這讓我想到代碼檢索狀態頁面和新URL格式的方式也是一個問題。

無論如何,我將不勝感激任何幫助,你可以給我試圖解決如何解決這個問題,謝謝!

的源代碼:https://github.com/munin-monitoring/contrib/blob/f444e600c9718ba249d46d3b44d6b6ebaccb0aa3/plugins/network/radio

從管線158中的舊的正則表達式:

preg_match_all("/.*?Stream Status: \<\/td\>\<td\>\<b\>\Stream is up at \d*? kbps with (\d*?) of \d*? listeners\<\/b\>\<\/td\>\<\/tr\>/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/s", $xml, $parser); 

問題代碼:

function getShout($host, $port, $name) { 
      $error = false; 
      $fp = fsockopen($host, $port, $errno, $errstr, 10); 
      if (!$fp) { 
        $error = $errstr ."(". $errno .")"; 
      } else { 
        fputs($fp, "GET/HTTP/1.0\r\n"); 
        fputs($fp, "User-Agent: Mozilla\r\n"); 
        fputs($fp, "Connection: close\r\n\r\n"); 

        $xml = ""; 

        while (!feof($fp)) { 
          $xml .= fgets($fp, 512); 
        } 
        fclose($fp); 

        if (stristr($xml, "HTTP/1.0 200 OK") == true) { 
          $xml = trim(substr($xml, 42)); 
        } else { 
          $error = "Bad login"; 
        } 
        if (!$error) { 
          $res = array("found" => true); 

          preg_match_all("/.*?Stream Status: \<\/font\>\<\/td\>\<td>\<font class\=default\>\<b\>Stream is up at (\d*?) kbps with \<B\>(\d*?) of (\d*?) listeners/s", $xml, $parser); 

          $res["listeners"] = $parser[1][0]; 
          $res["name"] = $name; 

        } else { 
          $res = $error; 
        } 
      } 
      return $res; 
    } 

的OU tput的我從在命令行運行插件得到...

rock.value B

pop.value B

rap.value B

dnb.value B

psytrance.value B

breaks.value B

dubstep.value B

reggae.value B

techno.value B

ambient.value B

complete.value 0

樣品Shoutcast的V1狀態頁:http://stream.radioamlo.info:8010/

樣品Shoutcast的V2狀態頁:http://91.221.151.237:8994/index.html?sid=1


更新#1 :我的第二個正則表達式看起來更好:

preg_match_all("/.*?Stream Status: \<\/font\>\<\/td\>\<td>\<font class\=default\>\<b\>Stream is up at \d*? kbps with \<B\>(\d*?) of \d*? 

UPDATE#2:添加'echo $ xml;' '$ error =「Bad login」後';'返回以下錯誤:

<html><head><title>Redirect</title></head><body>Click <a href="/index.html?sid=1">here</a> for redirect.</body></html>techno.value B 
HTTP/1.1 302 Found 
Content-Type:text/html;charset=utf-8 
Location:index.html?sid=1 
Content-Length:118 
+0

請問您可以發佈$ xml的示例輸出嗎? – fjellfly

+0

對不起,我該如何生成? –

+0

例如在帶有preg_match_all的行之後帶有'echo $ xml'。 – fjellfly

回答

0

根據你寫的問題是,你的信息被提取的位置已經改變。您發佈的錯誤消息已經告訴新的位置(在其之前加上主機名)。

我不熟悉fsock,bot我認爲它不能處理重定向。所以我會建議你使用cURL代替:只需堅持cURL manual上的第一個例子。然後cURL的輸出將取代您的$xml。請記得將GET-Information ?sid=1也傳遞給cURL!

只要測試一下,然後你會看到是否有更多關於字符串解析的錯誤。如果是,請認真發佈「新$xml」。