/speedtest-app-insights

Python code that runs the speedtest.net API and then posts the results to an Azure App Insights dashboard

Primary LanguagePythonMIT LicenseMIT

Capturing speedtest.net data in Azure App Insights

This version uses OpenTelemetry. Initial versions used OpenCensus

Changelog

  • 2.1.0 - Add OT views with mixed case and spaces in the name - restores the OC names.
  • 2.0.1 - Fix crontabe scripts broken directory reorganization
  • 2.0.0 - First version with OpenTelemetry
  • Unversioned - OpenCensus version - no version info

Known issues

  1. The labels for the open telemtry version are those than from the previous version. This is because of the way the OpenTelemetry specification says labels are case insensitive and the Python API converts all labels to lower case with spaces replaced with underscores. The OpenTelemetry Python team may change the casing part of this in the future see open-telemetry/opentelemetry-python#3207 This may be fixible by using views in the same way we did with OpenCensus
  2. The --verbose flag co-mingles the notion of more output and log routing to ApplicationMonitor
  3. This application disables automatic integrations because the metrics and traces are the end product and do not exist as KPIs and diagnostics as part of some greater application.
  4. This application does not demonstrate cross call tracing identifiers because they weren't needed

Purpose

This project captures internet connection statistics and sends them to an Azure dashboard.

Script Flow

  1. Capture the current ping time and optionally upload/doownload statistics of the internet connection for the machine this machine runs on as measured by the speedtest.net python API
  2. Publish the results to an Azure Application Insights dashboard

Application Insights Dashboard

Prerequisites

  1. Python3
  2. An internet connection
  3. An account in Azure that can run the free tier of Azure Application Insights. See references below for instructions.
    1. You can comment out the AppInsights.py call at the bottom of NetCheck.py if you don't wish to create a dashboard in Azure Application Insights

TODO

  1. Create IaC script that instantiates Application Insights so you don't have to use Azure Portal
  2. Create a script for the Mac that runs Install Certificates.command

Issues

  1. speedtest.net initialization can vary by platorm. Some machines require https and some don't work with it
    1. Some Windows WSL environments require s = speedtest.Speedtest(secure=1)
    2. The Mac with python 3.10 gets a cert error. CERTIFICATE_VERIFYFAILED Two options
      • Fix the certificate by running the Install Certificates.command Double click on /Applications/<python version>/Install Certificates.command
      • Disable secure with this code change s = speedtest.Speedtest(secure=0)

Scripts in this repository

Script Purpose
Linux/Pi Setup/Installation in Linux-Install
1-setup-host.sh downloads the Speedtest.net CLI, configures Python
2-install-crontab.sh Installs a crontab entry that runs NetCheck.py on a regular basis
11-remove-crontab.sh removes this user's NetCheck.py crontab entries
Network testing binaries in src
NetCheck.py The main program. Program that invokes the test code in SpeedTest.py
SpeedTest.py SpeedTest.net adapter. Runs the speedtest-cli and records metrics
AppInsights.py OpenCensus library wrapper used to send metrics to Azure Application Insights
Windows Python Setup
setup.ps1 Windows Python setup program. Will prompt to install python3 via Windows store

Usage - NetCheck and Azure App Insights

  1. Run 1-setup-host.sh to install dependencies on a raspberry pi

    1. Alternative for like a windows machine pip3 install -r requirements.txt or python3 -m pip install -r requirements.txt
  2. Create Application Inights and get key

    1. Get an Azure account
    2. Log into Azure Portal Insights blade
    3. Register in Application Insights
      1. Select your subscription
      2. Create a resource group or use an existing one
      3. Select an instance name.
      4. Select a Region.
      5. Select Resource Mode as a _Workspace-based.
      6. Select the Log Analytics Workspace. There is a DefaultWorkspace in each region. You can use that.
    4. Get an Application Insights Instrumentation Key from the Portal. It is on the Application Insights home page in the upper right corner.
      1. Open the Application Insights blade
      2. Copy the Connection String in the upper right corder
    5. Copy config.ini.template to config.ini
    6. Replace the dummy key in config.ini with your new key that you copied from the portal above. It should look something like
    azure_instrumentation_key=InstrumentationKey=0000000-0000-0000-0000-00000000000;IngestionEndpoint=https://westus2-1.in.applicationinsights.azure.com/
  3. Run main program NetCheck.py There are several options

    1. Run with only a ping check python3 src/NetCheck.py
    2. Run with ping, upload and download python3 src/NetCheck.py --download --upload
    3. Get help with python3 src/NetCheck.py --help

