This article is an excerpt from my book Mastering Python Networking, Third Edition. This book is a bestselling guide to Python for network automation, programmability, and DevOps. Taken from the chapter “Network Monitoring with Python” where we discuss various tools, including Simple Network Management Protocol (SNMP) and the related Python libraries to work with SNMP, this article explains Pygal with an example.
We gather network data for the purpose of gaining insight into our network. One of the best ways to know what the data means is to visualize it with graphs. This is true for almost all data, but especially true for time series data in the context of network monitoring. How much data was transmitted over the network in the last week? What is the percentage of the TCP protocol among all the traffic? These are values we can glean from using data-gathering mechanisms such as SNMP, and we can produce visualization graphs with some of the popular Python libraries. We can use two of the popular Python libraries, Matplotlib and Pygal, to graph the data collected using SNMP. In this article, we will focus on Pygal.
Pygal is a dynamic SVG charting library written in Python. The biggest advantage of Pygal, in my opinion, is that it produces Scalable Vector Graphics (SVG) format graphs easily and natively. There are many advantages of SVG over other graph formats, but two of the main advantages are that it is web browser-friendly and it provides scalability without sacrificing image quality. In other words, you can display the resulting image in any modern web browser and zoom in and out of the image without losing the details of the graph.
We can do this in a few lines of Python code? How cool is that?
To begin, let’s get Pygal installed, then move on to the first example.
The installation is done via pip:
(venv)$ pip install pygal
Pygal – The First Example
Let’s look at the line chart example demonstrated on Pygal’s documentation, available at http://pygal.org/en/stable/documentation/types/line.html:
>>> import pygal >>> line_chart = pygal.Line() >>> line_chart.title = 'Browser usage evolution (in %)' >>> line_chart.x_labels = map(str, range(2002, 2013)) >>> line_chart.add('Firefox', [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1]) <pygal.graph.line.Line object at 0x7f4883c52b38> >>> line_chart.add('Chrome', [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3]) <pygal.graph.line.Line object at 0x7f4883c52b38> >>> line_chart.add('IE', [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1]) <pygal.graph.line.Line object at 0x7f4883c52b38> >>> line_chart.add('Others', [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5]) <pygal.graph.line.Line object at 0x7f4883c52b38> >>> line_chart.render_to_file('pygal_example_1.svg')
In this example, we created a line object with the x_labels automatically rendered as strings for 11 units. Each of the objects can be added with the label and the value in a list format, such as Firefox, Chrome, and IE.
The interesting bit to focus on is the fact that each of the line chart items has the exact number of matching numbers to the number of x units. In years when there is no value, for example, the years 2002 – 2007 for Chrome, the value None is entered.
Here is the resulting graph, as viewed in the Firefox browser:
Figure 1: Pygal sample graph
Now that we can see the general usage of Pygal, we can use the same method to graph the SNMP results we have in hand.
Note: The SNMP results can be found on https://github.com/PacktPublishing/Mastering-Python-Networking-Third-Edition/blob/master/Chapter07/results.txt
Pygal for SNMP results
For the Pygal line graph, we no longer need to convert the x-axis value into an internal float, as we did for Matplotlib; however, we do need to convert the numbers in each of the values we would have received into float:
#!/usr/bin/env python3 import pygal x_time =  out_octets =  out_packets =  in_octets =  in_packets =  with open('results.txt', 'r') as f: for line in f.readlines(): # eval(line) reads in each line as dictionary instead of string line = eval(line) x_time.append(line['Time']) out_packets.append(float(line['Gig0-0_Out_uPackets'])) out_octets.append(float(line['Gig0-0_Out_Octet'])) in_packets.append(float(line['Gig0-0_In_uPackets'])) in_octets.append(float(line['Gig0-0_In_Octet']))
We can use the same mechanism that we saw to construct the line graph:
line_chart = pygal.Line() line_chart.title = "Router 1 Gig0/0" line_chart.x_labels = x_time line_chart.add('out_octets', out_octets) line_chart.add('out_packets', out_packets) line_chart.add('in_octets', in_octets) line_chart.add('in_packets', in_packets) line_chart.render_to_file('pygal_example_2.svg')
The outcome is that the graph is now in an SVG format that can be easily displayed on a web page. It can be viewed in a modern web browser:
Figure 2: Router 1— Pygal multiline graph
Just like Matplotlib, Pygal provides many more options for graphs. For example, to graph the pie chart we saw previously in Matplotlib, we can use the pygal.Pie() object. This is shown in pygal_2.py:
#!/usr/bin/env python3 import pygal line_chart = pygal.Pie() line_chart.title = "Protocol Breakdown" line_chart.add('TCP', 15) line_chart.add('UDP', 30) line_chart.add('ICMP', 45) line_chart.add('Others', 10) line_chart.render_to_file('pygal_example_3.svg')
The resulting SVG file is shown here:
Figure 3: Pygal pie chart
Pygal is a great tool when it comes to generating production ready SVG graphs. If this is the type of graph that is required, look no further than the Pygal library. Like Matplotlib, there are many additional resources to help us to learn about Pygal.
Additional Pygal Resources:
Pygal provides many more customizable features and graphing capabilities for the data you collect from basic network monitoring tools such as SNMP. We demonstrated a simple line graph and pie graph in this section.
You can find more information about the project here:
- Pygal documentation: http://www.pygal.org/en/stable/index.html
- Pygal GitHub project page: https://github.com/Kozea/pygal