#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2019 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# sccsid = "@(#)11   1.13   src/rsct/rm/ER/notifyevent, ERrm, rsct_rady, rady2035a 11/12/15 16:38:56"

#####################################################################
#
# Name:    logevent
#
# Purpose: Mails event information generated by the Event Response 
#          resource manager to a specified log file.
#
# Inputs:  Optional ID of the uesr to whom the evnt information 
#          will be mailed.
#          If userid is not specified, root is used as the default.  
#
#####################################################################

generate_message () {

        cat  <<EOF
=====================================

${EventTime}

${ConditionName}: $ERRM_COND_NAME
${Severity}: $COND_SEVERITY
${EventType}: $TYPE
${Expression}: $ERRM_EXPR

${ResourceName}: $ERRM_RSRC_NAME
${ResourceClass}: $ERRM_RSRC_CLASS_PNAME
${DataType}: $ERRM_DATA_TYPE
${DataValue}: $ERRM_VALUE
${NodeName}: $ERRM_NODE_NAME
${NodeNameList}: $ERRM_NODE_NAMELIST
${RsrcType}: $ERRM_RSRC_TYPE
EOF
        
print "====================================="

}

# main ()
# Set path to known value
PATH=/opt/rsct/bin:/usr/xpg4/bin:/usr/bin:/usr/sbin:/bin
export PATH

PERL=/usr/bin/perl

# Instead of using ctdspmsg, we directly call dspmsg to improve the
# performance. 
# Need to ensure the set id and message id are correct.  
#CTMSG=/opt/rsct/bin/ctdspmsg
CTMSG=/usr/bin/dspmsg
MSGMAPPATH=/opt/rsct/msgmaps
export MSGMAPPATH

ConditionName=`$CTMSG -s 5 IBM.ERrm.cat 2 'Condition Name'`
Severity=`$CTMSG -s 5 IBM.ERrm.cat 3 'Severity'`
EventType=`$CTMSG -s 5 IBM.ERrm.cat 4 'Event Type'`
Expression=`$CTMSG -s 5 IBM.ERrm.cat 5 'Expression'`

ResourceName=`$CTMSG -s 5 IBM.ERrm.cat 6 'Resource Name'`
ResourceClass=`$CTMSG -s 5 IBM.ERrm.cat 8 'Resource Class'`
DataType=`$CTMSG -s 5 IBM.ERrm.cat 9 'Data Type'`
DataValue=`$CTMSG -s 5 IBM.ERrm.cat 10 'Data Value'`
NodeName=`$CTMSG -s 5 IBM.ERrm.cat 7 'Node Name'`
NodeNameList=`$CTMSG -s 5 IBM.ERrm.cat 14 'Node NameList'`
RsrcType=`$CTMSG -s 5 IBM.ERrm.cat 15 'Resource Type'`
Usage=`$CTMSG -s 5 IBM.ERrm.cat 11 'Usage'`

# Convert Seveirty
case $ERRM_COND_SEVERITYID in

   0 ) COND_SEVERITY=`$CTMSG -s 5 IBM.ERrm.cat 19 'Informational'`;;
   1 ) COND_SEVERITY=`$CTMSG -s 5 IBM.ERrm.cat 20 'Warning'`;;
   2 ) COND_SEVERITY=`$CTMSG -s 5 IBM.ERrm.cat 21 'Critical'`;;

esac

# Convert EventType
case $ERRM_TYPEID in

   0 ) TYPE=`$CTMSG -s 5 IBM.ERrm.cat 17 'Event'`;;
   1 ) TYPE=`$CTMSG -s 5 IBM.ERrm.cat 18 'Rearm event'`;;

esac

while getopts ":h" opt
do
  case $opt in

    h ) print "${Usage}: `basename $0` [-h] [userid] "
        exit 0;;

    ? ) print "${Usage}: `basename $0` [-h] [userid] "
        exit 3;;
  esac
done

# Mail to root if a userid is not specified
if [[ $# -lt 1 ]]; then
   mailTo=root
else
   mailTo=$1
fi

# convert time string
seconds=${ERRM_TIME%,*}

EventTime=$(seconds=$seconds $PERL -e \
'
use POSIX qw(strftime);
print strftime("%A %D %T", localtime($ENV{seconds}) );  

'
)

generate_message | mail -s "$ERRM_COND_NAME" $mailTo