root / plugins / network / speedtest-download-bandwidth @ 31412baa
Historique | Voir | Annoter | Télécharger (3,45 ko)
| 1 |
#!/usr/bin/php |
|---|---|
| 2 |
<?php |
| 3 |
global $argc, $argv; |
| 4 |
|
| 5 |
/************************************************************************** |
| 6 |
SpeedTest multi-sites, test one site at a time (in background) |
| 7 |
v1.4 - tanguy.pruvot on gmail.com (24 Jan 2011) |
| 8 |
***************************************************************************/ |
| 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 |
//"bouygues" => "http://speedtest.lafibre.info/speedtest/random2000x2000.jpg?x=".date('U'),
|
| 15 |
//"cableinfo" => "http://ports.cable-info.net:43210/test.php", |
| 16 |
"bbox" => "http://speed.degrouptest.com/ookla/speedtest/random1500x1500.jpg?x=".date('U')
|
| 17 |
); |
| 18 |
|
| 19 |
$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 |
// 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 |
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 |
"; |
| 41 |
|
| 42 |
$order=""; |
| 43 |
foreach ($mire as $label => $url) {
|
| 44 |
if (isset($labels[$label])) |
| 45 |
echo "$label.label ".$labels[$label]."\n"; |
| 46 |
else |
| 47 |
echo "$label.label $label\n"; |
| 48 |
echo "$label.draw LINE2\n"; |
| 49 |
$order .= " $label"; |
| 50 |
} |
| 51 |
echo "graph_order grenouille ".trim($order)." maximum\n"; |
| 52 |
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 |
$cache['grenouille'] = @ floatval($last_data[2])*1000.0; |
| 67 |
} |
| 68 |
$output = "grenouille.value ".round($cache['grenouille'])."\n"; |
| 69 |
|
| 70 |
// OUTPUT ------------------------------------------------------------------ |
| 71 |
|
| 72 |
$item = 0; |
| 73 |
foreach ($mire as $label => $url) {
|
| 74 |
$cache[$label] = (float) @ $cache[$label]; |
| 75 |
$item++; |
| 76 |
|
| 77 |
$output .= "$label.value ".round($cache[$label])."\n"; |
| 78 |
} |
| 79 |
$output .= "maximum.value ".round($connexion * 1024 * 1024 / 10)."\n"; |
| 80 |
echo $output; |
| 81 |
|
| 82 |
// SPEED TEST -------------------------------------------------------------- |
| 83 |
|
| 84 |
// 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 |
|
| 127 |
?> |
