nodeinfo.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. import socket
  3. import re
  4. import netifaces as netif
  5. from lib.respondd import Respondd
  6. import lib.helper
  7. class Nodeinfo(Respondd):
  8. def __init__(self, config):
  9. Respondd.__init__(self, config)
  10. @staticmethod
  11. def getInterfaceAddresses(interface):
  12. addresses = []
  13. try:
  14. for ip6 in netif.ifaddresses(interface)[netif.AF_INET6]:
  15. addresses.append(ip6['addr'].split('%')[0])
  16. for ip in netif.ifaddresses(interface)[netif.AF_INET]:
  17. addresses.append(ip['addr'].split('%')[0])
  18. except:
  19. pass
  20. return addresses
  21. def getBatmanInterfaces(self, batmanInterface):
  22. ret = {}
  23. lines = lib.helper.call(['batctl', '-m', batmanInterface, 'if'])
  24. for line in lines:
  25. lineMatch = re.match(r'^([^:]*)', line)
  26. interface = lineMatch.group(0)
  27. interfaceType = ''
  28. if 'fastd' in self._config and interface == self._config['fastd']: # keep for compatibility
  29. interfaceType = 'tunnel'
  30. elif interface.find('l2tp') != -1:
  31. interfaceType = 'l2tp'
  32. elif 'mesh-vpn' in self._config and interface in self._config['mesh-vpn']:
  33. interfaceType = 'tunnel'
  34. elif 'mesh-wlan' in self._config and interface in self._config['mesh-wlan']:
  35. interfaceType = 'wireless'
  36. else:
  37. interfaceType = 'other'
  38. if interfaceType not in ret:
  39. ret[interfaceType] = []
  40. ret[interfaceType].append(lib.helper.getInterfaceMAC(interface))
  41. if 'l2tp' in ret:
  42. if 'tunnel' in ret:
  43. ret['tunnel'] += ret['l2tp']
  44. else:
  45. ret['tunnel'] = ret['l2tp']
  46. return ret
  47. @staticmethod
  48. def getCPUInfo():
  49. ret = {}
  50. with open('/proc/cpuinfo', 'r') as fh:
  51. for line in fh:
  52. lineMatch = re.match(r'^(.+?)[\t ]+:[\t ]+(.*)$', line, re.I)
  53. if lineMatch:
  54. ret[lineMatch.group(1)] = lineMatch.group(2)
  55. return ret
  56. @staticmethod
  57. def getVPN(batmanInterface):
  58. lines = lib.helper.call(['batctl', '-m', batmanInterface, 'gw_mode'])
  59. if re.match(r'^server', lines[0]):
  60. return True
  61. else:
  62. return False
  63. def _get(self):
  64. ret = {
  65. 'hostname': socket.gethostname(),
  66. 'network': {
  67. 'addresses': self.getInterfaceAddresses(self._config['bridge']),
  68. 'mesh': {
  69. 'bat0': {
  70. 'interfaces': self.getBatmanInterfaces(self._config['batman'])
  71. }
  72. },
  73. 'mac': lib.helper.getInterfaceMAC(self._config['batman'])
  74. },
  75. 'software': {
  76. 'firmware': {
  77. 'base': lib.helper.call(['lsb_release', '-is'])[0],
  78. 'release': lib.helper.call(['lsb_release', '-ds'])[0]
  79. },
  80. 'batman-adv': {
  81. 'version': open('/sys/module/batman_adv/version').read().strip(),
  82. # 'compat': # /lib/gluon/mesh-batman-adv-core/compat
  83. },
  84. 'status-page': {
  85. 'api': 0
  86. },
  87. 'autoupdater': {
  88. 'enabled': False
  89. }
  90. },
  91. 'hardware': {
  92. 'model': self.getCPUInfo()['model name'],
  93. 'nproc': int(lib.helper.call(['nproc'])[0])
  94. },
  95. 'owner': {},
  96. 'system': {},
  97. 'location': {},
  98. 'vpn': self.getVPN(self._config['batman'])
  99. }
  100. if 'mesh-vpn' in self._config and len(self._config['mesh-vpn']) > 0:
  101. try:
  102. ret['software']['fastd'] = {
  103. 'version': lib.helper.call(['fastd', '-v'])[0].split(' ')[1],
  104. 'enabled': True
  105. }
  106. except:
  107. pass
  108. return lib.helper.merge(ret, self._aliasOverlay['nodeinfo'])