#!/bin/sh
#
# Nagios plugin for checking the cpu load and notify if cpu load is too low
# written by Gerd Koenig <geko@deb.ymc.ch>
#
# Description:
#
# This plugin will return information about the
# cpu load

exit_code=0
message="OK"

while [ $# -gt 0 ]; do
    case "$1" in
        -w | --warning)
                shift
                WARNING_THRESHOLD=$1
                ;;
        -c | --critical)
               shift
                CRITICAL_THRESHOLD=$1
                ;;
        *)  echo "Unknown argument: $1"
            exit 1
            ;;
        esac
shift
done

if [ "x$WARNING_THRESHOLD" = "x" ] || [ "x$CRITICAL_THRESHOLD" = "x" ]; then
  echo "please provide arguments -w <warning_threshold> and -c <critical_threshold>"
  exit 1
else
  if [ $WARNING_THRESHOLD -lt $CRITICAL_THRESHOLD ]; then
    echo "warning threshold must be greater than critical threshold"
    exit 1
  elif [ $WARNING_THRESHOLD -ge 100 ] || [ $WARNING_THRESHOLD -ge 100 ]; then
    echo "warning and critical threshold needs to be less than 100"
    exit 1
  fi
fi

idle_percent=$(/usr/bin/vmstat 1 1 | tail -1 | /usr/bin/awk '{ print $15 }')

if [ $idle_percent -ge $(echo "100-$CRITICAL_THRESHOLD" | bc) ]; then
  message="CPU load too low, CPU $idle_percent % idle"
  exit_code=2
elif [ $idle_percent -ge $(echo "100-$WARNING_THRESHOLD" | bc) ]; then
  message="CPU load getting down, CPU $idle_percent % idle"
  exit_code=1
else
  message="OK. CPU $idle_percent % idle."
  exit_code=0
fi

echo $message
exit $exit_code