#!/bin/sh

############################################################################
# This script launches Splunk, passing in the hostname and Last Notify time
# of the event in the Smarts console, and another parameter to limit the
# events Splunk finds to within N minutes of the Last Notify time.
############################################################################

# Modify the following BROWSER and SPLUNK_HOST variables as necessary
# for your local environment

BROWSER="/usr/local/firefox/firefox"
SPLUNK_HOST="SPLUNK_HOST:8000"

# Modify MINUTES variable to limit the events found to within that number
# of minutes from the Last Notify time

MINUTES="1"

#  Convert command line arguments to env vars

for i in "$@"
do
    # Remove " at offset 0 in string, and add " after first = in string
    j=`echo $i | sed 's/"//' | sed 's/=/="/'`

    # echo $i "--converted to--> |" $j "|"

    # Evaluate the assignment into this shell, i.e., execute
    # the modified string as if it had been typed on the command line
    eval $j
done

# Check for required parameters

if test -z "$SM_OBJ_InstanceName"; then
   echo Can not process request without SM_OBJ_InstanceName
   exit 1
fi

if test -z "$SM_OBJ_LastNotifiedAt"; then
   echo Can not process request without SM_OBJ_LastNotifiedAt
   exit 1
fi

# Convert Last Notify time format for use by Splunk
# Last Notify example: Mon Feb 26 13:12:51 PST 2007
# Splunk time example: 02/26/2007:13:12:51

CMONTH=`echo ${SM_OBJ_LastNotifiedAt} | awk '{print $2}'`
DAY=`echo ${SM_OBJ_LastNotifiedAt} | awk '{print $3}'`
TIME=`echo ${SM_OBJ_LastNotifiedAt} | awk '{print $4}'`
YEAR=`echo ${SM_OBJ_LastNotifiedAt} | awk '{print $6}'`

if [ "$CMONTH" = "Jan" ]; then 
   MONTH="01"
fi
if [ "$CMONTH" = "Feb" ]; then
   MONTH="02"
fi
if [ "$CMONTH" = "Mar" ]; then
   MONTH="03"
fi
if [ "$CMONTH" = "Apr" ]; then 
   MONTH="04"
fi
if [ "$CMONTH" = "May" ]; then 
   MONTH="05"
fi
if [ "$CMONTH" = "Jun" ]; then
   MONTH="06"
fi
if [ "$CMONTH" = "Jul" ]; then
   MONTH="07"
fi
if [ "$CMONTH" = "Aug" ]; then
   MONTH="08"
fi
if [ "$CMONTH" = "Sep" ]; then
   MONTH="09"
fi
if [ "$CMONTH" = "Oct" ]; then
   MONTH="10"
fi
if [ "$CMONTH" = "Nov" ]; then
   MONTH="11"
fi
if [ "$CMONTH" = "Dec" ]; then
   MONTH="12"
fi

ENDTIME="$MONTH%2F$DAY%2F$YEAR%3A$TIME"

# Launch Splunk

SPLUNK_CMD="http://$SPLUNK_HOST/?q=HOST%3A%3A$SM_OBJ_InstanceName%20endtime%3A%3A$ENDTIME%20searchtimespanminutes%3A%3A$MINUTES"
$BROWSER $SPLUNK_CMD &
echo launched $BROWSER on: `uname -a`

