ffpb_nodeinfo.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import time
  4. import willie
  5. from ffpb import \
  6. ffpb_findnode_from_botparam, \
  7. mac2ipv6, playitsafe, pretty_date
  8. from batcave import BatcaveClient
  9. __batcave = None
  10. def setup(bot):
  11. """Called by willie upon loading this plugin."""
  12. global __batcave, highscores
  13. __batcave = BatcaveClient(bot.config.ffpb.batcave_url)
  14. pass
  15. def shutdown(bot):
  16. """Called by willie upon unloading this plugin."""
  17. pass
  18. @willie.module.commands('identify')
  19. def ffpb_identify(bot, trigger):
  20. """Identify node."""
  21. # query must be as OP in the channel
  22. if not playitsafe(bot, trigger, via_channel=True, need_op=True):
  23. # the check function already gives a bot reply, just exit here
  24. return
  25. ident = trigger.group(2)
  26. result = __batcave.identify(ident)
  27. if result is None:
  28. bot.say('Mist, ich erreiche den Detektiv nicht.')
  29. return
  30. if not ident in result:
  31. bot.say('Mein Detektiv hat getrunken und erzählt Blödsinn.')
  32. return
  33. if len(result[ident]) == 0:
  34. bot.say('"%s" konnte nicht zugeordnet werden :/' % ident)
  35. elif len(result[ident]) == 1:
  36. bot.say('"%s" ist eindeutig: %s' % (ident, result[ident][0]))
  37. else:
  38. bot.say('"{0}" ist mehrdeutig: {1}'.format(
  39. ident, str.join(', ', result[ident])))
  40. @willie.module.commands('raw-data')
  41. def ffpb_peerdata(bot, trigger):
  42. """Show ALFRED data of the given node."""
  43. # identify node or bail out
  44. target_name = trigger.group(2)
  45. node = ffpb_findnode_from_botparam(bot, target_name)
  46. if node is None:
  47. return
  48. # query must be a PM or as OP in the channel
  49. if not playitsafe(bot, trigger, via_privmsg=True, node=node):
  50. # the check function already gives a bot reply, just exit here
  51. return
  52. # reply each key in the node's data
  53. for key in node:
  54. # skip some fields
  55. if key in ['hostname']:
  56. continue
  57. bot.say("{0}.{1} = {2}".format(
  58. node.get('hostname', '?-' + target_name),
  59. key, node[key]))
  60. @willie.module.commands('info')
  61. def ffpb_peerinfo(bot, trigger):
  62. """Show information of the given node."""
  63. # identify node or bail out
  64. target_name = trigger.group(2)
  65. node = ffpb_findnode_from_botparam(bot, target_name)
  66. if node is None:
  67. return
  68. output = []
  69. # read node information
  70. info_mac = node.get('network', {}).get('mac', '??:??:??:??:??:??')
  71. info_id = node.get('node_id', info_mac.replace(':', ''))
  72. info_name = node.get('hostname', '?-' + info_id)
  73. output.append("[" + info_name + "]")
  74. if "hardware" in node:
  75. model = node["hardware"]
  76. output.append("model='" + model + "'")
  77. if "software" in node:
  78. if "firmware" in node["software"]:
  79. output.append("firmware=" + str(node["software"]["firmware"]))
  80. if "autoupdater" in node["software"]:
  81. autoupdater = node["software"]["autoupdater"]
  82. output.append("(autoupdater="+autoupdater+")")
  83. uptime = node.get('uptime', -1)
  84. if uptime > 0:
  85. days, rem_d = divmod(uptime, 86400)
  86. hours, rem_h = divmod(rem_d, 3600)
  87. minutes, _ = divmod(rem_h, 60)
  88. if days > 0:
  89. output.append('up {0}d {1}h'.format(days, hours))
  90. elif hours > 0:
  91. output.append('up {0}h {1}m'.format(hours, minutes))
  92. else:
  93. output.append('up {0}m'.format(minutes))
  94. clientcount = node.get('clientcount')
  95. if not clientcount is None:
  96. clientcount = int(clientcount)
  97. output.append('clients={0}'.format(clientcount))
  98. nodestatus = node.get('status', 'unknown')
  99. if nodestatus != 'active':
  100. output.append("[" + nodestatus.upper() + "]")
  101. bot.say(str.join(" ", output))
  102. @willie.module.commands('last-seen')
  103. @willie.module.commands('last_seen')
  104. @willie.module.commands('lastseen')
  105. def ffpb_lastseen(bot, trigger):
  106. """Display when the given node has last been seen."""
  107. # identify node or bail out
  108. target_name = trigger.group(2)
  109. node = ffpb_findnode_from_botparam(bot, target_name)
  110. if node is None:
  111. return
  112. node_name = node.get('hostname')
  113. last_seen = node.get('__UPDATED__')
  114. if last_seen is not None:
  115. a_value = int(last_seen.get('alfred'))
  116. b_value = int(last_seen.get('batadv'))
  117. else:
  118. a_value = b_value = None
  119. a_delta = time.time() - a_value if a_value is not None else None
  120. b_delta = time.time() - b_value if b_value is not None else None
  121. if a_value is None and b_value is None:
  122. bot.say('{0} wurde offenbar noch gar nicht gesehen?'.format(node_name))
  123. return
  124. if a_delta < 30 and b_delta < 30:
  125. bot.say('{0} wurde gerade eben gesehen.'.format(node_name))
  126. return
  127. if a_value is not None and b_value is not None and \
  128. abs(a_value - b_value) < 60:
  129. bot.say('{0} wurde zuletzt gesehen: {1}'.format(
  130. node_name,
  131. pretty_date((a_value + b_value) / 2)))
  132. else:
  133. bot.say('{0} wurde zuletzt gesehen: {1} (ALFRED,) bzw. {2} (BATMAN)'.format(
  134. node_name,
  135. pretty_date(a_value) if not a_value is None else "nie",
  136. pretty_date(b_value) if not b_value is None else "nie"
  137. ))
  138. @willie.module.commands('uptime')
  139. def ffpb_peeruptime(bot, trigger):
  140. """Display the uptime of the given node."""
  141. # identify node or bail out
  142. target_name = trigger.group(2)
  143. node = ffpb_findnode_from_botparam(bot, target_name)
  144. if node is None:
  145. return
  146. # get name and raw uptime from node
  147. info_name = node["hostname"]
  148. info_uptime = ''
  149. u_raw = None
  150. if 'statistics' in node and 'uptime' in node['statistics']:
  151. u_raw = node['statistics']['uptime']
  152. elif 'uptime' in node:
  153. u_raw = node['uptime']
  154. # pretty print uptime
  155. if not u_raw is None:
  156. uptime = int(float(u_raw))
  157. days, rem_d = divmod(uptime, 86400)
  158. hours, rem_h = divmod(rem_d, 3600)
  159. minutes, _ = divmod(rem_h, 60)
  160. if days > 0:
  161. info_uptime += '{0}d '.format(days)
  162. if hours > 0:
  163. info_uptime += '{0}h '.format(hours)
  164. info_uptime += '{0}m'.format(minutes)
  165. info_uptime += ' # raw: \'{0}\''.format(u_raw)
  166. else:
  167. info_uptime += '?'
  168. # reply to user
  169. bot.say('uptime(\'{0}\') = {1}'.format(info_name, info_uptime))
  170. @willie.module.commands('link')
  171. def ffpb_peerlink(bot, trigger):
  172. """Display MAC and link to statuspage for the given node."""
  173. # identify node or bail out
  174. target_name = trigger.group(2)
  175. node = ffpb_findnode_from_botparam(bot, target_name)
  176. if node is None:
  177. return
  178. # get node's MAC
  179. info_mac = node.get('mac')
  180. info_name = node.get('hostname')
  181. # get node's v6 address in the mesh (derived from MAC address)
  182. info_v6 = mac2ipv6(info_mac, 'fdca:ffee:ff12:132:')
  183. # reply to user
  184. bot.say('[{1}] mac {0} -> http://[{2}]/'.format(
  185. info_mac, info_name, info_v6))