#!/usr/bin/env bash
############################# Written and Manteined by Nicola Bandini     ###############
############################# Created and written by Matthias Luettermann ###############
############################# finetuning by primator@gmail.com
############################# finetuning by n.bandini@gmail.com
############################# finetuning by vitamin.b@mailbox.org
############################# with code by Tom Lesniak and Hugo Geijteman
############################# with code by Carsten Koebke
#
# copyright (c) 2008 Shahid Iqbal
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation;
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# contact the author directly for more information at: matthias@xcontrol.de
##########################################################################################
plgVer=1.00

usage() {
  echo
  echo "qnap_health $plgVer"
  echo
  echo "Warning: Wrong command line arguments."
  echo
  echo "Usage: ${0##*/} [-V protocol] -H <hostname> -C <community> -p <part> -w <warning> -c <critical>"
  echo
  echo "Where: -p|--part             - part to check"
  echo "       -h                    - no human-readable output; do not use unit suffixes"
  echo "       -w|--warning          - warning"
  echo "       -c|--critical         - critical"
  echo "       -t|--timeout          - timeout for snmp connect, default: 5s"
  echo "       --help                - show this help"
  echo
  echo "SNMP specific"
  echo "       -H|--hostname         - hostname or IP"
  echo "       -V                    - SNMP protocol version to use (1, 2c, 3); default: 2c"
  echo "       -P|--port             - SNMP port; default: 161"
  echo
  echo "SNMP Version 1|2c specific"
  echo "       -C|--community        - SNMP community name; default: public"
  echo
  echo "SNMP Version 3 specific"
  echo "       -l|--level            - security level (noAuthNoPriv|authNoPriv|authPriv)"
  echo "       -u|--user             - security name"
  echo "       -a|--authprotocol     - authentication protocol (MD5|SHA)"
  echo "       -A|--authpassphrase   - authentication protocol pass phrase"
  echo "       -x|--privprotocol     - privacy protocol (DES|AES)"
  echo "       -X|--privpassphrase   - privacy protocol pass phrase"
  echo
  echo "Parts are: status, sysinfo, systemuptime, temp, cpu, cputemp, freeram, iops, latency, powerstatus, fans, diskused, hdstatus, hdtemp, lunstatus (for iSCSI luns), volstatus (Raid Volume Status)"
  echo
  echo "volstatus & lunstatus checks all vols/luns and vols/lun space; powerstatus checks power supply"
  echo "<#> is 1-8 for hd, 1-5 for vol"
  echo
  echo " Example for diskusage: ${0##*/} -H 127.0.0.1 -C public -p diskused -w 80 -c 95"
  echo
  echo " Example for volstatus: ${0##*/} -H 127.0.0.1 -C public -p volstatus -w 15 -c 10"
  echo "                        critical and warning value are related to free disk space"
  echo
  echo " Example for fans: ${0##*/} -H 127.0.0.1 -C public -p fans -w 2000 -c 1900"
  echo "                   critical and warning are minimum speed in rpm for fans"
  echo
  echo " this plugin requires the following programs to be installed:"
  echo "  * bc"
  echo "  * awk"
  echo "  * snmpget"
  echo "  * snmpwalk"
  exit $EXIT_UNKOWN
}

# Default values
strProtocol="2c"
strCommunity="public"
strPort="161"
timeout="5"

# Exit codes
EXIT_OK="0"
EXIT_WARNING="1"
EXIT_CRITICAL="2"
EXIT_UNKNOWN="3"

# check for needed programs
SNMPGET_BIN=$(which snmpget)
SNMPWALK_BIN=$(which snmpwalk)
BC_BIN=$(which bc)
AWK_BIN=$(which awk)

if [ -z "$SNMPGET_BIN" ]; then
  echo "Could not find 'snmpget'!"
  usage
fi

if [ -z "$SNMPWALK_BIN" ]; then
  echo "Could not find 'snmpwalk'!"
  usage
fi

if [ -z "$BC_BIN" ]; then
  echo "Could not find 'bc'!"
  usage
fi

if [ -z "$AWK_BIN" ]; then
  echo "Could not find 'awk'!"
  usage
fi

# Options

PARSED_OPTIONS=$(getopt -n "$0" -o V:H:C:p:hw:c:t:l:u:a:A:x:X:P: --long "hostname:,community:,part:,warning:,critical:,timeout:,level:,user:,authprotocol:,authpassphrase:,privprotocol:,privpassphrase:,port:,help" -- "$@")
if [ $? -ne 0 ]; then
  usage
  exit $EXIT_UNKOWN
fi

