conditional-monitor.sh


The conditional-monitor.sh script serves as an example of script that executes only when one or more conditions are met. For example, this script will monitor and collect data only on Saturday at 23:00 for 30 minutes. The alerts and annotations will be displayed during the same time. 

This script is available at $PATROL_HOME\..\TRO\Conf\Scripts\Samples directory. The script content is as follows:

#!/bin/sh -f
#=======================================================================================================
# This sample script show how to make a script take required action on given day of the week starting
# from a given hour during a period of certain minutes.
# e.g. monitor and collect data every Saturday night at 23:00 for 30 minutes.
#     and during other times, the script will not monitor.
#     this example allow the user to have alerts only during that time as well.
# the script is used with the following format:
# ./conditional-monitor.sh -d <day of the week> -h <start time> -p <period in minutes>
#        <day of the week>:      1..7 (MON=1, TUE=2 etc.)
#        <start time>:           00..23
#        <period in minutes>:    0 or larger
# when used within a policy make sure the arguments are provided.
# NOTICE: the same script can run on the same host during different days or periods
#         it's the arguments which count.
# When collecting data (at the right day of the week, and time of day), the sample monitor
# CPU and memory utilization of the server
#=======================================================================================================
DEBUG_FILE=$0.debug
RegEx='^[0-9]+$'
RexExFloating='^[0-9]+([.][0-9]+)?$'
TodayInTheWeek=`date +%w`
NowTimeEPOCH=`date +%s`
# expecting args
# Mem:  2059580 total 1984532 used 75048 free 8868 buffers
function setMemStats
{
 if [ "$5" = "used" ]; then
   memPct=`echo "scale=2;( $4 / $2 ) * 100.0" | bc`
 elif [ "$8" = "used" ]; then
   memPct=`echo "scale=2;( $7 / $3 ) * 100.0" | bc`
 else
   memPct=-1.0
 fi
}
# expecting args
# Cpu(s): 1.5us 0.3sy 0.1ni 95.4id 2.0wa 0.3hi 0.5si 0.0st
# or output like that:
# Cpu(s): 1.4 us 0.5 sy 0.0 ni 97.9 id 0.2 wa 0.0 hi 0.0 si 0.0 st
function setCpuStats
{
 if [[doc:xwiki:IT-Operations-Management.Operations-Management.BMC-PATROL-for-Scripting.script21.Using.Linux-environment.Using-sample-scripts-for-monitoring-Linux.conditional-monitor-sh. $4 = $RexExFloating ]]; then
   idPct=`echo $4 | sed 's/[^0-9.]*//g'`
 else
   idPct=-1.0
 fi
 cpuPct=`printf "100-%.2f\n" $idPct | bc`
}
function getStatistic
{
 for rawLine in $(top -b -n 1 | grep "Cpu(s):\|Mem :\|Mem:" | sed -e 's/,\|k\|%//g' -e 's/ /_/g')
 do
   line=`echo $rawLine | sed 's/_/ /g'`
   lineType=`echo $rawLine | cut -d ':' -f 1`
   if [ "$lineType" = "Mem" -o "$lineType" = "KiB_Mem_" ]; then
      setMemStats `echo $line | sed 's/[:\/\(\)]//g'`
   else
      setCpuStats `echo $line | sed 's/[a-z]\|[A-Z]\|[:\/\(\)]//g'`
   fi
 done
 if [ "$memPct" != "" -a "$cpuPct" != "" ]; then
   echo ServerLoad=ServerLoad,CPULoad=$cpuPct,MEMLoad=$memPct
   exit 0
 else
   exit -1
 fi
}
function dateConditionAction
{
 if [ $# -eq 2 ]; then
   StartTimeEPOCH=`date --date="$1" +%s`
   EndTimeeEPOCH=`echo $StartTimeEPOCH + $2*60 | bc`
   if [ $NowTimeEPOCH -ge $StartTimeEPOCH -a $NowTimeEPOCH -lt $EndTimeeEPOCH ]; then
      getStatistic
   else
     exit -1
   fi
 fi
}
if ! [[doc:xwiki:IT-Operations-Management.Operations-Management.BMC-PATROL-for-Scripting.script21.Using.Linux-environment.Using-sample-scripts-for-monitoring-Linux.conditional-monitor-sh. $# -eq 6 ]]; then
 echo -e "Usage: $0 -d <day of the week> -h <start time> -p <period in minutes>"
 echo -e "\t<day of the week>:\t1..7"
 echo -e "\t<start time>:\t\t00..23"
 echo -e "\t<period in minutes>:\t0 or larger"
 exit -1
fi
for arg in $*
do
 case $arg in
   "-d")
      ;;
   "-h")
      ;;
   "-p")
      ;;
    *)
     if [[doc:xwiki:IT-Operations-Management.Operations-Management.BMC-PATROL-for-Scripting.script21.Using.Linux-environment.Using-sample-scripts-for-monitoring-Linux.conditional-monitor-sh. $arg = $RegEx ]]; then
       case $prevArg in
         "-d")
           activeDay=$arg
            ;;
         "-h")
           activeHour=$arg:00
            ;;
         "-p")
           activePeriod=$arg
            ;;
       esac
     else
       echo arg=$arg
       echo "Invalid flag ignored"
     fi
      ;;
 esac
 prevArg=$arg
done
if [ $TodayInTheWeek -eq $activeDay ]; then
  dateConditionAction $activeHour $activePeriod
else
 exit 1
fi--

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*

BMC PATROL for Scripting 2.1