A RESTful API for iftop

gdm85
HackerNoon.com
2 min readFeb 10, 2018

--

If you have ever used iftop or any other of the plethora of available network monitoring tools on Linux, you must be at least a bit impressed with the detailed information that you can view/summarize through them.

Most of these tools use libpcap to inspect packets in real time and aggregate/store their details; now comes the question: what if I would like to read this information with a webservice API?

An iftop microservice

It turns out this is a neat use-case for a tiny iftop microservice. With this API we could retrieve network usage statistics based on IP address, protocol type and sent/received bytes count.

I went ahead and added a minimal RESTful API to iftop; this is the (beautified) output of a call to /iftop/history:

Nice, isn’t it? There is also a method for /iftop/version that allows reading the current version.

You can run it with:

iftop -S 8080

By default it will drop root privileges to user nobody and group nogroup(configurable).

Enabling the webservice will disable the curses or text-based output and the iftop logging output (on standard error) will become more similar to a webservice:

You can get the latest version of my iftop patches from here:

Upstreaming

In hindsight I am delighted to have chosen iftop because the code comments are hilarious. My best pick:

Also, I haven't implemented removal of items from the tree. So sue me.

The patches have been sent upstream, however I have not received any reply from the maintainer yet. I do not have high hopes in them being accepted but regardless you can already use them and the webservice API feature by cloning the GitHub repository and building iftop.

--

--

gdm85
HackerNoon.com

Thinker, software developer, cryptography passionate and an avid reader of science and technology.