ffpb.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import willie
  4. import datetime
  5. import difflib
  6. from email.utils import mktime_tz
  7. import git
  8. import netaddr
  9. import json
  10. import urllib2
  11. import re
  12. import os
  13. import random
  14. import shelve
  15. import subprocess
  16. import time
  17. import dns.resolver,dns.reversename
  18. import socket
  19. import SocketServer
  20. import threading
  21. msgserver = None
  22. peers_repo = None
  23. monitored_nodes = None
  24. highscores = None
  25. alfred_method = None
  26. alfred_data = None
  27. alfred_update = datetime.datetime(1970,1,1,23,42)
  28. ffpb_resolver = dns.resolver.Resolver ()
  29. ffpb_resolver.nameservers = ['10.132.254.53']
  30. class MsgHandler(SocketServer.BaseRequestHandler):
  31. def handle(self):
  32. data = self.request.recv(2048).strip()
  33. sender = self._resolve_name (self.client_address[0])
  34. bot = self.server.bot
  35. if bot is None:
  36. print("ERROR: No bot in handle() :-(")
  37. return
  38. target = bot.config.core.owner
  39. if bot.config.has_section('ffpb'):
  40. is_public = data.lstrip().lower().startswith("public:")
  41. if is_public and not (bot.config.ffpb.msg_target_public is None):
  42. data = data[7:].lstrip()
  43. target = bot.config.ffpb.msg_target_public
  44. elif not (bot.config.ffpb.msg_target is None):
  45. target = bot.config.ffpb.msg_target
  46. bot.msg(target, "[{0}] {1}".format(sender, str(data)))
  47. def _resolve_name (self, ip):
  48. if ip.startswith ("127."):
  49. return "localhost"
  50. try:
  51. addr = dns.reversename.from_address (ip)
  52. return re.sub ("(.infra)?.ffpb.", "", str (ffpb_resolver.query (addr, "PTR")[0]))
  53. except dns.resolver.NXDOMAIN:
  54. return ip
  55. class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
  56. pass
  57. def setup(bot):
  58. global msgserver, peers_repo, alfred_method, highscores, monitored_nodes
  59. bot.memory['ffpb_in_setup'] = True
  60. highscores = shelve.open('highscoredata', writeback=True)
  61. if not 'nodes' in highscores:
  62. highscores['nodes'] = 0
  63. highscores['nodes_ts'] = time.time()
  64. if not 'clients' in highscores:
  65. highscores['clients'] = 0
  66. highscores['clients_ts'] = time.time()
  67. monitored_nodes = shelve.open('nodes.monitored', writeback=True)
  68. seen_nodes = shelve.open('nodes.seen', writeback=True)
  69. bot.memory['seen_nodes'] = seen_nodes
  70. if not bot.config.has_section('ffpb'):
  71. bot.memory['ffpb_in_setup'] = False
  72. return
  73. if not bot.config.ffpb.peers_directory is None:
  74. peers_repo = git.Repo(bot.config.ffpb.peers_directory)
  75. assert peers_repo.bare == False
  76. if int(bot.config.ffpb.msg_enable) == 1:
  77. host = "localhost"
  78. port = 2342
  79. if not bot.config.ffpb.msg_host is None: host = bot.config.ffpb.msg_host
  80. if not bot.config.ffpb.msg_port is None: port = int(bot.config.ffpb.msg_port)
  81. msgserver = ThreadingTCPServer((host,port), MsgHandler)
  82. msgserver.bot = bot
  83. ip, port = msgserver.server_address
  84. print("Messaging server listening on {}:{}".format(ip,port))
  85. msgserver_thread = threading.Thread(target=msgserver.serve_forever)
  86. msgserver_thread.daemon = True
  87. msgserver_thread.start()
  88. alfred_method = bot.config.ffpb.alfred_method
  89. ffpb_updatealfred(bot)
  90. bot.memory['ffpb_in_setup'] = False
  91. def shutdown(bot):
  92. global msgserver, highscores, monitored_nodes
  93. if not highscores is None:
  94. highscores.sync()
  95. highscores.close()
  96. highscores = None
  97. if not monitored_nodes is None:
  98. monitored_nodes.sync()
  99. monitored_nodes.close()
  100. monitored_nodes = None
  101. if 'seen_nodes' in bot.memory and bot.memory['seen_nodes'] != None:
  102. bot.memory['seen_nodes'].close()
  103. bot.memory['seen_nodes'] = None
  104. del(bot.memory['seen_nodes'])
  105. if not msgserver is None:
  106. msgserver.shutdown()
  107. print("Closed messaging server.")
  108. msgserver = None
  109. @willie.module.commands("help")
  110. @willie.module.commands("hilfe")
  111. @willie.module.commands("man")
  112. def ffpb_help(bot, trigger):
  113. functions = {
  114. "!ping <knoten>": "Prüfe ob der Knoten erreichbar ist.",
  115. "!status": "Aktuellen Status des Netzwerks (insb. Anzahl Knoten und Clients) ausgegeben.",
  116. "!info <knoten>": "Allgemeine Information zu dem Knoten anzeigen.",
  117. "!link <knoten>": "MAC-Adresse und Link zur Status-Seite des Knotens anzeigen.",
  118. "!exec-on-peer <knoten> <kommando>": "Befehl auf dem Knoten ausführen (nur möglich bei eigenen Knoten oder als Admin, in beiden Fällen auch nur wenn der SSH-Key des Bots hinterlegt wurde)",
  119. }
  120. param = trigger.group(2)
  121. if param is None:
  122. bot.say("Funktionen: " + str.join(", ", sorted(functions.keys())))
  123. return
  124. if param.startswith("!"): param = param[1:]
  125. for fun in functions.keys():
  126. if fun.startswith("!" + param + " "):
  127. bot.say("Hilfe zu '" + fun + "': " + functions[fun])
  128. return
  129. bot.say("Allgemeine Hilfe gibt's mit !help - ohne Parameter.")
  130. def ffpb_findnode(name):
  131. if name is None or len(name) == 0:
  132. return None
  133. name = str(name).strip()
  134. names = {}
  135. # try to match MAC
  136. m = re.search("^([0-9a-fA-F][0-9a-fA-F]:){5}[0-9a-fA-F][0-9a-fA-F]$", name)
  137. if (not m is None):
  138. mac = m.group(0).lower()
  139. if mac in alfred_data:
  140. return alfred_data[mac]
  141. # try to find alias MAC
  142. for nodeid in alfred_data:
  143. node = alfred_data[nodeid]
  144. if "network" in node:
  145. if "mac" in node["network"] and node["network"]["mac"].lower() == mac:
  146. return node
  147. if "mesh_interfaces" in node["network"]:
  148. for mim in node["network"]["mesh_interfaces"]:
  149. if mim.lower() == mac:
  150. return node
  151. return {
  152. 'hostname': '?-' + mac.replace(':','').lower(),
  153. 'network': { 'addresses': [ mac2ipv6(mac, 'fdca:ffee:ff12:132:') ], 'mac': mac, },
  154. 'hardware': { 'model': 'derived-from-mac' },
  155. }
  156. # look through the ALFRED peers
  157. for nodeid in alfred_data:
  158. node = alfred_data[nodeid]
  159. if 'hostname' in node:
  160. h = node['hostname']
  161. if h.lower() == name.lower():
  162. return node
  163. else:
  164. names[h] = nodeid
  165. # still not found -> try peers_repo
  166. if not peers_repo is None:
  167. peer_name = None
  168. peer_mac = None
  169. peer_file = None
  170. for b in peers_repo.heads.master.commit.tree.blobs:
  171. if b.name.lower() == name.lower():
  172. peer_name = b.name
  173. peer_file = b.abspath
  174. break
  175. if (not peer_file is None) and os.path.exists(peer_file):
  176. peerfile = open(peer_file, "r")
  177. for line in peerfile:
  178. if line.startswith("# MAC:"):
  179. peer_mac = line[6:].strip()
  180. peerfile.close()
  181. if not (peer_mac is None):
  182. return {
  183. 'hostname': peer_name,
  184. 'network': { 'addresses': [ mac2ipv6(peer_mac, 'fdca:ffee:ff12:132:') ], 'mac': peer_mac },
  185. 'hardware': { 'model': 'derived-from-vpnkeys' },
  186. }
  187. # do a similar name lookup in the ALFRED data
  188. possibilities = difflib.get_close_matches(name, [ x for x in names ], cutoff=0.8)
  189. print('findnode: Fuzzy matching \'{0}\' got {1} entries: {2}'.format(name, len(possibilities), ', '.join(possibilities)))
  190. if len(possibilities) == 1:
  191. # if we got exactly one candidate that might be it
  192. return alfred_data[names[possibilities[0]]]
  193. return None
  194. def ffpb_findnode_from_botparam(bot, name, ensure_recent_alfreddata = True):
  195. if (name is None or len(name) == 0):
  196. if not bot is None: bot.reply("Grün.")
  197. return None
  198. if ensure_recent_alfreddata and alfred_data is None:
  199. if not bot is None: bot.say("Informationen sind ausverkauft, kommen erst morgen wieder rein.")
  200. return None
  201. if ensure_recent_alfreddata and ffpb_alfred_data_outdated():
  202. if not bot is None: bot.say("Ich habe gerade keine aktuellen Informationen, daher sage ich mal lieber nichts zu '" + name + "'.")
  203. return None
  204. node = ffpb_findnode(name)
  205. if node is None:
  206. if not bot is None: bot.say("Kein Plan wer oder was mit '" + name + "' gemeint ist :(")
  207. return node
  208. def mac2ipv6(mac, prefix=None):
  209. result = str(netaddr.EUI(mac).ipv6_link_local())
  210. if (not prefix is None) and (result.startswith("fe80::")):
  211. result = prefix + result[6:]
  212. return result
  213. @willie.module.interval(30)
  214. def ffpb_updatealfred(bot):
  215. """Aktualisiere ALFRED-Daten"""
  216. global alfred_data, alfred_update
  217. if alfred_method is None or alfred_method == "None":
  218. return
  219. updated = None
  220. if alfred_method == "exec":
  221. rawdata = subprocess.check_output(['alfred-json', '-z', '-r', '158'])
  222. updated = datetime.datetime.now()
  223. elif alfred_method.startswith("http"):
  224. try:
  225. rawdata = urllib2.urlopen(alfred_method)
  226. except:
  227. print("Failed to download ALFRED data.")
  228. return
  229. updated = datetime.datetime.fromtimestamp(mktime_tz(rawdata.info().getdate_tz("Last-Modified")))
  230. else:
  231. print("Unknown ALFRED data method '", alfred_method, "', cannot load new data.", sep="")
  232. alfred_data = None
  233. return
  234. try:
  235. alfred_data = json.load(rawdata)
  236. #print("Fetched new ALFRED data:", len(alfred_data), "entries")
  237. alfred_update = updated
  238. except ValueError as e:
  239. print("Failed to parse ALFRED data: " + str(e))
  240. return
  241. seen_nodes = bot.memory['seen_nodes'] if 'seen_nodes' in bot.memory else None
  242. if not seen_nodes is None:
  243. new = []
  244. for nodeid in alfred_data:
  245. nodeid = str(nodeid)
  246. if not nodeid in seen_nodes:
  247. seen_nodes[nodeid] = updated
  248. new.append((nodeid,alfred_data[nodeid]['hostname']))
  249. print('First time seen: ' + str(nodeid))
  250. if len(new) > 0 and not bot.memory['ffpb_in_setup']:
  251. action_msg = None
  252. if len(new) == 1:
  253. action_msg = random.choice((
  254. 'bemerkt den neuen Knoten {0}',
  255. 'entdeckt {0}',
  256. 'reibt sich die Augen und erblickt einen verpackungsfrischen Knoten {0}',
  257. 'hat {0} noch nie gesehen',
  258. )).format('\'' + str(new[0][1]) + '\'')
  259. else:
  260. action_msg = random.choice((
  261. 'bemerkt die neuen Knoten {0} und {1}',
  262. 'hat {0} und {1} entdeckt',
  263. 'bewundert {0} sowie {1}',
  264. 'freut sich, dass {0} und {1} nun auch online sind',
  265. )).format('\'' + '\', \''.join([ str(x[1]) for x in new[0:-1] ]) + '\'', '\'' + str(new[-1][1]) + '\'')
  266. action_target = bot.config.ffpb.msg_target
  267. bot.msg(action_target, '\x01ACTION %s\x01' % action_msg)
  268. def ffpb_alfred_data_outdated():
  269. timeout = datetime.datetime.now() - datetime.timedelta(minutes=5)
  270. is_outdated = timeout > alfred_update
  271. #print("ALFRED outdated? {0} (timeout={1} vs. lastupdate={2})".format(is_outdated, timeout, alfred_update))
  272. return is_outdated
  273. @willie.module.commands('debug-alfred')
  274. def ffpb_debug_alfred(bot, trigger):
  275. if alfred_data is None:
  276. bot.say("Keine ALFRED-Daten vorhanden.")
  277. else:
  278. bot.say("ALFRED Daten: count={0} lastupdate={1}".format(len(alfred_data), alfred_update))
  279. @willie.module.commands('alfred-data')
  280. def ffpb_peerdata(bot, trigger):
  281. if (not trigger.admin):
  282. bot.say('I wont leak (possibly) sensitive data to you.')
  283. return
  284. if (not trigger.is_privmsg) and (not trigger.nick in bot.ops[trigger.sender]):
  285. bot.say('Kein Keks? Keine Daten.')
  286. return
  287. target_name = trigger.group(2)
  288. node = ffpb_findnode_from_botparam(bot, target_name)
  289. if node is None: return
  290. for key in node:
  291. if key in [ 'hostname' ]: continue
  292. bot.say("{0}.{1} = {2}".format(node['hostname'], key, str(node[key])))
  293. @willie.module.commands('info')
  294. def ffpb_peerinfo(bot, trigger):
  295. target_name = trigger.group(2)
  296. node = ffpb_findnode_from_botparam(bot, target_name)
  297. if node is None: return
  298. info_mac = node["network"]["mac"]
  299. info_name = node["hostname"]
  300. info_hw = ""
  301. if "hardware" in node:
  302. if "model" in node["hardware"]:
  303. model = node["hardware"]["model"]
  304. info_hw = " model='" + model + "'"
  305. info_fw = ""
  306. info_update = ""
  307. if "software" in node:
  308. if "firmware" in node["software"]:
  309. fwinfo = str(node["software"]["firmware"]["release"]) if "release" in node["software"]["firmware"] else "unknown"
  310. info_fw = " firmware=" + fwinfo
  311. if "autoupdater" in node["software"]:
  312. autoupdater = node["software"]["autoupdater"]["branch"] if node["software"]["autoupdater"]["enabled"] else "off"
  313. info_update = " (autoupdater="+autoupdater+")"
  314. info_uptime = ""
  315. u = -1
  316. if "statistics" in node and "uptime" in node["statistics"]:
  317. u = int(float(node["statistics"]["uptime"]))
  318. elif 'uptime' in node:
  319. u = int(float(node['uptime']))
  320. if u > 0:
  321. d, r1 = divmod(u, 86400)
  322. h, r2 = divmod(r1, 3600)
  323. m, s = divmod(r2, 60)
  324. if d > 0:
  325. info_uptime = ' up {0}d {1}h'.format(d,h)
  326. elif h > 0:
  327. info_uptime = ' up {0}h {1}m'.format(h,m)
  328. else:
  329. info_uptime = ' up {0}m'.format(m)
  330. bot.say('[{1}]{2}{3}{4}{5}'.format(info_mac, info_name, info_hw, info_fw, info_update, info_uptime))
  331. @willie.module.commands('uptime')
  332. def ffpb_peeruptime(bot, trigger):
  333. target_name = trigger.group(2)
  334. node = ffpb_findnode_from_botparam(bot, target_name)
  335. if node is None: return
  336. info_name = node["hostname"]
  337. info_uptime = ''
  338. u_raw = None
  339. if 'statistics' in node and 'uptime' in node['statistics']:
  340. u_raw = node['statistics']['uptime']
  341. elif 'uptime' in node:
  342. u_raw = node['uptime']
  343. if not u_raw is None:
  344. u = int(float(u_raw))
  345. d, r1 = divmod(u, 86400)
  346. h, r2 = divmod(r1, 3600)
  347. m, s = divmod(r2, 60)
  348. if d > 0:
  349. info_uptime += '{0}d '.format(d)
  350. if h > 0:
  351. info_uptime += '{0}h '.format(h)
  352. info_uptime += '{0}m'.format(m)
  353. info_uptime += ' # raw: \'{0}\''.format(u_raw)
  354. else:
  355. info_uptime += '?'
  356. bot.say('uptime(\'{0}\') = {1}'.format(info_name, info_uptime))
  357. @willie.module.commands('link')
  358. def ffpb_peerlink(bot, trigger):
  359. target_name = trigger.group(2)
  360. node = ffpb_findnode_from_botparam(bot, target_name)
  361. if node is None: return
  362. info_mac = node["network"]["mac"]
  363. info_name = node["hostname"]
  364. info_v6 = mac2ipv6(info_mac, 'fdca:ffee:ff12:132:')
  365. bot.say('[{1}] mac {0} -> http://[{2}]/'.format(info_mac, info_name, info_v6))
  366. @willie.module.interval(60)
  367. def ffpb_updatepeers(bot):
  368. """Aktualisiere die Knotenliste und melde das Diff"""
  369. if peers_repo is None:
  370. print('WARNING: peers_repo is None')
  371. return
  372. old_head = peers_repo.head.commit
  373. peers_repo.remotes.origin.pull()
  374. new_head = peers_repo.head.commit
  375. if new_head != old_head:
  376. print('git pull: from ' + str(old_head) + ' to ' + str(new_head))
  377. added = []
  378. changed = []
  379. renamed = []
  380. deleted = []
  381. for f in old_head.diff(new_head):
  382. if f.new_file:
  383. added.append(f.b_blob.name)
  384. elif f.deleted_file:
  385. deleted.append(f.a_blob.name)
  386. elif f.renamed:
  387. renamed.append([f.rename_from, f.rename_to])
  388. else:
  389. changed.append(f.a_blob.name)
  390. response = "Knoten-Update (VPN +{0} %{1} -{2}): ".format(len(added), len(renamed)+len(changed), len(deleted))
  391. for f in added:
  392. response += " +'{}'".format(f)
  393. for f in changed:
  394. response += " %'{}'".format(f)
  395. for f in renamed:
  396. response += " '{}'->'{}'".format(f[0],f[1])
  397. for f in deleted:
  398. response += " -'{}'".format(f)
  399. bot.msg(bot.config.ffpb.msg_target, response)
  400. def ffpb_fetch_stats(bot, url, memoryid):
  401. response = urllib2.urlopen(url)
  402. data = json.load(response)
  403. nodes_active = 0
  404. nodes_total = 0
  405. clients_count = 0
  406. for node in data['nodes']:
  407. if node['flags']['gateway'] or node['flags']['client']:
  408. continue
  409. nodes_total += 1
  410. if node['flags']['online']:
  411. nodes_active += 1
  412. if 'legacy' in node['flags'] and node['flags']['legacy']:
  413. clients_count -= 1
  414. for link in data['links']:
  415. if link['type'] == 'client':
  416. clients_count += 1
  417. if not memoryid in bot.memory:
  418. bot.memory[memoryid] = { }
  419. stats = bot.memory[memoryid]
  420. stats["fetchtime"] = time.time()
  421. stats["nodes_active"] = nodes_active
  422. stats["nodes_total"] = nodes_total
  423. stats["clients"] = clients_count
  424. return (nodes_active, nodes_total, clients_count)
  425. @willie.module.interval(15)
  426. def ffpb_get_stats(bot):
  427. (nodes_active, nodes_total, clients_count) = ffpb_fetch_stats(bot, 'http://map.paderborn.freifunk.net/nodes.json', 'ffpb_stats')
  428. highscore_changed = False
  429. if nodes_active > highscores['nodes']:
  430. highscores['nodes'] = nodes_active
  431. highscores['nodes_ts'] = time.time()
  432. highscore_changed = True
  433. if clients_count > highscores['clients']:
  434. highscores['clients'] = clients_count
  435. highscores['clients_ts'] = time.time()
  436. highscore_changed = True
  437. if highscore_changed:
  438. print('HIGHSCORE changed: {0} nodes ({1}), {2} clients ({3})'.format(highscores['nodes'], highscores['nodes_ts'], highscores['clients'], highscores['clients_ts']))
  439. if not (bot.config.ffpb.msg_target is None):
  440. action_msg = 'notiert sich den neuen Highscore: {0} Knoten ({1}), {2} Clients ({3})'.format(highscores['nodes'], pretty_date(int(highscores['nodes_ts'])), highscores['clients'], pretty_date(int(highscores['clients_ts'])))
  441. action_target = bot.config.ffpb.msg_target
  442. if (not bot.config.ffpb.msg_target_public is None):
  443. action_target = bot.config.ffpb.msg_target_public
  444. bot.msg(action_target, '\x01ACTION %s\x01' % action_msg)
  445. @willie.module.commands('status')
  446. def ffpb_status(bot, trigger):
  447. """Status des FFPB-Netzes: Anzahl (aktiver) Knoten + Clients"""
  448. stats = bot.memory['ffpb_stats'] if 'ffpb_stats' in bot.memory else None
  449. if stats is None:
  450. bot.say('Uff, kein Plan wo der Zettel ist. Fragst du später nochmal?')
  451. return
  452. bot.say('Es sind {0} Knoten und ca. {1} Clients online.'.format(stats["nodes_active"], stats["clients"]))
  453. def pretty_date(time=False):
  454. """
  455. Get a datetime object or a int() Epoch timestamp and return a
  456. pretty string like 'an hour ago', 'Yesterday', '3 months ago',
  457. 'just now', etc
  458. """
  459. from datetime import datetime
  460. now = datetime.now()
  461. compare = None
  462. if type(time) is int:
  463. compare = datetime.fromtimestamp(time)
  464. elif type(time) is float:
  465. compare = datetime.fromtimestamp(int(time))
  466. elif isinstance(time,datetime):
  467. compare = time
  468. elif not time:
  469. compare = now
  470. diff = now - compare
  471. second_diff = diff.seconds
  472. day_diff = diff.days
  473. if day_diff < 0:
  474. return ''
  475. if day_diff == 0:
  476. if second_diff < 10:
  477. return "gerade eben"
  478. if second_diff < 60:
  479. return "vor " + str(second_diff) + " Sekunden"
  480. if second_diff < 120:
  481. return "vor einer Minute"
  482. if second_diff < 3600:
  483. return "vor " + str(second_diff / 60) + " Minuten"
  484. if second_diff < 7200:
  485. return "vor einer Stunde"
  486. if second_diff < 86400:
  487. return "vor " + str(second_diff / 3600) + " Stunden"
  488. if day_diff == 1:
  489. return "gestern"
  490. if day_diff < 7:
  491. return "vor " + str(day_diff) + " Tagen"
  492. return "am " + compare.strftime('%d.%m.%Y um %H:%M Uhr')
  493. @willie.module.commands('highscore')
  494. def ffpb_highscore(bot, trigger):
  495. bot.say('Highscore: {0} Knoten ({1}), {2} Clients ({3})'.format(
  496. highscores['nodes'], pretty_date(int(highscores['nodes_ts'])),
  497. highscores['clients'], pretty_date(int(highscores['clients_ts']))))
  498. @willie.module.commands('rollout-status')
  499. def ffpb_rolloutstatus(bot, trigger):
  500. result = { }
  501. for branch in [ 'stable', 'testing' ]:
  502. result[branch] = None
  503. skipped = 0
  504. if (not (trigger.admin and trigger.is_privmsg)) and (not trigger.nick in bot.ops[trigger.sender]):
  505. bot.say('Geh zur dunklen Seite, die haben Kekse - ohne Keks kein Rollout-Status.')
  506. return
  507. expected_release = trigger.group(2)
  508. if expected_release is None or len(expected_release) == 0:
  509. bot.say('Von welcher Firmware denn?')
  510. return
  511. for nodeid in alfred_data:
  512. item = alfred_data[nodeid]
  513. if (not 'software' in item) or (not 'firmware' in item['software']) or (not 'autoupdater' in item['software']):
  514. skipped+=1
  515. continue
  516. release = item['software']['firmware']['release']
  517. branch = item['software']['autoupdater']['branch']
  518. enabled = item['software']['autoupdater']['enabled']
  519. if not branch in result or result[branch] is None:
  520. result[branch] = { 'auto_count': 0, 'auto_not': 0, 'manual_count': 0, 'manual_not': 0, 'total': 0 }
  521. result[branch]['total'] += 1
  522. match = 'count' if release == expected_release else 'not'
  523. mode = 'auto' if enabled else 'manual'
  524. result[branch][mode+'_'+match] += 1
  525. output = "Rollout von '{0}':".format(expected_release)
  526. for branch in result:
  527. auto_count = result[branch]['auto_count']
  528. auto_total = auto_count + result[branch]['auto_not']
  529. manual_count = result[branch]['manual_count']
  530. manual_total = manual_count + result[branch]['manual_not']
  531. bot.say("Rollout von '{0}': {1} = {2}/{3} per Auto-Update, {4}/{5} manuell".format(expected_release, branch, auto_count, auto_total, manual_count, manual_total))
  532. if skipped > 0:
  533. bot.say("Rollout von '{0}': {1} Knoten unklar".format(expected_release, skipped))
  534. @willie.module.commands('ping')
  535. def ffpb_ping(bot, trigger=None, target_name=None):
  536. """Ping FFPB-Knoten"""
  537. if target_name is None: target_name = trigger.group(2)
  538. node = ffpb_findnode_from_botparam(bot, target_name, ensure_recent_alfreddata=False)
  539. if node is None: return None
  540. target = [x for x in node["network"]["addresses"] if not x.lower().startswith("fe80:")][0]
  541. target_alias = node["hostname"]
  542. print("pinging '{0}' at {1} ...".format(target_name, target))
  543. result = os.system('ping6 -c 2 -W 1 ' + target + ' >/dev/null')
  544. if result == 0:
  545. print("ping to '{0}' succeeded".format(target_name))
  546. if not bot is None: bot.say('Knoten "' + target_alias + '" antwortet \o/')
  547. return True
  548. elif result == 1 or result == 256:
  549. print("ping to '{0}' failed".format(target_name))
  550. if not bot is None: bot.say('Keine Antwort von "' + target_alias + '" :-(')
  551. return False
  552. else:
  553. print("ping to '{0}' broken: result='{1}'".format(target_name, result))
  554. if not bot is None: bot.say('Uh oh, irgendwas ist kaputt. Chef, ping result = ' + str(result) + ' - darf ich das essen?')
  555. return None
  556. @willie.module.interval(3*60)
  557. def ffpb_monitor_ping(bot):
  558. notify_target = bot.config.core.owner
  559. if (not bot.config.ffpb.msg_target is None):
  560. notify_target = bot.config.ffpb.msg_target
  561. for node in monitored_nodes:
  562. mon = monitored_nodes[node]
  563. added = mon['added']
  564. last_status = mon['status']
  565. last_check = mon['last_check']
  566. last_success = mon['last_success']
  567. current_status = ffpb_ping(bot=None, target_name=node)
  568. if current_status is None: current_status = False
  569. mon['status'] = current_status
  570. mon['last_check'] = time.time()
  571. if current_status == True: mon['last_success'] = time.time()
  572. print("Monitoring ({0}) {1} (last: {2} at {3})".format(node, current_status, last_status, time.strftime('%Y-%m-%d %H:%M', time.localtime(last_check))))
  573. if last_status != current_status and (last_status or current_status):
  574. if last_check is None:
  575. # erster Check, keine Ausgabe
  576. continue
  577. if current_status == True:
  578. bot.msg(notify_target, 'Monitoring: Knoten \'{0}\' pingt wieder (zuletzt {1})'.format(node, pretty_date(last_success) if not last_success is None else '[nie]'))
  579. else:
  580. bot.msg(notify_target, 'Monitoring: Knoten \'{0}\' DOWN'.format(node))
  581. @willie.module.commands('monitor')
  582. def ffpb_monitor(bot, trigger):
  583. if not trigger.admin:
  584. bot.say('Ich ping hier nicht für jeden durch die Weltgeschichte.')
  585. return
  586. if trigger.group(2) is None or len(trigger.group(2)) == 0:
  587. bot.say('Das Monitoring sagt du hast doofe Ohren.')
  588. return
  589. cmd = trigger.group(3)
  590. node = trigger.group(4)
  591. if not node is None: node = str(node)
  592. if cmd == "add":
  593. if node in monitored_nodes:
  594. bot.say('Knoten \'{0}\' wird bereits gemonitored.'.format(node))
  595. return
  596. monitored_nodes[node] = {
  597. 'added': trigger.sender,
  598. 'status': None,
  599. 'last_check': None,
  600. 'last_success': None,
  601. }
  602. bot.say('Knoten \'{0}\' wird jetzt ganz genau beobachtet.'.format(node))
  603. return
  604. if cmd == "del":
  605. if not node in monitored_nodes:
  606. bot.say('Knoten \'{0}\' war gar nicht im Monitoring?!?'.format(node))
  607. return
  608. del monitored_nodes[node]
  609. bot.say('Okidoki, \'{0}\' lasse ich jetzt links liegen.'.format(node))
  610. return
  611. if cmd == "info":
  612. if node in monitored_nodes:
  613. info = monitored_nodes[node]
  614. bot.say('Knoten \'{0}\' wurde zuletzt {1} gepingt (Ergebnis: {2})'.format(node, pretty_date(info['last_check']) if not info['last_check'] is None else "^W noch nie", info['status']))
  615. else:
  616. bot.say('Knoten \'{0}\' ist nicht im Monitoring.'.format(node))
  617. return
  618. if cmd == "list":
  619. nodes = ""
  620. for node in monitored_nodes:
  621. nodes = nodes + " " + node
  622. bot.say('Monitoring aktiv für:' + nodes)
  623. return
  624. if cmd == "help":
  625. bot.say('Entweder "!monitor list" oder "!monitor {add|del|info} <node>"')
  626. return
  627. bot.say('Mit "' + str(cmd) + '" kann ich nix anfangen, probier doch mal "!monitor help".')
  628. @willie.module.commands('exec-on-peer')
  629. def ffpb_remoteexec(bot, trigger):
  630. """Remote Execution fuer FFPB_Knoten"""
  631. bot_params = trigger.group(2).split(' ',1)
  632. if len(bot_params) != 2:
  633. bot.say('Wenn du nicht sagst wo mach ich remote execution bei dir!')
  634. bot.say('Tipp: !exec-on-peer <peer> <cmd>')
  635. return
  636. target_name = bot_params[0]
  637. target_cmd = bot_params[1]
  638. if not trigger.admin:
  639. bot.say('I can haz sudo?')
  640. return
  641. if trigger.is_privmsg:
  642. bot.say('Bitte per Channel.')
  643. return
  644. if not trigger.nick in bot.ops[trigger.sender]:
  645. bot.say('Geh weg.')
  646. return
  647. node = ffpb_findnode_from_botparam(bot, target_name, ensure_recent_alfreddata=False)
  648. if node is None: return
  649. target = [x for x in node["network"]["addresses"] if not x.lower().startswith("fe80:")][0]
  650. target_alias = node["hostname"]
  651. cmd = 'ssh -6 -l root ' + target + ' -- "' + target_cmd + '"'
  652. print("REMOTE EXEC = " + cmd)
  653. try:
  654. result = subprocess.check_output(['ssh', '-6n', '-l', 'root', '-o', 'BatchMode=yes', '-o','StrictHostKeyChecking=no', target, target_cmd], stderr=subprocess.STDOUT, shell=False)
  655. lines = str(result).splitlines()
  656. if len(lines) == 0:
  657. bot.say('exec-on-peer(' + target_alias + '): No output')
  658. return
  659. msg = 'exec-on-peer(' + target_alias + '): ' + str(len(lines)) + ' Zeilen'
  660. if len(lines) > 8:
  661. msg += ' (zeige max. 8)'
  662. bot.say(msg + ':')
  663. for line in lines[0:8]:
  664. bot.say(line)
  665. except subprocess.CalledProcessError as e:
  666. bot.say('Fehler '+str(e.returncode)+' bei exec-on-peer('+target_alias+'): ' + e.output)