class ImportWorker(threading.Thread): def __init__(self, server, datareader): super(ImportWorker, self).__init__() self.server = server self.datareader = datareader self.queue = datareader.get_queue() self.processed_packets = 0 def init_voltdb(self): from voltdb import FastSerializer, VoltProcedure self.v_client = FastSerializer(self.server, 21212) print "making client with %s" % self.server self.v_proc = VoltProcedure(self.v_client, "InsertPacket", [ FastSerializer.VOLTTYPE_TIMESTAMP, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_INTEGER, FastSerializer.VOLTTYPE_INTEGER, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_INTEGER ]) def destroy_voltdb(self): self.v_client.close() def handle_packet(self, packet): time = packet["ts"] + packet["ms"] / 10000.0 time = datetime.datetime.fromtimestamp(time) srcIP = str(packet.get("srcIP", "")) dstIP = str(packet.get("dstIP", "")) srcPort = packet.get("srcPort", None) dstPort = packet.get("dstPort", None) flag = "" if packet["SYN"]: flag = "S" if packet["FIN"]: flag = "F" payload = str(packet.get("payload", "")) pType = str(packet.get("type", "")) size = packet.get("size", 0) self.v_proc.call( [time, srcIP, dstIP, srcPort, dstPort, flag, payload, pType, size]) def get_processed_packets(self): return self.processed_packets def run(self): self.init_voltdb() while True: batch = self.queue.get(True) for packet in batch: self.handle_packet(packet) self.processed_packets += len(batch) self.destroy_voltdb()
def query4(): client = get_voltdb_client() proc = VoltProcedure(client, "@AdHoc", [FastSerializer.VOLTTYPE_STRING]) result = proc.call([ "SELECT DST_IP, Dst_Port FROM Well_Known_Ports ORDER BY INET_ATON(Dst_IP), Dst_Port" ]) client.close() for elem in result.tables[0].tuples: print "IP %-16s Port %s" % (elem[0], elem[1]) pass
def init_voltdb(self): from voltdb import FastSerializer, VoltProcedure self.v_client = FastSerializer(self.server, 21212) print "making client with %s" % self.server self.v_proc = VoltProcedure(self.v_client, "InsertPacket", [ FastSerializer.VOLTTYPE_TIMESTAMP, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_INTEGER, FastSerializer.VOLTTYPE_INTEGER, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_INTEGER ])
def query5(): #All packets that contain a given byte sequence sequence = raw_input("Enter byte sequence: ") client = get_voltdb_client() proc = VoltProcedure(client, "SelectByteSequence", [FastSerializer.VOLTTYPE_STRING]) result = proc.call([sequence]) client.close() for elem in result.tables[0].tuples: print "Paket %s: %s" % (elem[1], elem[2])
def query3(): ip_str = str(raw_input("Enter IP in dotted Notation: ")) port_int = int(raw_input("Enter Port as single Number: ")) client = get_voltdb_client() proc = VoltProcedure( client, "query3", [FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_INTEGER]) result = proc.call([ip_str, port_int]) client.close() for elem in result.tables[0].tuples: print elem pass
def query2(): print "Retrieve the average data volume for all connections between IP a.b.c.d and IP w.x.y.z" ip_A = str(raw_input("Enter IP a.b.c.d in dotted Notation: ")) ip_B = str(raw_input("Enter IP w.x.y.z in dotted Notation: ")) client = get_voltdb_client() proc = VoltProcedure( client, "SelectAverageDataVolume", [FastSerializer.VOLTTYPE_STRING, FastSerializer.VOLTTYPE_STRING]) result = proc.call([ip_A, ip_B]) client.close() for elem in result.tables[0].tuples: print elem[0] pass
def query1(): dt = getCorrectDate("Enter Time [DD.MM.YYYY HH:MM]: ") dt_min = dt - datetime.timedelta(minutes=1) dt_max = dt + datetime.timedelta(minutes=1) client = get_voltdb_client() partitions = partitionsInRange(dtToTs(dt_min), dtToTs(dt_max)) for partition in partitions: proc = VoltProcedure(client, "SelectActiveConnections", [ FastSerializer.VOLTTYPE_INTEGER, FastSerializer.VOLTTYPE_TIMESTAMP, FastSerializer.VOLTTYPE_TIMESTAMP ]) res = proc.call([partition, dt_min, dt_max]) for elem in res.tables[0].tuples: print "From %s:%s To %s:%s" % (elem[0], elem[1], elem[2], elem[3]) client.close()