#!/bin/bash
# $Copyright$
# Copyright (C) Fujitsu Technology Solutions 2009, 2010, 2011
# All rights reserved
#
# $Copyright$
# Copyright (C) Fujitsu Siemens Computers GmbH 2008
# All rights reserved
#
# Exit status codes (see LSB 3 spec, ch. 20) are :
# 0     OK
# 1     generic or unspecified error
# 2     invalid or excess argument(s)
# 3     unimplemented feature (e.g. "reload")
# 4     user had insufficient privileges
# 5     program is not installed
# 6     program is not configured
# 7     program is not running
# 8+    reserved (see LSB Core Spec 3.1, ch. 20)

# Set up a decent path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH

if [ $# -lt 1 ]; then
    echo "Usage: $0 <XML output file>"
    exit 2
fi

# Set up library path for XalanTransform program.
# LD_LIBRARY_PATH="/etc/srvmagt/lib"
# export LD_LIBRARY_PATH

# Use LSB function library for convenience.

[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions

# Source ServerView function library

. /etc/srvmagt/functions
. /etc/srvmagt/config
SetSvEnv

# check if ServerView RAID is installed
if [ -f /etc/init.d/aurad ]; then
	SV_RAID_EXISTS=1
#	SV_RAID_READY=unknown
else
	SV_RAID_EXISTS=0
#	SV_RAID_READY=0
fi
export SV_RAID_EXISTS
# export SV_RAID_READY

umask 0066

# Get information from /var/log/messages about SNMP traps sent 
# and store it in /var/srvmagt/PCSysScan-traps for later evaluation by PCSysScan
fgrep "Serverview: [" /var/log/messages >/var/srvmagt/PCSysScan-traps 2>/dev/null 

# Without the service eecd HW monitoring is impossible.
# Wait until eecd sockets are ready. Then give eecd another grace period
# to complete init. 

pid=$(mypidof "/usr/sbin/eecd") || {
    mylog_failure_msg "Starting PCSysScan: service eecd is not running!"
    exit 1
}

count=${WaitForEecdSockets:=300}    # If <100 adjust \b's below
echo -e "Waiting for eecd sockets ...\c"
while [ $count -gt 0 ]; do
    [ -S /dev/eecda -a -S /dev/eecds ] && break
    sleep 1
    count=$(($count - 1))
    if [ $count -ge 100 ]; then
        echo -e "\b\b\b$count\c"
    elif [ $count -ge 10 ]; then
        echo -e "\b\b\b $count\c"
    else
        echo -e "\b\b\b  $count\c"
    fi
done

[ $count -eq 0 ] && {
    echo " giving up after $WaitForEecdSockets seconds!"
    mylog_failure_msg "Starting PCSysScan: eecd sockets do not exist!"
    exit 1
}
echo -e "\b\b\bdone ($count)"

# wait4eecd

echo -e "check if eecd is operative"
STIME=`/bin/date +%s`
/usr/sbin/eecdcp -c oc=e204 >/dev/null
ETIME=`/bin/date +%s`
TIME=`expr $ETIME - $STIME`
echo -e "check done after $TIME seconds"

if [ $TIME -gt 25 ]; then
    # eecd seems to be not operative
    exit 1
else
    rm -f /var/log/srvmagt/log.PCSysScan  # remove Logfile first

    /etc/srvmagt/PCSysScan -xmlreport $1.xml >/var/log/srvmagt/log.PCSysScan 2>&1 </dev/null

    rm -f /var/srvmagt/PCSysScan-traps
fi

exit 0

# EOF
