-
-
Save atdt/6441130 to your computer and use it in GitHub Desktop.
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment