#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2001,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 = "@(#)79   1.9   src/rsct/rm/ER/elogevent, ERrm, rsct_rady, rady2035a 11/12/15 16:40:03"

#####################################################################
#
# Name:    elogevent
#
# Purpose: Logs event information generated by Event Response resource
#          manager to a specified log file.
#
# Inputs:  Name of the file where event information is logged.
#          An absolute path for the logfile operand should be specified.  
#
#####################################################################

generate_message () {

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

Event generated at ${EventTime}

Condition Name: $ERRM_COND_NAME
Severity      : $COND_SEVERITY
Event Type    : $TYPE
Expression    : $ERRM_EXPR
Resource Name : $ERRM_RSRC_NAME 
Resource Class: $ERRM_RSRC_CLASS_PNAME
Data Type     : $ERRM_DATA_TYPE
Data Value    : $ERRM_VALUE
Node Name     : $ERRM_NODE_NAME
NodeNameList  : $ERRM_NODE_NAMELIST
Resource Type : $ERRM_RSRC_TYPE
EOF

}

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

PERL=/usr/bin/perl

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

    h ) print "Usage: `basename $0` [-h] logfile "
        exit 0;;

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



# Check if a required logfile is specified
if [[ $# -lt 1 ]]; then
   print -u2 "usage: `basename $0` [-h] logfile "
   exit 1
fi

# Check if the logfile path is valid
fileName=$1
pathName=${fileName%/*}

if [[ -z $pathName ]]; then
   pathName=/
fi

if [[ ! -d $pathName ]]; then
   print -u2 "invalid directory "
   exit 2
fi

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

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

# Convert Seveirty 
case $ERRM_COND_SEVERITYID in

    0 ) COND_SEVERITY=Informational;;
    1 ) COND_SEVERITY=Warning;;
    2 ) COND_SEVERITY=Critical;;

esac

# Convert EventType
case $ERRM_TYPEID in

    0 ) TYPE=Event;;
    1 ) TYPE=RearmEvent;;

esac

data=$(generate_message) ofile=$1   \
$PERL -e \
'
use Fcntl qw(:DEFAULT :flock);
$mytmpdir = `/opt/rsct/bin/cttmpdir`;
$rc = $?;
if ($rc != 0) {
    $mytmpdir = "/tmp";
} else {
    chomp($mytmpdir);
}
open(ofh,">>$mytmpdir/errmlogevent.lck");
flock(ofh,LOCK_EX);
$rc = 0;
$uname=`uname`;  chomp $uname;

# alog only exists on aix
if ("$uname" eq "AIX")
{

 `echo "\$data" | alog -q -f "\$ofile" -s 65536` ;
 $rc = $?>>8;
}
else
{
  open(ofh2,">>$ENV{ofile}");
  print ofh2 "$ENV{data}" ;
  close(ofh2);
}

close(ofh);
exit $rc;
'
exit $?