Skip to content

Instantly share code, notes, and snippets.

@vvuksan
Created November 18, 2011 22:37
Show Gist options
  • Save vvuksan/1377993 to your computer and use it in GitHub Desktop.
Save vvuksan/1377993 to your computer and use it in GitHub Desktop.
Ganglia XDR packet parser
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()
@jedi4ever
Copy link

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 ...

@vvuksan
Copy link
Author

vvuksan commented Nov 19, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment