ffpb_nodeinfo.py 6.0 KB

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