#!/bin/bash
#
# basecheck
#
# Copyright (c) 2010 SUSE Linux GmbH, Germany. All rights reserved.
# GNU Public Lincense.
#
# LP 20100615 16:00
#

ERR=/dev/null
TMP=/tmp/basecheck.$RANDOM

function echo_msgsep ()
{
   echo "======================================================== ${1:0:14} ==="
}

mkdir $TMP

#
echo_msgsep "Linux"
#
uname -a
echo -n "tainted = "; cat /proc/sys/kernel/tainted
rpm -V kernel-smp
SPident | awk '$1=="found" {print $2,$3,$4,$5}; $1=="CONCLUSION:" {print}'

F="kernel-smp multipath-tools sysstat supportutils"
for f in $F; do
        rpm -q --queryformat "%{name} %{version} %{release} %{arch} %{distribution}" $f
        echo
done

#
echo_msgsep "sysctl"
#
F="/proc/sys/vm/dirty_expire_centisecs /proc/sys/vm/dirty_background_ratio /proc/sys/vm/dirty_ratio /proc/sys/vm/swappiness /proc/sys/vm/overcommit_memory"
for f in $F; do
        echo -n "$f = "
        cat $f
done

#
echo_msgsep "scheduler"
#
F="scheduler nr_requests"
for f in $F; do
        find /sys/{block,devices}/ -name $f | \
        while read; do echo -n "$f = "; cat $REPLY; done
        #
        find /sys/{block,devices}/ -name $f >${TMP}/$f
done
echo -n "deadline = "
grep -c "[deadline]" ${TMP}/scheduler
echo -n "NOT deadline = "
grep -vc "[deadline]" ${TMP}/scheduler

#
echo_msgsep "mounts"
#
cat /proc/mounts

#
echo_msgsep "multipath"
multipath -ll >${TMP}/multipath
cat ${TMP}/multipath
echo -n "luns: "
grep -c ") dm-.*," ${TMP}/multipath
echo -n "active,ready paths: "
grep -c "^..\\_.*\[active\]\[ready\]$" ${TMP}/multipath
echo -n "NOT active,ready paths: "
grep "^..\\_.*\[.*]\[.*]" ${TMP}/multipath | grep -vc "\[active\]\[ready\]"

#
echo_msgsep "lvm"
#
for c in vgs pvs; do
        $c 2>&1 | grep "Found.duplicate"
        $c 2>&1 | grep "not.found"
done

F=/etc/lvm/lvm.conf
echo $F
# TODO: regexp '^[:space:]*filter'
grep '[:space:]*filter' $F | grep -v '#.*filter'

#
echo_msgsep "ocfs2"
#
# TODO: omit non-ocfs2
F="/dev/disk/by-id/*-3600*-part?"
for f in $F; do
        echo -n ${f}
        tunefs.ocfs2 -Q "\tbs=%B\tcs=%T\n" $f 2>>$ERR
done >${TMP}/ocfs2
#TODO: fschk.ocfs2 -n 
#cat ${TMP}/ocfs2
awk '   {a++; print $0};
        $2=="bs=4096" {b++};
        $3=="cs=1048576" {c++};
END{print "volumes: "a,"\nNOT cs 1M: "a-c,"\nNOT bs 4k: "a-b}' ${TMP}/ocfs2
#

#
echo_msgsep "config_files"
#
F="/boot/grub/menu.lst /etc/modprobe.conf.local /etc/init.d/boot.local /etc/hosts /etc/ntp.conf /etc/ocfs2/cluster.conf /etc/sysconfig/o2cb /etc/multipath.conf /etc/fstab /etc/sysconfig/network/ifcfg-bond?"
for f in $F; do
        echo $f
        grep -v '^[:space:]*#' $f | tr -s "\n"
        echo
done

#
echo_msgsep "chkconfig"
#
Y="acpid powersaved irqbalance ntp multipathd boot.multipath sysstat orarun o2cb ocfs2"
N="alsasound auditd slpd novell-zmd suseRegister"

for f in $Y; do
        echo -n "on = "
        chkconfig | grep "$f"
        echo
done

for f in $N; do
        echo -n "off = "
        chkconfig | grep "$f"
        echo
done
# TODO: calcutate sum for on/off expected on/off found


F=/etc/inittab
echo $F
grep "id:.:initdefault:" $F

#
echo_msgsep "messages"
#
# TODO: refine patterns
F=""multi.*path.*down" "bond.*link.*down" "lpfs.*err" "target.failure" "duplicate.VG" "duplicate.PV" "not.found" "ocfs2.*ERR" "ocfs2.*not.unmounted.cleanly" "reservation.conflict""
for f in $F; do
        echo -n "$f = "
        grep $f /var/log/messages | wc -l
done

echo
last | grep boot

#
echo_msgsep "network"
#
ip a s | grep inet.*bond.$
ip a s | grep master.*bond
route -n

ntpq -c peers | tr "=" "_"
echo
# TODO: ping ocfs2 heartbeat
# TODO: ping RAC heartbeat

#
echo_msgsep "oracle_rac_crs-version"
#
crsctl query crs softwareversion; query crs activeversion

rm -rf $TMP
#