if [ $# -eq 0 ]; then
  usage
  exit $EXIT_UNKOWN
fi
eval set -- "$PARSED_OPTIONS"

while true; do
  case "$1" in
    --help)
      usage
      shift
      ;;
    -C|--community)
      strCommunity="$2"
      shift 2
      ;;
    -P|--port)
      strPort="$2"
      shift 2
      ;;
    -h)
      inhuman=1
      shift
      ;;
    -H|--hostname)
      strHostname="$2"
      shift 2
      ;;
    -p|--part)
      strPart="$2"
      shift 2
      ;;
    -V)
      if [ -n "$2" ]; then
        strProtocol="$2"
        case "$strProtocol" in
          1|2c|3)
            ;;
          *)
            echo "ERROR: wrong protocol version" >&2
            usage
            ;;
        esac
      fi
      shift 2
      ;;
    -w|--warning)
      strWarning="$2"
      shift 2
      ;;
    -c|--critical)
      strCritical="$2"
      shift 2
      ;;
    -t|--timeout)
      timeout="$2"
      shift 2
      ;;
    -l|--level)
      if [ -n "$2" ]; then
        strLevel="$2"
        case "$strLevel" in
          noAuthNoPriv|authNoPriv|authPriv)
            ;;
          *)
            echo "ERROR: wrong security level" >&2
            usage
            ;;
        esac
      fi
      shift 2
      ;;
    -u|--user)
      if [ -n "$2" ]; then
        strUser="$2"
      fi
      shift 2
      ;;
    -a|--authprotocol)
      if [ -n "$2" ]; then
        strAuthprotocol="${2,,}"
        case "$strAuthprotocol" in
          md5|sha)
            ;;
          *)
            echo "ERROR: wrong authentication protocol" >&2
            usage
            ;;
        esac
      fi
      shift 2
      ;;
    -A|--authpassphrase)
      if [ -n "$2" ]; then
        strAuthpassphrase="$2"
      else
        echo "ERROR: authentification passphrase is missing" >&2
        usage
      fi
      shift 2
      ;;
    -x|--privprotocol)
      if [ -n "$2" ]; then
        strPrivprotocol="${2,,}"
        case "$strPrivprotocol" in
          des|aes)
            ;;
          *)
            echo "ERROR: wrong privacy protocol" >&2
            usage
            ;;
        esac
      fi
      shift 2
      ;;
    -X|--privpassphrase)
      if [ -n "$2" ]; then
        strPrivpassphrase="$2"
      else
        echo "ERROR: privacy passphrase is missing" >&2
        exit 3
      fi
      shift 2
      ;;
    --)
      shift
      break
      ;;
  esac
done

if ([ "$strPart" != "sysinfo" -a "$strPart" != "hdstatus" -a "$strPart" != "powerstatus" ]) && ( [ -z "$strWarning" -o -z "$strCritical" ]);then
  echo "UKNOWN: $strPart check needs warning and critical threshold set."
  exit 3
fi

function _cmdparam() {
  case "$strProtocol" in
    1|2c)
      cmd="-v $strProtocol -c $strCommunity"
      ;;
    3)
      cmd="-v $strProtocol -u $strUser -l $strLevel -a $strAuthprotocol -A $strAuthpassphrase -x $strPrivprotocol -X $strPrivpassphrase"
      ;;
  esac

  if [ -n "$strPort" ]; then
    cmd="$cmd $strHostname:$strPort"
  else
    cmd="$cmd $strHostname"
  fi

  echo "$cmd"
}

function _trim() {
  echo $1 | awk '{$1=$1};1'
}

function _snmpget() {
  snmpget $(_cmdparam) "$@"
}

function _snmpwalk() {
  snmpwalk $(_cmdparam) "$@"
}

function _snmpgetval() {
  snmpget $(_cmdparam) -Oqv "$@"
}

function _snmpstatus() {
  snmpstatus $(_cmdparam) "$@"
}

function _get_exp() {
  case "$1" in
    PB) echo "50" ;;
    TB) echo "40" ;;
    GB) echo "30" ;;
    MB) echo "20" ;;
    KB) echo "10" ;;
    '') echo "0" ;;
    *)  echo "ERROR: unknown unit '$1'" ;;
  esac
}
# Check if QNAP is online
TEST="$(_snmpstatus -t $timeout -r 0 2>&1)"
if [ "$TEST" == "Timeout: No Response from $strHostname:$strPort" ]; then
  echo "UNKOWN: SNMP to $strHostname is not available or wrong community string";
  exit $EXIT_UNKNOWN;
fi

