1
2
3
4 """csv_to_piechart.py
5
6 Uses pylab Python module to display a pie chart wich represent
7 the IP contacted by a source IP.
8 """
9
10 __author__ = "Jerome Hussenet, Cedric Bonhomme"
11 __version__ = "$Revision: 0.2 $"
12 __date__ = "$Date: 2009/02/22 $"
13 __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme"
14 __license__ = "Python"
15
16 import os
17 import sys
18 import csv
19
27 csv.register_dialect('excel_french', excel_french)
28
29
31 """Display the pie chart.
32
33 The generated histogram corresponds to the 10 most IP visited by 'ip_src'.
34 """
35
36 cr = csv.reader(open(csv_file, "rb"), 'excel_french')
37
38 liste = []
39 for row in cr:
40 if row[0] == ip_src:
41 liste.append((row[1], row[2]))
42
43 if liste:
44 liste = sorted(liste, key = lambda x: (x[1], x[0]), reverse = True)[:10]
45
46 pylab.figure(1, figsize=(6,6))
47 ax = pylab.axes([0.1, 0.1, 0.8, 0.8])
48
49 labels = tuple([elem[0] for elem in liste])
50 fracs = [int(elem[1]) for elem in liste]
51
52
53 explode = (0,)*len(fracs)
54 pylab.pie(fracs, explode=explode, labels=labels, autopct='%1.0f%%', \
55 shadow=True)
56 pylab.title(("IPs contacted by " + ip_src))
57
58 pylab.show()
59 else:
60 print "No result for ", ip_src
61
62 if __name__ == "__main__":
63
64 try:
65 import pylab
66 except ImportError:
67 print "Error : pylab module missing."
68 print "http://matplotlib.sourceforge.net/"
69 exit(1)
70
71 from optparse import OptionParser
72 parser = OptionParser()
73 parser.add_option("-i", "--input", dest="csv_file",
74 help="CSV file")
75 parser.add_option("-s", "--source-ip", dest="ip_src",
76 help="Source IP")
77 parser.add_option("-q", "--quiet",
78 action="store_false", dest="verbose",
79 help="be vewwy quiet (I'm hunting wabbits)")
80 parser.set_defaults(csv_file = './data/ip.csv',
81 ip_src = '192.168.1.1',
82 verbose = True)
83
84 (options, args) = parser.parse_args()
85
86 csv_to_piechart(options.csv_file, options.ip_src)
87