4
0

ffpb_nodeinfo.py 7.0 KB

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