Collection of GNS3 GUI Tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.0KB

  1. #!/usr/local/bin/python3
  2. """
  3. nodes_log - get log of nodes
  4. """
  5. import os
  6. import sys
  7. import gns3api
  8. def node_file(api, node, fname):
  9. """ get file from a node """
  10. data = None
  11. try:
  12. data = api.request("GET", ("/v2/projects", node['project_id'],
  13. "nodes", node['node_id'], "files", fname))
  14. except gns3api.GNS3ApiException as err:
  15. if api.status_code != 404:
  16. sys.exit("Can't get log file: {}".format(err))
  17. if data:
  18. data = fname + ":\n" + \
  19. data.decode('utf-8', errors='ignore').rstrip("\n") + "\n\n"
  20. else:
  21. data = ""
  22. return data
  23. def nodes_log(argv):
  24. """ parse command line, retrieve nodes and get log of nodes """
  25. # get arguments
  26. if len(argv) < 4:
  27. sys.exit("usage:\nstart_nodes version parameter-file project-id [sel-item ...]")
  28. try:
  29. with open(argv[2], "r") as file:
  30. cntl_url, cntl_user, cntl_passwd, *_ = file.read(512).splitlines()
  31. if argv[2].endswith(".tmp"):
  32. os.remove(argv[2])
  33. except (OSError, ValueError) as err:
  34. sys.exit("Can't get controller connection params: {}".format(err))
  35. project_id = argv[3]
  36. sel_items = argv[4:]
  37. # connect to GNS3 controller
  38. try:
  39. api = gns3api.GNS3Api(cntl_url, cntl_user, cntl_passwd)
  40. except gns3api.GNS3ApiException as err:
  41. sys.exit("Can't connect to GNS3 controller: {}".format(err))
  42. # get node information
  43. nodes = {}
  44. try:
  45. for node in api.request('GET', ('/v2/projects', project_id, 'nodes')):
  46. nodes[node['node_id']] = node
  47. except gns3api.GNS3ApiException as err:
  48. sys.exit("Can't get node information: {}".format(err))
  49. if not nodes:
  50. sys.exit("No node in project")
  51. # get selected nodes
  52. if not sel_items:
  53. sel_nodes = list(nodes.keys())
  54. else:
  55. sel_nodes = [item[6:] for item in sel_items
  56. if item.startswith("nodes/")]
  57. if not sel_nodes:
  58. sys.exit("No node selected")
  59. sel_nodes.sort(key=lambda k: nodes[k]['name'].lower())
  60. # get log of nodes
  61. log = ""
  62. for node_id in sel_nodes:
  63. node = nodes[node_id]
  64. log += "*** {} ({})\n\n".format(node['name'], node['node_id'])
  65. if node['node_type'] == 'docker':
  66. log += node_file(api, node, "vnc.log")
  67. elif node['node_type'] == 'dynamips':
  68. log += node_file(api, node,
  69. "{}_i{}_log.txt".format(
  70. node['properties']['platform'],
  71. node['properties']['dynamips_id']))
  72. elif node['node_type'] == 'qemu':
  73. log += node_file(api, node, "qemu.log")
  74. log += node_file(api, node, "qemu-img.log")
  75. elif node['node_type'] == 'vpcs':
  76. log += node_file(api, node, "vpcs.log")
  77. log += node_file(api, node, "ubridge.log")
  78. sys.stdout.write(log)
  79. try:
  80. nodes_log(sys.argv)
  81. except KeyboardInterrupt:
  82. print()
  83. sys.exit("Aborted\n")