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

Source Code for Module sqlite_to_picviz

  1  #! /usr/local/bin/python 
  2  #-*- coding: utf-8 -*- 
  3   
  4  """sqlite_to_picviz 
  5  """ 
  6   
  7  __author__ = "Jerome Hussenet, Cedric Bonhomme" 
  8  __version__ = "$Revision: 0.1 $" 
  9  __date__ = "$Date: 2009/03/30 $" 
 10  __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme" 
 11  __license__ = "Python" 
 12   
 13  import os 
 14  import sys 
 15   
 16  from datetime import datetime 
 17  from time import mktime 
 18   
 19  import sqlite3 
 20   
 21  # kinds of requests 
 22  requests = { \ 
 23              "all" : "SELECT tts, ip_src, ip_dst FROM ip_link", \ 
 24              "tts" : "SELECT tts, ip_src, ip_dst FROM ip_link WHERE tts >= tts1 AND tts <=  tts2",\ 
 25              "time" : "SELECT tts, ip_src, ip_dst FROM ip_link WHERE tts >= tts1 AND tts <=  tts2",\ 
 26              "ip_src" : "SELECT tts, ip_src, ip_dst FROM ip_link WHERE ip_src = ipsrc" ,\ 
 27              "ip_dst" : "SELECT tts, ip_src, ip_dst FROM ip_link WHERE ip_dst = ipdst" \ 
 28              } 
 29   
30 -def sqlite_to_picviz(sqlite_file, picviz_file, request_type, parameter):
31 """Querys SQLite data base. 32 33 Extracts information from the SQLite base 34 and serialize an object containing the result. 35 """ 36 if options.verbose: 37 print "DB connect" 38 conn = sqlite3.connect(sqlite_file, isolation_level = None) 39 40 # Builds the SQLite request 41 req = requests[request_type] 42 if request_type == "ip_src": 43 req = req.replace("ipsrc", '"' + parameter + '"') 44 elif request_type == "ip_dst": 45 req = req.replace("ipdst", '"' + parameter + '"') 46 elif request_type == "time": 47 parameters = parameter.split(":") 48 t1 = [int(elem) for elem in parameters[0].split("-")] 49 t2 = [int(elem) for elem in parameters[1].split("-")] 50 begin = datetime(t1[0], t1[1], t1[2], t1[3], t1[4], t1[5]) 51 end = datetime(t2[0], t2[1], t2[2], t2[3], t2[4], t2[5]) 52 53 tts1 = str(mktime(begin.timetuple()) + 1e-6 * begin.microsecond) 54 tts2 = str(mktime(end.timetuple()) + 1e-6 * end.microsecond) 55 56 req = req.replace("tts1", tts1) 57 req = req.replace("tts2", tts2) 58 elif request_type == "tts": 59 parameters = parameter.split(":") 60 61 req = req.replace("tts1", parameters[0]) 62 req = req.replace("tts2", parameters[1]) 63 64 if options.verbose: 65 print "Query sent to the base :\n\t" + req 66 liste = conn.execute(req).fetchall() 67 68 if options.verbose: 69 print "Creating Picviz file..." 70 picviz_header = 'header {\n\ttitle = "IP-Link - Picviz";\n}' 71 picviz_axes = 'axes {\n\ttimeline t [label="Time"];\n\t' + \ 72 'ipv4 i [label="Source IP"];\n\t' + \ 73 'ipv4 j [label="Destination IP"];\n}' 74 picviz_data = 'data {\n' 75 picviz = picviz_header + picviz_axes + picviz_data 76 77 for tts, ip_src, ip_dst in liste: 78 datetime_time = datetime.fromtimestamp(tts) 79 hour = datetime_time.strftime("%H:%M") 80 picviz += '\tt= "' + hour + '", i= "' + \ 81 ip_src + '", j= "' + ip_dst + '" '+ \ 82 '[color="red"];\n' 83 picviz += '}' 84 85 if options.verbose: 86 print "Writting file..." 87 pic = open(picviz_file, "w") 88 pic.write(picviz) 89 pic.close()
90 91 92 if __name__ == '__main__': 93 # Point of entry in execution mode. 94 from optparse import OptionParser 95 parser = OptionParser() 96 parser.add_option("-i", "--input", dest="sqlite_file", 97 help="SQLite base") 98 parser.add_option("-o", "--output", dest="picviz_file", 99 help="Picviz file") 100 parser.add_option("-r", "--request", dest="request_type", 101 help="type of the request") 102 parser.add_option("-p", "--parameter", dest="parameter", 103 help="The parameter of the request") 104 parser.add_option("-q", "--quiet", 105 action="store_false", dest="verbose", 106 help="be vewwy quiet (I'm hunting wabbits)") 107 parser.set_defaults(sqlite_file = './data/ip.sql', 108 picviz_file = './data/ip.pcv', 109 request_type = 'all', 110 parameter = '', 111 verbose = True) 112 113 (options, args) = parser.parse_args() 114 115 if options.request_type != 'all' and options.parameter == '': 116 parser.error("Request parameter needed") 117 118 sqlite_to_picviz(options.sqlite_file, options.picviz_file, 119 options.request_type, options.parameter) 120