<s> import xdrlib <EOL> from tensor . protocol . sflow . protocol import flows , counters <EOL> class Sflow ( object ) : <EOL> def __init__ ( self , payload , host ) : <EOL> self . host = host <EOL> u = xdrlib . Unpacker ( payload ) <EOL> self . version = u . unpack_uint ( ) <EOL> self . samplers = { <EOL> <NUM_LIT:1> : FlowSample , <EOL> <NUM_LIT:2> : CounterSample <EOL> } <EOL> if self . version == <NUM_LIT:5> : <EOL> self . sflow_v5 ( u ) <EOL> def sflow_v5 ( self , u ) : <EOL> self . addrtype = u . unpack_uint ( ) <EOL> if self . addrtype == <NUM_LIT:1> : <EOL> self . address = u . unpack_fstring ( <NUM_LIT:4> ) <EOL> if self . addrtype == <NUM_LIT:2> : <EOL> self . address = u . unpack_fstring ( <NUM_LIT:16> ) <EOL> self . sub_agent_id = u . unpack_uint ( ) <EOL> self . sequence_number = u . unpack_uint ( ) <EOL> self . uptime = u . unpack_uint ( ) <EOL> self . sample_count = u . unpack_uint ( ) <EOL> self . decode_samples ( u ) <EOL> self . samples . sort ( key = lambda x : x . sequence ) <EOL> def decode_samples ( self , u ) : <EOL> self . samples = [ ] <EOL> for i in range ( self . sample_count ) : <EOL> sample_type = u . unpack_uint ( ) <EOL> self . samples . append ( self . samplers [ sample_type ] ( u ) ) <EOL> class FlowSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . size = u . unpack_uint ( ) <EOL> self . sequence = u . unpack_uint ( ) <EOL> self . source_id = u . unpack_uint ( ) <EOL> self . sample_rate = u . unpack_uint ( ) <EOL> self . sample_pool = u . unpack_uint ( ) <EOL> self . dropped_packets = u . unpack_uint ( ) <EOL> self . if_inIndex = u . unpack_uint ( ) <EOL> self . if_outIndex = u . unpack_uint ( ) <EOL> self . record_count = u . unpack_uint ( ) <EOL> self . flows = { } <EOL> for i in range ( self . record_count ) : <EOL> flow_format = u . unpack_uint ( ) <EOL> flow_head = u . unpack_opaque ( ) <EOL> flow_u = xdrlib . Unpacker ( flow_head ) <EOL> d = flows . getDecoder ( flow_format ) <EOL> if d : <EOL> self . flows [ flow_format ] = d ( flow_u ) <EOL> class CounterSample ( object ) : <EOL> def __init__ ( self , u ) : <EOL> self . size = u . unpack_uint ( ) <EOL> self . sequence = u . unpack_uint ( ) <EOL> self . source_id = u . unpack_uint ( ) <EOL> self . record_count = u . unpack_uint ( ) <EOL> self . counters = { } <EOL> for i in range ( self . record_count ) : <EOL> counter_format = u . unpack_uint ( ) <EOL> counter = u . unpack_opaque ( ) <EOL> d = counters . getDecoder ( counter_format ) <EOL> if d : <EOL> self . counters [ counter_format ] = d ( xdrlib . Unpacker ( counter ) ) <EOL> else : <EOL> print "<STR_LIT>" , counter_format </s>
