#!/bin/sh
. /usr/lib64/nagios/plugins/utils.sh

taintval=$(cat /proc/sys/kernel/tainted)

if [ "$taintval" = 0 ]; then
    $ECHO "Not tainted"
    exit $STATE_OK
fi

# This is a bash reimplementation of kernel/panic.c:print_tainted
# Letters are as follows:
# (As quoted from http://lxr.linux.no/#linux+v2.6.38/kernel/panic.c#L181)
# *      print_tainted - return a string to represent the kernel taint state.
# *
# *  'P' - Proprietary module has been loaded.
# *  'F' - Module has been forcibly loaded.
# *  'S' - SMP with CPUs not designed for SMP.
# *  'R' - User forced a module unload.
# *  'M' - System experienced a machine check exception.
# *  'B' - System has hit bad_page.
# *  'U' - Userspace-defined naughtiness.
# *  'D' - Kernel has oopsed before
# *  'A' - ACPI table overridden.
# *  'W' - Taint on warning.
# *  'C' - modules from drivers/staging are loaded.
# *  'I' - Working around severe firmware bug.
# *

flag=1
taints=""
for i in P F S R M B U D A W C I; do
    if [ $(($taintval & $flag)) -ne 0 ]; then
	taints="$taints$i"
    else
	taints="$taints "
    fi
    flag=$(($flag * 2))
done

$ECHO "Tainted: $taints"

case "$taints" in
    *M*|*B*|*D*) exit $STATE_CRITICAL;;
    *U*|*W*) exit $STATE_WARNING;;
    *) exit $STATE_OK;;
esac

