ffpb_nodeinfo.py 5.9 KB

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