ffpb_nodeinfo.py 6.4 KB

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