ffpb_nodeinfo.py 6.5 KB

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