ffpb_nodeinfo.py 4.1 KB

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