#!/bin/ksh
#
#  
#
# File        : mon_logins.sh
#
# Objective   : report the number of successfull logins over a specified 
#               time interval into a specified objectmanager. 
#               The maximum interval is set to 24hs
#
# Argument    : Interval in seconds
#               Objectmgr logfile name 
#
# Returns     : -1      Error detected
#               0>      Number of logins 
#
# Date (yyyymmdd)   Who              What
# ---------------   --------------   -------------------------------------------------
# 2006 10 20        Peter van Nes    Initial release, based on other code

# Declare variables
NODE_NAME=`uname -n`
SIEBEL_LOG=/appl/sea78/siebsrvr/enterprises/siebel/$NODE_NAME/log

# Test if argument has been specified as a valid number
if [[ ( -z $1 )  || ( $1 != +([0-9]) ) || ( -z $2 ) ]]
then
  echo "-1"
  echo "Specify time interval in seconds as the first argument, a string (using filename generation characters) as a second argument matching the ObjectMgr logfile name"
else
  # export the interval, necessary for awk statement
  export INTERVAL=$1
  OBJMGR=$2
  # Test if siebel log directory exists
  if [[ ! -d $SIEBEL_LOG ]]
  then
    # siebel log directory does not exist
    echo "-1"
    echo "Filesystem \"${SIEBEL_LOG}\" does not exist or is not a directory"
  else
    # Calculate the # of logins in the relevant objmgr logfiles
    Logins=`find $SIEBEL_LOG -name $OBJMGR -atime 1|xargs fgrep "newUser="| fgrep -v failed| \
    awk -v now=\`date +"%y:%m:%d:%H:%M:%S"\` \
    'BEGIN {split(now,tnow,":");secnow=0;secnow=tnow[1]*31536000+tnow[2]*2678400+tnow[3]*86400+tnow[4]*3600+tnow[5]*60+tnow[6]} \
           {split($5,d,"-");sec=0;sec=sec+substr(d[1],3,2)*31536000+d[2]*2678400+d[3]*86400; \
            split($6,t,":");sec=sec+t[1]*3600+t[2]*60+t[3];diff=secnow-sec; \
            if (diff<=ENVIRON["INTERVAL"]){;lgns++} } \
    END{print lgns}'`

    # Print the number of logins
    if [[ ! -z "$Logins" ]]
    then
      echo "$Logins"
    else
      echo "0"
    fi
  fi
fi


