ffpb_nodeinfo.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import willie
  4. from ffpb import ffpb_findnode_from_botparam, ffpb_get_batcave_nodefield, mac2ipv6, playitsafe
  5. def setup(bot):
  6. """Called by willie upon loading this plugin."""
  7. pass
  8. def shutdown(bot):
  9. """Called by willie upon unloading this plugin."""
  10. pass
  11. @willie.module.commands('alfred-data')
  12. def ffpb_peerdata(bot, trigger):
  13. """Show ALFRED data of the given node."""
  14. # identify node or bail out
  15. target_name = trigger.group(2)
  16. node = ffpb_findnode_from_botparam(bot, target_name)
  17. if node is None:
  18. return
  19. # query must be a PM or as OP in the channel
  20. if not playitsafe(bot, trigger, via_privmsg=True, node=node):
  21. # the check function already gives a bot reply, just exit here
  22. return
  23. # reply each key in the node's data
  24. for key in node:
  25. # skip some fields
  26. if key in ['hostname']:
  27. continue
  28. bot.say("{0}.{1} = {2}".format(node['hostname'], key, node[key]))
  29. @willie.module.commands('info')
  30. def ffpb_peerinfo(bot, trigger):
  31. """Show information of the given node."""
  32. # identify node or bail out
  33. target_name = trigger.group(2)
  34. node = ffpb_findnode_from_botparam(bot, target_name)
  35. if node is None:
  36. return
  37. # read node information
  38. info_mac = node['network']['mac'] if 'network' in node and 'mac' in node['network'] else '??:??:??:??:??:??'
  39. info_id = node['node_id'] if 'node_id' in node else info_mac.replace(':', '')
  40. info_name = node['hostname'] if 'hostname' in node else '?-' + info_id
  41. info_hw = ""
  42. if "hardware" in node:
  43. if "model" in node["hardware"]:
  44. model = node["hardware"]["model"]
  45. info_hw = " model='" + model + "'"
  46. info_fw = ""
  47. info_update = ""
  48. if "software" in node:
  49. if "firmware" in node["software"]:
  50. if "release" in node["software"]["firmware"]:
  51. info_fw = " firmware=" + str(node["software"]["firmware"]["release"])
  52. else:
  53. info_fw = " unknown firmware"
  54. if "autoupdater" in node["software"]:
  55. autoupdater = node["software"]["autoupdater"]["branch"] if node["software"]["autoupdater"]["enabled"] else "off"
  56. info_update = " (autoupdater="+autoupdater+")"
  57. info_uptime = ""
  58. uptime = -1
  59. if "statistics" in node and "uptime" in node["statistics"]:
  60. uptime = int(float(node["statistics"]["uptime"]))
  61. elif 'uptime' in node:
  62. uptime = int(float(node['uptime']))
  63. if uptime > 0:
  64. days, rem_d = divmod(uptime, 86400)
  65. hours, rem_h = divmod(rem_d, 3600)
  66. minutes, _ = divmod(rem_h, 60)
  67. if days > 0:
  68. info_uptime = ' up {0}d {1}h'.format(days, hours)
  69. elif hours > 0:
  70. info_uptime = ' up {0}h {1}m'.format(hours, minutes)
  71. else:
  72. info_uptime = ' up {0}m'.format(minutes)
  73. info_clients = ""
  74. clientcount = ffpb_get_batcave_nodefield(info_id, 'clientcount')
  75. if not clientcount is None:
  76. clientcount = int(clientcount)
  77. info_clients = ' clients={0}'.format(clientcount)
  78. bot.say('[{1}]{2}{3}{4}{5}{6}'.format(info_mac, info_name, info_hw, info_fw, info_update, info_uptime, info_clients))
  79. @willie.module.commands('uptime')
  80. def ffpb_peeruptime(bot, trigger):
  81. """Display the uptime of the given node."""
  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. # get name and raw uptime from node
  88. info_name = node["hostname"]
  89. info_uptime = ''
  90. u_raw = None
  91. if 'statistics' in node and 'uptime' in node['statistics']:
  92. u_raw = node['statistics']['uptime']
  93. elif 'uptime' in node:
  94. u_raw = node['uptime']
  95. # pretty print uptime
  96. if not u_raw is None:
  97. uptime = int(float(u_raw))
  98. days, rem_d = divmod(uptime, 86400)
  99. hours, rem_h = divmod(rem_d, 3600)
  100. minutes, _ = divmod(rem_h, 60)
  101. if days > 0:
  102. info_uptime += '{0}d '.format(days)
  103. if hours > 0:
  104. info_uptime += '{0}h '.format(hours)
  105. info_uptime += '{0}m'.format(minutes)
  106. info_uptime += ' # raw: \'{0}\''.format(u_raw)
  107. else:
  108. info_uptime += '?'
  109. # reply to user
  110. bot.say('uptime(\'{0}\') = {1}'.format(info_name, info_uptime))
  111. @willie.module.commands('link')
  112. def ffpb_peerlink(bot, trigger):
  113. """Display MAC and link to statuspage for 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 node's MAC
  120. info_mac = node["network"]["mac"]
  121. info_name = node["hostname"]
  122. # get node's v6 address in the mesh (derived from MAC address)
  123. info_v6 = mac2ipv6(info_mac, 'fdca:ffee:ff12:132:')
  124. # reply to user
  125. bot.say('[{1}] mac {0} -> http://[{2}]/'.format(info_mac, info_name, info_v6))