gdm85
gdm85
Feb 10, 2018 · 2 min read

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.

HackerNoon.com

how hackers start their afternoons.

gdm85

Written by

gdm85

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

HackerNoon.com

how hackers start their afternoons.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade