#!/bin/sh
# $Copyright$
# Copyright (C) Fujitsu Technology Solutions 2009
# All rights reserved
#
# $Copyright$
# Copyright (C) Fujitsu Siemens Computers GmbH 2006, 2007, 2008
# All rights reserved
#

# ----------------------------------------------------------------------
# Initialization
# ----------------------------------------------------------------------
# Check if function,awk etc. is in the current directory. Usually if
# running fron a shar archive

Functions="/etc/srvmagt/functions"
[ -f functions ] && Functions=functions
. $Functions

archiveAwk="/etc/srvmagt/archive.awk"
[ -f archive.awk ] && archiveAwk="archive.awk"
startOid="/etc/srvmagt/StartOids.ini"
[ -f StartOids.ini ] && startOid="StartOids.ini"
bladeStartOid="/etc/srvmagt/BladeStartOids.ini"
[ -f BladeStartOids.ini ] && bladeStartOid="BladeStartOids.ini"
parsIni="-v parsIni=/etc/srvmagt/parser.ini"
[ -f parser.ini ] && parsIni="-v parsIni=parser.ini"

archPath="/var/log/srvmagt/ARCHIVE"
ARCHIVE=`date +%Y%m%d%H%M%S`

quiet=0
pid=$$
snmpLog="/tmp/archive_$pid.log"
awkMsg="/tmp/archiveAwk.msg"
walkMsg="/tmp/archiveSnmpwalk.msg"
localhost="127.0.0.1"

USAGE="#### Usage:
    $0 [-c community -h]

    Options:
    c	community    specify the correct SNMP community
    h   display this message
    q   don\'t report snmpwalk failures to stdout\n"

# ----------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------

while getopts c:hdq optChar; do
	case "$optChar" in
	h|[?])
		printf "$USAGE"
		[ "$1" = "-h" ] && exit 0
		exit 2
		;;
	c)	SV_COMMUNITY=$OPTARG
		;;
	d)	awkDebug="-v debug=1"
		>/tmp/archiveDebug.txt
		;;
	q)	quiet=1
		;;
	esac
done
shift $(($OPTIND - 1))

# initialise logfiles and directories
> $snmpLog
> $awkMsg
> $walkMsg
mkdir -p $archPath

/etc/init.d/snmpd status >$snmpLog 2>&1 || {
	echo "No SNMP communication, snmp daemon not running (see also $snmpLog)"
	exit 1
}
# if we run on a server blade create the archive for MMB as well
ipMMB=$(eecdcp -e -c oc=e222 oe=0000 oi=00 2>/dev/null) && {
	ipMMB=$(makeDottedIPaddr "$ipMMB")
		
	HostComm=$(chkAndGetCommunity $ipMMB $snmpLog $SV_COMMUNITY)
	if [ $? -eq 0 ]; then
		hostName=${HostComm%:*}
		community=${HostComm#*:}
		archPath="$archPath/$hostName"
		mkdir -p $archPath
		for oid in `awk '/^[a-zA-Z].*=/ {print $NF} /^1\./ {print $1}' $bladeStartOid`; do
			snmpwalk -Cc -On -c $community -v 1 $ipMMB $oid 2>>$walkMsg | awk -f $archiveAwk $awkDebug $parsIni >>$archPath/$ARCHIVE 2>>$awkMsg
		done
		retMsg=$(printf "Archive for MMB %s written in file %s/%s\n" $hostName $archPath $ARCHIVE)
		
		# prepare the environment for sever blade archive
		archPath="$archPath/C01"
		mkdir -p $archPath
	else		
		printf "No SNMP communication with host %s, specify the correct community" $ipMMB >>$walkMsg
	fi
}
# create the archive for server and server blade
HostComm=$(chkAndGetCommunity $localhost $snmpLog $SV_COMMUNITY) && {
	hostName=${HostComm%:*}
	community=${HostComm#*:}
	for oid in `awk '/^[a-zA-Z].*=/ {print $NF} /^1\./ {print $1}' $startOid`; do
		snmpwalk -Cc -On -c $community -v 1 $localhost $oid 2>>$walkMsg | awk -f $archiveAwk $awkDebug $parsIni >>$archPath/$ARCHIVE 2>>$awkMsg
	done
}
 
[ $quiet -eq 1 ] || {
	[ -s "$walkMsg" ] && {
		echo "snmpwalk error messages:"
		cat $walkMsg
	}

	[ -s "$awkMsg" ] && {
		echo "awk error messages:"
		cat $awkMsg
	}
}

echo $retMsg
printf "Archive for Server %s written in file %s/%s\n" $hostName $archPath $ARCHIVE

[ -s "$walkMsg"  -o -s "$awkMsg" ] && exit 1
exit 0
