- Timestamp:
- May 8, 2012, 9:37:52 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/gformat.py
r10715 r10729 31 31 import time 32 32 import rdnap 33 import math 33 34 import make_network_kml 34 35 from pprint import pprint … … 140 141 141 142 142 143 def make_relations(): 143 def network(ip): 144 addr, mask = ip.split('/') 145 # Not parsing of these folks please 146 addr = parseaddr(addr) 147 mask = int(mask) 148 network = addr & ~((1 << (32 - mask)) - 1) 149 return network 150 151 152 153 def make_relations(datadumps=None): 144 154 """ Process _ALL_ yaml files to get connection relations """ 145 errors = ""155 errors = [] 146 156 poel = defaultdict(list) 147 for host in get_hostlist(): 157 158 if not datadumps: 159 for host in get_hostlist(): 160 datadumps[host] = get_yaml(host) 161 162 for host, datadump in datadumps.iteritems(): 148 163 try: 149 datadump = get_yaml(host)150 164 for iface_key in datadump['autogen_iface_keys']: 151 l = datadump[iface_key]['ip'] 152 addr, mask = l.split('/') 153 154 # Not parsing of these folks please 155 if not valid_addr(addr): 156 continue 157 158 addr = parseaddr(addr) 159 mask = int(mask) 160 network = addr & ~((1 << (32 - mask)) - 1) 161 poel[network] += [(host,datadump[iface_key])] 165 net_addr = network(datadump[iface_key]['ip']) 166 poel[net_addr] += [(host,datadump[iface_key])] 162 167 except (KeyError, ValueError), e: 163 errors += "[FOUT] in '%s' interface '%s'" % (host,iface_key) 164 errors += e 168 errors.append("[FOUT] in '%s' interface '%s' (%s)" % (host,iface_key, e)) 165 169 continue 166 170 return (poel, errors) … … 189 193 """ 190 194 dy = lat2 - lat1 191 dx = math.cos( math.pi/180*lat1)*(long2 - long1)195 dx = math.cos(lat1)*(long2 - long1) 192 196 angle = math.atan2(dy,dx) 193 197 return angle 194 198 199 200 195 201 def angle_to_cd(angle): 196 202 """ Return Dutch Cardinal Direction estimation in 'one digit' of radian angle """ … … 198 204 # For easy conversion get positive degree 199 205 degrees = math.degrees(angle) 200 if degrees < 0: 201 360 - abs(degrees) 206 abs_degrees = 360 + degrees if degrees < 0 else degrees 202 207 203 208 # Numbers can be confusing calculate from the 4 main directions 204 209 p = 22.5 205 if degrees < p:206 return"n"207 elif degrees < (90 - p):208 return"no"209 elif degrees < (90 + p):210 return"o"211 elif degrees < (180 - p):212 return"zo"213 elif degrees < (180 + p):214 return"z"215 elif degrees < (270 - p):216 return"zw"217 elif degrees < (270 + p):218 return"w"219 elif degrees < (360 - p):220 return"nw"210 if abs_degrees < p: 211 cd = "n" 212 elif abs_degrees < (90 - p): 213 cd = "no" 214 elif abs_degrees < (90 + p): 215 cd = "o" 216 elif abs_degrees < (180 - p): 217 cd = "zo" 218 elif abs_degrees < (180 + p): 219 cd = "z" 220 elif abs_degrees < (270 - p): 221 cd = "zw" 222 elif abs_degrees < (270 + p): 223 cd = "w" 224 elif abs_degrees < (360 - p): 225 cd = "nw" 221 226 else: 222 return "n" 227 cd = "n" 228 return cd 229 230 231 232 def cd_between_hosts(hostA, hostB, datadumps): 233 # Using RDNAP coordinates 234 dx = float(int(datadumps[hostA]['rdnap_x']) - int(datadumps[hostB]['rdnap_x'])) * -1 235 dy = float(int(datadumps[hostA]['rdnap_y']) - int(datadumps[hostB]['rdnap_y'])) * -1 236 return angle_to_cd(math.atan2(dx,dy)) 237 238 # GPS coordinates seems to fail somehow 239 #latA = float(datadumps[hostA]['latitude']) 240 #latB = float(datadumps[hostB]['latitude']) 241 #lonA = float(datadumps[hostA]['longitude']) 242 #lonB = float(datadumps[hostB]['longitude']) 243 #return angle_to_cd(angle_between_points(latA, latB, lonA, lonB)) 223 244 224 245 … … 333 354 def is_member(ip, mask, canidate): 334 355 """ Return True if canidate is part of ip/mask block""" 335 ip_addr = gformat.parseaddr(ip)336 ip_canidate = gformat.parseaddr(canidate)356 ip_addr = parseaddr(ip) 357 ip_canidate = parseaddr(canidate) 337 358 mask = int(mask) 338 359 ip_addr = ip_addr & ~((1 << (32 - mask)) - 1) … … 1320 1341 # First generate all datadumps 1321 1342 datadumps = dict() 1343 ssid_to_node = dict() 1322 1344 for host in get_hostlist(): 1323 1345 logger.info("# Processing: %s", host) … … 1327 1349 datadumps[datadump['autogen_realname']] = datadump 1328 1350 1351 (poel, errors) = make_relations(datadumps) 1352 print "\n".join(["# WARNING: %s" % x for x in errors]) 1329 1353 1330 1354 for host,datadump in datadumps.iteritems(): … … 1364 1388 if not datadump[iface_key].has_key('desc'): 1365 1389 datadump[iface_key]['desc'] = 'autogen-FIXME' 1390 # Set the compass value based on the angle between the poels 1391 if datadump[iface_key].has_key('ns_ip'): 1392 my_pool = poel[network(datadump[iface_key]['ip'])] 1393 remote_hosts = list(set([x[0] for x in my_pool]) - set([host])) 1394 if remote_hosts: 1395 compass_target = remote_hosts[0] 1396 datadump[iface_key]['compass'] = cd_between_hosts(host, compass_target, datadumps) 1397 1366 1398 store_yaml(datadump) 1367 1399 elif sys.argv[1] == "list":
Note:
See TracChangeset
for help on using the changeset viewer.