1 | #!/bin/bash |
---|
2 | ######################################################################## |
---|
3 | # # |
---|
4 | # This script creates a tarball containing all logs and necesary files # |
---|
5 | # in order to debug a remote system. Initially the tarball would be # |
---|
6 | # manually sent by the final user to the support team. On a second # |
---|
7 | # stage this support save would be inclued in the GUI. # |
---|
8 | # # |
---|
9 | # Autor: Fredy <aluque@soleta.eu> 2018 Q1 # |
---|
10 | # # |
---|
11 | ######################################################################## |
---|
12 | |
---|
13 | # Basic structure |
---|
14 | # Date, Hostname and Paths |
---|
15 | # List of desired files to be saved |
---|
16 | # Usefull system commands output to be saved (ie. uname -a > file.txt) |
---|
17 | # Final compression |
---|
18 | |
---|
19 | PATH=/bin:/sbin:/usr/bin:/usr/sbin |
---|
20 | |
---|
21 | if [ "$(whoami)" != "root" ]; then |
---|
22 | echo "ERROR: Need to be root." >&2 |
---|
23 | exit 1 |
---|
24 | fi |
---|
25 | |
---|
26 | tmp_name=`date +%Y%m%d_%H%M` |
---|
27 | hostname=`hostname` |
---|
28 | home_dir="/opt/opengnsys" |
---|
29 | ss_dir="supportsave_${hostname}_${tmp_name}" |
---|
30 | prefix="/tmp" |
---|
31 | |
---|
32 | if [ ! -d ${home_dir} ]; then |
---|
33 | echo "ERROR: The OpenGnsys directory does not exist." >&2 |
---|
34 | exit 1 |
---|
35 | fi |
---|
36 | |
---|
37 | if [ -d "$1" ]; then |
---|
38 | prefix=${1} |
---|
39 | fi |
---|
40 | |
---|
41 | backup_dir="${prefix}/${ss_dir}" |
---|
42 | |
---|
43 | config_paths="${home_dir}/etc ${home_dir}/tftpboot/menu.lst ${home_dir}/client/etc ${home_dir}/log /etc/default/opengnsys" |
---|
44 | other_paths="/var/log/syslog* /var/log/messages*" |
---|
45 | |
---|
46 | echo "Saving information for support in the path ${backup_dir}.tar.gz" |
---|
47 | mkdir -p $backup_dir |
---|
48 | |
---|
49 | |
---|
50 | echo "Saving system information:" |
---|
51 | ################################# |
---|
52 | |
---|
53 | echo "- System version" |
---|
54 | if which lsb_release &>/dev/null; then |
---|
55 | lsb_release -a >> $backup_dir/operating_system.txt 2>&1 |
---|
56 | elif [ -r /etc/os-release ]; then |
---|
57 | cat /etc/os-release >> $backup_dir/operating_system.txt 2>&1 |
---|
58 | elif [ -r /etc/system-release ]; then |
---|
59 | cat /etc/system-release >> $backup_dir/operating_system.txt 2>&1 |
---|
60 | fi |
---|
61 | |
---|
62 | echo "- Hardware" |
---|
63 | echo "--- hostname ---" >> $backup_dir/hardware.txt |
---|
64 | hostname >> $backup_dir/hardware.txt 2>&1 |
---|
65 | echo -e "\n--- dmidecode ---" >> $backup_dir/hardware.txt |
---|
66 | dmidecode >> $backup_dir/hardware.txt 2>&1 |
---|
67 | echo -e "\n--- lshw -short ---" >> $backup_dir/hardware.txt |
---|
68 | lshw -short >> $backup_dir/hardware.txt 2>&1 |
---|
69 | echo -e "\n--- lspci ---" >> $backup_dir/hardware.txt |
---|
70 | lspci >> $backup_dir/hardware.txt 2>&1 |
---|
71 | echo -e "\n--- lsusb ---" >> $backup_dir/hardware.txt |
---|
72 | lsusb >> $backup_dir/hardware.txt 2>&1 |
---|
73 | |
---|
74 | echo "- Kernel" |
---|
75 | echo "--- uname -a ---" >> $backup_dir/kernel.txt |
---|
76 | uname -a >> $backup_dir/kernel.txt 2>&1 |
---|
77 | echo -e "\n--- lsmod ---" >> $backup_dir/kernel.txt |
---|
78 | lsmod >> $backup_dir/kernel.txt 2>&1 |
---|
79 | echo "- Kernel boot messages" |
---|
80 | echo "--- dmesg ---" >> $backup_dir/kernel.txt |
---|
81 | dmesg >> $backup_dir/kernel.txt 2>&1 |
---|
82 | |
---|
83 | echo "- Packages" |
---|
84 | if [ -f /etc/debian_version ]; then |
---|
85 | echo "--- dpkg -l ---" >> $backup_dir/package_list.txt |
---|
86 | dpkg -l >> $backup_dir/package_list.txt 2>&1 |
---|
87 | elif [ -f /etc/redhat-release ]; then |
---|
88 | echo "--- rpm -qa ---" >> $backup_dir/package_list.txt |
---|
89 | rpm -qa | sort >> $backup_dir/package_list.txt 2>&1 |
---|
90 | else |
---|
91 | echo "- WARNING: The package list can not be retrieved" | tee $backup_dir/package_list.txt |
---|
92 | fi |
---|
93 | |
---|
94 | echo "- Processes" |
---|
95 | echo "ps aux" >> $backup_dir/ps.txt |
---|
96 | ps aux >> $backup_dir/ps.txt 2>&1 |
---|
97 | |
---|
98 | echo "- Resources" |
---|
99 | echo "--- Uptime information ---" >> $backup_dir/system_resources.txt |
---|
100 | uptime >> $backup_dir/system_resources.txt 2>&1 |
---|
101 | echo -e "\n--- Memory information ---" >> $backup_dir/system_resources.txt |
---|
102 | free -m >> $backup_dir/system_resources.txt 2>&1 |
---|
103 | echo -e "\n--- CPU information ---" >> $backup_dir/system_resources.txt |
---|
104 | cat /proc/cpuinfo >> $backup_dir/system_resources.txt 2>&1 |
---|
105 | echo -e "\n--- TOP information ---" >> $backup_dir/system_resources.txt |
---|
106 | top -b -n1 >> $backup_dir/system_resources.txt 2>&1 |
---|
107 | |
---|
108 | echo "- Filesystems" |
---|
109 | echo "--- cat /etc/fstab ---" >> $backup_dir/filesystems.txt |
---|
110 | cat /etc/fstab >> $backup_dir/filesystems.txt 2>&1 |
---|
111 | echo -e "\n--- df -h ---" >> $backup_dir/filesystems.txt |
---|
112 | df -h >> $backup_dir/filesystems.txt 2>&1 |
---|
113 | echo -e "\n--- blkid ---" >> $backup_dir/filesystems.txt |
---|
114 | blkid >> $backup_dir/filesystems.txt 2>&1 |
---|
115 | echo -e "\n--- lsblk -Jbp ---" >> $backup_dir/filesystems.txt |
---|
116 | lsblk -Jbp >> $backup_dir/filesystems.txt 2>&1 |
---|
117 | |
---|
118 | |
---|
119 | echo "Saving network information:" |
---|
120 | ################################## |
---|
121 | |
---|
122 | echo "- Interfaces" |
---|
123 | ifconfig -a >> $backup_dir/ifconfig.txt 2>&1 |
---|
124 | ip link show >> $backup_dir/ip_link.txt 2>&1 |
---|
125 | ip addr show >> $backup_dir/ip_addr.txt 2>&1 |
---|
126 | |
---|
127 | echo "- Routes" |
---|
128 | for i in `cat /etc/iproute2/rt_tables | grep "table_" | awk {'print $2'}` |
---|
129 | do |
---|
130 | echo "ip route list table $i" >> $backup_dir/route.txt |
---|
131 | ip route list table $i >> $backup_dir/route.txt 2>&1 |
---|
132 | done |
---|
133 | echo "ip route list table main" >> $backup_dir/route.txt |
---|
134 | ip route list table main >> $backup_dir/route.txt 2>&1 |
---|
135 | echo "ip rule list" >> $backup_dir/route.txt |
---|
136 | ip rule list >> $backup_dir/route.txt 2>&1 |
---|
137 | |
---|
138 | echo "- Sockets" |
---|
139 | echo "netstat -putan" >> $backup_dir/netstat.txt |
---|
140 | netstat -putan >> $backup_dir/netstat.txt 2>&1 |
---|
141 | echo "netstat -nr" >> $backup_dir/netstat.txt |
---|
142 | netstat -nr >> $backup_dir/netstat.txt 2>&1 |
---|
143 | |
---|
144 | echo "- Netfilter" |
---|
145 | echo "Filter table " >> $backup_dir/netfilter.txt |
---|
146 | iptables -nL -t filter >> $backup_dir/netfilter.txt 2>&1 |
---|
147 | echo -e "\nNAT table " >> $backup_dir/netfilter.txt |
---|
148 | iptables -nL -t nat >> $backup_dir/netfilter.txt 2>&1 |
---|
149 | echo -e "\nMangle table " >> $backup_dir/netfilter.txt |
---|
150 | iptables -nL -t mangle >> $backup_dir/netfilter.txt 2>&1 |
---|
151 | echo -e "\nRaw table " >> $backup_dir/netfilter.txt |
---|
152 | iptables -nL -t raw >> $backup_dir/netfilter.txt 2>&1 |
---|
153 | |
---|
154 | echo "- nf_conntrack" |
---|
155 | if which conntrack &>/dev/null; then |
---|
156 | conntrack -L >> $backup_dir/conntrack.txt 2>&1 |
---|
157 | fi |
---|
158 | |
---|
159 | echo "- ipset" |
---|
160 | if which ipset &>/dev/null; then |
---|
161 | ipset save >> $backup_dir/ipset_tables.txt 2>&1 |
---|
162 | fi |
---|
163 | |
---|
164 | echo "Saving OpenGnsys information:" |
---|
165 | ################################## |
---|
166 | |
---|
167 | echo "- OpenGnsys version" |
---|
168 | #echo `dpkg -l | grep opengnsys\ | awk '{print $3}'` > $backup_dir/opengnsys_version |
---|
169 | curl -ks --connect-timeout 10 https://localhost/opengnsys/rest/info | jq . > ${backup_dir}/opengnsys_version.txt 2>/dev/null |
---|
170 | if [ ! -s ${backup_dir}/opengnsys_version.txt ]; then |
---|
171 | cp -a ${home_dir}/doc/VERSION.txt ${backup_dir}/opengnsys_version.txt 2>&1 |
---|
172 | fi |
---|
173 | |
---|
174 | echo "- Directory list" |
---|
175 | ls -Ral ${home_dir} >> $backup_dir/opengnsys_files.txt 2>&1 |
---|
176 | |
---|
177 | if [ -r ${home_dir}/etc/ogAdmServer.cfg ]; then |
---|
178 | echo "- Database schema" |
---|
179 | source ${home_dir}/etc/ogAdmServer.cfg |
---|
180 | mysqldump -u "$USUARIO" -p"$PASSWORD" -d "$CATALOG" >> ${backup_dir}/opengnsys_schema.sql 2>&1 |
---|
181 | else |
---|
182 | echo "- WARNING: The OpenGnsys database can not be accessed" | tee ${backup_dir}/db_schema.txt |
---|
183 | fi |
---|
184 | |
---|
185 | echo "- Configuration and log files" |
---|
186 | # Looking for huge log files (> 1 MB). |
---|
187 | for log in $(find ${home_dir}/log -name "*.log" -size +1024 -print); do |
---|
188 | # Copying last 5000 lines and excluding file. |
---|
189 | tail -5000 ${log} > ${log}-tail5k 2>&1 |
---|
190 | config_paths="$config_paths --exclude=${log}" |
---|
191 | done |
---|
192 | tar zcf ${backup_dir}/opengnsys_config.tar.gz ${config_paths} 2>/dev/null |
---|
193 | |
---|
194 | echo "Saving other files" |
---|
195 | ############################## |
---|
196 | tar zcf ${backup_dir}/logs.tar.gz ${other_paths} 2>/dev/null |
---|
197 | |
---|
198 | echo "Packing supportsave" |
---|
199 | ########################## |
---|
200 | cd ${prefix} |
---|
201 | tar zcf ${ss_dir}.tar.gz ${ss_dir} 2>/dev/null |
---|
202 | cd - >/dev/null |
---|
203 | |
---|
204 | echo "Cleaning temporal files" |
---|
205 | ########################## |
---|
206 | rm -rf ${backup_dir} ${home_dir}/log/*-tail5k |
---|
207 | |
---|
208 | ls -lh ${backup_dir}.tar.gz |
---|