Monitoring my internet connection status

I have annoying internet connection issues, is that sometimes it just stops working. Most of the time it's not a big deal: Either I am not doing anything and I don't notice it, or I can wait some minutes until it's back up. However it did happen a few times times in the past that my internet just dropped off while I was in the middle of an important work meeting.

Communicating with my ISP (Telekom) is also annoying, because they always ask me to restart the router. Since most of these interruptions last about 5 to 10 minutes, by the time I finish the call with the ISP, the internet is back on, so they end up not doing anything on their side.

It's time I collect some data to monitor internet interruptions, so that I have something to backup my claims that my internet is unreliable.

Architecture

This is the simplest solution I could come up with: I ping google.com and my router at regular intervals. If I can ping my router, and not google, that means I cannot reach the internet but my router is not the problem.

Since I already have a Raspberry PI home server running Prometheus and Grafana on Nix OS, this was pretty easy to setup.

Prometheus Blackbox Exporter

Not surprisingly, there is an already out of the box solution from Prometheus called Black Box Exporters, which are basically exporter that allow probing endpoints, including using the ICMP protocol (used by ping).

This made this while setup very easy.

Setting this exporter on NixOs is really simple. Here is what is looks like

services.prometheus = {
  exporters = {
    blackbox = {
      enable = true;
      configFile = pkgs.writeText "blackbox.yml" ''
        modules:
          icmp:
            prober: icmp
      '';
    };
  };

  extraFlags = [
  ];

  enable = true;
  port = 9090;
  globalConfig.scrape_interval = "10s";
  stateDir = "prometheus";
  scrapeConfigs = [
    {
      job_name = "blackbox-ping";
      metrics_path = "/probe";
      params = {
        module = [ "icmp" ];
      };
      static_configs = [
        {
          targets = [
            "google.com"
            "fritz.box"
          ];
        }
      ];
      relabel_configs = [
        {
          source_labels = [ "__address__" ];
          target_label = "__param_target";
        }
        {
          source_labels = [ "__param_target" ];
          target_label = "instance";
        }
        {

          # where blackbox_exporter listens
          target_label = "__address__";
          replacement = "127.0.0.1:9115";
        }
      ];
    }
  ];
};
Setup blackbox exporter on Nix OS. The relabel_configs part is necessary.

The relabel part are necessary to make prometheus scrape on the correct url.

On your Prometheus UI, you can then view these metrics by searching probe_success.

Visualizing in Grafana

Setting up Grafana on Nix Os is also very simple

services.grafana = {
  enable = true;
  settings = {
    server = {
      http_addr = "0.0.0.0";
      http_port = 3000;
    };
    security = {
      secret_key = "top-secret";
    };

    # Prevents Grafana from phoning home
    analytics.reporting_enabled = false;
  };
  provision = {
    enable = true;
    datasources.settings.datasources = [
      {
        name = "Prometheus";
        type = "prometheus";
        url = "http://localhost:9090";
        isDefault = true;
        editable = false;
      }
    ];
  };
};
Setting up Grafana to connect to Prometheus. There is probably a good way to define the dashboards declaratively here as well, but I have not done that so far.

From here you can just setup a dashboard, and then you can see this

Grafana Dashboard with my Internet State
Grafana dashboard showing the state of my internet from 15:30 to 21:30 on a particular problematic day. Green line shows when I can ping my router, and yellow line when I can ping google.com

Some notes

Sources