# STATUS ---------------------------------------------------------------------------------------------------------------------------------------
if [ "$strPart" == "status" ]; then
  echo "$TEST";

# DISKUSED ---------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "diskused" ]; then
  diskStr="$(_snmpget 1.3.6.1.4.1.24681.1.2.17.1.4.1)"
  freeStr="$(_snmpget 1.3.6.1.4.1.24681.1.2.17.1.5.1)"

  diskSize="$(echo "$diskStr" | awk '{print $4}' | sed 's/.\(.*\)/\1/')"
  freeSize="$(echo "$freeStr" | awk '{print $4}' | sed 's/.\(.*\)/\1/')"
  diskUnit="$(echo "$diskStr" | awk '{print $5}' | sed 's/.*\(.B\).*/\1/')"
  freeUnit="$(echo "$freeStr" | awk '{print $5}' | sed 's/.*\(.B\).*/\1/')"

  diskExp="$(_get_exp "$diskUnit")"
  freeExp="$(_get_exp "$freeUnit")"

  disk="$(echo "scale=0; $diskSize*(2^$diskExp)" | bc -l)"
  free="$(echo "scale=0; $freeSize*(2^$freeExp)" | bc -l)"

  used="$(echo "scale=0; $disk-$free" | bc -l)"
  perc="$(echo "scale=0; $used*100/$disk" | bc -l)"

  diskH="$(echo "scale=2; $disk/(2^$diskExp)" | bc -l)"
  freeH="$(echo "scale=2; $free/(2^$freeExp)" | bc -l)"
  usedH="$(echo "scale=2; $used/(2^$diskExp)" | bc -l)"

  if [ "${inhuman:-0}" -eq 1 ]; then
    diskF="$disk"
    freeF="$free"
    usedF="$used"
  else
    diskF="$diskH$diskUnit"
    freeF="$freeH$freeUnit"
    usedF="$usedH$diskUnit"
  fi

  #wdisk=$(echo "scale=0; $strWarning*$disk/100" | bc -l)
  #cdisk=$(echo "scale=0; $strCritical*$disk/100" | bc -l)

  OUTPUT="Total:$diskF - Used:$usedF - Free:$freeF - Used Space: $perc%|Used=$perc;$strWarning;$strCritical;0;100"

  if [ $perc -ge $strCritical ]; then
    echo "CRITICAL: $OUTPUT"
    exit $EXIT_CRITICAL 
  elif [ $perc -ge $strWarning ]; then
    echo "WARNING: $OUTPUT"
    exit $EXIT_WARNING
  else
    echo "OK: $OUTPUT"
    exit $EXIT_OK
  fi

# CPU ----------------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "cpu" ]; then
  CPU="$(_snmpgetval 1.3.6.1.4.1.24681.1.2.1.0 | sed -E 's/"([0-9.]+) ?%"/\1/')"

  OUTPUT="CPU Load=$CPU%|CPU load=$CPU%;$strWarning;$strCritical;0;100"

  if (( $(echo "$CPU > $strCritical" | bc -l) )); then
    echo "CRITICAL: $OUTPUT"
    exit $EXIT_CRITICAL
  elif ((  $(echo "$CPU > $strWarning" | bc -l) )); then
    echo "WARNING: $OUTPUT"
    exit $EXIT_WARNING
  else
    echo "OK: $OUTPUT"
    exit $EXIT_OK
  fi

# CPUTEMP ----------------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "cputemp" ]; then
  TEMP0="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.5.0 | sed -E 's/"([0-9.]+) ?C.*/\1/')"
  OUTPUT="CPU Temperature: ${TEMP0}°C|NAS CPUtemperature=${TEMP0}C;$strWarning;$strCritical;;"

  if [ "$TEMP0" -ge "$strCritical" ]; then
    echo "CRITICAL: $OUTPUT"
    exit $EXIT_CRITICAL
  elif [ "$TEMP0" -ge "$strWarning" ]; then
      echo "WARNING: $OUTPUT"
      exit $EXIT_WARNING
  else
    echo "OK: $OUTPUT"
    exit $EXIT_OK
  fi

