Skip to content

Instantly share code, notes, and snippets.

@perryflynn
Created January 14, 2017 15:50
Show Gist options
  • Save perryflynn/d81ef9e637c8c0ee38748f5f8d057ef8 to your computer and use it in GitHub Desktop.
Save perryflynn/d81ef9e637c8c0ee38748f5f8d057ef8 to your computer and use it in GitHub Desktop.

Installation

  • Eine VM mit Linux
  • InfuxDB
  • Grafana
  • PHP + CURL

Einrichtung

  • Datenbank + User in InfluxDB anlegen
  • Username, Host, Password in test.php anpassen
  • test.sh um eigene Download Testserver erweitern
  • test.sh als Cron aufrufen

Grafana

  • In Grafana ein Dashboard erstellen
  • InfluxDB als Datenquelle anlegen
  • Graf erstellen

Beispiel:

png

Beispiel Panel JSON:

{
  "aliasColors": {
    "bitspersec.mean": "#BF1B00"
  },
  "bars": false,
  "datasource": "public_speedtest",
  "decimals": 2,
  "editable": true,
  "error": false,
  "fill": 1,
  "grid": {},
  "height": "400",
  "id": 4,
  "legend": {
    "alignAsTable": true,
    "avg": true,
    "current": true,
    "max": true,
    "min": true,
    "rightSide": false,
    "show": true,
    "total": false,
    "values": true
  },
  "lines": true,
  "linewidth": 2,
  "links": [],
  "nullPointMode": "connected",
  "percentage": false,
  "pointradius": 3,
  "points": true,
  "renderer": "flot",
  "seriesOverrides": [],
  "span": 12,
  "stack": false,
  "steppedLine": false,
  "targets": [
    {
      "alias": "$tag_source",
      "dsType": "influxdb",
      "groupBy": [
        {
          "params": [
            "$interval"
          ],
          "type": "time"
        },
        {
          "params": [
            "host"
          ],
          "type": "tag"
        },
        {
          "params": [
            "source"
          ],
          "type": "tag"
        },
        {
          "params": [
            "null"
          ],
          "type": "fill"
        }
      ],
      "measurement": "bitspersec",
      "policy": "default",
      "refId": "A",
      "resultFormat": "time_series",
      "select": [
        [
          {
            "params": [
              "value"
            ],
            "type": "field"
          },
          {
            "params": [],
            "type": "mean"
          }
        ]
      ],
      "tags": []
    }
  ],
  "thresholds": [],
  "timeFrom": "7d",
  "timeShift": null,
  "title": "",
  "tooltip": {
    "msResolution": true,
    "shared": true,
    "sort": 0,
    "value_type": "cumulative"
  },
  "type": "graph",
  "xaxis": {
    "mode": "time",
    "name": null,
    "show": true,
    "values": []
  },
  "yaxes": [
    {
      "format": "bps",
      "label": "Megabit pro Sekunde",
      "logBase": 1,
      "max": null,
      "min": null,
      "show": true
    },
    {
      "format": "bps",
      "label": "",
      "logBase": 1,
      "max": null,
      "min": null,
      "show": true
    }
  ]
}
<?php
// Original by http://www.cowburn.info/2008/11/29/download-speed-php-curl/
// Modified by Christian B. (https://anysrc.net)
define('DEVNULL', '/dev/null');
define('AGENT', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36');
$url = $argv[1];
$what = $argv[2];
$lock = "lockfile";
if(is_file($lock))
{
echo "echo \"lockfile\"";
exit();
}
touch($lock);
// Functions
$line = function($str) { echo "[".date('H:i:s')."] ".$str."\n"; };
$tsp = function() { return microtime(true); };
$bench = function($url) use($tsp)
{
$ch = curl_init($url);
$devnull = fopen(DEVNULL, 'w');
if(!is_resource($ch))
{
die("Could not create curl handle");
}
if(!is_resource($devnull))
{
die("Could not open ".DEVNULL);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_USERAGENT, AGENT);
curl_setopt($ch, CURLOPT_FILE, $devnull);
$starttsp = $tsp();
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
fclose($devnull);
unset($ch, $devnull);
$bruttosec = ($tsp() - $starttsp);
$nettosec = $info['total_time'] - $info['starttransfer_time'];
$result = array(
"date" => date('c'),
"url" => $url,
"downloadbytes" => $info['size_download'],
"downloadmegabytes" => $info['size_download'] / 1024 / 1024,
"bytespersec" => $info['speed_download'],
"megabytespersec" => $info['speed_download'] / 1024 / 1024,
"bitspersec" => $info['speed_download'] * 8,
"megabitspersec" => $info['speed_download'] * 8 / 1024 / 1024,
"bruttosecs" => $bruttosec,
"nettosecs" => $nettosec,
"dnssecs" => $info['namelookup_time'],
"connectsecs" => $info['connect_time'],
"initsecs" => $info['starttransfer_time'],
);
return $result;
};
$res = $bench($url.(strpos($url, "?")!==false ? "&" : "?")."entropy=".microtime(true));
if(is_array($res) && isset($res['bitspersec']))
{
// Username:Password von InfluxDB
$infusr = "speedtest:password";
// Host von InfluxDB
$infurl = "http://localhost:8086/write?db=speedtest";
// Insert erzeugen
$infstr = "bitspersec,host=vodafone,source=".$what." value=".$res['bitspersec']." ".time()."000000000";
echo "curl -u ".$infusr." -i -XPOST '".$infurl."' --data-binary '".$infstr."'";
}
else
{
echo "false";
}
unlink($lock);
#!/bin/bash
cd "$(dirname "$0")"
echo "Test hetzner"
php -f test.php http://speed.hetzner.de/1GB.bin hetznerspeed | bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment