add the ability to save the debconf package selection states in the sys handler,...
[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 `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    echo $HASHES >> $sysreportfile
192    echo "# $STATUS" >> $sysreportfile
193    echo $HASHES >> $sysreportfile
194    $1  >> $sysreportfile 2>&1 || info "executing of $1 failed"
195 }
196    
197
198 STATUS="Determining $os version:"
199 catiffile $osversion
200
201 STATUS="Determinding your current hostname: " 
202 catifexec "/bin/hostname"
203
204 STATUS="Getting the date:"
205 catifexec "/bin/date"
206
207 STATUS="Checking your systems current uptime and load average:"
208 catifexec "/usr/bin/uptime"
209
210 STATUS="Checking available memory:"
211 catifexec "/usr/bin/free"
212
213 STATUS="Checking free disk space:"
214 catifexec "/bin/df" "-al"
215
216 STATUS="Collecting what services run at what run level:"
217 if [ $os = "redhat" ]; then
218    catifexec "/sbin/chkconfig --list"
219    STATUS="Collecting information about /etc/rc.d:"
220    catiffile "/bin/ls /etc/rc.d/rc*.d/"
221
222 elif [ $os = "debian" ]; then
223     for level in 0 1 2 3 4 5 6 S; do
224        echo "Level: $level" >> $sysreportfile
225        for f in /etc/rc${level}.d/*; do
226         # Remove /etc/Knn or Snn from beginning
227           ff=$(echo $f | @SED@ 's_/etc/rc..d/[KS][0-9][0-9]__')
228           if [ $f != $ff ]; then
229              echo $ff >> $sysreportfile
230           fi
231        done
232        echo "" >> $sysreportfile
233     done
234 fi
235
236 STATUS="Getting bootloader information:"
237 catifexec "/bin/ls -alR /boot"
238
239 # This covers sparc, alpha, and intel (respectively)
240 # updated for grub -mpg
241 if [ -f /etc/silo.conf ]; then
242   STATUS="Collecting information about the boot process (silo):"
243   catiffile "/etc/silo.conf"
244 fi
245 if [ -f /etc/milo.conf ]; then
246   STATUS="Collecting information about the boot process (milo):"
247   catiffile "/etc/milo.conf"
248 fi
249 if [ -f /etc/lilo.conf ]; then
250   STATUS="Collecting information about the boot process (lilo):"
251   catiffile "/etc/lilo.conf"
252   catifexec "/sbin/lilo -q"
253 fi
254 if [ -d /boot/grub -a -f /boot/grub/grub.conf -a -f /boot/grub/device.map ]; then
255   STATUS="Collecting information about the boot process (grub.conf):"
256   catiffile "/boot/grub/grub.conf"
257   STATUS="Collecting information about the boot process (grub.map):"
258   catiffile "/boot/grub/device.map"
259 fi
260 if [ -f /etc/cluster.conf -o -f /etc/cluster.xml ] ; then
261   STATUS="Gathering information on cluster setup"
262   # 2.1 AS
263   if [ -f /etc/cluster.conf ] ; then
264     catiffile "/etc/cluster.conf"
265   fi
266   # Taroon
267   if [ -f /etc/cluster.xml ] ; then
268     catiffile "/etc/cluster.xml"
269   fi
270 fi
271
272 STATUS="Gathering sysctl information (sysctl -a):"
273 catiffile "sysctl -a 2>/dev/null"
274 STATUS="Gathering sysctl information (/etc/sysctl.conf):"
275 catiffile "/etc/sysctl.conf"
276
277 STATUS="Gathering IP information (/sbin/ifconfig):"
278 catifexec "/sbin/ifconfig -a"
279
280 STATUS="Gathering additional IP information (/bin/ip addr list):"
281 catifexec "/bin/ip addr list"
282
283 STATUS="Checking network routes:"
284 catifexec "/sbin/route -n"
285
286 STATUS="Collecting Name Service Switch config information:"
287 catiffile "/etc/nsswitch.conf"
288
289 STATUS="Collecting information about system authentication (pam):"
290 catiffile "/etc/pam.conf"
291 catiffile "/etc/pam.d"
292
293 echo
294 echo "Getting information about the kernel."
295 echo
296 STATUS="Getting kernel version:"
297 catifexec "/bin/uname" "-a"
298 STATUS="Checking module information:"
299 catifexec "/sbin/lsmod"
300 for x  in $(/sbin/lsmod | /bin/cut -f1 -d" " 2>/dev/null | /bin/grep -v Module 2>/dev/null 
301 ) ; do
302   STATUS="Checking module information $x:"
303   catifexec "/sbin/modinfo  $x"
304 done
305
306 STATUS="Gathering information about your filesystems:"
307 catiffile "/proc/filesystems"
308
309 STATUS="Gathering information about your system stat:"
310 catiffile "/proc/stat"
311
312 STATUS="Gathering information about your partitions:"
313 catiffile "/proc/partitions"
314
315 STATUS="Gathering information about your ksyms:"
316 catiffile "/proc/kallsyms"
317
318 STATUS="Gathering information about slabinfo:"
319 catiffile "/proc/slabinfo"
320
321 # Added support to cover for the new modules.conf layout in Red Hat 7
322 STATUS="Collecting information regarding kernel modules"
323 VER=`uname -r`
324 catiffile "/lib/modules/$VER/modules.dep"
325 if [ -f /etc/conf.modules ]; then
326   STATUS="Collecting information regarding kernel modules (conf.modules)"
327   catiffile "/etc/conf.modules"
328 fi
329 if [ -f /etc/modules.conf ]; then
330   STATUS="Collecting information regarding kernel modules (modules.conf)"
331   catiffile "/etc/modules.conf"
332 fi
333 if [ -f /etc/modprobe.conf ]; then
334   STATUS="Collecting information regarding kernel modules (modeprobe.conf)"
335   catiffile "/etc/modprobe.conf"
336 fi
337
338 # dkms status
339 if [ -x /usr/sbin/dkms ] ; then
340    STATUS="Gathering current status of modules, versions and kernels (dkms):"
341   catifexec "/usr/sbin/dkms" "status"
342 fi
343
344 if [ -f /etc/sysconfig/isdncard ] ; then
345   STATUS="Gathering information about ISDN:"
346   catiffile "/etc/sysconfig/isdncard"
347 fi
348
349 STATUS="Collecting information from the proc directory:"
350 catiffile "/proc/pci"
351
352 STATUS="Getting kernel command line"
353 catiffile "/proc/cmdline"
354
355 STATUS="Gathering information about your CPU:"
356 catiffile "/proc/cpuinfo"
357
358 STATUS="Gathering information about your Ram:"
359 catiffile "/proc/meminfo"
360
361 STATUS="Gathering information about your ioports:"
362 catiffile "/proc/ioports"
363
364 STATUS="Gathering information about your interrupts:"
365 catiffile "/proc/interrupts"
366
367 STATUS="Gathering information about your scsi devices:"
368 catiffile "/proc/scsi"
369
370 STATUS="Gathering information about your dma:"
371 catiffile "/proc/dma"
372
373 STATUS="Gathering information about your devices (/proc/devices):"
374 catiffile "/proc/devices"
375
376 STATUS="Gathering information about your rtc:"
377 catiffile "/proc/rtc"
378
379 STATUS="Gathering information about your ide drivers:"
380 catiffile "/proc/ide"
381
382 STATUS="Gathering information about your bus:"
383 catifexec lspci
384 catiffile "/proc/bus"
385
386 echo
387 echo "Getting disk and filesystem information."
388 echo
389
390 STATUS="Collecting information from /etc/fstab:"
391 catiffile "/etc/fstab"
392
393 STATUS="Collecting disk partition information:"
394 catifexec "fdisk -l"
395
396 STATUS="Checking mounted file systems (mount) "
397 catifexec "/bin/mount"
398
399 STATUS="Checking mounted file systems (/proc/mounts)"
400 catiffile "/proc/mounts"
401
402 STATUS="Collecting Software RAID information (/proc/mdstat)"
403 catiffile "/proc/mdstat"
404
405 STATUS="Collecting Software RAID information (/etc/raidtab)"
406 catiffile "/etc/raidtab"
407
408 STATUS="Collecting Software RAID information (/etc/mdadm.conf)"
409 catiffile "/etc/mdadm.conf"
410
411 STATUS="Collecting Automount information (auto.master)"
412 catiffile "/etc/auto.master"
413
414 STATUS="Collecting Automount information (auto.misc)"
415 catiffile "/etc/auto.misc"
416
417 STATUS="Collecting Automount information (auto.net)"
418 catiffile "/etc/auto.net"
419
420 STATUS="Collecting LVM information:"
421 if [ $os = "redhat" ]; then
422    catifexec "/usr/sbin/vgdisplay" "-vv"
423 elif [ $os = "debian" ]; then
424    catifexec "/sbin/vgdisplay" "-vv"
425 fi
426    
427 STATUS="Collecting SCSI Tape information (/etc/stinit.def)"
428 catiffile "/etc/stinit.def"
429
430 if [ -x /sbin/lsusb ] ; then
431   STATUS="Collecting USB devices list (lsusb):"
432   catifexec "/sbin/lsusb"
433 fi
434
435 if [ -x /usr/bin/lshal ] ; then
436   STATUS="Collecting global devices list (lshal):"
437   catifexec "/usr/bin/lshal"
438 fi
439
440
441 STATUS="Gathering information on SELinux setup"
442 catifexec "/usr/bin/selinuxconfig"
443 catifexec "/usr/sbin/sestatus"
444 if [ $os = "redhat" ]; then
445    catifexec "rpm" "-q -V selinux-policy-targeted"
446    catifexec "rpm" "-q -V selinux-policy-strict"
447 fi
448
449 if [ $usevserver = yes ]; then
450    STATUS="Gathering vserver information"
451    catiffile "/proc/virtual"
452 fi
453
454 if [ "$partitions" == "yes" ]; then
455    if [ "$dosfdisk" == "yes" ]; then
456         if [ ! -x "$SFDISK" ]; then
457                 warning "can't find sfdisk, skipping sfdisk report."
458                 partitions="no"
459         fi
460    fi
461    if [ "$dohwinfo" == "yes" ]; then
462         if [ ! -x "$HWINFO" ]; then
463                 warning "can't find hwinfo, skipping partition report."
464                 partitions="no"
465         fi
466    fi
467 fi
468
469 if [ "$hardware" == "yes" ]; then
470         if [ ! -x "$HWINFO" ]; then
471                 warning "can't find hwinfo, skipping hardware report."
472                 hardware="no"
473         fi
474 fi
475
476 ## HARDWARE #############################
477
478 #
479 # here we use hwinfo to dump a table listing all the
480 # information we can find on the hardware of this machine
481
482
483 if [ "$hardware" == "yes" ]; then
484    if [ "dohwinfo" == "yes" ]; then
485       if [ -f $hardwarefile ]; then
486          rm $hardwarefile
487       fi
488       touch $hardwarefile
489       echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
490       debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
491       $HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
492       for flag in cpu network bios pci; do
493          echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
494          $HWINFO --$flag >> $hardwarefile
495       done
496    fi
497 fi
498
499
500 ## PARTITIONS #############################
501
502 # here we use sfdisk to dump a listing of all the partitions. 
503 # these files can be used to directly partition a disk of the same size.
504
505 if [ "$partitions" == "yes" ]; then
506    if [ "$dosfdisk" == "yes" ]; then
507       devices=`LC_ALL=C $SFDISK -l 2>/dev/null | grep "^Disk /dev" | @AWK@ '{print $2}' | cut -d: -f1`
508         if [ "$devices" == "" ]; then 
509            warning "No harddisks found" 
510         fi
511         for dev in $devices; do
512                 debug "$SFDISK will try to backup partition tables for device $dev"
513                 [ -b $dev ] || continue
514                 label=${dev#/dev/}
515                 label=${label//\//-}
516                 outputfile=${partitionsfile//__star__/$label}
517                 debug "$SFDISK $sfdisk_options -d $dev > $outputfile 2>/dev/null"
518                 $SFDISK $sfdisk_options -d $dev > $outputfile 2>/dev/null
519                 if [ $? -ne 0 ]; then
520                    warning "The partition table for $dev could not be saved."
521                 fi
522         done
523    fi
524    if [ "$dohwinfo" == "yes" ]; then
525       debug "Using $HWINFO to get all available disk information"
526       echo -e "\n\n====================== $disk ======================\n" >>  $hardwarefile
527       $HWINFO --disk >> $hardwarefile
528    fi
529 fi