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