Network traffic plots

Here is how I did the network traffic plots. They are generated using a systemd timer unit together with a shell and a python script.

The timer executes the shell script once every hour. The shell script in turn uses vnstat to generate a .xml file with all the traffic data in it. Then it invokes the python script to convert the .xml file into .csv files which gnuplot can read and generate the plot graphics.

Being used to cron it took me quite some time to figure out how systemd timers work. To execute some command, one needs to write three files. Each a .timer a .target and a .service file. If they are correctly set up, the timer activates the service which in turn executes the command.

Here are the three files used for the network graphs:

The time interval is defined by the line starting with “OnCalendar=” in the .timer file. There are lots of options how the time can be specified. All options are well explained in the systemd.time man page. I chose minute 55 of every hour to start the timer.

The command to execute by the .service file is given in the line starting with “ExecStart=”.

To install the new timer, the three files need to be moved to “/usr/lib/systemd/system/” and started with the commands

The first two commands register the timer and the service in the system and the third command starts the timer. Whether the timer works correctly can always be verified with the commmand

Now having the timer running, we can concentrate on the actual software. The files reside in “/usr/share/netgraph/”. The entry point is the shell file “”, which controls vnstat, the python script and gnuplot.

The first line invokes vnstat and directs the output to the file “vnstat.xml”. The second and third line calls the python script to convert the .xml file to .csv files and the last line calls gnuplot.

Now lets have a look at the python script “”

It parses the file “vnstat.xml” with the xml library. All the data points get written into a list of tuples which is sorted and subsequently written to the output files.

The gnuplot script in turn is rather simple. It uses “set xdata time” to generate plots with time on the x axis and directly outputs the .svg graphics into the nginx server directory so that they can be imported into the site.

This entry was posted in Projects. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *