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