#!/bin/bash
#
#
# last modified: 13 Jun 2025
# creation: 05 Jun 2025
# 13 06 2025 -exclude option to exclude pattern from the grep
# author: Andrea Bolongaro
#
# Description:
# check the networker daemon raw file for any lines containing the pattern passed as argument in the time period also passed as an argument
# the pattern to be searched and the time interval in hours must be passed as arguments

# Output:
# returns OK if the pattern is not present
# returns CRITICAL if the pattern is present, lines containing the pattern are provided as output
#



PLUGIN_NAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`

# set default value
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
CHK_EXCLUDE=0
NW_DAEMONLOG_FILE=/nsr/logs/daemon.raw

exitstatus=$STATE_UNKNOWN

print_help() {
        echo ""
        echo "$PLUGIN_NAME"
        echo ""
        echo "Usage: $PLUGIN_NAME -pattern PATTERN -hrsago HOURS [-exclude EXCLUDE_PATTERN]"
        echo ""
        echo "  Options:"
        echo "  -pattern \"PATTERN\", check if PATTERN (case insensitive) is present in daemon.raw, exit status is critical if present"
        echo "  -hrsago HOURS, number of passed hours to search in daemon.raw file"
        echo "  -exclude \"EXCLUDE_PATTERN\", pattern (case insensitive) to filter out unwanted matches"
        echo "  -h display this help"
        echo ""
        echo " Check some stuff"
        echo ""
        exit 0
}


if [ $# -lt 4 ]
then
    	echo "[CRITICAL] insufficient arguments"
        print_help
        exit 1
fi

while [ $# -gt 0 ]; do
    case "$1" in
    -h | --help)
        print_help
        exit 0
        ;;
    -pattern)
        CHK_PATTERN=1
        PATTERN=$2
        shift
        ;;
    -hrsago)
        CHK_HOURS=1
        HOURS=$2
        shift
        ;;
    -exclude)
        CHK_EXCLUDE=1
        TO_EXCLUDE=$2
        shift
        ;;
    esac
    shift
done

# check for parameters CHK_PATTERN=1 and CHK_HOURS=1 
if ! [[ "$CHK_PATTERN" == "1" && "$CHK_HOURS" == "1" ]]; then
     echo "[CRITICAL] check arguments"
     print_help
     exit 1
fi

# not defined OUTPUT=""

if [ ${CHK_EXCLUDE} -eq 1  ] ; then
   # if CHK_EXCLUDE is 1, there are patterns to exclude from the grep, i.e. something matches PATTERN but I don't want it to match TO_EXCLUDE
   # result is formatted in html due to line breaks by converting \n to <br>
   OUTPUT=$(nsr_render_log -S "$HOURS hours ago" "$NW_DAEMONLOG_FILE" 2>&1 |egrep -i "$PATTERN" | egrep -vi "$TO_EXCLUDE" | sed ':a;N;$!ba;s/\n/<br>/g')
   RETRESULT=$?

   # it returns the number of lines that match PATTERN but not TO_EXCLUDE"
   #ERRCNT=$(echo -e "${OUTPUT}" | egrep -i "$PATTERN" | egrep -vi "$TO_EXCLUDE" | wc -l)
else
   # result is formatted in html due to line breaks by converting \n to <br>
   OUTPUT=$(nsr_render_log -S "$HOURS hours ago" "NW_DAEMONLOG_FILE" 2>&1 |egrep -i "$PATTERN" | sed ':a;N;$!ba;s/\n/<br>/g') 
   RETRESULT=$?

   # it returns the number of lines that match PATTERN 
   #ERRCNT=$(echo -e "${OUTPUT}" | egrep -i "$PATTERN" | wc -l)
fi

  
#if [ ${ERRCNT} -eq 0  ] ; then 
if [ -z "${OUTPUT}" ] ; then 
   # OUTPUT is an empty string, so the pattern was not found, and that is OK
   RETURN_MESSAGE="[OK] no ${PATTERN} found"

   # replaces any pipes in the pattern with a comma, otherwise it would be interpreted as perf data
   RETURN_MESSAGE=`echo ${RETURN_MESSAGE}| sed -e 's/|/,/g'`\
   exitstatus=${STATE_OK}
else
   # OUTPUT contains something, i.e the PATTERN was found. It returns critical and output message
   # replaces any pipes in the pattern with a comma, otherwise it would be interpreted as perf data
   OUTPUT=`echo ${OUTPUT}| sed -e 's/|/!/g'`

   RETURN_MESSAGE=${OUTPUT}
   exitstatus=${STATE_CRITICAL}
fi

#if [ ${RETRESULT} -ne 0  ] ; then
#     # RETRESULT is not zero, the nsr_render_log command failed, it returns critical and the output message
#     # nsr_render_log command failed execution
#     RETURN_MESSAGE=`echo "[CRITICAL] nsr_render_log command execution failed"`
#     exitstatus=${STATE_CRITICAL}
#fi

echo "${RETURN_MESSAGE}"
exit $exitstatus
