__init__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 .filestorage import FileStorage
  14. __all__ = [
  15. 'AlfredParser', 'BatmanParser',
  16. 'DashingClient', 'GraphitePush',
  17. 'FileStorage', '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 %d seconds. ' +
  79. 'Won\'t resolve \'%s\' now.',
  80. int(no_ipblock_resolves_until-time.time()), ipaddr)
  81. return None
  82. url = 'http://rest.db.ripe.net/search.json?query-string=' + str(ipaddr)
  83. try:
  84. response = json.load(urllib2.urlopen(url))
  85. assert isinstance(response, dict)
  86. obj = [x for x in response['objects']['object'] if x['type'] in ['inetnum', 'inet6num']][0]
  87. attrib = obj['attributes']['attribute']
  88. netname = '\n'.join([x['value'] for x in attrib if x['name'] == 'netname'])
  89. netblock = '\n'.join([x['value'] for x in attrib if x['name'] in ['inetnum', 'inet6num']])
  90. desc = '\n'.join([x['value'] for x in attrib if x['name'] == 'descr'])
  91. return {
  92. 'name': netname,
  93. 'block': netblock,
  94. 'description': desc,
  95. }
  96. except urllib2.URLError as err:
  97. output = err.read()
  98. logger.error('Error "%s" querying ip \'%s\' from RIPE API: %s',
  99. err, ipaddr, output)
  100. if 'Retry-After' in err.headers:
  101. retry = int(err.headers['Retry-After'])
  102. logger.warn(
  103. 'I won\'t resolve IPs for %d seconds as requested by RIPE API' +
  104. ' (header=\'%s\').',
  105. retry, err.header['Retry-After'])
  106. no_ipblock_resolves_until = \
  107. time.time() + int(err.headers['Retry-After'])
  108. else:
  109. logger.warn('I won\'t resolve IPs for the next hour ' +
  110. '(API didn\'t give better hint).')
  111. no_ipblock_resolves_until = time.time() + 3600