nodeinfo.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.batctlMeshif([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. if 'model name' not in ret:
  56. ret["model name"] = ret["Processor"]
  57. return ret
  58. @staticmethod
  59. def getVPNFlag(batmanInterface):
  60. lines = lib.helper.batctlMeshif([batmanInterface, 'gw_mode'])
  61. if re.match(r'^server', lines[0]):
  62. return True
  63. else:
  64. return False
  65. def _get(self):
  66. ret = {
  67. 'hostname': socket.gethostname(),
  68. 'network': {
  69. 'addresses': self.getInterfaceAddresses(self._config['bridge']),
  70. 'mesh': {
  71. 'bat0': {
  72. 'interfaces': self.getBatmanInterfaces(self._config['batman'])
  73. }
  74. },
  75. 'mac': lib.helper.getInterfaceMAC(self._config['batman'])
  76. },
  77. 'software': {
  78. 'firmware': {
  79. 'base': lib.helper.call(['lsb_release', '-ds'])[0],
  80. },
  81. 'batman-adv': {
  82. 'version': open('/sys/module/batman_adv/version').read().strip(),
  83. # 'compat': # /lib/gluon/mesh-batman-adv-core/compat
  84. },
  85. 'status-page': {
  86. 'api': 0
  87. },
  88. 'autoupdater': {
  89. 'enabled': False
  90. }
  91. },
  92. 'hardware': {
  93. 'model': self.getCPUInfo()['model name'],
  94. 'nproc': int(lib.helper.call(['nproc'])[0])
  95. },
  96. 'owner': {},
  97. 'system': {},
  98. 'location': {},
  99. 'vpn': self.getVPNFlag(self._config['batman'])
  100. }
  101. if 'mesh-vpn' in self._config and len(self._config['mesh-vpn']) > 0:
  102. try:
  103. ret['software']['fastd'] = {
  104. 'version': lib.helper.call(['fastd', '-v'])[0].split(' ')[1],
  105. 'enabled': True
  106. }
  107. except:
  108. pass
  109. if 'nodeinfo' in self._config:
  110. return lib.helper.merge(ret, self._config['nodeinfo'])
  111. else:
  112. return ret