Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@advancedxy
Created July 9, 2014 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save advancedxy/bdfcf1c23ff70c463062 to your computer and use it in GitHub Desktop.
Save advancedxy/bdfcf1c23ff70c463062 to your computer and use it in GitHub Desktop.
hadoop rack awareness script
#!/usr/bin/env python
import sys
import socket
data = """
100.200.208.12-100.200.208.46 : /xxx/rack_1
"""
def ip_integer_from_string(s):
return reduce(lambda a,b: a<<8 | b, map(int, s.split(".")))
def ip_integer_to_string(ip):
return ".".join(map(lambda n: str(ip>>n & 0xFF), [24,16,8,0]))
def hostname_to_ip(ip):
socket.setdefaulttimeout(1)
try:
return socket.gethostbyname(ip)
except Exception,e:
return "255.255.255.255"
if __name__ == "__main__":
rack_info = {}
for line in filter(lambda x : ':' in x ,data.split('\n')):
k,v = map(lambda x: x.strip(), line.split(":"))
if '-' in k:
start,end = map(lambda x: ip_integer_from_string(x.strip()), k.split('-'))
if start < end :
for ip in range(start, end+1):
rack_info[ip_integer_to_string(ip)] = v
else:
rack_info[k.strip()] = v
sys.stdout.write(' '.join(map(lambda x: rack_info.get(hostname_to_ip(x), "/default/default_rack"), sys.argv[1:])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment