Monitoring
Photo by Daniel Korpai on Unsplash

Monitoring an OpenWRT router with Grafana and InfluxDB

ENVIRONMENT

  • A Debian 10 VM running InfluxDB (v1.7.10+) and Grafana (v6.7.2+) services
  • Network appliance running OpenWRT (v18.06+)

INSTALL AND CONFIGURE INFLUXDB (@ VM)

Install needed package and dependencies:

sudo wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
echo "deb https://repos.influxdata.com/debian buster stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
sudo apt update
sudo apt install -y influxdb

NOTE: if you receive the following error:

E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation

You need to install ‘gpg’ package:

sudo apt install gpg

Once InfluxDB is installed, edit /etc/influxdb/influxdb.conf file to enable Collectd plugin (locate and uncomment the following lines):

[[collectd]]
   enabled = true
   bind-address = ":25826"
   database = "${YOUR_DB_NAME}"
   retention-policy = ""
   typesdb = "/usr/local/share/collectd/types.db"
   security-level = "none"
   batch-size = 5000
   batch-pending = 10
   batch-timeout = "10s"
   read-buffer = 0

Create ‘types.db’ file:

sudo mkdir -p /usr/local/share/collectd/
sudo wget -O /usr/local/share/collectd/types.db https://raw.githubusercontent.com/CactusProjects/openwrt_influxdb/master/types.db

Start and enable the InfluxDB service on startup:

sudo systemctl enable --now influxdb

Create Influx database:

influx
CREATE DATABASE ${YOUR_DB_NAME}
exit

Assign a one-month based retention policy to the database:

DROP RETENTION POLICY "autogen" ON "${YOUR_DB_NAME}"
CREATE RETENTION POLICY "one_month" ON "${YOUR_DB_NAME}" DURATION 730h0m REPLICATION 1 DEFAULT

INSTALL AND CONFIGURE GRAFANA (@ VM)

Install needed package:

sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
sudo apt update
sudo apt install grafana

NOTE if you receive the following error:

bash: add-apt-repository: command not found

You need to install ‘software-properties-common’ package:

apt install software-properties-common

If you want to run Grafana service on port 80, edit the /etc/grafana/grafana.ini file:

http_port = 80

After that, you need to run this command to allow running Grafana service as non-root (the default user is ‘grafana’):

sudo setcap 'cap_net_bind_service=+ep' /usr/sbin/grafana-server

Start and enable the Grafana service on startup:

sudo systemctl enable --now grafana-server.service

INSTALL AND CONFIGURE COLLECTD (@ OPENWRT)

Install needed packages:

sudo opkg install collectd collectd-mod-cpu collectd-mod-dns collectd-mod-interface collectd-mod-iwinfo collectd-mod-load collectd-mod-logfile collectd-mod-memory collectd-mod-network collectd-mod-openvpn collectd-mod-ping collectd-mod-rrdtool collectd-mod-thermal collectd-mod-uptime collectd-mod-wireless

Edit /etc/collectd/collectd.conf to configure the service:

BaseDir "/var/run/collectd"
Include "/etc/collectd/conf.d"
PIDFile "/var/run/collectd.pid"
PluginDir "/usr/lib/collectd"
TypesDB "/usr/share/collectd/types.db"
Interval 10
ReadThreads 2
Hostname "${YOUR_OPENWRT_HOSTNAME_HERE}"

LoadPlugin ping
<Plugin ping>
        TTL 127
        Interval 10
        Host "1.1.1.1"
</Plugin>

LoadPlugin memory
LoadPlugin cpu
LoadPlugin load
LoadPlugin uptime

LoadPlugin interface
<Plugin interface>
        IgnoreSelected false
        Interface "YOUR_INTERFACE_1_NAME_HERE" # i.e. "pppoe-wan"
        Interface "YOUR_INTERFACE_2_NAME_HERE" # i.e. "br-lan"
        Interface "YOUR_INTERFACE_3_NAME_HERE"
</Plugin>

LoadPlugin dns
<Plugin dns>
        Interface "YOUR_INTERFACE_1_NAME_HERE" # i.e. "pppoe-wan"
        Interface "YOUR_INTERFACE_2_NAME_HERE" # i.e. "br-lan"
        Interface "YOUR_INTERFACE_3_NAME_HERE"
        IgnoreSource "127.0.0.1"
</Plugin>

LoadPlugin thermal
<Plugin thermal>
        IgnoreSelected false
</Plugin>

LoadPlugin network
<Plugin network>
        Server "${YOUR_INFLUXDB_SERVER_ADDRESS_HERE}" "25826"
        CacheFlush 86400
        Forward false
</Plugin>

Start and enable Collectd service on startup:

sudo /etc/init.d/collectd start
sudo /etc/init.d/collectd enable

TESTING IF IT WORKS PROPERLY

Once OpenWRT is configured, check if it’s working. SSH to your Grafana/InfluxDB server and issue:

influx
use ${YOUR_DB_NAME}
show measurements
select * from uptime_value

And verify if every 10 seconds the records are getting updated.

GRAFANA DASHBOARD

Login to your Grafana webUI and follow this steps:

CREATE NEW DATA SOURCE

  • Go to https://${YOUR_GRAFANA_SERVER_ADDRESS}/datasources and click on "Add"
  • Change "Name"
  • Fill "URL" field under HTTP settings
  • Fill "Database" field and set "HTTP method" to GET under "InfluxDB Details" settings

IMPORT AN EXISTING DASHBOARD

  • Place your mouse over the + sign at the left side of screen
  • Click on Import
  • In the Grafana.com Dashboard field, type "11858"
  • Click on the Load button

IMPORTANT!

There are things on that dashboard that won’t be working by default right after you create it, such as the Wi-Fi related stuff and maybe other elements depending on your setup.

At this point, it’s time for you to verify the queries definition and modifying it to suit your needs.