root / plugins / network / speedtest-download-bandwidth @ dd4afac8
Historique | Voir | Annoter | Télécharger (3,45 ko)
| 1 | 36142642 | Tanguy PRUVOT | #!/usr/bin/php |
|---|---|---|---|
| 2 | <?php |
||
| 3 | global $argc, $argv; |
||
| 4 | |||
| 5 | /************************************************************************** |
||
| 6 | 3abf7831 | Tanguy Pruvot | SpeedTest multi-sites, test one site at a time (in background) |
| 7 | v1.4 - tanguy.pruvot on gmail.com (24 Jan 2011) |
||
| 8 | 36142642 | Tanguy PRUVOT | ***************************************************************************/ |
| 9 | $cache_file = '/var/lib/munin/speedtest.cache'; |
||
| 10 | $grenouille_csv = '/usr/local/pygrenouille/logs/download.csv'; |
||
| 11 | |||
| 12 | $mire = array( |
||
| 13 | "free" => "http://test-debit.free.fr/16384.rnd", |
||
| 14 | 3abf7831 | Tanguy Pruvot | //"bouygues" => "http://speedtest.lafibre.info/speedtest/random2000x2000.jpg?x=".date('U'),
|
| 15 | //"cableinfo" => "http://ports.cable-info.net:43210/test.php", |
||
| 16 | 36142642 | Tanguy PRUVOT | "bbox" => "http://speed.degrouptest.com/ookla/speedtest/random1500x1500.jpg?x=".date('U')
|
| 17 | ); |
||
| 18 | |||
| 19 | 3abf7831 | Tanguy Pruvot | $labels = array( |
| 20 | "bbox" => "Speedtest.net BBox fibre (ookla)", |
||
| 21 | "free" => "test-debit.free.fr" |
||
| 22 | ); |
||
| 23 | |||
| 24 | //Connexion Mbits (30/100) |
||
| 25 | $connexion = 35; |
||
| 26 | |||
| 27 | 36142642 | Tanguy PRUVOT | // CONFIG ------------------------------------------------------------------ |
| 28 | if ($argc > 1 && $argv[1]=='config'){
|
||
| 29 | |||
| 30 | echo "graph_category network |
||
| 31 | graph_title Speed test |
||
| 32 | graph_args --base 1024 |
||
| 33 | 3abf7831 | Tanguy Pruvot | graph_vlabel DL (MB/s) |
| 34 | grenouille.label Grenouille (NC) |
||
| 35 | grenouille.type GAUGE |
||
| 36 | maximum.label Connexion (max) |
||
| 37 | maximum.type GAUGE |
||
| 38 | maximum.colour ff0000 |
||
| 39 | maximum.max ".$connexion."00000 |
||
| 40 | 36142642 | Tanguy PRUVOT | "; |
| 41 | |||
| 42 | $order=""; |
||
| 43 | foreach ($mire as $label => $url) {
|
||
| 44 | 3abf7831 | Tanguy Pruvot | if (isset($labels[$label])) |
| 45 | echo "$label.label ".$labels[$label]."\n"; |
||
| 46 | else |
||
| 47 | echo "$label.label $label\n"; |
||
| 48 | 36142642 | Tanguy PRUVOT | echo "$label.draw LINE2\n"; |
| 49 | $order .= " $label"; |
||
| 50 | } |
||
| 51 | 3abf7831 | Tanguy Pruvot | echo "graph_order grenouille ".trim($order)." maximum\n"; |
| 52 | 36142642 | Tanguy PRUVOT | exit; |
| 53 | |||
| 54 | } |
||
| 55 | |||
| 56 | // CACHE & GRENOUILLE ----------------------------------------------------- |
||
| 57 | $cache = @ unserialize(file_get_contents($cache_file)); |
||
| 58 | if (empty($cache)) |
||
| 59 | $cache = array('item'=>0);
|
||
| 60 | |||
| 61 | |||
| 62 | if (is_file($grenouille_csv) ) {
|
||
| 63 | $grenouille = explode("\n",trim(file_get_contents($grenouille_csv)));
|
||
| 64 | $grenouille = end($grenouille); |
||
| 65 | $last_data = explode(";",$grenouille);
|
||
| 66 | 3abf7831 | Tanguy Pruvot | $cache['grenouille'] = @ floatval($last_data[2])*1000.0; |
| 67 | 36142642 | Tanguy PRUVOT | } |
| 68 | $output = "grenouille.value ".round($cache['grenouille'])."\n"; |
||
| 69 | |||
| 70 | 3abf7831 | Tanguy Pruvot | // OUTPUT ------------------------------------------------------------------ |
| 71 | 36142642 | Tanguy PRUVOT | |
| 72 | $item = 0; |
||
| 73 | foreach ($mire as $label => $url) {
|
||
| 74 | 3abf7831 | Tanguy Pruvot | $cache[$label] = (float) @ $cache[$label]; |
| 75 | $item++; |
||
| 76 | 36142642 | Tanguy PRUVOT | |
| 77 | 3abf7831 | Tanguy Pruvot | $output .= "$label.value ".round($cache[$label])."\n"; |
| 78 | 36142642 | Tanguy PRUVOT | } |
| 79 | 3abf7831 | Tanguy Pruvot | $output .= "maximum.value ".round($connexion * 1024 * 1024 / 10)."\n"; |
| 80 | 36142642 | Tanguy PRUVOT | echo $output; |
| 81 | |||
| 82 | 3abf7831 | Tanguy Pruvot | // SPEED TEST -------------------------------------------------------------- |
| 83 | 36142642 | Tanguy PRUVOT | |
| 84 | 3abf7831 | Tanguy Pruvot | // Background Process |
| 85 | if ($argc == 2 && $argv[1]=='speedtest'){
|
||
| 86 | $item = 0; |
||
| 87 | foreach ($mire as $label => $url) {
|
||
| 88 | $cache[$label] = (float) @ $cache[$label]; |
||
| 89 | |||
| 90 | if ($item == $cache['item']) {
|
||
| 91 | $data = ""; $timeout = 10; |
||
| 92 | $before = microtime(true); |
||
| 93 | while ($timeout > 0 && strlen($data) < 15000000) {
|
||
| 94 | $data .= file_get_contents($url); |
||
| 95 | $timeout--; |
||
| 96 | } |
||
| 97 | $after = microtime(true); |
||
| 98 | $len = strlen($data); |
||
| 99 | |||
| 100 | if (($after - $before) > 0) {
|
||
| 101 | $speed = $len / ($after - $before); |
||
| 102 | if ($cache[$label] > 0) |
||
| 103 | $cache[$label] = ($cache[$label] + $speed) / 2; |
||
| 104 | else |
||
| 105 | $cache[$label] = $speed; |
||
| 106 | } else |
||
| 107 | $cache[$label] = 0; |
||
| 108 | } |
||
| 109 | $item++; |
||
| 110 | } |
||
| 111 | $cache['item'] = ($cache['item'] + 1) % count($mire); |
||
| 112 | |||
| 113 | //save cache for next munin call |
||
| 114 | @file_put_contents($cache_file,serialize($cache)); |
||
| 115 | @chmod ($cache_file, 0666); |
||
| 116 | @chown($cache_file,'munin'); |
||
| 117 | } |
||
| 118 | else |
||
| 119 | {
|
||
| 120 | //do all speedtests in background |
||
| 121 | $processes = trim(shell_exec("ps aux | grep php | grep speedtest"));
|
||
| 122 | if (count(explode("\n", $processes)) <= 2) {
|
||
| 123 | shell_exec("nohup ".$argv[0].' speedtest > /dev/null 2>&1 &echo $!');
|
||
| 124 | } |
||
| 125 | } |
||
| 126 | 36142642 | Tanguy PRUVOT | |
| 127 | ?> |
