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