#!/bin/sh

# Copyright 2023, Mark Schouten <mark@tuxis.nl>, Tuxis B.V.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. 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.
#
# 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 HOLDER 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.

unknown() {
    echo "UNKNOWN: $1"
    exit 3
}

critical() {
    echo "CRITICAL: $1"
    exit 2
}

warning() {
    echo "WARNING: $1"
    exit 1
}

ok() {
    echo "OK: $1"
    exit 0
}

usage() {
    echo "check_cpu_tuxis [--critical value] [--warning value] [--steal-critical value] [--steal-warning value]"
    echo "Value is a percentage-value. Defaults:"
    echo " - Critical: 95"
    echo " - Warning: 90"
    echo " - Steal Critical: 5"
    echo " - Steal Warning: 3"
    
    unknown "Invalid configuration"
}


UCRIT=95
UWARN=90
SCRIT=5
SWARN=3

while [ ! -z "$1" ]; do
    case "$1" in
        --critical)
            UCRIT=$2
            ;;
        --warning)
            UWARN=$2
            ;;
        --steal-critical)
            SCRIT=$2
            ;;
        --steal-warning)
            SWARN=$2
            ;;
        *)
            usage
            ;;
    esac
    shift
    shift
done

T1=$(head -1 /proc/stat)
sleep 1
T2=$(head -1 /proc/stat)

# tag user      nice system    idle       iowat  irq softirq steal guest guest_nice
# cpu 301559682 3764 154914257 1997605668 649693 0   2286466 0     0     0

totaltime=0
usage=0
for i in `seq 2 11`; do
    ltime=$(echo -n "${T2}\n${T1}" | awk "NR==1{total =\$$i;} END{print total-\$$i;}")
    case $i in
        2)
            tuser=$ltime
            usage=$(($usage+$ltime))
            ;;
        3)
            tnice=$ltime
            usage=$(($usage+$ltime))
            ;;
        4)
            tsystem=$ltime
            usage=$(($usage+$ltime))
            ;;
        5)
            tidle=$ltime
            ;;
        6)
            tiowait=$ltime
            usage=$(($usage+$ltime))
            ;;
        7)
            tirq=$ltime
            usage=$(($usage+$ltime))
            ;;
        8)
            tsoftirq=$ltime
            usage=$(($usage+$ltime))
            ;;
        9)
            tsteal=$ltime
            usage=$(($usage+$ltime))
            ;;
        10|11)
            tguest=$ltime
            usage=$(($usage+$ltime))
            ;;
    esac
    totaltime=$(($totaltime+$ltime))
done

puser=$(printf "%0.2f" `echo "100*$tuser/$totaltime" | bc -l`)
pnice=$(printf "%0.2f" `echo "100*$tnice/$totaltime" | bc -l`)
psystem=$(printf "%0.2f" `echo "100*$tsystem/$totaltime" | bc -l`)
pidle=$(printf "%0.2f" `echo "100*$tidle/$totaltime" | bc -l`)
piowait=$(printf "%0.2f" `echo "100*$tiowait/$totaltime" | bc -l`)
pirq=$(printf "%0.2f" `echo "100*$tirq/$totaltime" | bc -l`)
psoftirq=$(printf "%0.2f" `echo "100*$tsoftirq/$totaltime" | bc -l`)
psteal=$(printf "%0.2f" `echo "100*$tsteal/$totaltime" | bc -l`)
pguest=$(printf "%0.2f" `echo "100*$tguest/$totaltime" | bc -l`)
pusage=$(printf "%0.2f" `echo "100*$usage/$totaltime" | bc -l`)

MSG="Total: $pusage%, Steal: $psteal% [User: $puser Nice: $pnice System: $psystem Idle: $pidle IOWait: $piowait IRQ: $pirq SoftIRQ: $psoftirq Guest: $pguest]"

if [ `echo "$pusage > $UCRIT" | bc ` -eq 1 ]; then
    critical "CPU USAGE $MSG"
fi
if [ `echo "$psteal > $SCRIT" | bc ` -eq 1 ]; then
    critical "CPU STEAL $MSG"
fi

if [ `echo "$pusage > $UWARN" | bc ` -eq 1 ]; then
    warning "CPU USAGE $MSG"
fi
if [ `echo "$psteal > $SWARN" | bc ` -eq 1 ]; then
    warning "CPU STEAL $MSG"
fi

ok "$MSG"
