#!/bin/bash

#########################################
#                                       #
#   DRM Plugin for Icinga (Bash)        #
#                                       #
#   Version 1.2 (29/10/2017)            #
#   Manuel Bolzoni                      #
#                                       #
#########################################

### VARIABLES ##########

DRM_URL="https://monlab.altervista.org/drm/api/"
DRM_CURL="curl"
DRM_TIMEOUT=30
DRM_RETRY=2
DRM_STATUS_OK=0
DRM_STATUS_WARNING=1
DRM_STATUS_CRITICAL=2
DRM_STATUS_UNKNOWN=3

### FUNCTIONS ##########

function drm_handler {
    if [[ $DRM_EXEC == *"INFO000"* ]]; then
        echo "UNKNOWN: DRM API disabled"
        exit $DRM_STATUS_UNKNOWN
    elif [[ $DRM_EXEC == *"ERROR101"* ]]; then
        echo "UNKNOWN: DRM authentication failed"
        exit $DRM_STATUS_UNKNOWN
    elif [[ $DRM_EXEC == *"ERROR102"* ]]; then
        echo "UNKNOWN: DRM database error"
        exit $DRM_STATUS_UNKNOWN
    else
        echo "UNKNOWN: DRM unknown data"
        exit $DRM_STATUS_UNKNOWN
    fi
}

function drm_usage {
    echo "Usage: `basename $0` [-h] {-q|-s|-x} <API key>"
}

### CHECK ##########

if [ -z "$1" ]; then
    drm_usage
    exit $DRM_STATUS_UNKNOWN
elif [ ! "$1" == "-h" ] && [ -z "$2" ]; then
    drm_usage
    exit $DRM_STATUS_UNKNOWN
fi

if ! DRM_CURL_CHECK="$(type -p "$DRM_CURL")" || [ -z "$DRM_CURL_CHECK" ]; then
    echo "ERROR: cURL not found"
    exit $DRM_STATUS_UNKNOWN
fi

### CORE ##########

case "$1" in
    -h)
        echo
        echo "::::: DRM Plugin for Icinga"
        echo
        drm_usage
        echo
        echo "Options:"
        echo "-h"
        echo "    Print detailed help screen"
        echo "-q"
        echo "    Check DRM events"
        echo "-s"
        echo "    Check DRM global status"
        echo "-x"
        echo "    Check DRM global status and events"
        echo "API key"
        echo "    Your DRM API key"
        echo
        exit $DRM_STATUS_UNKNOWN
        ;;
    -q)
        DRM_EXEC=$($DRM_CURL -s -m $DRM_TIMEOUT --retry $DRM_RETRY $DRM_URL"?req=query&out=xml&key="$2)
        if [[ $DRM_EXEC == *"<totalevents>0</totalevents>"* ]]; then
            echo "OK: no DRM event"
            exit $DRM_STATUS_OK
        elif [[ $DRM_EXEC == *"<type>ALARM</type>"* ]]; then
            echo "CRITICAL: DRM event alarm"
            exit $DRM_STATUS_CRITICAL
        elif [[ $DRM_EXEC == *"<type>WARNING</type>"* ]]; then
            echo "WARNING: DRM event warning"
            exit $DRM_STATUS_WARNING
        else
            drm_handler
        fi
        ;;
    -s)
        DRM_EXEC=$($DRM_CURL -s -m $DRM_TIMEOUT --retry $DRM_RETRY $DRM_URL"?req=status&out=xml&key="$2)
        if [[ $DRM_EXEC == *"<status>operative</status>"* ]]; then
            echo "OK: DRM operative"
            exit $DRM_STATUS_OK
        elif [[ $DRM_EXEC == *"<status>partial</status>"* ]]; then
            echo "WARNING: partial DRM operativity"
            exit $DRM_STATUS_WARNING
        elif [[ $DRM_EXEC == *"<status>failure</status>"* ]]; then
            echo "CRITICAL: DRM failure"
            exit $DRM_STATUS_CRITICAL
        else
            drm_handler
        fi
        ;;
    -x)
        DRM_STATUS_GLOBAL="NONE"
        DRM_EXEC=$($DRM_CURL -s -m $DRM_TIMEOUT --retry $DRM_RETRY $DRM_URL"?req=status&out=xml&key="$2)
        if [[ $DRM_EXEC == *"<status>operative</status>"* ]]; then
            DRM_STATUS_GLOBAL="OK"
        elif [[ $DRM_EXEC == *"<status>partial</status>"* ]]; then
            echo "WARNING: partial DRM operativity"
            exit $DRM_STATUS_WARNING
        elif [[ $DRM_EXEC == *"<status>failure</status>"* ]]; then
            echo "CRITICAL: DRM failure"
            exit $DRM_STATUS_CRITICAL
        else
            drm_handler
        fi
        DRM_EXEC=$($DRM_CURL -s -m $DRM_TIMEOUT --retry $DRM_RETRY $DRM_URL"?req=query&out=xml&key="$2)
        if [[ $DRM_EXEC == *"<totalevents>0</totalevents>"* ]]; then
            DRM_STATUS_GLOBAL=$DRM_STATUS_GLOBAL"OK"
        elif [[ $DRM_EXEC == *"<type>ALARM</type>"* ]]; then
            echo "CRITICAL: DRM event alarm"
            exit $DRM_STATUS_CRITICAL
        elif [[ $DRM_EXEC == *"<type>WARNING</type>"* ]]; then
            echo "WARNING: DRM event warning"
            exit $DRM_STATUS_WARNING
        else
            drm_handler
        fi
        if [[ $DRM_STATUS_GLOBAL == "OKOK" ]]; then
            echo "OK: DRM operative and no event"
            exit $DRM_STATUS_OK
        fi
    	;;
    *)
        drm_usage
    	exit $DRM_STATUS_UNKNOWN
    	;;
esac
echo "ERROR: execution failed"
exit $DRM_STATUS_UNKNOWN
