Example #1
0
File: v5.py Project: bruceSz/pyflow
def read_datagram(addr, data):
    """Yield all record (flow and counter records) from the sFlow v5
    datagram given by up, which is expected to be an xdrlib.Unpacker
    object."""

    up = Unpacker(data)

    version = up.unpack_int()
    if not version == 5:
        hexdump_bytes(data)
        raise Exception()

    af = up.unpack_int()
    if af == 1:                 # IPv4
        agent_address = ntohl(up.unpack_uint())
    else:
        raise Exception()

    sf = Datagram(addr, agent_address)

    sub_agent_id = up.unpack_uint()
    sequence_number = up.unpack_uint()
    uptime = up.unpack_uint()
    nb_sample_records = up.unpack_uint()

    # Iterating over sample records
    for i in range(nb_sample_records):
        try:
            return read_sample_record(up, sf)
        except EOFError:
            stderr.write("read_sample_datagram: EOFError reading sample_record,", \
                      "Premature end of data stream, Skipping record\n")
            up.set_position(len(up.get_buffer()))
            break
Example #2
0
def read_datagram(addr, data):
    """Yield all record (flow and counter records) from the sFlow v5
    datagram given by up, which is expected to be an xdrlib.Unpacker
    object."""

    up = Unpacker(data)

    version = up.unpack_int()
    if not version == 5:
        hexdump_bytes(data)
        raise Exception()

    af = up.unpack_int()
    if af == 1:  # IPv4
        agent_address = ntohl(up.unpack_uint())
    else:
        raise Exception()

    sf = Datagram(addr, agent_address)

    sub_agent_id = up.unpack_uint()
    sequence_number = up.unpack_uint()
    uptime = up.unpack_uint()
    nb_sample_records = up.unpack_uint()

    # Iterating over sample records
    for i in range(nb_sample_records):
        try:
            return read_sample_record(up, sf)
        except EOFError:
            stderr.write("read_sample_datagram: EOFError reading sample_record,", \
                      "Premature end of data stream, Skipping record\n")
            up.set_position(len(up.get_buffer()))
            break
class Trajectory:
    def __init__(self, topology, trajFileName):
        # since we need to be able to do seeks, can't use osOpen
        # which might return an unseekable stream
        self.topology = topology
        from OpenSave import osUncompressedPath
        path = osUncompressedPath(trajFileName)
        import os
        self.trajFileSize = os.stat(path).st_size
        self.traj = open(path, "rb")
        from xdrlib import Unpacker
        self.fileString = FileString(self.traj, 0, self.trajFileSize)
        self.xdr = Unpacker(self.fileString)
        self.crdStarts = []
        while True:
            replyobj.status("Reading frame %d header\n" %
                            (len(self.crdStarts) + 1))
            try:
                crdStart, endFrame = self._readHeader()
            except ValueError, e:
                raise ValueError("Frame %d: %s" %
                                 (len(self.crdStarts) + 1, str(e)))
            if endFrame > self.trajFileSize:
                if not self.crdStarts:
                    raise ValueError("Computed size of"
                                     " first frame (%d) greater than"
                                     " trajectory file size (%s)" %
                                     (endFrame, self.trajFileSize))
                replyobj.warning("Truncated trajectory file;"
                                 " skipping last partial frame.\n")
            else:
                self.crdStarts.append(crdStart)
            if endFrame == self.trajFileSize:
                break
            self.xdr.set_position(endFrame)
Example #4
0
class Trajectory:
	def __init__(self, topology, trajFileName):
		# since we need to be able to do seeks, can't use osOpen
		# which might return an unseekable stream
		self.topology = topology
		from OpenSave import osUncompressedPath
		path = osUncompressedPath(trajFileName)
		import os
		self.trajFileSize = os.stat(path).st_size
		self.traj = open(path, "rb")
		from xdrlib import Unpacker
		self.fileString = FileString(self.traj, 0, self.trajFileSize)
		self.xdr = Unpacker(self.fileString)
		self.crdStarts = []
		while True:
			replyobj.status("Reading frame %d header\n" % (
						len(self.crdStarts) + 1))
			try:
				crdStart, endFrame = self._readHeader()
			except ValueError, e:
				raise ValueError("Frame %d: %s" %
					(len(self.crdStarts) + 1, str(e)))
			if endFrame > self.trajFileSize:
				if not self.crdStarts:
					raise ValueError("Computed size of"
						" first frame (%d) greater than"
						" trajectory file size (%s)" %
						(endFrame, self.trajFileSize))
				replyobj.warning("Truncated trajectory file;"
					" skipping last partial frame.\n")
			else:
				self.crdStarts.append(crdStart)
			if endFrame == self.trajFileSize:
				break
			self.xdr.set_position(endFrame)
Example #5
0
 def unpack(self, u: xdrlib.Unpacker):
     last = u.unpack_uint() + u.get_position()
     if u.get_position() < last:
         self.time_A = struct.unpack('>Q', u.unpack_fopaque(8))[0]
     if u.get_position() < last:
         self.time_B = struct.unpack('>Q', u.unpack_fopaque(8))[0]
     u.set_position(last)