# Free RAM---------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "freeram" ]; then
  totalMemStr="$(_snmpgetval 1.3.6.1.4.1.24681.1.2.2.0)"
  freeMemStr="$(_snmpgetval 1.3.6.1.4.1.24681.1.2.3.0)"

  totalMemSize="$(echo "$totalMemStr" | sed -E 's/"([0-9.]+) ?.?B"/\1/')"
  freeMemSize="$(echo "$freeMemStr" | sed -E 's/"([0-9.]+) ?.?B"/\1/')"
  totalMemUnit="$(echo "$totalMemStr" | sed -E 's/"[0-9.]+ ?(.?B)"/\1/')"
  freeMemUnit="$(echo "$freeMemStr" | sed -E 's/"[0-9.]+ ?(.?B)"/\1/')"

  totalMemExp="$(_get_exp "$totalMemUnit")"
  freeMemExp="$(_get_exp "$freeMemUnit")"

  totalMem="$(echo "scale=0; $totalMemSize*(2^$totalMemExp)" | bc -l)"
  freeMem="$(echo "scale=0; $freeMemSize*(2^$freeMemExp)" | bc -l)"

  usedMem="$(echo "scale=0; $totalMem-$freeMem" | bc -l)"
  percMem="$(echo "scale=0; $usedMem*100/$totalMem" | bc -l)"

  totalMemH="$(echo "scale=1; $totalMem/(2^$totalMemExp)" | bc -l)"
  freeMemH="$(echo "scale=1; $freeMem/(2^$freeMemExp)" | bc -l)"
  usedMemH="$(echo "scale=1; $usedMem/(2^$freeMemExp)" | bc -l)"

  if [ "${inhuman:-0}" -eq 1 ]; then
    totalMemF="$totalMem"
    freeMemF="$freeMem"
    usedMemF="$usedMem"
  else
    totalMemF="$totalMemH$totalMemUnit"
    freeMemF="$freeMemH$freeMemUnit"
    usedMemF="$usedMemH$freeMemUnit"
  fi

  OUTPUT="Total:$totalMemF - Used:$usedMemF - Free:$freeMemF = $percMem%|Memory usage=$percMem%;$strWarning;$strCritical;0;100"

  if [ $percMem -ge $strCritical ]; then
    echo "CRITICAL: $OUTPUT"
    exit $EXIT_CRITICAL
  elif [ $percMem -ge $strWarning ]; then
    echo "WARNING: $OUTPUT"
    exit $EXIT_WARNING
  else
    echo "OK: $OUTPUT"
    exit $EXIT_OK
  fi

# System Temperature---------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "temp" ]; then
  TEMP0="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.6.0 | sed -E 's/"([0-9.]+) ?C.*/\1/')"
  OUTPUT="Temperature: ${TEMP0}°C|NAS temperature=${TEMP0}C;$strWarning;$strCritical;;"

  if [ "$TEMP0" -ge "$strCritical" ]; then
    echo "CRITICAL: $OUTPUT"
    exit $EXIT_CRITICAL
  elif [ "$TEMP0" -ge "$strWarning" ]; then
    echo "WARNING: $OUTPUT"
    exit $EXIT_WARNING
  else
    echo "OK: $OUTPUT"
    exit $EXIT_OK
  fi

# Disk IOPS ----------------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strpart" == "iops" ]; then
 IOPS=$(_snmpgetval 1.3.6.1.4.1.24681.1.4.1.11.5.6.2.1.3.1 | sed -E 's/"|\s%//g')

 OUTPUT="IOPS="$IOPS"|IOPS="$IOPS";$strWarning;$strCritical;0;100"

 if (( $(echo "$IOPS > $strCritical" | bc -l) )); then
   echo "CRITICAL: $OUTPUT"
   exit $EXIT_CRITICAL
 elif ((  $(echo "$IOPS > $strWarning" | bc -l) )); then
   echo "WARNING: $OUTPUT"
   exit $EXIT_WARNING
 else
   echo "OK: $OUTPUT"
   exit $EXIT_OK
 fi

# Disk Latency ----------------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strpart" == "latency" ]; then
  LATENCY=$(_snmpgetval 1.3.6.1.4.1.24681.1.4.1.11.5.6.2.1.4.1 | sed -E 's/"|\s%//g')

  OUTPUT="Latency="$LATENCY"|Latency="$LATENCY";$strWarning;$strCritical;0;100"

  if (( $(echo "$LATENCY > $strCritical" | bc -l) )); then
    echo "CRITICAL: $OUTPUT"
    exit $EXIT_CRITICAL
  elif ((  $(echo "$LATENCY > $strWarning" | bc -l) )); then
    echo "WARNING: $OUTPUT"
    exit $EXIT_WARNING
  else
    echo "OK: $OUTPUT"
    exit $EXIT_OK
  fi

