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

Source Code for Module sqlite_to_object

  1  #! /usr/local/bin/python 
  2  #-*- coding: utf-8 -*- 
  3   
  4  """sqlite_to_object 
  5   
  6  Read the data from the base and generate a serialized graph object. 
  7   
  8  Allows to filter the content of the base. 
  9  Filters can be made on: 
 10   - the timestamp ; 
 11   - destination address ; 
 12   - source address. 
 13  """ 
 14   
 15  __author__ = "Jerome Hussenet, Cedric Bonhomme" 
 16  __version__ = "$Revision: 0.3 $" 
 17  __date__ = "$Date: 2009/02/19 $" 
 18  __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme" 
 19  __license__ = "Python" 
 20   
 21  import os 
 22  import sys 
 23   
 24  from datetime import datetime 
 25  from time import mktime 
 26   
 27  import pickle 
 28  import sqlite3 
 29   
 30  # kinds of requests 
 31  requests = { \ 
 32              "all" : "SELECT ip_src, ip_dst FROM ip_link", \ 
 33              "tts" : "SELECT ip_src, ip_dst FROM ip_link WHERE tts >= tts1 AND tts <=  tts2",\ 
 34              "time" : "SELECT ip_src, ip_dst FROM ip_link WHERE tts >= tts1 AND tts <=  tts2",\ 
 35              "ip_src" : "SELECT ip_src, ip_dst FROM ip_link WHERE ip_src = ipsrc" ,\ 
 36              "ip_dst" : "SELECT ip_src, ip_dst FROM ip_link WHERE ip_dst = ipdst" \ 
 37              } 
 38   
39 -def sqlite_to_object(sqlite_file, obj_file, request_type, parameter):
40 """Querys SQLite data base. 41 42 Extracts information from the SQLite base 43 and serialize an object containing the result. 44 """ 45 if options.verbose: 46 print "DB connect" 47 conn = sqlite3.connect(sqlite_file, isolation_level = None) 48 49 # Builds the SQLite request 50 req = requests[request_type] 51 if request_type == "ip_src": 52 req = req.replace("ipsrc", '"' + parameter + '"') 53 elif request_type == "ip_dst": 54 req = req.replace("ipdst", '"' + parameter + '"') 55 elif request_type == "time": 56 parameters = parameter.split(":") 57 t1 = [int(elem) for elem in parameters[0].split("-")] 58 t2 = [int(elem) for elem in parameters[1].split("-")] 59 begin = datetime(t1[0], t1[1], t1[2], t1[3], t1[4], t1[5]) 60 end = datetime(t2[0], t2[1], t2[2], t2[3], t2[4], t2[5]) 61 62 tts1 = str(mktime(begin.timetuple()) + 1e-6 * begin.microsecond) 63 tts2 = str(mktime(end.timetuple()) + 1e-6 * end.microsecond) 64 65 req = req.replace("tts1", tts1) 66 req = req.replace("tts2", tts2) 67 elif request_type == "tts": 68 parameters = parameter.split(":") 69 70 req = req.replace("tts1", parameters[0]) 71 req = req.replace("tts2", parameters[1]) 72 73 if options.verbose: 74 print "Query sent to the base :\n\t" + req 75 liste = conn.execute(req).fetchall() 76 77 dic_ip = {} 78 if options.verbose: 79 print "Creating object..." 80 print "Reading query result..." 81 for ip_src, ip_dst in liste: 82 if ip_src not in dic_ip: 83 dic_ip[ip_src] = {} 84 dic_ip[ip_src][ip_dst] = 1 85 else: 86 if ip_dst not in dic_ip[ip_src]: 87 dic_ip[ip_src][ip_dst] = 1 88 else: 89 dic_ip[ip_src][ip_dst] += 1 90 91 if options.verbose: 92 print "Serialization..." 93 dic_obj = open(obj_file, "w") 94 pickle.dump(dic_ip, dic_obj) 95 dic_obj.close()
96 97 98 if __name__ == '__main__': 99 # Point of entry in execution mode. 100 from optparse import OptionParser 101 parser = OptionParser() 102 parser.add_option("-i", "--input", dest="sqlite_file", 103 help="SQLite base") 104 parser.add_option("-o", "--output", dest="obj_file", 105 help="The Python serialized object") 106 parser.add_option("-r", "--request", dest="request_type", 107 help="type of the request") 108 parser.add_option("-p", "--parameter", dest="parameter", 109 help="The parameter of the request") 110 parser.add_option("-q", "--quiet", 111 action="store_false", dest="verbose", 112 help="be vewwy quiet (I'm hunting wabbits)") 113 parser.set_defaults(sqlite_file = './data/ip.sql', 114 obj_file = './data/dic.pyobj', 115 request_type = 'all', 116 parameter = '', 117 verbose = True) 118 119 (options, args) = parser.parse_args() 120 121 if options.request_type != 'all' and options.parameter == '': 122 parser.error("Request parameter needed") 123 124 sqlite_to_object(options.sqlite_file, options.obj_file, 125 options.request_type, options.parameter) 126