#!/usr/bin/ksh
#
  Copyright="(C) 2019 - Carlos Ijalba & Aitor Rodenas, GPLv3" # <perkolator @ gmail.com>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
########################################################################################################################
#
#  Program: check_lpar2rrd.ksh 
#
#  Parameters:
#              $1   -  TYPE of check, it can be [ LPAR | POOL ]
#              $2   -  SERVER, the physical host of the LPAR or POOL
#              $3   -  NAME of the object
#
#  Output:
#              3    -  Error:    Missing parameters, usage shown. 
#              2    -  CRITICAL: CPU Alarm generated by lpar2rrd. 
#              0    -  OK:       CPU OK, actual CPU utilization reported. 
#
#  Description:
#
#    Shell Script for Nagios, checks if there are alerts generated by lpar2rrd in a log. It can be used to check the CPU
#    status of a PowerVM LPAR or POOL.
#
#    LPAR2RRD log alerts have the following syntax:
#
#       Alert target   : OBJECT TYPE:SERVER NAME:OBJECT NAME utilization=%.%
#
#      "Alert nagios   : LPAR=aix_server_1:P9_Pool:aix_server_1 utilization=0.491"
#
#    Based in original "LPAR2RRD Nagios plugin" script which comes with the LPAR2RRD application.
#
#    Verified compatible with the following OS:
#                                      IBM AIX v7.2
#                                      Ubuntu v18.04 LTS
#                                      CygWin v2.5.1 & BusyBox v1.22.1
#  Debugging:
#
#    To debug this script, touching a file named DEBUG (defined in the constants block) in the scripts PATH, 
#    will show extra debug info and deleting the file will switch back to normal. To fully debug it from KSH,
#    take the coment off "#set -x", in the Debug section.
#
#  Versions       Date         Programmer, Modification
#  ------------   ----------   ----------------------------------------------------
#  Version=1.00   24/01/2018 - Aitor Rodenas, Original version.
#  Version=1.02   24/01/2018 - Carlos Ijalba, Optimized for NagiosXI
#  Version=1.03   13/07/2018 - Aitor Rodenas, Added CPU utilization (CPU_UTIL).
#  Version=2.00   24/08/2018 - Carlos Ijalba, GPLv3 open source release. 
#  Version=2.03   27/08/2018 - Carlos Ijalba, Added alarm definition checks, debug mode & functions. 
   Version=2.04 # 05/01/2019 - Carlos Ijalba, Updates. Published to Nagios Exchange.
#
###########################################################################################
# Debug ------------------------------------------- scripts Debug section (also see Constants) ####

#set -x 


# Constants ------------------------------------------- scripts Constants declaration ####

PROGRAM=$(basename $0)						# Name of this script
DEBUG=DEBUG									# Debug flag-filename to check for debug mode on/off
LPAR_DEFINED="loading"						# text to search in LPAR2RRD log for definitions
LPAR_ALARM="nagios"							# text to search in LPAR2RRD log for alarms
LPAR_KO="Alert"								# text to search in LPAR2RRD log for CRITICAL
LPAR_OK="No alerting"						# text to search in LPAR2RRD log for OK
NAGIOS_ERROR=3								# Return code given if ERROR
NAGIOS_CRIT=2								# Return code for Nagios if CRITICAL
NAGIOS_WARN=1								# Return code for Nagios if WARNING
NAGIOS_OK=0									# Return code for Nagios if OK


# Variables ------------------------------------------- scripts Variables declaration ####

LPAR2RRD_ALOG=/home/lpar2rrd/lpar2rrd/load_alert.out		# LPAR2RRD alert log
TYPE=$1								# $1 is TYPE: LPAR, POOL
SERVER=$2								# $2 is the physical server name
NAME=$3								# $3 is the VM name (LPAR name) 


# Functions ------------------------------------------- scripts Functions declaration ####

function usage
 ############################
 #
 # function to show the script's help screen or usage
 #
 ###########################
{
cat << EOF
$PROGRAM v$Version - $Copyright

   ERROR - Missing parameters!

   USE:
             $PROGRAM  [ TYPE ] [ SERVER ] [ NAME ]

      Reports:
             OK -       lpar2rrd: \$SERVER:\$NAME, CPU OK (act utilization %.%%).
             CRITICAL - Alarm generated by lpar2rrd: \$ALERT.
             ERROR -    Missing parameters!

      Examples:
             $PROGRAM  LPAR  P9_Server  db2_server    <-- check the CPU of LPAR db2_server in P9_Server
             $PROGRAM  POOL  P9_Server  db2_pool      <-- check the CPU of CPU POOL db2_pool in P9_Server


      Debug:
             ON:  in the same PATH as the executable script:

	 		touch DEBUG

             OFF: in the same PATH as the executable script:

			rm DEBUG

EOF
}

function echo_debug
 ############################
 #
 # function to show debugging messages 
 #
 ###########################
{
  if [ -f $DEBUG ]
    then
      echo "o-> DEBUG::: "$1 
  fi
}

function check_alarm_def
 ############################
 #
 # function to check if the alert passed has been defined in LPAR2RRD alarm log file 
 #
 ###########################
{
  echo_debug "Looking on LPAR2RRD alarm log: $LPAR2RRD_ALOG ..."	                       	# DEBUG INFO

  EXISTS=$(grep $LPAR_DEFINED $LPAR2RRD_ALOG | grep $TYPE | grep "$SERVER:$NAME")			# Check alert definition
  if [[ $EXISTS ]] 
    then
      echo_debug "+++OK: Alarm definition found for: $TYPE - $SERVER - $NAME, checking alarms..." # DEBUG INFO
    else
      echo "---ERROR: There is no alarm defined for: $TYPE - $SERVER - $NAME!"
      RC=$NAGIOS_ERROR
      exit $RC
  fi
}


#Usage ------------------------------------------- show scripts usage ####

if [ $# -lt 3 ]										# if the script is invoked without the right parameters...
  then												# show its usage and version, and exit
    usage
    RC=$NAGIOS_ERROR
    exit $RC
fi


# Main ------------------------------------------- scripts Main code ####

check_alarm_def										# check alarm definition

ALERT=$(grep $LPAR_ALARM $LPAR2RRD_ALOG | grep $TYPE | grep "$SERVER:$NAME")	# check alerts
if [ $(echo $ALERT | grep -c "$LPAR_KO") -gt 0 ]
  then
    ALERT="Alarm generated by lpar2rrd: $ALERT"
    RC=$NAGIOS_CRIT
  else
    CPU_UTIL=$(grep "$LPAR_OK" $LPAR2RRD_ALOG | grep $TYPE | grep -w $SERVER:$NAME | awk -F: '{ print $6 }')
    ALERT="lpar2rrd: $SERVER:$NAME, CPU OK (act utilization $CPU_UTIL)"
    RC=$NAGIOS_OK
fi

echo $ALERT 
echo $PROGRAM v$Version   
return $RC

# End ------------------------------------------- scripts last entry ####