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