My home router is a Ubiquiti EdgeMAX router running EdgeOS 2.0.6. I chose it because wanted a router that could run sflow for testing purposes. I recently came across someone successfully installing Prometheus node_exporter on EdgeOS, so I gave it a try. Since I’d already ansible-automated the install of node_exporter on a switch running Cumulus Linux, I wrote a new playbook with only minor modifications for EdgeOS and it worked perfectly.

Example code used here can be found on Gitlab

Ansible Inventory#

This is the content of my ansible hosts file. Note you will need to set the router login name to something other than admin if your setup differs.

You will also need to replace router01.domain.com with your correct router hostname, and possibly specify it’s IP address if it doesn’t have a DNS entry. Refer to Ansible’s docs on how to build your inventory.

[all:vars]
ansible_user=admin
ansible_network_os=edgeos

[routers]
router01.domain.com

Ansible Playbook#

Below is the content of my ansible playbook called setup.yml, with comments inline:

---

- hosts: router01
  remote_user: admin
  gather_facts: 'yes'
  become: 'yes'

  tasks:

  - name: "Copy SSH public key to router"
    copy:
      src: "~/.ssh/id_rsa.pub"
      dest: "/home/admin/authorized_keys"
      mode: 0600

The above task isn’t needed, but something helpful to allow ansible to ssh to your router without needing a password.

  - set_fact:
      node_exporter_version: '1.0.1'

The current version of Prometheus node_exporter as of the time of this blog post is 1.0.1. You may want to update this to a newer release.

  - name: add node_exporter user
    user:
      name: node_exporter
      shell: /bin/false

I chose to run node_exporter as a different user on the router, so that logging and security could be easily separated out.

  - name: download and unzip node_exporter
    unarchive:
      src: https://github.com/prometheus/node_exporter/releases/download/v{{node_exporter_version}}/node_exporter-{{node_exporter_version}}.linux-mips64.tar.gz
      dest: /tmp
      creates: /tmp/node_exporter-{{node_exporter_version}}.linux-mips64/NOTICE
      remote_src: yes

Note that this task both downloads and unzips the node_exporter release. In addition, it’s downloading the mips64 binary, since that’s the correct CPU architecture for the Ubituiti EdgeMAX router I have. Finally, it will only re-download the tgz file if it’s not already been downloaded, saving time if you want this part of a larger ansible playbook that’s run regularly. This is accomplished by using the creates: option to check if a specific file exists. If the file is present, it won’t try and re-download the tgz file and unzip it again.

  - name: copy node_exporter to bin directory
    copy:
      remote_src: True
      src: /tmp/node_exporter-{{node_exporter_version}}.linux-mips64/node_exporter
      dest: /usr/bin/node_exporter

  - name: change node_exporter ownership
    file:
      path: /usr/bin/node_exporter
      owner: node_exporter
      group: node_exporter
      mode: 0755

  - name: create node_exporter service
    blockinfile:
      path: /etc/systemd/system/node_exporter.service
      create: yes
      block: |
        [Unit]
        Description=Node Exporter
        Wants=network-online.target
        After=network-online.target

        [Service]
        User=node_exporter
        Group=node_exporter
        Type=simple
        ExecStart=/usr/bin/node_exporter --collector.systemd --collector.logind --no-collector.bonding --no-collector.hwmon --no-collector.infiniband --no-collector.ipvs --no-collector.mdadm --no-collector.nfs --no-collector.nfsd --no-collector.powersupplyclass --no-collector.zfs --no-collector.xfs --no-collector.thermal_zone --no-collector.schedstat --no-collector.rapl --no-collector.btrfs --no-collector.bcache --no-collector.cpufreq --no-collector.edac
        Restart=on-failure

        [Install]
        WantedBy=multi-user.target        

  - name: systemctl daemon-reload
    shell: systemctl daemon-reload

  - name: start node_exporter
    service:
      name: node_exporter
      state: restarted
      enabled: yes

At this point, you should be able to pull up a browser to http://192.168.1.1:9100 and get a page loaded if the installation was successful:

node_exporter

Prometheus Config#

Note that you must configure your Prometheus server to periodically poll node_exporter running on your Ubiquiti router in order to gather the stats. Here is an example of the relevant part of my /etc/prometheus/prometheus.yml configuration in the scrape_configs section:

  - job_name: router
    static_configs:
      - targets: ['192.168.1.1:9100']