ext-respondd.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #!/usr/bin/env python3
  2. # Code-Base: https://github.com/ffggrz/ffnord-alfred-announce
  3. # + https://github.com/freifunk-mwu/ffnord-alfred-announce
  4. # + https://github.com/FreifunkBremen/respondd
  5. import sys
  6. import socket
  7. import select
  8. import struct
  9. import subprocess
  10. import argparse
  11. import re
  12. # Force encoding to UTF-8
  13. import locale # Ensures that subsequent open()s
  14. locale.getpreferredencoding = lambda _=None: 'UTF-8' # are UTF-8 encoded.
  15. import json
  16. import zlib
  17. import netifaces as netif
  18. def toUTF8(line):
  19. return line.decode("utf-8")
  20. def call(cmdnargs):
  21. output = subprocess.check_output(cmdnargs)
  22. lines = output.splitlines()
  23. lines = [toUTF8(line) for line in lines]
  24. return lines
  25. def merge(a, b):
  26. if isinstance(a, dict) and isinstance(b, dict):
  27. d = dict(a)
  28. d.update({k: merge(a.get(k, None), b[k]) for k in b})
  29. return d
  30. if isinstance(a, list) and isinstance(b, list):
  31. return [merge(x, y) for x, y in itertools.izip_longest(a, b)]
  32. return a if b is None else b
  33. def getGateway():
  34. #/sys/kernel/debug/batman_adv/bat0/gateways
  35. output = subprocess.check_output(["batctl","-m",config['batman'],"gwl","-n"])
  36. output_utf8 = output.decode("utf-8")
  37. lines = output_utf8.splitlines()
  38. gw = None
  39. for line in lines:
  40. gw_line = re.match(r"^=> +([0-9a-f:]+) ", line)
  41. if gw_line:
  42. gw = gw_line.group(1)
  43. return gw
  44. def getClients():
  45. #/sys/kernel/debug/batman_adv/bat0/transtable_local
  46. output = subprocess.check_output(["batctl","-m",config['batman'],"tl","-n"])
  47. output_utf8 = output.decode("utf-8")
  48. lines = output_utf8.splitlines()
  49. batadv_mac = getDevice_MAC(config['batman'])
  50. j = {"total": 0, "wifi": 0}
  51. for line in lines:
  52. # batman-adv -> translation-table.c -> batadv_tt_local_seq_print_text
  53. # R = BATADV_TT_CLIENT_ROAM
  54. # P = BATADV_TT_CLIENT_NOPURGE
  55. # N = BATADV_TT_CLIENT_NEW
  56. # X = BATADV_TT_CLIENT_PENDING
  57. # W = BATADV_TT_CLIENT_WIFI
  58. # I = BATADV_TT_CLIENT_ISOLA
  59. # . = unset
  60. # * c0:11:73:b2:8f:dd -1 [.P..W.] 1.710 (0xe680a836)
  61. ml = re.match(r"^\s\*\s([0-9a-f:]+)\s+-\d\s\[([RPNXWI\.]+)\]", line, re.I)
  62. if ml:
  63. if not batadv_mac == ml.group(1): # Filter bat0
  64. if not ml.group(1).startswith('33:33:') and not ml.group(1).startswith('01:00:5e:'): # Filter Multicast
  65. j["total"] += 1
  66. if ml.group(2)[4] == 'W':
  67. j["wifi"] += 1
  68. return j
  69. def getDevice_Addresses(dev):
  70. l = []
  71. try:
  72. for ip6 in netif.ifaddresses(dev)[netif.AF_INET6]:
  73. raw6 = ip6['addr'].split('%')
  74. l.append(raw6[0])
  75. except:
  76. pass
  77. return l
  78. def getDevice_MAC(dev):
  79. try:
  80. interface = netif.ifaddresses(dev)
  81. mac = interface[netif.AF_LINK]
  82. return mac[0]['addr']
  83. except:
  84. return None
  85. def getMesh_Interfaces():
  86. j = {}
  87. output = subprocess.check_output(["batctl","-m",config['batman'],"if"])
  88. output_utf8 = output.decode("utf-8")
  89. lines = output_utf8.splitlines()
  90. for line in lines:
  91. dev_re = re.match(r"^([^:]*)", line)
  92. dev = dev_re.group(1)
  93. j[dev] = getDevice_MAC(dev)
  94. return j
  95. def getBat0_Interfaces():
  96. j = {}
  97. output = subprocess.check_output(["batctl","-m",config['batman'],"if"])
  98. output_utf8 = output.decode("utf-8")
  99. lines = output_utf8.splitlines()
  100. for line in lines:
  101. dev_line = re.match(r"^([^:]*)", line)
  102. nif = dev_line.group(0)
  103. if_group = ""
  104. if "fastd" in config and nif == config["fastd"]: # keep for compatibility
  105. if_group = "tunnel"
  106. elif nif.find("l2tp") != -1:
  107. if_group = "l2tp"
  108. elif ("mesh-vpn" in config and nif in config["mesh-vpn"]):
  109. if_group = "tunnel"
  110. elif "mesh-wlan" in config and nif in config["mesh-wlan"]:
  111. if_group = "wireless"
  112. else:
  113. if_group = "other"
  114. if not if_group in j:
  115. j[if_group] = []
  116. j[if_group].append(getDevice_MAC(nif))
  117. return j
  118. def getTraffic(): # BUG: falsches interfaces?
  119. return (lambda fields:
  120. dict(
  121. (key, dict(
  122. (type_, int(value_))
  123. for key_, type_, value_ in fields
  124. if key_ == key))
  125. for key in ['rx', 'tx', 'forward', 'mgmt_rx', 'mgmt_tx']
  126. )
  127. )(list(
  128. (
  129. key.replace('_bytes', '').replace('_dropped', ''),
  130. 'bytes' if key.endswith('_bytes') else 'dropped' if key.endswith('_dropped') else 'packets',
  131. value
  132. )
  133. for key, value in map(lambda s: list(map(str.strip, s.split(': ', 1))), call(['ethtool', '-S', config['batman']])[1:])
  134. ))
  135. def getMemory():
  136. return dict(
  137. (key.replace('Mem', '').lower(), int(value.split(' ')[0]))
  138. for key, value in map(lambda s: map(str.strip, s.split(': ', 1)), open('/proc/meminfo').readlines())
  139. if key in ('MemTotal', 'MemFree', 'Buffers', 'Cached')
  140. )
  141. def getFastd():
  142. fastd_data = b""
  143. try:
  144. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  145. sock.connect(config["fastd_socket"])
  146. except socket.error as err:
  147. print("socket error: ", sys.stderr, err)
  148. return None
  149. while True:
  150. data = sock.recv(1024)
  151. if not data: break
  152. fastd_data+= data
  153. sock.close()
  154. return json.loads(fastd_data.decode("utf-8"))
  155. def getMeshVPNPeers():
  156. j = {}
  157. if "fastd_socket" in config:
  158. fastd = getFastd()
  159. for peer, v in fastd["peers"].items():
  160. if v["connection"]:
  161. j[v["name"]] = {
  162. "established": v["connection"]["established"],
  163. }
  164. else:
  165. j[v["name"]] = None
  166. return j
  167. else:
  168. return None
  169. def getNode_ID():
  170. if 'node_id' in aliases["nodeinfo"]:
  171. return aliases["nodeinfo"]["node_id"]
  172. else:
  173. return getDevice_MAC(config["batman"]).replace(':','')
  174. def getStationDump(dev_list):
  175. j = {}
  176. for dev in dev_list:
  177. try:
  178. # iw dev ibss3 station dump
  179. output = subprocess.check_output(["iw","dev",dev,"station", "dump"], stderr=STDOUT)
  180. output_utf8 = output.decode("utf-8")
  181. lines = output_utf8.splitlines()
  182. mac=""
  183. for line in lines:
  184. # Station 32:b8:c3:86:3e:e8 (on ibss3)
  185. ml = re.match('^Station ([0-9a-f:]+) \(on ([\w\d]+)\)', line, re.I)
  186. if ml:
  187. mac = ml.group(1)
  188. j[mac] = {}
  189. else:
  190. ml = re.match('^[\t ]+([^:]+):[\t ]+([^ ]+)', line, re.I)
  191. if ml:
  192. j[mac][ml.group(1)] = ml.group(2)
  193. except:
  194. pass
  195. return j
  196. def getNeighbours():
  197. # https://github.com/freifunk-gluon/packages/blob/master/net/respondd/src/respondd.c
  198. j = { "batadv": {}}
  199. stationDump = None
  200. if 'mesh-wlan' in config:
  201. j["wifi"] = {}
  202. stationDump = getStationDump(config["mesh-wlan"])
  203. mesh_ifs = getMesh_Interfaces()
  204. output = subprocess.check_output(["batctl","-m",config['batman'],"o","-n"])
  205. output_utf8 = output.decode("utf-8")
  206. lines = output_utf8.splitlines()
  207. for line in lines:
  208. # * e2:ad:db:b7:66:63 2.712s (175) be:b7:25:4f:8f:96 [mesh-vpn-l2tp-1]
  209. ml = re.match(r"^[ \*\t]*([0-9a-f:]+)[ ]*([\d\.]*)s[ ]*\(([ ]*\d*)\)[ ]*([0-9a-f:]+)[ ]*\[[ ]*(.*)\]", line, re.I)
  210. if ml:
  211. dev = ml.group(5)
  212. mac_origin = ml.group(1)
  213. mac_nhop = ml.group(4)
  214. tq = ml.group(3)
  215. lastseen = ml.group(2)
  216. if mac_origin == mac_nhop:
  217. if 'mesh-wlan' in config and dev in config["mesh-wlan"] and not stationDump is None:
  218. if not mesh_ifs[dev] in j["wifi"]:
  219. j["wifi"][mesh_ifs[dev]] = {}
  220. j["wifi"][mesh_ifs[dev]]["neighbours"] = {}
  221. if mac_origin in stationDump:
  222. j["wifi"][mesh_ifs[dev]]["neighbours"][mac_origin] = {
  223. "signal": stationDump[mac_origin]["signal"],
  224. "noise": 0, # BUG: fehlt noch
  225. "inactive": stationDump[mac_origin]["inactive time"],
  226. }
  227. if dev in mesh_ifs:
  228. if not mesh_ifs[dev] in j["batadv"]:
  229. j["batadv"][mesh_ifs[dev]] = {}
  230. j["batadv"][mesh_ifs[dev]]["neighbours"] = {}
  231. j["batadv"][mesh_ifs[dev]]["neighbours"][mac_origin] = {
  232. "tq": int(tq),
  233. "lastseen": float(lastseen),
  234. }
  235. return j
  236. def getCPUInfo():
  237. j = {}
  238. with open("/proc/cpuinfo", 'r') as fh:
  239. for line in fh:
  240. ml = re.match(r"^(.+?)[\t ]+:[\t ]+(.*)$", line, re.I)
  241. if ml:
  242. j[ml.group(1)] = ml.group(2)
  243. return j
  244. # ======================== Output =========================
  245. # =========================================================
  246. def createNodeinfo():
  247. j = {
  248. "node_id": getNode_ID(),
  249. "hostname": socket.gethostname(),
  250. "network": {
  251. "addresses": getDevice_Addresses(config['bridge']),
  252. "mesh": {
  253. "bat0": {
  254. "interfaces": getBat0_Interfaces(),
  255. },
  256. },
  257. "mac": getDevice_MAC(config["batman"]),
  258. "mesh_interfaces": list(getMesh_Interfaces().values()),
  259. },
  260. "software": {
  261. "firmware": {
  262. "base": call(['lsb_release','-is'])[0],
  263. "release": call(['lsb_release','-ds'])[0],
  264. },
  265. "batman-adv": {
  266. "version": open('/sys/module/batman_adv/version').read().strip(),
  267. # "compat": # /lib/gluon/mesh-batman-adv-core/compat
  268. },
  269. "status-page": {
  270. "api": 0,
  271. },
  272. "autoupdater": {
  273. # "branch": "stable",
  274. "enabled": False,
  275. },
  276. },
  277. "hardware": {
  278. "model": getCPUInfo()["model name"],
  279. "nproc": int(call(['nproc'])[0]),
  280. },
  281. # "vpn": True,
  282. "owner": {},
  283. "system": {},
  284. "location": {},
  285. }
  286. try:
  287. j["software"]["fastd"] = {
  288. "version": call(['fastd','-v'])[0].split(' ')[1],
  289. "enabled": True,
  290. };
  291. except:
  292. pass
  293. return merge(j, aliases["nodeinfo"])
  294. def createStatistics():
  295. j = {
  296. "node_id": getNode_ID(),
  297. "clients": getClients(),
  298. "traffic": getTraffic(),
  299. "idletime": float(open('/proc/uptime').read().split(' ')[1]),
  300. "loadavg": float(open('/proc/loadavg').read().split(' ')[0]),
  301. "memory": getMemory(),
  302. "processes": dict(zip(('running', 'total'), map(int, open('/proc/loadavg').read().split(' ')[3].split('/')))),
  303. "uptime": float(open('/proc/uptime').read().split(' ')[0]),
  304. "mesh_vpn" : { # HopGlass-Server: node.flags.uplink = parsePeerGroup(_.get(n, 'statistics.mesh_vpn'))
  305. "groups": {
  306. "backbone": {
  307. "peers": getMeshVPNPeers(),
  308. },
  309. },
  310. },
  311. }
  312. gateway = getGateway()
  313. if gateway != None:
  314. j["gateway"] = gateway
  315. return j
  316. def createNeighbours():
  317. #/sys/kernel/debug/batman_adv/bat0/originators
  318. j = {
  319. "node_id": getNode_ID(),
  320. }
  321. j = merge(j, getNeighbours())
  322. return j
  323. def sendResponse(request, compress):
  324. json_data = {}
  325. #https://github.com/freifunk-gluon/packages/blob/master/net/respondd/src/respondd.c
  326. if request == 'statistics':
  327. json_data[request] = createStatistics()
  328. elif request == 'nodeinfo':
  329. json_data[request] = createNodeinfo()
  330. elif request == 'neighbours':
  331. json_data[request] = createNeighbours()
  332. else:
  333. print("unknown command: " + request)
  334. return
  335. json_str = bytes(json.dumps(json_data, separators=(',', ':')), 'UTF-8')
  336. if compress:
  337. encoder = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15) # The data may be decompressed using zlib and many zlib bindings using -15 as the window size parameter.
  338. gzip_data = encoder.compress(json_str)
  339. gzip_data = gzip_data + encoder.flush()
  340. sock.sendto(gzip_data, sender)
  341. else:
  342. sock.sendto(json_str, sender)
  343. if options["verbose"]:
  344. print(json.dumps(json_data, sort_keys=True, indent=4))
  345. # ===================== Mainfunction ======================
  346. # =========================================================
  347. parser = argparse.ArgumentParser()
  348. parser.add_argument( '-d', '--debug', action='store_true', help='Debug Output',required=False,)
  349. parser.add_argument( '-v', '--verbose', action='store_true', help='Verbose Output',required=False)
  350. args = parser.parse_args()
  351. options = vars(args)
  352. config = {}
  353. try:
  354. with open("config.json", 'r') as cfg_handle:
  355. config = json.load(cfg_handle)
  356. except IOError:
  357. raise
  358. aliases = {}
  359. try:
  360. with open("alias.json", 'r') as cfg_handle:
  361. aliases = json.load(cfg_handle)
  362. except IOError:
  363. raise
  364. if options["debug"]:
  365. print(json.dumps(createNodeinfo(), sort_keys=True, indent=4))
  366. print(json.dumps(createStatistics(), sort_keys=True, indent=4))
  367. print(json.dumps(createNeighbours(), sort_keys=True, indent=4))
  368. #print(json.dumps(getFastd(config["fastd_socket"]), sort_keys=True, indent=4))
  369. #print(json.dumps(getMesh_VPN(), sort_keys=True, indent=4))
  370. sys.exit(1)
  371. if 'addr' in config:
  372. addr = config['addr']
  373. else:
  374. addr = 'ff02::2'
  375. if 'addr' in config:
  376. port = config['port']
  377. else:
  378. port = 1001
  379. if_idx = socket.if_nametoindex(config["bridge"])
  380. group = socket.inet_pton(socket.AF_INET6, addr) + struct.pack("I", if_idx)
  381. sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
  382. sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, group)
  383. sock.bind(('::', port))
  384. # =========================================================
  385. while True:
  386. if select.select([sock],[],[],1)[0]:
  387. msg, sender = sock.recvfrom(2048)
  388. if options["verbose"]:
  389. print(msg)
  390. msg_spl = str(msg, 'UTF-8').split(" ")
  391. if msg_spl[0] == 'GET': # multi_request
  392. for request in msg_spl[1:]:
  393. sendResponse(request, True)
  394. else: # single_request
  395. sendResponse(msg_spl[0], False)