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