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

small bug fixed in v1.1

This commit is contained in:
Dju 2010-09-04 02:47:04 +02:00 committed by Steve Schnepp
parent d3c0b24204
commit 5d5cb34f1e

View File

@ -1,122 +1,125 @@
#!/usr/bin/php #!/bin/bash
<?php <?php
# plugin made by Dju # plugin made by Dju
# allows to monitor the mirrors number and bandwidth from Launchpad for a specific country # v1.1
# saves the results in cache and connects to launchpad website every 3 hours # allows to monitor the mirrors number and bandwidth from Launchpad for a specific country
# saves the results in cache and connects to launchpad website every 3 hours
define( 'URL', 'https://launchpad.net/ubuntu/+cdmirrors' ); # the country constant it=s the name of he country, as it appears on the launchpad page (bold, on a gray background)
define( 'CACHE_FILE', '/var/lib/munin/plugin-state/ubuntu_mirrors.txt' );
define( 'CACHE_UPTIME', 3 ); define( 'URL', 'https://launchpad.net/ubuntu/+cdmirrors' );
define( 'COUNTRY', 'France' ); define( 'CACHE_FILE', 'ubuntu_mirrors.txt' );
define( 'CACHE_UPTIME', 3 );
define( 'COUNTRY', 'France' );
function find_and_short( $str, &$html, $with_str=true )
{ function find_and_short( $str, &$html, $with_str=true )
$pos = strpos( $html, $str ); {
if( $pos === false ) { $pos = strpos( $html, $str );
throw new Exception( "error finding '$str'", 2 ); if( $pos === false ) {
} else { throw new Exception( "error finding '$str'", 2 );
$removed = ( $with_str==true ) ? substr( $html, 0, $pos+strlen($str) ) : substr( $html, 0, $pos ); } else {
$html = substr( $html, $pos + strlen($str) ); $removed = ( $with_str == true ) ? substr( $html, 0, $pos+strlen($str) ) : substr( $html, 0, $pos );
return $removed; $html = substr( $html, $pos + strlen($str) );
} return $removed;
} }
}
function get_cache()
{ function get_cache()
if( !file_exists(CACHE_FILE) ) { {
return false; if( !file_exists(CACHE_FILE) ) {
} else { return false;
$cache_time = @filemtime( CACHE_FILE ); } else {
$cache_age = ( time() - $cache_time ) / 3600; $cache_time = @filemtime( CACHE_FILE );
if( $cache_age <= CACHE_UPTIME ) { $cache_age = ( time() - $cache_time ) / 3600;
$content = @file_get_contents( CACHE_FILE ); if( $cache_age <= CACHE_UPTIME ) {
if( !$content ) throw new Exception( 'Erreur lecture cache '.CACHE_FILE, 3 ); $content = @file_get_contents( CACHE_FILE );
return array( if( !$content ) throw new Exception( 'Erreur lecture cache '.CACHE_FILE, 3 );
'datas' => explode( '|', $content ), return array(
'date' => $cache_time 'datas' => explode( '|', $content ),
); 'date' => $cache_time
} else { );
@unlink( CACHE_FILE ); } else {
return false; @unlink( CACHE_FILE );
} return false;
} }
} }
}
function set_cache( $MIRRORS, $BW_RATE, $BW_UNIT )
{ function set_cache( $MIRRORS, $BW_RATE, $BW_UNIT )
$content = $MIRRORS . '|' . $BW_RATE . '|' . $BW_UNIT; {
$write = @file_put_contents( CACHE_FILE, $content ); $content = $MIRRORS . '|' . $BW_RATE . '|' . $BW_UNIT;
if( !$write ) throw new Exception( 'erreur ecriture cache '.CACHE_FILE, 4 ); $write = @file_put_contents( CACHE_FILE, $content );
} if( !$write ) throw new Exception( 'erreur ecriture cache '.CACHE_FILE, 4 );
}
function get_from_launchpad()
{ function get_from_launchpad()
$html = @file_get_contents( URL ); {
if( !$html ) throw new Exception( 'Error connecting to launchpad', 1 ); $html = @file_get_contents( URL );
$str1 = COUNTRY.'</th>'; if( !$html ) throw new Exception( 'Error connecting to launchpad', 1 );
$str2 = '<th style="text-align: left">'; $str1 = COUNTRY.'</th>';
$str3 = '</th>'; $str2 = '<th style="text-align: left">';
$str4 = '<span>mirrors</span>'; $str3 = '</th>';
find_and_short( $str1, $html ); $str4 = '<span>mirrors</span>';
find_and_short( $str2, $html ); find_and_short( $str1, $html );
$BW = find_and_short( $str3, $html, false ); find_and_short( $str2, $html );
list( $BW_RATE, $BW_UNIT ) = explode( ' ', $BW ); $BW = find_and_short( $str3, $html, false );
$rest = find_and_short( $str4, $html, false ); list( $BW_RATE, $BW_UNIT ) = explode( ' ', $BW );
$rest = str_replace( "\n", '', $rest ); $rest = find_and_short( $str4, $html, false );
find_and_short( '<th style="text-align: left">', $rest ); $rest = str_replace( "\n", '', $rest );
$MIRRORS = trim( $rest ); find_and_short( '<th style="text-align: left">', $rest );
return array( $MIRRORS, $BW_RATE, $BW_UNIT ); $MIRRORS = trim( $rest );
} return array( $MIRRORS, $BW_RATE, $BW_UNIT );
}
try
{ try
{
$CACHE = get_cache();
if( $CACHE != false ) { $CACHE = get_cache();
list( $MIRRORS, $BW_RATE, $BW_UNIT ) = $CACHE['datas']; if( $CACHE != false ) {
$CACHE_DATE = date('d/m/y H:i', $CACHE['date'] ); list( $MIRRORS, $BW_RATE, $BW_UNIT ) = $CACHE['datas'];
} $CACHE_DATE = date('d/m/y H:i', $CACHE['date'] );
}
if( isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] != '' ) {
$arg = $_SERVER['argv'][1]; if( isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] != '' ) {
if( $arg == 'autoconf' ) { switch( $_SERVER['argv'][1] ) {
echo "yes\n"; case 'autoconf':
exit( 0 ); echo "yes\n";
} elseif( $arg == 'config' ) { exit( 0 );
echo "graph_title Ubuntu Mirrors in ".COUNTRY."\n"; break;
echo "graph_category Ubuntu\n"; case 'config':
echo "graph_args --base 1000 -l 0\n"; echo "graph_title Ubuntu Mirrors in ".COUNTRY."\n";
echo "graph_scale no\n"; echo "graph_category Ubuntu\n";
echo "graph_vlabel Bandwith / Mirrors\n"; echo "graph_args --base 1000 -l 0\n";
$graph_info = 'This graph shows the available bandwidth and mirrors for ubuntu'; echo "graph_scale no\n";
if( $CACHE != false ) $graph_info .= ' (cache: '.CACHE_UPTIME.'h - last: '.$CACHE_DATE.')'; echo "graph_vlabel Bandwith / Mirrors\n";
echo "graph_info ".$graph_info.")\n"; $graph_info = 'This graph shows the available bandwidth and mirrors for ubuntu';
echo "nbm.label Mirrors number\n"; if( $CACHE != false ) $graph_info .= ' (cache: '.CACHE_UPTIME.'h - last: '.$CACHE_DATE.')';
$bwUnit = ( $CACHE == false ) ? '' : ' in '.$BW_UNIT; echo "graph_info ".$graph_info.")\n";
echo "bw.label Bandwidth".$bwUnit."\n"; echo "nbm.label Mirrors number\n";
exit( 0 ); $bwUnit = ( $CACHE == false ) ? '' : ' in '.$BW_UNIT;
} else { echo "bw.label Bandwidth".$bwUnit."\n";
echo "Unknown arg: ".$arg."\n"; exit( 0 );
exit( 2 ); break;
} default:
} echo "Unknown arg: ".$arg."\n";
exit( 2 );
if( $CACHE == false ) { }
list( $MIRRORS, $BW_RATE, $BW_UNIT ) = get_from_launchpad(); }
set_cache( $MIRRORS, $BW_RATE, $BW_UNIT );
} if( $CACHE == false ) {
list( $MIRRORS, $BW_RATE, $BW_UNIT ) = get_from_launchpad();
echo "nbm.value $mirrors\n"; set_cache( $MIRRORS, $BW_RATE, $BW_UNIT );
echo "bw.value $bw\n"; }
exit( 0 );
echo "nbm.value ".$MIRRORS."\n";
} echo "bw.value ".$BW_RATE."\n";
catch( Exception $ex ) exit( 0 );
{
echo 'Exception '.$ex->getCode() . ' ('.$ex->getMessage() . ")\n"; }
exit( $ex->getCode() ); catch( Exception $ex )
} {
echo 'Exception '.$ex->getCode() . ' ('.$ex->getMessage() . ")\n";
?> exit( $ex->getCode() );
}
?>