#!/bin/bash


while getopts "u:w:c:" Input;
do
       case ${Input} in
       u)               unit=${OPTARG};;
       w)               warning=${OPTARG};;
       c)               critical=${OPTARG};;
        esac
done

if [ -z "$unit" ]; then echo "Es wurde keine Hardwareunit benannt" && exit 1; fi
if [ -z "$warning" ]; then echo "Es muss ein Warning-Wert angegeben werden" && exit 1; fi
if [ -z "$critical" ]; then echo "Es muss ein Critical-Wert angegeben werden" &&  exit 1; fi

anzahl_items=$(($(cat /opt/data/ipmi | grep "$unit" | wc -l)))
array_warning=()
array_critical=()
exit_code1="0"
exit_code2="0"
exit_code="0"

for (( i=1; i<=$anzahl_items; i++))
do
    performace_data="$unit $i=$(cat /opt/data/ipmi | grep "$unit $i" | awk -F '|' '{print $2}'| tr -d " "| tr ',' .);$warning;$critical"
    measuring_point[$((i-1))]=$performace_data
    vergleich=$(echo "${measuring_point[$((i-1))]}" | awk -F ';' '{print $1}' | awk -F '.' '{print $1}' | awk -F '=' '{print $2}')
    if [ -z "${vergleich// }" ]; then vergleich="1" && measuring_point[$((i-1))]="" ; fi
    if [ $vergleich -gt $critical ] || [ $vergleich -eq "0" ]; then
            array_critical+=("$unit $i");
            exit_code1="2"
    else
        if [ $vergleich -gt $warning ]; then
                array_warning+=("$unit $i");
                exit_code2="1"
        fi
    fi
done
if [ $exit_code1 != 0 ] || [ $exit_code2 != 0 ]; then
        if [ $exit_code1 -gt $exit_code2 ]; then
                exit_code="2"
        else
                exit_code="1"
        fi
fi

case $exit_code in

        2)
        echo "[CRITICAL] Folgende Werte sind auf Critical:${array_critical[@]} "
        echo " Folgende Werte sind auf Warning: ${array_warning[@]} |${measuring_point[@]}"
        exit 2
        ;;
        1)
        echo "[WARNING] Folgende Werte sind auf Warning: ${array_warning[@]} |${measuring_point[@]}"
        exit 1
        ;;
        *)
        echo "Alle Werte sind okay |${measuring_point[@]}"
        exit 0
        ;;
esac