# HD# Temperature---------------------------------------------------------------------------------------------------------------------------------------
elif [[ "$strPart" == hdtemp ]]; then
  PERFOUTPUT=""
  UnknownStr=""
  CriticalStr=""
  WarningStr=""
  EXIT=0

  hdnum="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.10.0)"
  for (( c=1; c<=$hdnum; c++ ))
  do
    TEMPHD="$(_snmpgetval "1.3.6.1.4.1.24681.1.2.11.1.3.$c" | sed -E 's/"([0-9.]+) ?C.*/\1/')"

    if [ "$(echo "$TEMPHD" | sed -E 's/[0-9.]+//g')" != "" ]; then
      if [ -n "$UnknownStr"]; then
        UnknownStr="${UnknownStr},HDD${c}"
      else
        UnknownStr="UNKNOWN: HD${c}"
      fi
    fi
    if [ $TEMPHD -ge $strCritical ]; then
      if [ -n "$CriticalStr" ]; then
        CriticalStr="${CriticalStr}, HDD${c}: ${TEMPHD}°C"
      else
        CriticalStr="CRITICAL: HDD${c}: ${TEMPHD}°C"
      fi
    elif [ $TEMPHD -ge $strWarning -a $TEMPHD -lt $strCritical ]; then
      if [ -n "$WarningStr" ]; then
        WarningStr="${WarningStr}, HDD${c}: ${TEMPHD}°C"
      else
        WarningStr="WARNING: HDD${c}: ${TEMPHD}°C"
      fi
    fi
    if [ -n "$PERFOUTPUT" ]; then
      PERFOUTPUT="${PERFOUTPUT} HDD${c}_temperature=${TEMPHD};$strWarning;$strCritical;;"
    else
      PERFOUTPUT="HDD${c}_temperature=${TEMPHD};$strWarning;$strCritical;;"
    fi
  done

  if [ -z "$CriticalStr" -a -z "$WarningStr" -a -z "$UnknownStr" ]; then
    OUTPUT="OK: All HDD are fine."
  else
    if [ -n "$CriticalStr" ]; then
      EXIT=$EXIT_CRITICAL
      OUTPUT="$CriticalStr"
    fi
    if [ -n "$WarningStr" ]; then
      if [ ! $EXIT -eq 2 ]; then
        EXIT=$EXIT_WARNING
      fi
      OUTPUT="$OUTPUT $WarningStr"
    fi
    if [ -n "$UnknownStr" ];then
      if [ ! $EXIT -eq 1 -o ! $EXIT -eq 2 ];then
        EXIT=$EXIT_UNKNOWN
      fi
      OUTPUT="$OUTPUT $UnknownStr"
    fi
  fi
  echo "$(_trim "$OUTPUT")|$PERFOUTPUT"
  exit $EXIT

# HD Status----------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "hdstatus" ]; then
  hdnum="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.10.0)"
  hdok=0
  hdnok=0
  hdnop=0
  output_crit=""

  for (( c=1; c<=$hdnum; c++ ))
  do
    HD="$(_snmpgetval "1.3.6.1.4.1.24681.1.2.11.1.7.$c" | sed 's/^"\(.*\).$/\1/')"

    if [ "$HD" == "GOOD" ]; then
      ((hdok+=1))
    elif [ "$HD" == "--" ]; then
      ((hdnop+=1))
    else
      output_crit="${output_crit} Disk ${c}"
      ((hdnok+=1))
    fi
  done

  PERFDATA="|hdok=$hdok;;;; hdnok=$hdnok;;;; unused=$hdnop;;;;"

  if [ -n "$output_crit" ]
  then
    echo "CRITICAL: $(_trim "${output_crit}")$PERFDATA"
    exit $EXIT_CRITICAL
  else
    echo "OK: Online Disk $hdok, Free Slot ${hdnop}$PERFDATA"
    exit $EXIT_OK
  fi

