ffpb_nodeinfo.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. bot.say('{0} wurde zuletzt gesehen: {1} (ALFRED,) bzw. {2} (BATMAN)'.format(
  102. node['hostname'],
  103. pretty_date(a_value) if not a_value is None else "nie",
  104. pretty_date(b_value) if not b_value is None else "nie"
  105. ))
  106. @willie.module.commands('uptime')
  107. def ffpb_peeruptime(bot, trigger):
  108. """Display the uptime of the given node."""
  109. # identify node or bail out
  110. target_name = trigger.group(2)
  111. node = ffpb_findnode_from_botparam(bot, target_name)
  112. if node is None:
  113. return
  114. # get name and raw uptime from node
  115. info_name = node["hostname"]
  116. info_uptime = ''
  117. u_raw = None
  118. if 'statistics' in node and 'uptime' in node['statistics']:
  119. u_raw = node['statistics']['uptime']
  120. elif 'uptime' in node:
  121. u_raw = node['uptime']
  122. # pretty print uptime
  123. if not u_raw is None:
  124. uptime = int(float(u_raw))
  125. days, rem_d = divmod(uptime, 86400)
  126. hours, rem_h = divmod(rem_d, 3600)
  127. minutes, _ = divmod(rem_h, 60)
  128. if days > 0:
  129. info_uptime += '{0}d '.format(days)
  130. if hours > 0:
  131. info_uptime += '{0}h '.format(hours)
  132. info_uptime += '{0}m'.format(minutes)
  133. info_uptime += ' # raw: \'{0}\''.format(u_raw)
  134. else:
  135. info_uptime += '?'
  136. # reply to user
  137. bot.say('uptime(\'{0}\') = {1}'.format(info_name, info_uptime))
  138. @willie.module.commands('link')
  139. def ffpb_peerlink(bot, trigger):
  140. """Display MAC and link to statuspage for the given node."""
  141. # identify node or bail out
  142. target_name = trigger.group(2)
  143. node = ffpb_findnode_from_botparam(bot, target_name)
  144. if node is None:
  145. return
  146. # get node's MAC
  147. info_mac = node["network"]["mac"]
  148. info_name = node["hostname"]
  149. # get node's v6 address in the mesh (derived from MAC address)
  150. info_v6 = mac2ipv6(info_mac, 'fdca:ffee:ff12:132:')
  151. # reply to user
  152. bot.say('[{1}] mac {0} -> http://[{2}]/'.format(info_mac, info_name, info_v6))