Example #6
0
 def run(self):
     while self.server_running:
         potential_read = [self.server_socket]
         if self.client is not None:
             potential_read.append(self.client)
         try:
             ready_to_read, ready_to_write, in_erro = select.select(
                 potential_read, [], [])
             if self.server_socket in ready_to_read:
                 conn, addr = self.server_socket.accept()
                 self.client = conn
                 print('New connection from ', addr)
             elif self.client in ready_to_read:
                 # self.client.recv_into(self.buffer, 512)
                 recv = self.client.recv(128)
                 self.buffer += recv
                 if len(recv) == 0:
                     print('Disconnection from client')
                     self.client.close()
                     self.client = None
                     self.buffer = ''
                     continue
                 unpack = Unpacker(self.buffer)
                 if len(self.buffer) >= unpack.unpack_int():
                     unpack.set_position(0)
                     size = unpack.unpack_int()
                     cmd = unpack.unpack_int()
                     if cmd == ServerMouseController.PACKET_MOVE:
                         # Mouse move control
                         x = unpack.unpack_float()
                         y = unpack.unpack_float()
                         print(size, cmd, x, y)
                         self.mouse_controller.move(
                             self.mouse_controller.position()[0] - x,
                             self.mouse_controller.position()[1] - y)
                     elif cmd == ServerMouseController.PACKET_CLICK:
                         # Mouse click control
                         button = unpack.unpack_int()
                         nb_click = unpack.unpack_int()
                         print(size, cmd, button, nb_click)
                         self.mouse_controller.click(
                             self.mouse_controller.position()[0],
                             self.mouse_controller.position()[1],
                             button,
                             nb_click)
                     elif cmd == ServerMouseController.PACKET_SCROLL:
                         # Mouse scrolling
                         x = unpack.unpack_float()
                         y = unpack.unpack_float()
                         print(size, cmd, x, y)
                         self.mouse_controller.scroll(
                             vertical=int(y), horizontal=int(x))
                     self.buffer = self.buffer[unpack.get_position():]
         except select.error as e:
             print(e)
     if self.client is not None:
         self.client.close()
     self.server_socket.close()
     print('Server stop')
Example #7
0
    def unpack(self, u: xdrlib.Unpacker):
        last = u.unpack_uint() + u.get_position()
        ret = ReturnCode.unpack(u)

        if ret == ReturnCode.OK:
            last2 = u.unpack_uint() + u.get_position()
            if u.get_position() < last2:
                self.digest = u.unpack_fopaque(20)
            if u.get_position() < last2:
                self.salt = u.unpack_fopaque(8)
            u.set_position(last2)
        u.set_position(last)
Example #8
0
    def unpack(self, u: xdrlib.Unpacker):
        last = u.unpack_uint() + u.get_position()
        if u.get_position() < last:
            last2 = u.unpack_uint()
            self.canditate_selection_type = CandidateSelectionType(
                u.unpack_uint())
            if self.canditate_selection_type == CandidateSelectionType.CANDIDATE_SEL_MANUAL:
                last3 = u.unpack_uint() + u.get_position()
                if u.get_position() < last3:
                    nb = u.unpack_uint()
                    self.drive_refs = []
                    for i in range(nb):
                        self.drive_refs.append(u.unpack_fopaque(20))
                u.set_position(last3)
            u.set_position(last2)

        if u.get_position() < last:
            self.raid_level = RaidLevel(u.unpack_int())
        if u.get_position() < last:
            self.phyiscal_drive_type = PhysicalDriveType(u.unpack_int())
Example #9
0
    def unpack(self, u: xdrlib.Unpacker):
        self.needs_attention = u.unpack_bool()
        self.fixing = u.unpack_bool()
        self.wwn = u.unpack_opaque()
        self.management_class_name = u.unpack_string()
        self.storage_array_label = u.unpack_string().decode('utf-16be')
        self.boot_time = struct.unpack('>Q', u.unpack_fopaque(8))[0]
        self.fw_version = u.unpack_fopaque(4)
        self.app_version = u.unpack_fopaque(4)
        self.boot_version = u.unpack_fopaque(4)
        self.nvsram_version = u.unpack_string()
        self.fw_prefix = u.unpack_string()
        self.chassis_serial_number = u.unpack_string()
        self.event_configuration_data_version = u.unpack_string()

        self.array_attributes = []
        nb = u.unpack_uint()
        for i in range(nb):
            self.array_attributes.append(u.unpack_uint())
        self.res4 = []
        nb = u.unpack_uint()
        for i in range(nb):
            self.res4.append(u.unpack_uint())
        self.res5 = []
        nb = u.unpack_uint()
        for i in range(nb):
            self.res5.append(u.unpack_uint())
        self.res6 = []
        nb = u.unpack_uint()
        for i in range(nb):
            self.res6.append(u.unpack_uint())
        self.res7 = u.unpack_opaque()

        last = u.unpack_int()
        if last > 0:
            this.reserved1 = u.get_opaque()
            u.set_position(u.get_position() + last)
Example #10
0
                        elif record_ent_form_number == [
                                4413, 1
                        ]:  # Broadcom selected egress queue
                            flow_index["_source"].update(
                                broad_sel_egress_queue(unpacked_record_data))

                        # Documented pmacct bug https://github.com/pmacct/pmacct/issues/71
                        #elif record_ent_form_number == [8800,1]: # Extended Class
                        #flow_index["_source"].update(extended_class(unpacked_record_data))

                        # Documented pmacct bug https://github.com/pmacct/pmacct/issues/71
                        #elif record_ent_form_number == [8800,2]: # Extended Tag
                        #flow_index["_source"].update(extended_tag(unpacked_record_data))

                        else:  # Something we don't know about - SKIP it
                            unpacked_sample_data.set_position(
                                skip_position)  # Skip the unknown type
                            logging.info("Received unknown flow record type " +
                                         str(record_ent_form_number) +
                                         " from " +
                                         str(datagram_info["Agent IP"]) +
                                         " (sub agent " +
                                         str(datagram_info["Sub Agent"]) +
                                         ") - SKIPPING")
                            flow_index = False

                    except Exception as flow_unpack_error:
                        flow_index = False
                        unpacked_sample_data.set_position(
                            skip_position)  # Skip the unknown type
                        logging.warning(str(flow_unpack_error))
                        logging.warning("Failed to unpack Flow record " +