__init__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from copy import deepcopy
  4. import json
  5. import logging
  6. import time
  7. import urllib2
  8. from .alfred import AlfredParser
  9. from .batman import BatmanParser
  10. from .dashing import DashingClient
  11. from .graphite import GraphitePush
  12. from .server import ApiServer
  13. from .storage import Storage
  14. __all__ = [
  15. 'AlfredParser', 'BatmanParser',
  16. 'DashingClient', 'GraphitePush',
  17. 'Storage', 'ApiServer',
  18. 'dict_merge', 'merge_alfred_batman',
  19. 'resolve_ipblock', 'mac2id',
  20. ]
  21. logger = logging.getLogger('ffstatus')
  22. def mac2id(mac):
  23. return mac.lower().replace(':', '')
  24. def dict_merge(a, b):
  25. '''recursively merges dict's. not just simple a['key'] = b['key'], if
  26. both a and bhave a key who's value is a dict then dict_merge is called
  27. on both values and the result stored in the returned dictionary.'''
  28. if not isinstance(b, dict):
  29. return b
  30. result = deepcopy(a)
  31. for k, v in b.iteritems():
  32. if k in result and isinstance(result[k], dict):
  33. result[k] = dict_merge(result[k], v)
  34. elif k in result and isinstance(result[k], list):
  35. result[k] = result[k] + [deepcopy(x) for x in v if x not in result[k]]
  36. else:
  37. result[k] = deepcopy(v)
  38. return result
  39. def merge_alfred_batman(alfreddata, batmandata):
  40. merged = {}
  41. batlookup = {}
  42. for nodeid in batmandata:
  43. batlookup[nodeid] = nodeid
  44. for bda in batmandata[nodeid]['aliases']:
  45. batlookup[bda] = nodeid
  46. for nodeid in alfreddata:
  47. nodeinfo = alfreddata[nodeid]
  48. candidates = set()
  49. candidates.add(nodeid)
  50. if 'mac' in nodeinfo:
  51. candidates.add(mac2id(nodeinfo['mac']))
  52. if 'macs' in nodeinfo:
  53. for mac in nodeinfo['macs']:
  54. candidates.add(mac2id(mac))
  55. if 'network' in nodeinfo:
  56. net = nodeinfo['network']
  57. if 'mac' in net:
  58. candidates.add(mac2id(net['mac']))
  59. if 'mesh_interfaces' in net:
  60. for mac in net['mesh_interfaces']:
  61. candidates.add(mac2id(mac))
  62. if not 'neighbours' in nodeinfo:
  63. nodeinfo['neighbours'] = []
  64. for candidate_raw in candidates:
  65. candidate = batlookup[candidate_raw] if candidate_raw in batlookup else candidate_raw
  66. if candidate in batmandata:
  67. nodeinfo = dict_merge(nodeinfo, batmandata[candidate])
  68. merged[nodeid] = nodeinfo
  69. return merged
  70. no_ipblock_resolves_until = None
  71. def resolve_ipblock(ipaddr):
  72. """Resolve the given IP address to its inetnum entry at RIPE."""
  73. global no_ipblock_resolves_until
  74. if no_ipblock_resolves_until is not None:
  75. if no_ipblock_resolves_until < time.time():
  76. no_ipblock_resolves_until = None
  77. else:
  78. logger.info('IP-Block-Resolving suspended for {0} seconds. Won\'t resolve \'{1}\' now.'.format(int(no_ipblock_resolves_until-time.time()), ipaddr))
  79. return None
  80. url = 'http://rest.db.ripe.net/search.json?query-string=' + str(ipaddr)
  81. try:
  82. response = json.load(urllib2.urlopen(url))
  83. assert isinstance(response, dict)
  84. obj = [x for x in response['objects']['object'] if x['type'] in ['inetnum', 'inet6num']][0]
  85. attrib = obj['attributes']['attribute']
  86. netname = '\n'.join([x['value'] for x in attrib if x['name'] == 'netname'])
  87. netblock = '\n'.join([x['value'] for x in attrib if x['name'] in ['inetnum', 'inet6num']])
  88. desc = '\n'.join([x['value'] for x in attrib if x['name'] == 'descr'])
  89. return {
  90. 'name': netname,
  91. 'block': netblock,
  92. 'description': desc,
  93. }
  94. except urllib2.URLError as err:
  95. output = err.read()
  96. logger.error('Error "{1}" querying ip \'{0}\' from RIPE API: {2}'.format(ipaddr, err, output))
  97. if 'Retry-After' in err.headers:
  98. retry = int(err.headers['Retry-After'])
  99. logger.warn('I won\'t resolve IPs for {0} seconds as requested by RIPE API (header=\'{1}\').'.format(retry, err.header['Retry-After']))
  100. no_ipblock_resolves_until = time.time() + int(err.headers['Retry-After'])
  101. else:
  102. logger.warn('I won\'t resolve IPs for the next hour (API didn\'t give better hint).')
  103. no_ipblock_resolves_until = time.time() + 3600