nodeinfo.py 3.1 KB

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