# LUN Status----------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "lunstatus" ]; then
  ALLOUTPUT=""
  PERFOUTPUT=""
  WARNING=0
  CRITICAL=0
  lun=1
  lunCOUNT="$(_snmpwalk .1.3.6.1.4.1.24681.1.4.1.1.2.1.10.2.1.2 | wc -l)"

  #if [ "$strWarning" -lt "$strCritical" ]; then
  #  echo "Warning threshold ($strWarning) is less than critical threshold ($strCritical) ! -- Are you sure ?"
  #fi
  while [ "$lun" -le "$lunCOUNT" ]; do
    lun_Status="$(_snmpgetval ".1.3.6.1.4.1.24681.1.4.1.1.2.1.10.2.1.5.$lun" | sed 's/^"\(.*\).$/\1/')"
    lun_name="$(_snmpgetval ".1.3.6.1.4.1.24681.1.4.1.1.2.1.10.2.1.6.$lun" | sed 's/^"\(.*\).$/\1/')"
    if [ "$lun_Status" == "Enabled" ]; then
      lunSTAT="OK($lun_Status)"
    else
      lunSTAT="CRITICAL($lun_Status)"
      CRITICAL=1
    fi

    lunCpctStr="$(_snmpgetval ".1.3.6.1.4.1.24681.1.4.1.1.2.1.10.2.1.3.$lun")"
    lunUsedPct="$(_snmpgetval ".1.3.6.1.4.1.24681.1.4.1.1.2.1.10.2.1.4.$lun")"

    lunCpct="$(echo "scale=0; $lunCpctStr*(2^0)" | bc -l)"
    lunUsedCpct="$(echo "scale=0; $lunCpct/100*$lunUsedPct" | bc -l)"
    lunUsedWarn="$(echo "scale=0; $lunCpct/100*$strWarning" | bc -l)"
    lunusedCrit="$(echo "scale=0; $lunCpct/100*$strCritical" | bc -l)"

    if [ "$lunUsedPct" -ge "$strCritical" ]; then
      lunUsedPctStr="CRITICAL($lunUsedPct)"
      CRITICAL=1
    elif [ "$lunUsedPct" -ge "$strWarning" -a "$lunUsedPct" -lt "$strCritical" ]; then
      lunUsedPctStr="WARNING($lunUsedPct)"
      WARNING=1
    fi

    if [ -n "$ALLOUTPUT" ] ; then
      ALLOUTPUT="${ALLOUTPUT} :: "
    fi
    if [ "$lunUsedPctStr" != "" ]; then
      ALLOUTPUT="${ALLOUTPUT}$lun_name: $lunSTAT, used: ${lunUsedPctStr}%"
    fi

    #Performance Data
    if [ $lun -gt 1 ]; then
      PERFOUTPUT="$PERFOUTPUT "
    fi
    PERFOUTPUT="${PERFOUTPUT}usedpct_$lun_name=${lunUsedPct}%;$strWarning;$strCritical;0;100 used_$lun_name=${lunUsedCpct}b;$lunUsedWarn;$lunusedCrit;0;$lunCpct"

    lun="`expr $lun + 1`"
    unset lunUsedPctStr
  done

  if [ -z $ALLOUTPUT ]; then
    ALLOUTPUT="OK: All luns are fine."
  fi
  echo "$ALLOUTPUT| $PERFOUTPUT"

  if [ $CRITICAL -eq 1 ]; then
    exit $EXIT_CRITICAL
  elif [ $WARNING -eq 1 ]; then
    exit $EXIT_WARNING
  else
    exit $EXIT_OK
  fi
