#!/usr/bin/php
explode( '|', $content ),
'date' => $cache_time
);
} else {
@unlink( CACHE_FILE );
return false;
}
}
}
function set_cache( $MIRRORS, $BW_RATE, $BW_UNIT )
{
$content = $MIRRORS . '|' . $BW_RATE . '|' . $BW_UNIT;
$write = @file_put_contents( CACHE_FILE, $content );
if( !$write ) throw new Exception( 'erreur ecriture cache '.CACHE_FILE, 4 );
}
function get_from_launchpad()
{
$html = @file_get_contents( URL );
if( !$html ) throw new Exception( 'Error connecting to launchpad', 1 );
$str1 = COUNTRY.'';
$str2 = '
';
$str3 = ' | ';
$str4 = 'mirrors';
find_and_short( $str1, $html );
find_and_short( $str2, $html );
$BW = find_and_short( $str3, $html, false );
list( $BW_RATE, $BW_UNIT ) = explode( ' ', $BW );
$rest = find_and_short( $str4, $html, false );
$rest = str_replace( "\n", '', $rest );
find_and_short( '', $rest );
$MIRRORS = trim( $rest );
return array( $MIRRORS, $BW_RATE, $BW_UNIT );
}
try
{
$CACHE = get_cache();
if( $CACHE != false ) {
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] != '' ) {
switch( $_SERVER['argv'][1] ) {
case 'autoconf':
echo "yes\n";
exit( 0 );
break;
case 'config':
echo "graph_title Ubuntu Mirrors in ".COUNTRY."\n";
echo "graph_category Ubuntu\n";
echo "graph_args --base 1000 -l 0\n";
echo "graph_scale no\n";
echo "graph_vlabel Bandwidth / Mirrors\n";
$graph_info = 'This graph shows the available bandwidth and mirrors for ubuntu';
if( $CACHE != false ) $graph_info .= ' (cache: '.CACHE_UPTIME.'h - last: '.$CACHE_DATE.')';
echo "graph_info ".$graph_info.")\n";
echo "nbm.label Mirrors number\n";
$bwUnit = ( $CACHE == false ) ? '' : ' in '.$BW_UNIT;
echo "bw.label Bandwidth".$bwUnit."\n";
exit( 0 );
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 );
}
echo "nbm.value ".$MIRRORS."\n";
echo "bw.value ".$BW_RATE."\n";
exit( 0 );
}
catch( Exception $ex )
{
echo 'Exception '.$ex->getCode() . ' ('.$ex->getMessage() . ")\n";
exit( $ex->getCode() );
}
?>
|