예제 #1
0
        if 'udp.port' in layers and None != layers['udp.port']:
            udp_ports = layers['udp.port']
        elif 'tcp.port' in layers and None != layers['tcp.port']:
            tcp_ports = layers['tcp.port']
        else:
            # Not TCP or UDP, ignore
            continue
        traffic = Traffic.new(src, src_mac, dst, dst_mac, udp_ports, tcp_ports,
                              size)
        # Which end(s), src or dst or both, is/are inside the LAN?
        if src.startswith(SUBNET):
            if not (src in MACHINES):
                MACHINES[src] = Machine.new(src, src_mac)
            Machine.add_traffic_out(MACHINES[src], traffic)
        if dst.startswith(SUBNET):
            if not (dst in MACHINES):
                MACHINES[dst] = Machine.new(dst, dst_mac)
            Machine.add_traffic_in(MACHINES[dst], traffic)
        # Periodically push the buffered data to the DB, and reset the buffer
        n += 1
        if n > PUSH_TO_DB_AFTER:
            n = 0
            for ip in sorted(MACHINES.keys(),
                             key=lambda ip: int(ip.split('.')[3])):
                m = MACHINES[ip]
                dbm = db.get(ip)
                if dbm:
                    m = Machine.merge(dbm, m)
                db.put(ip, m)
            MACHINES = {}