# Volume Status----------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "volstatus" ]; then
  ALLOUTPUT=""
  PERFOUTPUT=""
  WARNING=0
  CRITICAL=0
  VOL=1
  VOLCOUNT="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.16.0)"

  if [ "$strWarning" -lt "$strCritical" ]; then
    echo "Warning threshold ($strWarning) is less than critical threshold ($strCritical) ! -- Are you sure ?"
  fi

  while [ "$VOL" -le "$VOLCOUNT" ]; do
    Vol_Status="$(_snmpgetval ".1.3.6.1.4.1.24681.1.2.17.1.6.$VOL" | sed 's/^"\(.*\).$/\1/')"

    if [ "$Vol_Status" == "Ready" ]; then
      VOLSTAT="OK: $Vol_Status"
    elif [ "$Vol_Status" == "Rebuilding..." ]; then
      VOLSTAT="WARNING: $Vol_Status"
      WARNING=1
    else
      VOLSTAT="CRITICAL: $Vol_Status"
      CRITICAL=1
    fi

    volCpctStr="$(_snmpget ".1.3.6.1.4.1.24681.1.2.17.1.4.$VOL")"
    volFreeStr="$(_snmpget ".1.3.6.1.4.1.24681.1.2.17.1.5.$VOL")"

    volCpctSize="$(echo "$volCpctStr" | awk '{print $4}' | sed 's/.\(.*\)/\1/')"
    volFreeSize="$(echo "$volFreeStr" | awk '{print $4}' | sed 's/.\(.*\)/\1/')"
    volCpctUnit="$(echo "$volCpctStr" | awk '{print $5}' | sed 's/.*\(.B\).*/\1/')"
    volFreeUnit="$(echo "$volFreeStr" | awk '{print $5}' | sed 's/.*\(.B\).*/\1/')"

    volCpctExp="$(_get_exp "$volCpctUnit")"
    volFreeExp="$(_get_exp "$volFreeUnit")"

    volCpct="$(echo "scale=0; $volCpctSize*(2^$volCpctExp)" | bc -l)"
    volFree="$(echo "scale=0; $volFreeSize*(2^$volFreeExp)" | bc -l)"
    volUsed="$(echo "scale=0; $volCpct-$volFree" | bc -l)"

    volFreePct="$(echo "scale=0; $volFree*100/$volCpct" | bc -l)"
    volUsedPct="$(echo "scale=0; $volUsed*100/$volCpct" | bc -l)"

    volCpctH="$(echo "scale=2; $volCpct/(2^$volCpctExp)" | bc -l)"
    volFreeH="$(echo "scale=2; $volFree/(2^$volFreeExp)" | bc -l)"
    volUsedH="$(echo "scale=2; $volUsed/(2^$volFreeExp)" | bc -l)"

    if [ "${inhuman:-0}" -eq 1 ]; then
      volCpctF="$volCpct"
      volFreeF="$volFree"
      volUsedF="$volUsed"
    else
      volCpctF="$volCpctH $volCpctUnit"
      volFreeF="$volFreeH $volFreeUnit"
      volUsedF="$volUsedH $volFreeUnit"
    fi

    if [ "$volFreePct" -le "$strCritical" ]; then
      volFreePct="CRITICAL: $volFreePct"
      CRITICAL=1
    elif [ "$volFreePct" -le "$strWarning" ]; then
      volFreePct="WARNING: $volFreePct"
      WARNING=1
    fi

    ALLOUTPUT="${ALLOUTPUT}Volume #$VOL: $VOLSTAT, Total Size (bytes): $volCpctF, Free: $volFreeF (${volFreePct}%)"
    if [ "$VOL" -lt "$VOLCOUNT" ]; then
      ALLOUTPUT="${ALLOUTPUT}, "
    fi

    #Performance Data
    if [ $VOL -gt 1 ]; then
      PERFOUTPUT="$PERFOUTPUT "
    fi
    PERFOUTPUT="${PERFOUTPUT}FreeSize_Volume-$VOL=${volFreePct}%;$strWarning;$strCritical;0;100"

    VOL="`expr $VOL + 1`"
  done

  echo "$ALLOUTPUT|$PERFOUTPUT"

  if [ $CRITICAL -eq 1 ]; then
    exit $EXIT_CRITICAL
  elif [ $WARNING -eq 1 ]; then
    exit $EXIT_WARNING
  else
    exit $EXIT_OK
  fi

# Power Supply Status  ----------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "powerstatus" ]; then
  ALLOUTPUT=""
  WARNING=0
  CRITICAL=0
  PS=1
  COUNT="$(_snmpgetval .1.3.6.1.4.1.24681.1.4.1.1.1.1.3.1.0)"

  if [[ "$COUNT" == 'No Such Object'* ]]; then
    echo "UNKOWN: Could not get information about powersupplies"
    exit $EXIT_UNKNOWN
  fi

  while [ "$PS" -le "$COUNT" ]; do
    STATUS="$(_snmpgetval ".1.3.6.1.4.1.24681.1.4.1.1.1.1.3.2.1.4.$PS")"
    if [ "$STATUS" -eq 0 ]; then
      PSSTATUS="OK: GOOD"
    else
      PSSTATUS="CRITICAL: ERROR"
      CRITICAL=1
    fi
    ALLOUTPUT="${ALLOUTPUT}Power Supply #$PS - $PSSTATUS"
    if [ "$PS" -lt "$COUNT" ]; then
      ALLOUTPUT="$ALLOUTPUT\n"
    fi
    PS="`expr $PS + 1`"
  done

  echo "$ALLOUTPUT"

  if [ $CRITICAL -eq 1 ]; then
    exit $EXIT_CRITICAL
  elif [ $WARNING -eq 1 ]; then
    exit $EXIT_WARNING
  else
    exit $EXIT_OK
  fi

