Module csv_to_histogram
[hide private]
[frames] | no frames]

Source Code for Module csv_to_histogram

  1  #! /usr/local/bin/python 
  2  #-*- coding: utf-8 -*- 
  3   
  4  """csv_to_histogram.py 
  5   
  6  Uses pylab Python module to display a histogram 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   
20 -class excel_french(csv.Dialect):
21 delimiter = ';' 22 quotechar = '"' 23 doublequote = True 24 skipinitialspace = False 25 lineterminator = '\n' 26 quoting = csv.QUOTE_MINIMAL
27 csv.register_dialect('excel_french', excel_french) 28 29
30 -def csv_to_histogram(csv_file, ip_src):
31 """Display a histogram. 32 33 The generated histogram corresponds to the 10 most IP visited by 'ip_src". 34 """ 35 # list of IP contacted by ip_src. 36 cr = csv.reader(open(csv_file, "rb"), 'excel_french') 37 # contains the tuples (ip_dest, weight) 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 length = len(liste) 47 ind = pylab.arange(length) # ip destinations in abscissa 48 width = 0.35 # bars width 49 50 ip_dst = [elem[0] for elem in liste] 51 weight = [int(elem[1]) for elem in liste] 52 53 max_weight = max(weight) # max weight 54 55 p = pylab.bar(ind, weight, width, color='r') 56 57 pylab.ylabel("weight") 58 pylab.title("IPs contacted by " + ip_src) 59 pylab.xticks(ind + (width / 2), range(1, len(ip_dst)+1)) 60 pylab.xlim(-width, len(ind)) 61 62 # changing the ordinate scale according to the max. 63 if max_weight <= 100: 64 pylab.ylim(0, max_weight + 5) 65 pylab.yticks(pylab.arange(0, max_weight + 5, 5)) 66 elif max_weight <= 200: 67 pylab.ylim(0, max_weight + 10) 68 pylab.yticks(pylab.arange(0, max_weight + 10, 10)) 69 elif max_weight <= 600: 70 pylab.ylim(0, max_weight + 25) 71 pylab.yticks(pylab.arange(0, max_weight + 25, 25)) 72 elif max_weight <= 800: 73 pylab.ylim(0, max_weight + 50) 74 pylab.yticks(pylab.arange(0, max_weight + 50, 50)) 75 76 pylab.show() 77 else: 78 print "No result for", ip_src
79 80 81 if __name__ == '__main__': 82 # Point of entry in execution mode. 83 try: 84 import pylab 85 except ImportError: 86 print "Error : pylab module missing." 87 print "http://matplotlib.sourceforge.net/" 88 exit(1) 89 90 from optparse import OptionParser 91 parser = OptionParser() 92 parser.add_option("-i", "--input", dest="csv_file", 93 help="CSV file") 94 parser.add_option("-s", "--source-ip", dest="ip_src", 95 help="Source IP") 96 parser.add_option("-q", "--quiet", 97 action="store_false", dest="verbose", 98 help="be vewwy quiet (I'm hunting wabbits)") 99 parser.set_defaults(csv_file = './data/ip.csv', 100 ip_src = '192.168.1.1', 101 verbose = True) 102 103 (options, args) = parser.parse_args() 104 105 csv_to_histogram(options.csv_file, options.ip_src) 106