diff --git a/plugins/other/ubuntu-mirrors b/plugins/other/ubuntu-mirrors new file mode 100755 index 00000000..9d5bfc1d --- /dev/null +++ b/plugins/other/ubuntu-mirrors @@ -0,0 +1,122 @@ +#!/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] != '' ) { + $arg = $_SERVER['argv'][1]; + if( $arg == 'autoconf' ) { + echo "yes\n"; + exit( 0 ); + } elseif( $arg == '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 Bandwith / 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 ); + } else { + 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\n"; + exit( 0 ); + +} +catch( Exception $ex ) +{ + echo 'Exception '.$ex->getCode() . ' ('.$ex->getMessage() . ")\n"; + exit( $ex->getCode() ); +} + +?>