ffpb_nodeinfo.py 5.9 KB

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