Monitoring traffic throughput with Grafana and Prometheus

Hi!

I’ve set up a Tor Node yesterday and i wanted to monitor it’s traffic (not just the total bandwidth, I already accomplished this with a python script and the Control Port).

I know that nyx exists and it can show realtime network traffic as well as total traffic.

However, i am unable to find out where i can get the realtime metrics for the traffic from the Control Port and thus importing it into Grafana with Prometheus.

How can i achieve that? Where do i have to look to find realtime traffic data? Where does nyx get that data from?

I have found some Grafana dashboards using Influxdb, but i’d like to get it running with Prometheus.

Reading the Control Port documentation I found bw-event-cache, but this doesn’t seem to generate new data unless Nyx is opened and running.
Sample python code to get bw-event-cache output:

from stem import Signal
from stem.control import Controller

def get_bandwidth_data():
    with Controller.from_port(port=9051) as controller:
        controller.authenticate("xxxxx")

        # Request bandwidth information using get_info
        bw_info = controller.get_info("bw-event-cache")

        # Parse the bandwidth information
        bw_data = {}
        for entry in bw_info.split():
            parts = entry.split(',')
            if len(parts) == 2:
                read, write = map(int, parts)
                bw_data['Read'] = read
                bw_data['Write'] = write
                print(bw_data)

if __name__ == "__main__":
    get_bandwidth_data()

Hi, if you are trying to use Prometheus with tor, I recommend you look on the side of MetricsPort. tor can export metrics in a format Prometheus understands, which is probably easier than making your own control-port <=> prometheus adapter.

Now to anwser “Where does nyx get that data from?”: it loops sending GETINFO traffic/read and GETINFO traffic/written, which gives it back total bytes transferred, and subtract from that the value it got a second ago.

What about to put this in your torrc:

MetricsPort 127.0.0.1:9052
MetricsPortPolicy accept 127.0.0.1

reload the Tor service and run

curl -s localhost:9052/metrics

You can then use Prometheus to scrape that and Grafana to display it.

This seems like a good idea. I’ll try that out!
Since i am also new to grafana do you by chance know, if grafana can do the substraction of the current and last value?

prometheus has rate() and irate() functions to derive a rate from an ever growing total count. I’m not sure what the difference between both is.

I do use only __rate_interval, examples are in these dashboards : https://github.com/toralf/torutils/tree/main/dashboards .