310d921de92a9c89559469ec9b9e7f7115d9190a
[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         if [ $usevserver = yes ]
84         then
85                 nodpkg="lost+found|ARCHIVES"
86                 info "vserver root directory set to: $VROOTDIR"
87                 for vserver in $found_vservers
88                 do
89                         info "examining vserver: $vserver"
90                         running=`$VSERVERINFO $vserver RUNNING`
91                         if [ "$running" = "1" ]; then
92                            if [ ! -x "$VROOTDIR/$vserver`$VSERVER $vserver exec which $packagemgr`" ]; then
93                               warning "can't find $packagemgr in vserver $vserver, skipping installed packages report."
94                               nodpkg="$nodpkg|$vserver"
95                            fi
96                         else
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          # 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 noglog
119       done
120    fi
121    
122 # We want to perform this on the host as well
123    if [ "$packages" == "yes" ]; then
124       # don't expand * since it can be used in $packagemgroptions
125       set -o noglob
126       debug "$packagemgr $packagemgroptions > $packagesfile"
127       $packagemgr $packagemgroptions > $packagesfile || fatal "can not save $packagemgr info to $packagesfile"
128       set +o noglob
129    fi
130 fi
131
132 ## System report ##############################
133
134 #
135 # here we grab a bunch of system stuff for a report
136 #
137
138 export STATUS
139
140 HASHES="#################################################################"
141 DASHES="-----------------------------------------------------------------"
142
143 cat /dev/null > $sysreportfile || fatal "can not write to $sysreportfile"
144
145
146 catiffile () {
147    echo $HASHES >> $sysreportfile
148    echo "# $STATUS" >> $sysreportfile
149    echo $HASHES >> $sysreportfile
150    if [ -f $1 ]; then
151       echo "file: $1" >> $sysreportfile
152       echo $DASHES >> $sysreportfile
153       cat $1 >> $sysreportfile 2>&1 || info "reading of $1 failed"
154    fi
155    if [ -d $1 ]; then
156       echo "directory: $1" >> $sysreportfile
157       echo $DASHES >> $sysreportfile
158       for file in `find $1 -maxdepth 3 -noleaf -type f`
159       do
160        catiffile $file
161       done
162    fi
163    echo $DASHES >> $sysreportfile
164
165
166 catifexec () {
167    echo $HASHES >> $sysreportfile
168    echo "# $STATUS" >> $sysreportfile
169    echo $HASHES >> $sysreportfile
170    $1  >> $sysreportfile 2>&1 || info "executing of $1 failed"
171 }
172    
173
174 STATUS="Determining $os version:"
175 catiffile $osversion
176
177 STATUS="Determinding your current hostname: " 
178 catifexec "/bin/hostname"
179
180 STATUS="Getting the date:"
181 catifexec "/bin/date"
182
183 STATUS="Checking your systems current uptime and load average:"
184 catifexec "/usr/bin/uptime"
185
186 STATUS="Checking available memory:"
187 catifexec "/usr/bin/free"
188
189 STATUS="Checking free disk space:"
190 catifexec "/bin/df" "-al"
191
192 STATUS="Collecting what services run at what run level:"
193 if [ $os = "redhat" ]; then
194    catifexec "/sbin/chkconfig --list"
195    STATUS="Collecting information about /etc/rc.d:"
196    catiffile "/bin/ls /etc/rc.d/rc*.d/"
197
198 elif [ $os = "debian" ]; then
199     for level in 0 1 2 3 4 5 6 S; do
200        echo "Level: $level" >> $sysreportfile
201        for f in /etc/rc${level}.d/*; do
202         # Remove /etc/Knn or Snn from beginning
203           ff=$(echo $f | sed 's_/etc/rc..d/[KS][0-9][0-9]__')
204           if [ $f != $ff ]; then
205              echo $ff >> $sysreportfile
206           fi
207        done
208        echo "" >> $sysreportfile
209     done
210 fi
211
212 STATUS="Getting bootloader information:"
213 catifexec "/bin/ls -alR /boot"
214
215 # This covers sparc, alpha, and intel (respectively)
216 # updated for grub -mpg
217 if [ -f /etc/silo.conf ]; then
218   STATUS="Collecting information about the boot process (silo):"
219   catiffile "/etc/silo.conf"
220 fi
221 if [ -f /etc/milo.conf ]; then
222   STATUS="Collecting information about the boot process (milo):"
223   catiffile "/etc/milo.conf"
224 fi
225 if [ -f /etc/lilo.conf ]; then
226   STATUS="Collecting information about the boot process (lilo):"
227   catiffile "/etc/lilo.conf"
228   catifexec "/sbin/lilo -q"
229 fi
230 if [ -d /boot/grub -a -f /boot/grub/grub.conf -a -f /boot/grub/device.map ]; then
231   STATUS="Collecting information about the boot process (grub.conf):"
232   catiffile "/boot/grub/grub.conf"
233   STATUS="Collecting information about the boot process (grub.map):"
234   catiffile "/boot/grub/device.map"
235 fi
236 if [ -f /etc/cluster.conf -o -f /etc/cluster.xml ] ; then
237   STATUS="Gathering information on cluster setup"
238   # 2.1 AS
239   if [ -f /etc/cluster.conf ] ; then
240     catiffile "/etc/cluster.conf"
241   fi
242   # Taroon
243   if [ -f /etc/cluster.xml ] ; then
244     catiffile "/etc/cluster.xml"
245   fi
246 fi
247
248 STATUS="Gathering sysctl information (sysctl -a):"
249 catiffile "sysctl -a 2>/dev/null"
250 STATUS="Gathering sysctl information (/etc/sysctl.conf):"
251 catiffile "/etc/sysctl.conf"
252
253 STATUS="Gathering IP information (/sbin/ifconfig):"
254 catifexec "/sbin/ifconfig -a"
255
256 STATUS="Gathering additional IP information (/bin/ip addr list):"
257 catifexec "/bin/ip addr list"
258
259 STATUS="Checking network routes:"
260 catifexec "/sbin/route -n"
261
262 STATUS="Collecting Name Service Switch config information:"
263 catiffile "/etc/nsswitch.conf"
264
265 STATUS="Collecting information about system authentication (pam):"
266 catiffile "/etc/pam.conf"
267 catiffile "/etc/pam.d"
268
269 echo
270 echo "Getting information about the kernel."
271 echo
272 STATUS="Getting kernel version:"
273 catifexec "/bin/uname" "-a"
274 STATUS="Checking module information:"
275 catifexec "/sbin/lsmod"
276 for x  in $(/sbin/lsmod | /bin/cut -f1 -d" " 2>/dev/null | /bin/grep -v Module 2>/dev/null 
277 ) ; do
278   STATUS="Checking module information $x:"
279   catifexec "/sbin/modinfo  $x"
280 done
281
282 STATUS="Currently getting ksysms information:"
283 catifexec "/sbin/ksyms"
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 ksysms:"
295 catiffile "/proc/ksyms"
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 [ ! -x "$SFDISK" ]; then
435                 warning "can't find sfdisk, skipping sfdisk report."
436                 partitions="no"
437         fi
438         if [ ! -x "$HWINFO" ]; then
439                 warning "can't find hwinfo, skipping partition report."
440                 partitions="no"
441         fi
442 fi
443
444 if [ "$hardware" == "yes" ]; then
445         if [ ! -x "$HWINFO" ]; then
446                 warning "can't find hwinfo, skipping hardware report."
447                 hardware="no"
448         fi
449 fi
450
451
452 ## PARTITIONS #############################
453
454 # here we use sfdisk to dump a listing of all the partitions. 
455 # these files can be used to directly partition a disk of the same size.
456
457 if [ "$partitions" == "yes" ]; then
458         devices=`$SFDISK -l | grep "^Disk /dev" | cut -d/  -f2,3 | cut -d: -f1`
459         if [ "$devices" == "" ]; then 
460            warning "No harddisks found" 
461         fi
462         for dev in $devices; do
463                 [ -b $dev ] || continue
464                 label=${dev#/dev/}
465                 label=${label//\//-}
466                 outputfile=${partitionsfile//__star__/$label}
467                 debug "$SFDISK $sfdisk_options -d /$dev > $outputfile"
468                 $SFDISK $sfdisk_options -d /$dev > $outputfile
469         done
470 fi
471
472 ## HARDWARE #############################
473
474 #
475 # here we use hwinfo to dump a table listing all the
476 # information we can find on the hardware of this machine
477
478
479 if [ "$hardware" == "yes" ]; then
480         if [ -f $hardwarefile ]; then
481                 rm $hardwarefile
482         fi
483         touch $hardwarefile
484         echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
485         debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
486         $HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
487         for flag in cpu network disk bios pci; do
488                 echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
489                 $HWINFO --$flag >> $hardwarefile
490         done
491 fi