sys: start to fix silly output display and data loss
[matthijs/upstream/backupninja.git] / handlers / sys
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # this handler will save various reports of vital system information.
4 # by default, all the reports are enabled and are saved in /var/backups.
5 #
6 # (1) a list of all the packages installed and removed.
7 #     this file can be used to restore the state of installed packages
8 #     by running "dpkg --set-selections < dpkg-selections.txt
9
10 # (2) the partition table of all disks. 
11 #     this partition table can be used to format another disk of
12 #     the same size. this can be handy if using software raid and 
13 #     you have a disk go bad. just replace the disk and partition it
14 #     by running "sfdisk /dev/sdb < partitions.sdb.txt"
15 #     (MAKE SURE YOU PARTITION THE CORRECT DISK!!!)
16 #
17 # (3) hardware information. 
18 #     write to a text file the important things which hwinfo can gleen.
19 #
20
21 if [ -f /etc/debian_version ]
22 then
23    os=debian
24    debug "Debian detected"
25    osversion="/etc/debian_version"
26 elif [ -f /etc/redhat-release ]
27 then
28    os=redhat
29    debug "Redhat detected"
30    osversion="/etc/redhat-release"
31 else
32    warning "Unknown OS detected!"
33 fi
34
35 getconf packages yes
36 if [ $os = "debian" ]
37 then
38    getconf packagesfile /var/backups/dpkg-selections.txt
39    getconf packagemgr   `which dpkg`
40    getconf packagemgroptions ' --get-selections *'
41 elif [ $os = "redhat" ]
42 then
43    getconf packagesfile  /var/backups/rpmpackages.txt 
44    getconf packagemgr   `which rpm`
45    getconf packagemgroptions    ' -qa '
46
47    getconf SYSREPORT `which sysreport`
48    getconf sysreport_options ' -norpm '
49 else
50    getconf packagesfile /var/backups/unknownOS.txt
51 fi
52 packagemgroptions="${packagemgroptions//__star__/*}"
53
54 getconf partitions yes
55 getconf partitionsfile /var/backups/partitions.__star__.txt
56
57 getconf hardware yes
58 getconf hardwarefile /var/backups/hardware.txt
59
60 getconf sysreport yes
61 getconf sysreportfile /var/backups/sysreport.txt
62
63 getconf SFDISK `which sfdisk`
64 getconf HWINFO `which hwinfo`
65 getconf sfdisk_options ""
66 getconf hwinfo_options ""
67
68 # See if vservers are configured
69 local usevserver=no
70 if [ $vservers_are_available = yes ]
71 then
72    info "vserver method enabled"
73    usevserver=yes
74 fi
75
76 ## PACKAGES ##############################
77
78 #
79 # here we grab a list of the packages installed and removed.
80 #
81
82 if [ "$packages" == "yes" ]; then
83
84    if [ $usevserver = yes ]; then
85       info "vserver root directory set to: $VROOTDIR"
86       for vserver in $found_vservers; do
87          info "examining vserver: $vserver"
88          # is it running ?
89          $VSERVERINFO -q $vserver RUNNING
90          if [ $? -ne 0 ]; then
91             warning "The vserver $vserver is not running."
92             continue
93          fi
94          # is $packagemgr available inside $vserver ?
95          if [ ! -x "$VROOTDIR/$vserver`$VSERVER $vserver exec which $packagemgr`" ]; then
96             warning "can't find $packagemgr in vserver $vserver, skipping installed packages report."
97             continue
98          fi
99          # don't expand * since it can be used in $packagemgroptions
100          set -o noglob
101          debug "$VSERVER $vserver exec $packagemgr $packagemgroptions > $VROOTDIR/$vserver$packagesfile"
102          $VSERVER $vserver exec $packagemgr $packagemgroptions > $VROOTDIR/$vserver$packagesfile || fatal "can not save $packagemgr info to $packagesfile"
103          set +o noglob
104       done
105    fi
106    
107    # We want to perform this on the host as well
108    if [ -z "$packagemgr" -o ! -x "$packagemgr" ]; then 
109       warning "can't find ${packagemgr}, skipping installed packages report."
110    else
111       # don't expand * since it can be used in $packagemgroptions
112       set -o noglob
113       debug "$packagemgr $packagemgroptions > $packagesfile"
114       $packagemgr $packagemgroptions > $packagesfile || fatal "can not save $packagemgr info to $packagesfile"
115       set +o noglob
116    fi
117
118 fi
119
120 ## System report ##############################
121
122 #
123 # here we grab a bunch of system stuff for a report
124 #
125
126 export STATUS
127
128 HASHES="#################################################################"
129 DASHES="-----------------------------------------------------------------"
130
131 cat /dev/null > $sysreportfile || fatal "can not write to $sysreportfile"
132
133
134 catiffile () {
135    echo $HASHES >> $sysreportfile
136    echo "# $STATUS" >> $sysreportfile
137    echo $HASHES >> $sysreportfile
138    if [ -f $1 ]; then
139       echo "file: $1" >> $sysreportfile
140       echo $DASHES >> $sysreportfile
141       cat $1 >> $sysreportfile 2>&1 || info "reading of $1 failed"
142    fi
143    if [ -d $1 ]; then
144       echo "directory: $1" >> $sysreportfile
145       echo $DASHES >> $sysreportfile
146       for file in `find $1 -maxdepth 3 -noleaf -type f`
147       do
148        catiffile $file
149       done
150    fi
151    echo $DASHES >> $sysreportfile
152
153
154 catifexec () {
155    echo $HASHES >> $sysreportfile
156    echo "# $STATUS" >> $sysreportfile
157    echo $HASHES >> $sysreportfile
158    $1  >> $sysreportfile 2>&1 || info "executing of $1 failed"
159 }
160    
161
162 STATUS="Determining $os version:"
163 catiffile $osversion
164
165 STATUS="Determinding your current hostname: " 
166 catifexec "/bin/hostname"
167
168 STATUS="Getting the date:"
169 catifexec "/bin/date"
170
171 STATUS="Checking your systems current uptime and load average:"
172 catifexec "/usr/bin/uptime"
173
174 STATUS="Checking available memory:"
175 catifexec "/usr/bin/free"
176
177 STATUS="Checking free disk space:"
178 catifexec "/bin/df" "-al"
179
180 STATUS="Collecting what services run at what run level:"
181 if [ $os = "redhat" ]; then
182    catifexec "/sbin/chkconfig --list"
183    STATUS="Collecting information about /etc/rc.d:"
184    catiffile "/bin/ls /etc/rc.d/rc*.d/"
185
186 elif [ $os = "debian" ]; then
187     for level in 0 1 2 3 4 5 6 S; do
188        echo "Level: $level" >> $sysreportfile
189        for f in /etc/rc${level}.d/*; do
190         # Remove /etc/Knn or Snn from beginning
191           ff=$(echo $f | sed 's_/etc/rc..d/[KS][0-9][0-9]__')
192           if [ $f != $ff ]; then
193              echo $ff >> $sysreportfile
194           fi
195        done
196        echo "" >> $sysreportfile
197     done
198 fi
199
200 STATUS="Getting bootloader information:"
201 catifexec "/bin/ls -alR /boot"
202
203 # This covers sparc, alpha, and intel (respectively)
204 # updated for grub -mpg
205 if [ -f /etc/silo.conf ]; then
206   STATUS="Collecting information about the boot process (silo):"
207   catiffile "/etc/silo.conf"
208 fi
209 if [ -f /etc/milo.conf ]; then
210   STATUS="Collecting information about the boot process (milo):"
211   catiffile "/etc/milo.conf"
212 fi
213 if [ -f /etc/lilo.conf ]; then
214   STATUS="Collecting information about the boot process (lilo):"
215   catiffile "/etc/lilo.conf"
216   catifexec "/sbin/lilo -q"
217 fi
218 if [ -d /boot/grub -a -f /boot/grub/grub.conf -a -f /boot/grub/device.map ]; then
219   STATUS="Collecting information about the boot process (grub.conf):"
220   catiffile "/boot/grub/grub.conf"
221   STATUS="Collecting information about the boot process (grub.map):"
222   catiffile "/boot/grub/device.map"
223 fi
224 if [ -f /etc/cluster.conf -o -f /etc/cluster.xml ] ; then
225   STATUS="Gathering information on cluster setup"
226   # 2.1 AS
227   if [ -f /etc/cluster.conf ] ; then
228     catiffile "/etc/cluster.conf"
229   fi
230   # Taroon
231   if [ -f /etc/cluster.xml ] ; then
232     catiffile "/etc/cluster.xml"
233   fi
234 fi
235
236 STATUS="Gathering sysctl information (sysctl -a):"
237 catiffile "sysctl -a 2>/dev/null"
238 STATUS="Gathering sysctl information (/etc/sysctl.conf):"
239 catiffile "/etc/sysctl.conf"
240
241 STATUS="Gathering IP information (/sbin/ifconfig):"
242 catifexec "/sbin/ifconfig -a"
243
244 STATUS="Gathering additional IP information (/bin/ip addr list):"
245 catifexec "/bin/ip addr list"
246
247 STATUS="Checking network routes:"
248 catifexec "/sbin/route -n"
249
250 STATUS="Collecting Name Service Switch config information:"
251 catiffile "/etc/nsswitch.conf"
252
253 STATUS="Collecting information about system authentication (pam):"
254 catiffile "/etc/pam.conf"
255 catiffile "/etc/pam.d"
256
257 echo
258 echo "Getting information about the kernel."
259 echo
260 STATUS="Getting kernel version:"
261 catifexec "/bin/uname" "-a"
262 STATUS="Checking module information:"
263 catifexec "/sbin/lsmod"
264 for x  in $(/sbin/lsmod | /bin/cut -f1 -d" " 2>/dev/null | /bin/grep -v Module 2>/dev/null 
265 ) ; do
266   STATUS="Checking module information $x:"
267   catifexec "/sbin/modinfo  $x"
268 done
269
270 STATUS="Currently getting ksysms information:"
271 catifexec "/sbin/ksyms"
272
273 STATUS="Gathering information about your filesystems:"
274 catiffile "/proc/filesystems"
275
276 STATUS="Gathering information about your system stat:"
277 catiffile "/proc/stat"
278
279 STATUS="Gathering information about your partitions:"
280 catiffile "/proc/partitions"
281
282 STATUS="Gathering information about your ksyms:"
283 catiffile "/proc/ksyms"
284
285 STATUS="Gathering information about slabinfo:"
286 catiffile "/proc/slabinfo"
287
288 # Added support to cover for the new modules.conf layout in Red Hat 7
289 STATUS="Collecting information regarding kernel modules"
290 VER=`uname -r`
291 catiffile "/lib/modules/$VER/modules.dep"
292 if [ -f /etc/conf.modules ]; then
293   STATUS="Collecting information regarding kernel modules (conf.modules)"
294   catiffile "/etc/conf.modules"
295 fi
296 if [ -f /etc/modules.conf ]; then
297   STATUS="Collecting information regarding kernel modules (modules.conf)"
298   catiffile "/etc/modules.conf"
299 fi
300 if [ -f /etc/modprobe.conf ]; then
301   STATUS="Collecting information regarding kernel modules (modeprobe.conf)"
302   catiffile "/etc/modprobe.conf"
303 fi
304
305 # dkms status
306 if [ -x /usr/sbin/dkms ] ; then
307    STATUS="Gathering current status of modules, versions and kernels (dkms):"
308   catifexec "/usr/sbin/dkms" "status"
309 fi
310
311 if [ -f /etc/sysconfig/isdncard ] ; then
312   STATUS="Gathering information about ISDN:"
313   catiffile "/etc/sysconfig/isdncard"
314 fi
315
316 STATUS="Collecting information from the proc directory:"
317 catiffile "/proc/pci"
318
319 STATUS="Getting kernel command line"
320 catiffile "/proc/cmdline"
321
322 STATUS="Gathering information about your CPU:"
323 catiffile "/proc/cpuinfo"
324
325 STATUS="Gathering information about your Ram:"
326 catiffile "/proc/meminfo"
327
328 STATUS="Gathering information about your ioports:"
329 catiffile "/proc/ioports"
330
331 STATUS="Gathering information about your interrupts:"
332 catiffile "/proc/interrupts"
333
334 STATUS="Gathering information about your scsi devices:"
335 catiffile "/proc/scsi"
336
337 STATUS="Gathering information about your dma:"
338 catiffile "/proc/dma"
339
340 STATUS="Gathering information about your devices (/proc/devices):"
341 catiffile "/proc/devices"
342
343 STATUS="Gathering information about your rtc:"
344 catiffile "/proc/rtc"
345
346 STATUS="Gathering information about your ide drivers:"
347 catiffile "/proc/ide"
348
349 STATUS="Gathering information about your bus:"
350 catifexec lspci
351 catiffile "/proc/bus"
352
353 echo
354 echo "Getting disk and filesystem information."
355 echo
356
357 STATUS="Collecting information from /etc/fstab:"
358 catiffile "/etc/fstab"
359
360 STATUS="Collecting disk partition information:"
361 catifexec "fdisk -l"
362
363 STATUS="Checking mounted file systems (mount) "
364 catifexec "/bin/mount"
365
366 STATUS="Checking mounted file systems (/proc/mounts)"
367 catiffile "/proc/mounts"
368
369 STATUS="Collecting Software RAID information (/proc/mdstat)"
370 catiffile "/proc/mdstat"
371
372 STATUS="Collecting Software RAID information (/etc/raidtab)"
373 catiffile "/etc/raidtab"
374
375 STATUS="Collecting Software RAID information (/etc/mdadm.conf)"
376 catiffile "/etc/mdadm.conf"
377
378 STATUS="Collecting Automount information (auto.master)"
379 catiffile "/etc/auto.master"
380
381 STATUS="Collecting Automount information (auto.misc)"
382 catiffile "/etc/auto.misc"
383
384 STATUS="Collecting Automount information (auto.net)"
385 catiffile "/etc/auto.net"
386
387 STATUS="Collecting LVM information:"
388 if [ $os = "redhat" ]; then
389    catifexec "/usr/sbin/vgdisplay" "-vv"
390 elif [ $os = "debian" ]; then
391    catifexec "/sbin/vgdisplay" "-vv"
392 fi
393    
394 STATUS="Collecting SCSI Tape information (/etc/stinit.def)"
395 catiffile "/etc/stinit.def"
396
397 if [ -x /sbin/lsusb ] ; then
398   STATUS="Collecting USB devices list (lsusb):"
399   catifexec "/sbin/lsusb"
400 fi
401
402 if [ -x /usr/bin/lshal ] ; then
403   STATUS="Collecting global devices list (lshal):"
404   catifexec "/usr/bin/lshal"
405 fi
406
407
408 STATUS="Gathering information on SELinux setup"
409 catifexec "/usr/bin/selinuxconfig"
410 catifexec "/usr/sbin/sestatus"
411 if [ $os = "redhat" ]; then
412    catifexec "rpm" "-q -V selinux-policy-targeted"
413    catifexec "rpm" "-q -V selinux-policy-strict"
414 fi
415
416 if [ $usevserver = yes ]; then
417    STATUS="Gathering vserver information"
418    catiffile "/proc/virtual"
419 fi
420
421 if [ "$partitions" == "yes" ]; then
422         if [ ! -x "$SFDISK" ]; then
423                 warning "can't find sfdisk, skipping sfdisk report."
424                 partitions="no"
425         fi
426         if [ ! -x "$HWINFO" ]; then
427                 warning "can't find hwinfo, skipping partition report."
428                 partitions="no"
429         fi
430 fi
431
432 if [ "$hardware" == "yes" ]; then
433         if [ ! -x "$HWINFO" ]; then
434                 warning "can't find hwinfo, skipping hardware report."
435                 hardware="no"
436         fi
437 fi
438
439
440 ## PARTITIONS #############################
441
442 # here we use sfdisk to dump a listing of all the partitions. 
443 # these files can be used to directly partition a disk of the same size.
444
445 if [ "$partitions" == "yes" ]; then
446         devices=`$SFDISK -l 2>/dev/null | grep "^Disk /dev" | awk '{print $2}' | cut -d: -f1`
447         debug "$SFDISK will try to backup partition tables for devices $devices"
448         if [ "$devices" == "" ]; then 
449            warning "No harddisks found" 
450         fi
451         for dev in $devices; do
452                 debug "$SFDISK will try to backup partition tables for device $dev"
453                 [ -b $dev ] || continue
454                 label=${dev#/dev/}
455                 label=${label//\//-}
456                 outputfile=${partitionsfile//__star__/$label}
457                 debug "$SFDISK $sfdisk_options -d $dev > $outputfile"
458                 $SFDISK $sfdisk_options -d $dev > $outputfile
459         done
460 fi
461
462 ## HARDWARE #############################
463
464 #
465 # here we use hwinfo to dump a table listing all the
466 # information we can find on the hardware of this machine
467
468
469 if [ "$hardware" == "yes" ]; then
470         if [ -f $hardwarefile ]; then
471                 rm $hardwarefile
472         fi
473         touch $hardwarefile
474         echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
475         debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
476         $HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
477         for flag in cpu network disk bios pci; do
478                 echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
479                 $HWINFO --$flag >> $hardwarefile
480         done
481 fi