Optional

  1. Manage data retention the data retention period. Learn how long data is kept.

Installing in crontab to run repeately

  1. cd into this directory
  2. Verify the cycle times you wish in 2-install-crontab.sh. The file is in crontab format.
  3. run 2-install-crontab.sh

Sample crontab -l output after installing the crontab

pi@pi-153a3987b:~/Documents/speedtest-app-insights $ crontab -l
*/3 * * * * cd /home/pi/Documents/speedtest-app-insights && python3 NetCheck.py
13 */4 * * * cd /home/pi/Documents/speedtest-app-insights && python3 NetCheck.py --upload --download
*/6 * * * * cd /home/pi/Documents/speedtest-app-insights && python3 DnsCheck.py

Example speedtest.net cli output

Raspberry Pi3 on 1GB port on 1GB FIOS internet service.

speedtest installed as part of apt package

Installed in /usr/bin

pi@pi-52863f1:~/Documents/speedtest-app-insights $  /usr/bin/speedtest

   Speedtest by Ookla

     Server: Windstream - Ashburn, VA (id = 17383)
        ISP: Verizon Fios
    Latency:     4.04 ms   (0.04 ms jitter)
   Download:    94.19 Mbps (data used: 42.4 MB)
     Upload:    93.90 Mbps (data used: 42.3 MB)
Packet Loss:     0.0%
 Result URL: https://www.speedtest.net/result/c/dee19d27-1f5a-4cff-aa42-d8084a145b8f

speedtest installed as part of speedtest-cli github pull

Installed in ~/.local/bin

$ speedtest
Retrieving speedtest.net configuration...
Testing from Verizon Fios (108.48.69.33)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by PBW Communications, LLC (Herndon, VA) [22.70 km]: 8.306 ms
Testing download speed................................................................................
Download: 91.92 Mbit/s
Testing upload speed......................................................................................................
Upload: 93.90 Mbit/s

Application Insights

See Application Insights Readme

Raspberry Pi Networking

Some Raspberry Pi models are speed limited on their ethernet. magpi posted ethernet test results

  • In my testing, the Raspberry Pi 3 hardwire Ethernet seems to max out about 94 Mbit/s. The Raspberry Pi speed should be sigficantly higher because its eithernet interface is part of the SoC instead of being USB attached like previous boards.
  • My results align with numbers in this blog article https://www.jeffgeerling.com/blogs/jeff-geerling/getting-gigabit-networking . The same article says you can get slightly over 200 Mbit/s with a USB 3.0 Gigabit adapter.
  • This site shows Raspberry Pi 3 and Pi 4 speeds. Note that the Pi3 speeds on this page are higher than other reference sites Hackaday Raspberry Pi Benchmarks

References

Experimental Features

DnsCheck.py

This module uses dnspython to do a dns check. I found that my network experience was heavily driven by my DNS times.

It should install fine but you can see in 1-setup-host.sh that there is a bit of overhead to get this working because it depends on a library that is best installed from git.

Release Notes

The speed test team changes something in their API in April 2021. This changed some format that broke speedtest.py. You need the 2.1.3 version, or later, of the speedtest. Grab it with pip3 or if not available then the following assuming you run pip not as root

wget https://raw.githubusercontent.com/sivel/speedtest-cli/v2.1.3/speedtest.py -O ~/.local/lib/python3.<version>/site-packages/speedtest.py

Ex:

wget https://raw.githubusercontent.com/sivel/speedtest-cli/v2.1.3/speedtest.py -O ~/.local/lib/python3.7/site-packages/speedtest.py