root / plugins / ubuntu / ubuntu-mirrors @ eaf6c2d7
Historique | Voir | Annoter | Télécharger (3,92 ko)
| 1 | 016752f3 | Stig Sandbeck Mathisen | #!/usr/bin/php |
|---|---|---|---|
| 2 | 5d5cb34f | Dju | <?php |
| 3 | |||
| 4 | # plugin made by Dju |
||
| 5 | # v1.1 |
||
| 6 | # allows to monitor the mirrors number and bandwidth from Launchpad for a specific country |
||
| 7 | # saves the results in cache and connects to launchpad website every 3 hours |
||
| 8 | # the country constant it=s the name of he country, as it appears on the launchpad page (bold, on a gray background) |
||
| 9 | |||
| 10 | define( 'URL', 'https://launchpad.net/ubuntu/+cdmirrors' ); |
||
| 11 | define( 'CACHE_FILE', 'ubuntu_mirrors.txt' ); |
||
| 12 | define( 'CACHE_UPTIME', 3 ); |
||
| 13 | define( 'COUNTRY', 'France' ); |
||
| 14 | |||
| 15 | function find_and_short( $str, &$html, $with_str=true ) |
||
| 16 | {
|
||
| 17 | $pos = strpos( $html, $str ); |
||
| 18 | if( $pos === false ) {
|
||
| 19 | throw new Exception( "error finding '$str'", 2 ); |
||
| 20 | } else {
|
||
| 21 | $removed = ( $with_str == true ) ? substr( $html, 0, $pos+strlen($str) ) : substr( $html, 0, $pos ); |
||
| 22 | $html = substr( $html, $pos + strlen($str) ); |
||
| 23 | return $removed; |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | function get_cache() |
||
| 28 | {
|
||
| 29 | if( !file_exists(CACHE_FILE) ) {
|
||
| 30 | return false; |
||
| 31 | } else {
|
||
| 32 | $cache_time = @filemtime( CACHE_FILE ); |
||
| 33 | $cache_age = ( time() - $cache_time ) / 3600; |
||
| 34 | if( $cache_age <= CACHE_UPTIME ) {
|
||
| 35 | $content = @file_get_contents( CACHE_FILE ); |
||
| 36 | if( !$content ) throw new Exception( 'Erreur lecture cache '.CACHE_FILE, 3 ); |
||
| 37 | return array( |
||
| 38 | 'datas' => explode( '|', $content ), |
||
| 39 | 'date' => $cache_time |
||
| 40 | ); |
||
| 41 | } else {
|
||
| 42 | @unlink( CACHE_FILE ); |
||
| 43 | return false; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | function set_cache( $MIRRORS, $BW_RATE, $BW_UNIT ) |
||
| 49 | {
|
||
| 50 | $content = $MIRRORS . '|' . $BW_RATE . '|' . $BW_UNIT; |
||
| 51 | $write = @file_put_contents( CACHE_FILE, $content ); |
||
| 52 | if( !$write ) throw new Exception( 'erreur ecriture cache '.CACHE_FILE, 4 ); |
||
| 53 | } |
||
| 54 | |||
| 55 | function get_from_launchpad() |
||
| 56 | {
|
||
| 57 | $html = @file_get_contents( URL ); |
||
| 58 | if( !$html ) throw new Exception( 'Error connecting to launchpad', 1 ); |
||
| 59 | $str1 = COUNTRY.'</th>'; |
||
| 60 | $str2 = '<th style="text-align: left">'; |
||
| 61 | $str3 = '</th>'; |
||
| 62 | $str4 = '<span>mirrors</span>'; |
||
| 63 | find_and_short( $str1, $html ); |
||
| 64 | find_and_short( $str2, $html ); |
||
| 65 | $BW = find_and_short( $str3, $html, false ); |
||
| 66 | list( $BW_RATE, $BW_UNIT ) = explode( ' ', $BW ); |
||
| 67 | $rest = find_and_short( $str4, $html, false ); |
||
| 68 | $rest = str_replace( "\n", '', $rest ); |
||
| 69 | find_and_short( '<th style="text-align: left">', $rest ); |
||
| 70 | $MIRRORS = trim( $rest ); |
||
| 71 | return array( $MIRRORS, $BW_RATE, $BW_UNIT ); |
||
| 72 | } |
||
| 73 | |||
| 74 | try |
||
| 75 | {
|
||
| 76 | |||
| 77 | $CACHE = get_cache(); |
||
| 78 | if( $CACHE != false ) {
|
||
| 79 | list( $MIRRORS, $BW_RATE, $BW_UNIT ) = $CACHE['datas']; |
||
| 80 | $CACHE_DATE = date('d/m/y H:i', $CACHE['date'] );
|
||
| 81 | } |
||
| 82 | |||
| 83 | if( isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] != '' ) {
|
||
| 84 | switch( $_SERVER['argv'][1] ) {
|
||
| 85 | case 'autoconf': |
||
| 86 | echo "yes\n"; |
||
| 87 | exit( 0 ); |
||
| 88 | break; |
||
| 89 | case 'config': |
||
| 90 | echo "graph_title Ubuntu Mirrors in ".COUNTRY."\n"; |
||
| 91 | eaf6c2d7 | dipohl | echo "graph_category other\n"; |
| 92 | 5d5cb34f | Dju | echo "graph_args --base 1000 -l 0\n"; |
| 93 | echo "graph_scale no\n"; |
||
| 94 | fba800ae | Veres Lajos | echo "graph_vlabel Bandwidth / Mirrors\n"; |
| 95 | 5d5cb34f | Dju | $graph_info = 'This graph shows the available bandwidth and mirrors for ubuntu'; |
| 96 | if( $CACHE != false ) $graph_info .= ' (cache: '.CACHE_UPTIME.'h - last: '.$CACHE_DATE.')'; |
||
| 97 | echo "graph_info ".$graph_info.")\n"; |
||
| 98 | echo "nbm.label Mirrors number\n"; |
||
| 99 | $bwUnit = ( $CACHE == false ) ? '' : ' in '.$BW_UNIT; |
||
| 100 | echo "bw.label Bandwidth".$bwUnit."\n"; |
||
| 101 | exit( 0 ); |
||
| 102 | break; |
||
| 103 | default: |
||
| 104 | echo "Unknown arg: ".$arg."\n"; |
||
| 105 | exit( 2 ); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | if( $CACHE == false ) {
|
||
| 110 | list( $MIRRORS, $BW_RATE, $BW_UNIT ) = get_from_launchpad(); |
||
| 111 | set_cache( $MIRRORS, $BW_RATE, $BW_UNIT ); |
||
| 112 | } |
||
| 113 | |||
| 114 | echo "nbm.value ".$MIRRORS."\n"; |
||
| 115 | echo "bw.value ".$BW_RATE."\n"; |
||
| 116 | exit( 0 ); |
||
| 117 | |||
| 118 | } |
||
| 119 | catch( Exception $ex ) |
||
| 120 | {
|
||
| 121 | echo 'Exception '.$ex->getCode() . ' ('.$ex->getMessage() . ")\n";
|
||
| 122 | exit( $ex->getCode() ); |
||
| 123 | } |
||
| 124 | |||
| 125 | ?> |
