paint-brush
A RESTful API for iftopby@gdm85
743 reads
743 reads

A RESTful API for iftop

by Giuseppe D.M.February 10th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

If you have ever used <a href="http://www.ex-parrot.com/pdw/iftop/" target="_blank">iftop</a> or <a href="http://www.binarytides.com/linux-commands-monitor-network/" target="_blank">any other</a> of the plethora of <a href="https://blog.serverdensity.com/80-linux-monitoring-tools-know/" target="_blank">available network monitoring tools on Linux</a>, you must be at least a bit impressed with the detailed information that you can view/summarize through them.
featured image - A RESTful API for iftop
Giuseppe D.M. HackerNoon profile picture

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:


gdm85/iftopMirror of current iftop git repository with the webservice API patches

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.