File check_zcpuload.sh of Package monitoring-plugins-zcpuload

#!/bin/bash
#
# Copyright (C) 2015-2017, SUSE Linux GmbH
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the Novell nor the names of its contributors may be
#   used to endorse or promote products derived from this software without
#   specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

NRCP=16
NRIFL=10
PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1 $' | sed -e 's/[^0-9.]//g'`
declare -a MACHINES

. $PROGPATH/utils.sh


print_usage() {
        echo "Usage: $PROGNAME"
		echo "       -m <machine>    : LPAR or Guest name. Can be used multiple times."
#		echo "       -w <level>      : Warning threshold"
#		echo "       -c <level>      : Critical threshold"
#		echo "       -n <NRCP>       : amount of NRCP" 
#		echo "       -l <IFL>        : amount of IFL"
		echo "       -h              : this output"
		echo "       -t <CPU type>   : select CPU types that are used for CPU time calculations. See 'man 8 hyptop' for details"
		echo "       -p <percentage> : print out the sum in percentage"
		echo "       -V              : print version"
        exit 0
}

print_help() {
        print_revision $PROGNAME $REVISION
        echo ""
        print_usage
        echo ""
        echo "This plugin checks load of a z Series using the hyptop from s390-tools package."
        echo ""
        echo ""
        support
        exit 0
}

TEMP=`getopt -o hm:w:c:Vt:n:l:p -- "$@"`
eval set -- "$TEMP"

while [ $# -gt 0 ] ; do
    case $1 in
      -m)
         MACHINES=(${MACHINES[*]} "$2")
         shift 2
         ;;
      -w)
         WARN="$1"
         shift 2
         ;;
      -n)
         NRCP=$1
         shift 2
         ;;
      -l)
         NRIFL=$1
         shift 2
         ;;
      -c)
         CRIT=$1
         shift 2
         ;;
      -h|--help)
         print_usage
         shift
         ;;
      -V)
         print_revision $PROGNAME $REVISION
         shift
         exit 0
         ;;
      -t)
	 TYPE="-t $2"
         shift 2
         ;;
      -p)
         PERCENTAGE=$1
         shift
         ;;
      --)
         shift
         ;;
      *) 
         shift
         ;;
      esac
done

get_partitions()
{
# $1 = "" : all cpu types
# $1 = ifl | cpu : only respective cpu type
#echo "calling get_partitions with $1"
if [ "$1" = "ifl" -o "$1" = "cp" ]; then
   export TYPE="-t $1"
fi
declare -a MACHINES
if [ "${#MACHINES[*]}" = "0" ]; then
   MACHINES=()
   hyptop -b -n 1 $TYPE | grep -v "^  " | \
   { 
      read line 
      read line
      read line
      while read partition cpu_nr rest ; do
         if [ $cpu_nr -gt 0 ]; then
            MACHINES=(${MACHINES[*]} $partition)
         fi
      done
      echo ${MACHINES[*]}
   }
fi
}

sum_up()
{
   sum=0
   for i in $*; do
      j=${i#*=}
      sum=$(echo "$sum + $j" | bc )
   done
   echo $sum
}

get_load()
{
# $1: MACHINES
   PARM="$*"
   hyptop -b -n 1 -s ${PARM// /,} 2> /dev/null | \
   {
      declare -a partition_load
      read line
      read line
      read line
      while read partition cpu_nr load rest; do
         # ignore output that does not start with a partition name
         if [ "${partition:0:1}" = "L" -o "${partition:0:1}" = "P" ]; then
             partition_load+=("$partition=$load;")
         fi
      done
      SAVEIFS=$IFS
      IFS=$'\n'
      partition_load=($(sort <<<"${partition_load[*]}"))
      IFS=$SAVEIFS
      echo "${partition_load[*]}"
   }         
}

nr_cpus()
{
if [ "$1" = "ifl" -o "$1" = "cp" ]; then
   export TYPE="-t $1"
fi
   line=$(hyptop -b -n 1 $TYPE | head -n 1 )
   #echo "LINE: " $line
   ifl=$(echo $line | sed -n 's/.*IFL(\([0-9]*\)).*/\1/p')
   #echo "ifl: " $ifl
   cp=$(echo $line | sed -n 's/.* CP(\([0-9]*\).*/\1/p')
   #echo "cp: " $cp
   #echo "cp: $cp, ifl: $ifl"
   if [ -z "$ifl" ]; then 
      ifl="0"
   fi
   if [ -z "$cp" ]; then 
      cp="0"
   fi
   echo "$ifl + $cp" | bc
}

if [ "${#MACHINES[*]}" = "0" ]; then
   MACHINES=( $(get_partitions $CPU_TYPE) )
fi

nr_cpu=$(nr_cpus $CPU_TYPE)
partition_load=$(get_load  ${MACHINES[*]})
#$(get_load  ${MACHINES[*]})
sum=$(sum_up ${partition_load[*]})
if [ "$PERCENTAGE" = "1" ]; then
   sum=$(echo "$sum / $nr_cpu" | bc)
fi
# Output:
echo -n "OK - load average: $sum|"
echo "global=$sum; "${partition_load[*]}