-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkchart-matplotlib-donut-prot.py
72 lines (57 loc) · 1.59 KB
/
mkchart-matplotlib-donut-prot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import psycopg2
INTERVAL = '5 minutes'
CONNSTR = 'postgresql://xenoeye:password@localhost/xenoeyedb'
PTHRESHOLD = 4.0
protocols = []
octets = []
explode = []
protolabels = []
# Load dataset
query = """
select iana_protocols.name, sum(octets) as oct
from ingress_proto
join iana_protocols on ingress_proto.proto=iana_protocols.num
where time >= now() - interval '{}'
group by iana_protocols.name
order by oct desc
""".format(INTERVAL)
conn = psycopg2.connect(CONNSTR)
cursor = conn.cursor()
cursor.execute(query)
records = cursor.fetchall()
for record in records:
protocols.append(record[0])
octets.append(record[1])
explode.append(0.05)
cursor.close()
conn.close()
# prepare labels
sm = sum(octets)
for i in range(len(protocols)):
proc = 100 * octets[i] / sm
protolabels.append('{} - {:.2f}%'.format(protocols[i], 100 * octets[i] / sm))
# don't display protocol name when % less than threshold
if proc < PTHRESHOLD:
protocols[i] = ''
# plot
# Pie Chart
plt.pie(octets,
labels=protocols,
autopct=lambda p: format(p, '.2f')+'%' if p > 4 else None,
pctdistance=0.7,
explode=explode)
# draw circle
centre_circle = plt.Circle((0, 0), 0.50, fc='white')
fig = plt.gcf()
# Adding Circle in Pie chart
fig.gca().add_artist(centre_circle)
# Adding Title of chart
plt.title('IP protocols for the last {}'.format(INTERVAL))
# Add Legends
plt.legend(protolabels, title='IP protocols', bbox_to_anchor=(1, 1), loc='upper left')
plt.tight_layout()
plt.savefig('mpl-donut.png')