__init__.py 4.7 KB

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