ffpb_nodeinfos.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import willie
  4. def setup(bot):
  5. pass
  6. @willie.module.commands('info')
  7. def ffpb_peerinfo(bot, trigger):
  8. """Informationen zu einem Knoten abrufen"""
  9. # identify node or bail out
  10. target_name = trigger.group(2)
  11. node = ffpb_findnode_from_botparam(bot, target_name)
  12. if node is None: return
  13. # read node information
  14. info_mac = node["network"]["mac"]
  15. info_name = node["hostname"]
  16. info_hw = ""
  17. if "hardware" in node:
  18. if "model" in node["hardware"]:
  19. model = node["hardware"]["model"]
  20. info_hw = " model='" + model + "'"
  21. info_fw = ""
  22. info_update = ""
  23. if "software" in node:
  24. if "firmware" in node["software"]:
  25. fwinfo = str(node["software"]["firmware"]["release"]) if "release" in node["software"]["firmware"] else "unknown"
  26. info_fw = " firmware=" + fwinfo
  27. if "autoupdater" in node["software"]:
  28. autoupdater = node["software"]["autoupdater"]["branch"] if node["software"]["autoupdater"]["enabled"] else "off"
  29. info_update = " (autoupdater="+autoupdater+")"
  30. info_uptime = ""
  31. u = -1
  32. if "statistics" in node and "uptime" in node["statistics"]:
  33. u = int(float(node["statistics"]["uptime"]))
  34. elif 'uptime' in node:
  35. u = int(float(node['uptime']))
  36. if u > 0:
  37. d, r1 = divmod(u, 86400)
  38. h, r2 = divmod(r1, 3600)
  39. m, s = divmod(r2, 60)
  40. if d > 0:
  41. info_uptime = ' up {0}d {1}h'.format(d,h)
  42. elif h > 0:
  43. info_uptime = ' up {0}h {1}m'.format(h,m)
  44. else:
  45. info_uptime = ' up {0}m'.format(m)
  46. bot.say('[{1}]{2}{3}{4}{5}'.format(info_mac, info_name, info_hw, info_fw, info_update, info_uptime))
  47. @willie.module.commands('uptime')
  48. def ffpb_peeruptime(bot, trigger):
  49. """Zeige Uptime eines Knotens"""
  50. # identify node or bail out
  51. target_name = trigger.g roup(2)
  52. node = ffpb_findnode_from_botparam(bot, target_name)
  53. if node is None: return
  54. # get name and raw uptime from node
  55. info_name = node["hostname"]
  56. info_uptime = ''
  57. u_raw = None
  58. if 'statistics' in node and 'uptime' in node['statistics']:
  59. u_raw = node['statistics']['uptime']
  60. elif 'uptime' in node:
  61. u_raw = node['uptime']
  62. # pretty print uptime
  63. if not u_raw is None:
  64. u = int(float(u_raw))
  65. d, r1 = divmod(u, 86400)
  66. h, r2 = divmod(r1, 3600)
  67. m, s = divmod(r2, 60)
  68. if d > 0:
  69. info_uptime += '{0}d '.format(d)
  70. if h > 0:
  71. info_uptime += '{0}h '.format(h)
  72. info_uptime += '{0}m'.format(m)
  73. info_uptime += ' # raw: \'{0}\''.format(u_raw)
  74. else:
  75. info_uptime += '?'
  76. # reply to user
  77. bot.say('uptime(\'{0}\') = {1}'.format(info_name, info_uptime))
  78. @willie.module.commands('link')
  79. def ffpb_peerlink(bot, trigger):
  80. """Zeige MAC und Link zur Statusseite eines Knotens"""
  81. # identify node or bail out
  82. target_name = trigger.group(2)
  83. node = ffpb_findnode_from_botparam(bot, target_name)
  84. if node is None: return
  85. # get node's mac
  86. info_mac = node["network"]["mac"]
  87. info_name = node["hostname"]
  88. # get node's v6 address in the mesh (derived from MAC address)
  89. info_v6 = mac2ipv6(info_mac, 'fdca:ffee:ff12:132:')
  90. # reply to user
  91. bot.say('[{1}] mac {0} -> http://[{2}]/'.format(info_mac, info_name, info_v6))