debug_snapshot.py
Go to the documentation of this file.
1 """
2 The snapshot command creates a zip file of relevant robot debug info.
3 
4 Copyright 2016-2019 Fetch Robotics Inc.
5 Authors: Aaron Blasdel, Eric Relson
6 """
7 
8 import shutil
9 import os
10 import tempfile
11 import subprocess
12 import datetime
13 
14 from ..util import ssh, add_robot
15 
16 name = "debug-snapshot"
17 help_text = "Take a debug snapshot of a running robot"
18 
19 snapshot_version = "0.0.2"
20 
21 fileroot = "debug_snapshot"
22 fileroot = '_' + fileroot + '_'
23 
24 bagname = 'robot.bag'
25 
26 # all ros command must source setup.bash
27 rosbash = 'source /opt/ros/melodic/setup.bash;'
28 devnull = open(os.devnull, 'w+')
29 
30 topics = ["/robot_state",
31  "/diagnostics",
32  "/diagnostics_agg",
33  "/battery_state"]
34 
35 # All commands requiring sudo must be added in main
36 commands = {"dpkg_fetch": "COLUMNS=200 dpkg -l ros-melodic-fetch-*",
37  "dpkg_ros": "COLUMNS=200 dpkg -l ros-melodic-*",
38  "dpkg_all": "COLUMNS=200 dpkg -l",
39  "lsusb": "lsusb -v",
40  "lspci": "lspci -vv",
41  "roswtf": rosbash + "roswtf",
42  "rosnode_list": rosbash + "rosnode list",
43  "rostopic_list": rosbash + "rostopic list",
44  "rossrv_list": rosbash + "rosservice list",
45  "rosparam_list": rosbash + "rosparam list",
46  "rosparam_dump": rosbash + "rosparam get /",
47  "dmesg": "dmesg",
48  "syslog": "cat /var/log/syslog",
49  "date": "date",
50  "meminfo": "cat /proc/meminfo",
51  "ip_route": "ip route",
52  "ifconfig": "ifconfig -a",
53  "iwconfig": "iwconfig",
54  "network_interfaces": "cat /etc/network/interfaces",
55  "netplan_full": "cat /etc/netplan/01-network-manager-all.yaml /etc/netplan/99-fetch-ethernet.yaml",
56  "hosts": "cat /etc/hosts",
57  "env": "env",
58  "roscore.service": "cat /lib/systemd/system/roscore.service",
59  "roscore.service_status": "service roscore status",
60  "robot.service": "cat /lib/systemd/system/robot.service",
61  "robot.service_status": "service robot status",
62  "robot.journalctl": "journalctl -u robot",
63  "ps3joy.service": "cat /lib/systemd/system/ps3joy.service",
64  "ps3joy.service_status": "service ps3joy status",
65  "ps4joy.service": "cat /lib/systemd/system/ps4joy.service",
66  "ps4joy.service_status": "service ps4joy status",
67  "read_board_charger": rosbash + "rosrun fetch_drivers read_board 0x3f",
68  "read_board_mainboard": rosbash + "rosrun fetch_drivers read_board 0x00",
69  "read_board_wheel_left": rosbash + "rosrun fetch_drivers read_board 0x11",
70  "read_board_wheel_right": rosbash + "rosrun fetch_drivers read_board 0x12",
71  "read_board_gripper": rosbash + "rosrun fetch_drivers read_board 0x80",
72  "battery_state": rosbash + "rostopic echo -n 1 /battery_state",
73  "robot_state": rosbash + "rostopic echo -n 1 /robot_state",
74  "top": "top -n 1 -b",
75  }
76 
77 
78 def main(args):
79  # all commands requiring sudo must prepend this
80  sudostr = "echo %s | sudo -S " % args.fetch_password[-1]
81 
82  commands.update({"robot_log":sudostr + "cat /var/log/ros/robot.log"})
83 
84  print('Running debug snapshot tool.')
85  dirpath = tempfile.mkdtemp()
86 
87  # Start bag recording
88  bag_process = ['rosbag', 'record', '-q', '--duration=10', '-j',
89  '-O', bagname] + topics
90  bag = subprocess.Popen(bag_process, cwd=dirpath, stdout=devnull)
91 
92  # Record version file
93  with open(dirpath + '/version.txt', 'w') as version_file:
94  version_file.write(snapshot_version)
95 
96  # Execute remote commands on robot
97  for key, value in commands.iteritems():
98  print("Creating '%s.txt'." % key)
99  ssh('fetch', args.robot, value, fname=dirpath + '/' + key,
100  password=args.fetch_password[-1])
101 
102  print('Waiting for data gathering to complete')
103 
104  # Wait for the rosbag record parent process to exit cleanly
105  bag.wait()
106 
107  # Zip directory
108  timestr = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
109  filename = args.robot + fileroot + timestr + '.zip'
110 
111  print('Data gathering complete creating %s' % filename)
112  proc = subprocess.Popen(["zip", "-r", filename, dirpath],
113  stdout=devnull,
114  stderr=devnull)
115  proc.wait()
116 
117  # remove temp directory
118  shutil.rmtree(dirpath)
119 
120  if proc.returncode == 0:
121  print("Created %s" % filename)
122  else:
123  print("ERROR: failed to zip directory: %s" % dirpath)
124 
125 def add_arguments(parser):
126  parser.add_argument(
127  "--fetch-password", nargs="?", action="append", default=["robotics"],
128  help="Password for the fetch account (or blank to prompt)"
129  )
130  add_robot(parser)
def ssh(user, host, command, password=None, fname=None)
Definition: util.py:12
def add_robot(parser)
Definition: util.py:59


fetch_tools
Author(s): Alex Henning
autogenerated on Mon Feb 28 2022 22:19:10