Created
November 18, 2011 22:37
-
-
Save vvuksan/1377993 to your computer and use it in GitHub Desktop.
Ganglia XDR packet parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from socket import * | |
from xdrlib import Packer, Unpacker | |
# Set the socket parameters | |
host = "localhost" | |
port = 1234 | |
buf = 1024 | |
addr = (host,port) | |
slope_str2int = {'zero':0, | |
'positive':1, | |
'negative':2, | |
'both':3, | |
'unspecified':4} | |
# could be autogenerated from previous but whatever | |
slope_int2str = {0: 'zero', | |
1: 'positive', | |
2: 'negative', | |
3: 'both', | |
4: 'unspecified'} | |
def gmetric_read(msg): | |
unpacker = Unpacker(msg) | |
values = dict() | |
type = unpacker.unpack_int() | |
if type == 128: | |
print "Processing Meta Data packet" | |
values['HOSTNAME'] = unpacker.unpack_string() | |
values['NAME'] = unpacker.unpack_string() | |
values['SPOOF'] = unpacker.unpack_uint() | |
values['TYPE'] = unpacker.unpack_string() | |
values['NAME'] = unpacker.unpack_string() | |
values['UNITS'] = unpacker.unpack_string() | |
values['SLOPE'] = slope_int2str[unpacker.unpack_int()] | |
values['TMAX'] = unpacker.unpack_uint() | |
values['DMAX'] = unpacker.unpack_uint() | |
values['GROUP_INCLUDED'] = unpacker.unpack_uint() | |
if type == 133: | |
print "Processing Metric packet" | |
values['HOSTNAME'] = unpacker.unpack_string() | |
values['NAME'] = unpacker.unpack_string() | |
values['SPOOF'] = unpacker.unpack_uint() | |
values['PRINTF'] = unpacker.unpack_string() | |
values['VAL'] = unpacker.unpack_string() | |
print values | |
unpacker.done() | |
return values | |
# Create socket and bind to address | |
UDPSock = socket(AF_INET,SOCK_DGRAM) | |
UDPSock.bind(addr) | |
# Receive messages | |
while 1: | |
data,addr = UDPSock.recvfrom(buf) | |
if not data: | |
break | |
else: | |
gmetric_read(data) | |
# Close socket | |
UDPSock.close() |
Yeah also need to figure out how to parse extra data. Unpacker doesn't like extra unpacked data-- Sent from my phoneOn Nov 19, 2011 1:57, Patrick Debois [email protected] wrote: great work; be aware that values['VAL'] = unpacker.unpack_string() will need to change depending on the type of the submitted packet.
## This could become unpack_float, unpack_int, etc ...
Reply to this email directly or view it on GitHub:
https://gist.github.com/1377993
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great work; be aware that values['VAL'] = unpacker.unpack_string() will need to change depending on the type of the submitted packet.
This could become unpack_float, unpack_int, etc ...