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