# Fan Status----------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "fans" ]; then
  ALLOUTPUT=""
  PERFOUTPUT=""
  WARNING=0
  CRITICAL=0
  FAN=1
  FANCOUNT="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.14.0)"

  if [ "$strWarning" -lt "$strCritical" ]; then
    echo "Warning threshold ($strWarning) is less than critical threshold ($strCritical) ! -- Are you sure ?"
  fi

  while [ "$FAN" -le "$FANCOUNT" ]; do
    FANSPEED="$(_snmpgetval ".1.3.6.1.4.1.24681.1.2.15.1.3.$FAN" | sed -E 's/"([0-9]+) ?RPM"/\1/')"

    #Performance data
    if [ $FAN -gt 1 ]; then
      PERFOUTPUT="$PERFOUTPUT "
    fi
    PERFOUTPUT="${PERFOUTPUT}Fan-$FAN=$FANSPEED;$strWarning;$strCritical;;"

    if [ "$FANSPEED" == "" ]; then
      FANSTAT="CRITICAL: $FANSPEED RPM"
      CRITICAL=1
    elif [ "$FANSPEED" -le "$strCritical" ]; then
      FANSTAT="CRITICAL: $FANSPEED RPM"
      CRITICAL=1
    elif [ "$FANSPEED" -le "$strWarning" ]; then
      FANSTAT="WARNING: $FANSPEED RPM"
      WARNING=1
    else
      FANSTAT="OK: $FANSPEED RPM"
    fi

    ALLOUTPUT="${ALLOUTPUT}Fan #${FAN}: $FANSTAT"
    if [ "$FAN" -lt "$FANCOUNT" ]; then
      ALLOUTPUT="${ALLOUTPUT}, "
    fi
    FAN="`expr $FAN + 1`"
  done

  echo "$ALLOUTPUT|$PERFOUTPUT"

  if [ $CRITICAL -eq 1 ]; then
    exit $EXIT_CRITICAL
  elif [ $WARNING -eq 1 ]; then
    exit $EXIT_WARNING
  else
    exit $EXIT_OK
  fi

# System Uptime----------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "systemuptime" ]; then
  EXIT=0
  netuptime="$(echo "scale=0; $(_snmpget .1.3.6.1.2.1.1.3.0 | sed -r 's/.*\(([0-9]+)\).*/\1/g')/100" | bc -l)"
  sysuptime="$(echo "scale=0; $(_snmpget .1.3.6.1.2.1.25.1.1.0 | sed -r 's/.*\(([0-9]+)\).*/\1/g')/100" | bc -l)"

  if [ -z $sysuptime -o -z $netuptime ]; then
    OUTPUT="UNKOWN: Could not get uptime information!"
    EXIT=$EXIT_UNKOWN
  fi
  if [ "$sysuptime" -le "$strCritical" ]; then
    OUTPUT="CRITICAL: System up since $(date -d @$(( $(date +%s) - $sysuptime )))"
    EXIT=$EXIT_CRITICAL
  elif [ "$sysuptime" -le "$strWarning" ]; then
    OUTPUT="WARNING: System up since $(date -d @$(( $(date +%s) - $sysuptime )))"
    EXIT=$EXIT_WARNING
  elif [ "$netuptime" -le "$strCritical" ]; then
    OUTPUT="$OUTPUT CRITICAL: Network up since $(date -d @$(( $(date +%s) - $netuptime )))"
    EXIT=$EXIT_CRITICAL
  elif [ "$netuptime" -le "$strWarning" ]; then
    OUTPUT="$OUTPUT WARNING: Network up since $(date -d @$(( $(date +%s) - $netuptime )))"
    if [ ! $EXIT -eq 2 ];then
      EXIT=$EXIT_WARNING
    fi
  else
    OUTPUT="OK: System up since $(date -d @$(( $(date +%s) - $sysuptime ))) - Network up since $(date -d @$(( $(date +%s) - $netuptime )))"
  fi

  PERFDATA="| system_uptime=${sysuptime}s;$strWarning;$strCritical;; network_uptime=${netuptime}s;$strWarning;$strCritical;;"

  echo "$(_trim "$OUTPUT $PERFDATA")"
  exit $EXIT

# System Info------------------------------------------------------------------------------------------------------------------------------------------
elif [ "$strPart" == "sysinfo" ]; then
  model="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.12.0 | sed 's/^"\(.*\).$/\1/')"
  hdnum="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.10.0)"
  VOLCOUNT="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.16.0)"
  lunCOUNT="$(_snmpwalk .1.3.6.1.4.1.24681.1.4.1.1.2.1.10.2.1.2 | wc -l)"
  name="$(_snmpgetval .1.3.6.1.4.1.24681.1.2.13.0 | sed 's/^"\(.*\)$/\1/')"
  firmware="$(_snmpgetval .1.3.6.1.2.1.47.1.1.1.1.9.1 | sed 's/^"\(.*\)$/\1/')"

  echo "NAS $name, Model $model, Firmware $firmware, Max HD number $hdnum, No. Volume $VOLCOUNT, No. iSCSI LUNs $lunCOUNT"
  exit $EXIT_OK

#----------------------------------------------------------------------------------------------------------------------------------------------------
else
  echo "Unknown Part!" && exit $EXIT_UNKNOWN
fi
exit $EXIT_OK
