#!/bin/bash
######################################################
#                                                    #
#  icinga2-check_ucopia plugin                       #
#  written by: Harald Thomas (denkteich@gmx.de)      #
#  https://github.com/denkteich/icinga2-check_ucopia #
#                                                    #
######################################################

before=$(date +%s%N)

# parameters:
# -v: snmpversion: 2c or 3, if unset: 2c
#   if 2c:
#     -c: snmpcomunity, if unset: public
#   if 3:
#     -u: user
#     -s: security: authNoPriv, noAuthNoPriv or authPriv,
#                if unset: noAuthNoPriv
#       if authNoPriv:
#         -a: authentication_algorithm: MD5 or SHA, if unset: MD5
#         -A: authentication_password
#       if authPriv:
#         -a: authentication_algorithm: MD5 or SHA, if unset: MD5
#         -A: authentication_password
#         -e: encryption_algorithm: AES or DES, if unset: AES
#         -E: encryption_password
# -h: hostip/name
# -t: type of query:
#                 totalConnectedUsers:
#                   .1.3.6.1.4.1.31218.3.1.0
#                   A value which indicates the number of connected users on this controller.
#                 licensedUsers:
#                   .1.3.6.1.4.1.31218.3.5.0
#                   The number of users that the license allow to be connected simulteaneously.
#                 licensesleft:
#                   calculated value
#                   licensedUsers - totalConnectedUsers
#                   -lw: licensesleft_warning in percent, default 90
#                   -lc: licensesleft_critical in percent, default 95
#                 cpuTemperature:
#                   .1.3.6.1.4.1.31218.3.3.0
#                   Processor temperature (in degree Celsius)
#                   on VM always 0
#                   -cw: cpuTemperature_warning in celsius, default 40
#                   -cc: cpuTemperature_critical in celsius, default 45
#                 diskTemperature:
#                   .1.3.6.1.4.1.31218.3.4.0
#                   Disk temperature (in degree Celsius)
#                   on VM always 0
#                   -dw: diskTemperature_warning in celsius, default 40
#                   -dc: diskTemperature_critical in celsius, default 45
#                 Services:
#                   Indicates the status of xxxxxx service
#                   (1) Running, service is running
#                   (2) Stopped, service is stopped or crashed
#                   (3) Disabled, service is disabled by configuration.
#                     webServerService:
#                       .1.3.6.1.4.1.31218.4.1.0
#                     sqlServerService:
#                       .1.3.6.1.4.1.31218.4.2.0
#                     urlSnifferService:
#                       .1.3.6.1.4.1.31218.4.3.0
#                     portalService:
#                       .1.3.6.1.4.1.31218.4.4.0
#                     webProxyService:
#                       .1.3.6.1.4.1.31218.4.5.0
#                     autodisconnectService:
#                       .1.3.6.1.4.1.31218.4.6.0
#                     printingServerService:
#                       .1.3.6.1.4.1.31218.4.7.0
#                     dhcpServerService:
#                       .1.3.6.1.4.1.31218.4.8.0
#                     dnsServerService:
#                       .1.3.6.1.4.1.31218.4.9.0
#                     staticIpManagerService:
#                       .1.3.6.1.4.1.31218.4.10.0
#                     highAvailabilityService:
#                       .1.3.6.1.4.1.31218.4.11.0
#                     ldapDirectoryService:
#                       .1.3.6.1.4.1.31218.4.12.0
#                     ldapReplicationManagerService:
#                       .1.3.6.1.4.1.31218.4.13.0
#                     timeServerService:
#                       .1.3.6.1.4.1.31218.4.14.0
#                     radiusServerService:
#                       .1.3.6.1.4.1.31218.4.15.0
#                     sambaService:
#                       .1.3.6.1.4.1.31218.4.16.0
#                     sshService:
#                       .1.3.6.1.4.1.31218.4.17.0
#                     syslogService:
#                       .1.3.6.1.4.1.31218.4.18.0
#                     usersLogService:
#                       .1.3.6.1.4.1.31218.4.19.0
#                     pmsClientService:
#                       .1.3.6.1.4.1.31218.4.20.0
#                   if unset: totalConnectedUsers
#

while getopts v:c:s:a:A:e:E:h:t:u:lw:lc:cw:cc:dw:dc: OPT
do
  case "$OPT" in
    v) snmpversion="$OPTARG";;
    c) community="$OPTARG";;
    s) security="$OPTARG" ;;
    a) authentication_algorithm="$OPTARG" ;;
    A) authentication_password="$OPTARG" ;;
    e) encryption_algorithm="$OPTARG" ;;
    E) encryption_password="$OPTARG" ;;
    h) host="$OPTARG" ;;
    t) type="$OPTARG" ;;
    u) user="$OPTARG" ;;
    lw) licensesleft_warning="$OPTARG" ;;
    lc) licensesleft_critical="$OPTARG" ;;
    cw) cpuTemperature_warning="$OPTARG" ;;
    cc) cpuTemperature_critical="$OPTARG" ;;
    dw) diskTemperature_warning="$OPTARG" ;;
    dc) diskTemperature_critical="$OPTARG" ;;
   esac
done

if [ -z ${licensesleft_warning+x} ]; then
    licensesleft_warning=90
fi
if [ -z ${licensesleft_critical+x} ]; then
    licensesleft_critical=95
fi

if [ -z ${cpuTemperature_warning+x} ]; then
    cpuTemperature_warning=40
fi
if [ -z ${cpuTemperature_critical+x} ]; then
    cpuTemperature_critical=45
fi
if [ -z ${diskTemperature_warning+x} ]; then
    diskTemperature_warning=40
fi
if [ -z ${diskTemperature_critical+x} ]; then
    diskTemperature_critical=45
fi


if [ -z ${host+x} ]; then
    echo "host not set"
    exit
fi

if [ -z ${snmpversion+x} ]; then
    snmpversion="2c"
fi
if [ $snmpversion != "2c" ] && [ $snmpversion != "3" ] ; then
    echo "wrong snmpversion, possible versions: 2c or 3"
    exit
fi

if [ $snmpversion = "2c" ] ; then
  if [ -z ${community+x} ]; then
      community="public"
  fi
fi

if [ $snmpversion = "3" ] ; then
  if [ -z ${user+x} ]; then
      echo "user not set"
      exit
  fi
  if [ -z ${security+x} ]; then
      security="noAuthNoPriv"
  fi
  if [ $security = "AuthNoPriv" ]; then
    if [ -z ${authentication_algorithm+x} ]; then
      echo "authentication_algorithm not set"
      exit
    fi
    if [ -z ${authentication_password+x} ]; then
      echo "authentication_password not set"
      exit
    fi
  fi
  if [ $security = "AuthPriv" ]; then
    if [ -z ${authentication_algorithm+x} ]; then
      echo "authentication_algorithm not set"
      exit
    fi
    if [ -z ${authentication_password+x} ]; then
      echo "authentication_password not set"
      exit
    fi
    if [ -z ${encryption_algorithm+x} ]; then
      echo "encryption_algorithm not set"
      exit
    fi
    if [ -z ${encryption_password+x} ]; then
      echo "encryption_password not set"
      exit
    fi
  fi
fi

if [ -z ${type+x} ]; then
    type="totalConnectedUsers"
fi
if [ $type != "totalConnectedUsers" ] && [ $type != "licensesleft" ] && [ $type != "licensedUsers" ] && [ $type != "cpuTemperature" ] && [ $type != "diskTemperature" ] && [ $type != "webServerService" ] && [ $type != "sqlServerService" ] && [ $type != "urlSnifferService" ] && [ $type != "portalService" ] && [ $type != "webProxyService" ] && [ $type != "autodisconnectService" ] && [ $type != "printingServerService" ] && [ $type != "dhcpServerService" ] && [ $type != "dnsServerService" ] && [ $type != "staticIpManagerService" ] && [ $type != "highAvailabilityService" ] && [ $type != "ldapDirectoryService" ] && [ $type != "ldapReplicationManagerService" ] && [ $type != "timeServerService" ] && [ $type != "radiusServerService" ] && [ $type != "sambaService" ] && [ $type != "sshService" ] && [ $type != "syslogService" ] && [ $type != "usersLogService" ] && [ $type != "pmsClientService" ] ; then
    echo "wrong type, possible types: totalConnectedUsers, "
    echo "licensesleft, licensedUsers, cpuTemperature, "
    echo "diskTemperature, webServerService, sqlServerService, "
    echo "urlSnifferService, portalService, webProxyService, "
    echo "autodisconnectService, printingServerService,"
    echo "dhcpServerService, dnsServerService, staticIpManagerService,"
    echo "highAvailabilityService, ldapDirectoryService,"
    echo "ldapReplicationManagerService, timeServerService,"
    echo "radiusServerService, sambaService, sshService, syslogService,"
    echo "usersLogService or pmsClientService"
    exit
fi


case $type in
  "totalConnectedUsers")
    oid=".1.3.6.1.4.1.31218.3.1.0"
    ;;
  "licensedUsers")
    oid=".1.3.6.1.4.1.31218.3.5.0"
    ;;
  "cpuTemperature")
    oid=".1.3.6.1.4.1.31218.3.3.0"
    ;;
  "diskTemperature")
    oid=".1.3.6.1.4.1.31218.3.4.0"
    ;;
  "webServerService")
    oid=".1.3.6.1.4.1.31218.4.1.0"
    ;;
  "sqlServerService")
    oid=".1.3.6.1.4.1.31218.4.2.0"
    ;;
  "urlSnifferService")
    oid=".1.3.6.1.4.1.31218.4.3.0"
    ;;
  "portalService")
    oid=".1.3.6.1.4.1.31218.4.4.0"
    ;;
  "webProxyService")
    oid=".1.3.6.1.4.1.31218.4.5.0"
    ;;
  "autodisconnectService")
    oid=".1.3.6.1.4.1.31218.4.6.0"
    ;;
  "printingServerService")
    oid=".1.3.6.1.4.1.31218.4.7.0"
    ;;
  "dhcpServerService")
    oid=".1.3.6.1.4.1.31218.4.8.0"
    ;;
  "dnsServerService")
    oid=".1.3.6.1.4.1.31218.4.9.0"
    ;;
  "staticIpManagerService")
    oid=".1.3.6.1.4.1.31218.4.10.0"
    ;;
  "highAvailabilityService")
    oid=".1.3.6.1.4.1.31218.4.11.0"
    ;;
  "ldapDirectoryService")
    oid=".1.3.6.1.4.1.31218.4.12.0"
    ;;
  "ldapReplicationManagerService")
    oid=".1.3.6.1.4.1.31218.4.13.0"
    ;;
  "timeServerService")
    oid=".1.3.6.1.4.1.31218.4.14.0"
    ;;
  "radiusServerService")
    oid=".1.3.6.1.4.1.31218.4.15.0"
    ;;
  "sambaService")
    oid=".1.3.6.1.4.1.31218.4.16.0"
    ;;
  "sshService")
    oid=".1.3.6.1.4.1.31218.4.17.0"
    ;;
  "syslogService")
    oid=".1.3.6.1.4.1.31218.4.18.0"
    ;;
  "usersLogService")
    oid=".1.3.6.1.4.1.31218.4.19.0"
    ;;
  "pmsClientService")
    oid=".1.3.6.1.4.1.31218.4.20.0"
    ;;
esac

function function_licensesleft {
  licensesleft=$[$licensedUsers-$totalConnectedUsers]
  percentage_free=$[100 * $licensesleft / $licensedUsers]
  percentage_used=$[100 - $percentage_free]
  result="$type: $licensesleft (used: $percentage_used%)"
  if [ $percentage_used -lt $licensesleft_warning ]; then
    prefix="OK -"
    result="$prefix $result"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$result | licensesleft=${licensesleft} percentage_free=${percentage_free} percentage_used=${percentage_used};$licensesleft_warning;$licensesleft_critical elapsed_time=${elapsed_time}"
    exit 0
  fi

  if [ $percentage_used -lt $licensesleft_critical ]; then
    prefix="WARNING -"
    result="$prefix $result"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$result | licensesleft=${licensesleft} percentage_free=${percentage_free} percentage_used=${percentage_used};$licensesleft_warning;$licensesleft_critical elapsed_time=${elapsed_time}"
    exit 1
  fi

  if [ $percentage_used -ge $licensesleft_critical ]; then
    prefix="CRITICAL -"
    result="$prefix $result"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$result | licensesleft=${licensesleft} percentage_free=${percentage_free} percentage_used=${percentage_used};$licensesleft_warning;$licensesleft_critical elapsed_time=${elapsed_time}"
    exit 2
  fi
}

function function_servicecheck {
  if [ $result = "1" ]; then
    prefix="OK -"
    result="$prefix $type is running"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$result | running=1 stopped=0 disabled=0 elapsed_time=${elapsed_time}"
    exit 0
  fi

  if [ $result = "2" ]; then
    prefix="CRITICAL -"
    result="$prefix $type is stopped or crashed"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$result | running=0 stopped=1 disabled=0 elapsed_time=${elapsed_time}"
    exit 2
  fi

  if [ $result = "3" ]; then
    prefix="OK -"
    result="$prefix $type is disabled by configuration"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$result | running=0 stopped=0 disabled=1 elapsed_time=${elapsed_time}"
    exit 0
  fi

}

function function_totalConnected_Licensed {
  after=$(date +%s%N)
  elapsed_time=$(((after - $before)/1000000))ms
  echo "$type: $result | $type=${result} elapsed_time=${elapsed_time}"
  exit 0
}

function function_cpuTemperature {
  if [ $result -ge  $cpuTemperature_critical ]; then
    prefix="CRITICAL -"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$prefix $type: $result | critical=1 warning=0 OK=0 $type=${result};$cpuTemperature_warning;$cpuTemperature_critical elapsed_time=${elapsed_time}"
    exit 2
  fi
  if [ $result -ge  $cpuTemperature_warning ]; then
    prefix="WARNING -"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$prefix $type: $result | critical=0 warning=1 OK=0 $type=${result};$cpuTemperature_warning;$cpuTemperature_critical elapsed_time=${elapsed_time}"
    exit 1
  fi
  prefix="OK -"
  after=$(date +%s%N)
  elapsed_time=$(((after - $before)/1000000))ms
  echo "$prefix $type: $result | critical=0 warning=0 OK=1 $type=${result};$cpuTemperature_warning;$cpuTemperature_critical elapsed_time=${elapsed_time}"
  exit 0
}

function function_diskTemperature {
  if [ $result -ge  $diskTemperature_critical ]; then
    prefix="CRITICAL -"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$prefix $type: $result | critical=1 warning=0 OK=0 $type=${result};$diskTemperature_warning;$diskTemperature_critical elapsed_time=${elapsed_time}"
    exit 2
  fi
  if [ $result -ge  $diskTemperature_warning ]; then
    prefix="WARNING -"
    after=$(date +%s%N)
    elapsed_time=$(((after - $before)/1000000))ms
    echo "$prefix $type: $result | critical=0 warning=1 OK=0 $type=${result};$diskTemperature_warning;$diskTemperature_critical elapsed_time=${elapsed_time}"
    exit 1
  fi
  prefix="OK -"
  after=$(date +%s%N)
  elapsed_time=$(((after - $before)/1000000))ms
  echo "$prefix $type: $result | critical=0 warning=0 OK=1 $type=${result};$diskTemperature_warning;$diskTemperature_critical elapsed_time=${elapsed_time}"
  exit 0
}

if [ $snmpversion = "2c" ]; then
  if [ $type = "licensesleft" ]; then
    totalConnectedUsers=$(snmpget -v$snmpversion -c "$community" "$host" ".1.3.6.1.4.1.31218.3.1.0" | awk '{print $4}')
    licensedUsers=$(snmpget -v$snmpversion -c "$community" "$host" ".1.3.6.1.4.1.31218.3.5.0" | awk '{print $4}')
    function_licensesleft
  else
    result=$(snmpget -v$snmpversion -c "$community" "$host" "$oid" | awk '{print $4}')
    if [ $type = "webServerService" ] || [ $type = "sqlServerService" ] || [ $type = "urlSnifferService" ] || [ $type = "portalService" ] || [ $type = "webProxyService" ] || [ $type = "autodisconnectService" ] || [ $type = "printingServerService" ] || [ $type = "dhcpServerService" ] || [ $type = "dnsServerService" ] || [ $type = "staticIpManagerService" ] || [ $type = "highAvailabilityService" ] || [ $type = "ldapDirectoryService" ] || [ $type = "ldapReplicationManagerService" ] || [ $type = "timeServerService" ] || [ $type = "radiusServerService" ] || [ $type = "sambaService" ] || [ $type = "sshService" ] || [ $type = "syslogService" ] || [ $type = "usersLogService" ] || [ $type = "pmsClientService" ] ; then
      function_servicecheck
    fi
    if [ $type = "totalConnectedUsers" ] || [ $type = "licensedUsers" ]; then
      function_totalConnected_Licensed
    fi
    if [ $type = "cpuTemperature" ]; then
      function_cpuTemperature
    fi
    if [ $type = "diskTemperature" ]; then
	  function_diskTemperature
    fi
  fi
fi

if [ $snmpversion = "3" ]; then
  if [ $security = "noAuthNoPriv" ]; then
    if [ $type = "licensesleft" ]; then
      totalConnectedUsers=$(snmpget -v$snmpversion -l "$security" -u "$user" "$host" ".1.3.6.1.4.1.31218.3.1.0" | awk '{print $4}')
      licensedUsers=$(snmpget -v$snmpversion -l "$security" -u "$user" "$host" ".1.3.6.1.4.1.31218.3.5.0" | awk '{print $4}')
      function_licensesleft
    else
      result=$(snmpget -v$snmpversion -l "$security" -u "$user" "$host" "$oid" | awk '{print $4}')
      if [ $type = "webServerService" ] || [ $type = "sqlServerService" ] || [ $type = "urlSnifferService" ] || [ $type = "portalService" ] || [ $type = "webProxyService" ] || [ $type = "autodisconnectService" ] || [ $type = "printingServerService" ] || [ $type = "dhcpServerService" ] || [ $type = "dnsServerService" ] || [ $type = "staticIpManagerService" ] || [ $type = "highAvailabilityService" ] || [ $type = "ldapDirectoryService" ] || [ $type = "ldapReplicationManagerService" ] || [ $type = "timeServerService" ] || [ $type = "radiusServerService" ] || [ $type = "sambaService" ] || [ $type = "sshService" ] || [ $type = "syslogService" ] || [ $type = "usersLogService" ] || [ $type = "pmsClientService" ] ; then
        function_servicecheck
      fi
      if [ $type = "totalConnectedUsers" ] || [ $type = "licensedUsers" ]; then
        function_totalConnected_Licensed
      fi
      if [ $type = "cpuTemperature" ]; then
        function_cpuTemperature
      fi
      if [ $type = "diskTemperature" ]; then
	    function_diskTemperature
      fi
    fi
  fi
  if [ $security = "AuthNoPriv" ]; then
    if [ $type = "licensesleft" ]; then
      totalConnectedUsers=$(snmpget -v$snmpversion -l "$security" -u "$user" -a $authentication_algorithm -A $authentication_password "$host" ".1.3.6.1.4.1.31218.3.1.0" | awk '{print $4}')
      licensedUsers=$(snmpget -v$snmpversion -l "$security" -u "$user" -a $authentication_algorithm -A $authentication_password "$host" ".1.3.6.1.4.1.31218.3.5.0" | awk '{print $4}')
      function_licensesleft
    else
      result=$(snmpget -v$snmpversion -l "$security" -u "$user" -a $authentication_algorithm -A $authentication_password "$host" "$oid" | awk '{print $4}')
      if [ $type = "webServerService" ] || [ $type = "sqlServerService" ] || [ $type = "urlSnifferService" ] || [ $type = "portalService" ] || [ $type = "webProxyService" ] || [ $type = "autodisconnectService" ] || [ $type = "printingServerService" ] || [ $type = "dhcpServerService" ] || [ $type = "dnsServerService" ] || [ $type = "staticIpManagerService" ] || [ $type = "highAvailabilityService" ] || [ $type = "ldapDirectoryService" ] || [ $type = "ldapReplicationManagerService" ] || [ $type = "timeServerService" ] || [ $type = "radiusServerService" ] || [ $type = "sambaService" ] || [ $type = "sshService" ] || [ $type = "syslogService" ] || [ $type = "usersLogService" ] || [ $type = "pmsClientService" ] ; then
        function_servicecheck
      fi
      if [ $type = "totalConnectedUsers" ] || [ $type = "licensedUsers" ]; then
        function_totalConnected_Licensed
      fi
      if [ $type = "cpuTemperature" ]; then
        function_cpuTemperature
      fi
      if [ $type = "diskTemperature" ]; then
	    function_diskTemperature
      fi
    fi
  fi
  if [ $security = "AuthPriv" ]; then
    if [ $type = "licensesleft" ]; then
      totalConnectedUsers=$(snmpget -v$snmpversion -l "$security" -u "$user" -a $authentication_algorithm -A $authentication_password -x $encryption_algorithm -X $encryption_password "$host" ".1.3.6.1.4.1.31218.3.1.0" | awk '{print $4}')
      licensedUsers=$(snmpget -v$snmpversion -l "$security" -u "$user" -a $authentication_algorithm -A $authentication_password -x $encryption_algorithm -X $encryption_password "$host" ".1.3.6.1.4.1.31218.3.5.0" | awk '{print $4}')
      function_licensesleft
    else
      result=$(snmpget -v$snmpversion -l "$security" -u "$user" -a $authentication_algorithm -A $authentication_password -x $encryption_algorithm -X $encryption_password "$host" "$oid" | awk '{print $4}')
      if [ $type = "webServerService" ] || [ $type = "sqlServerService" ] || [ $type = "urlSnifferService" ] || [ $type = "portalService" ] || [ $type = "webProxyService" ] || [ $type = "autodisconnectService" ] || [ $type = "printingServerService" ] || [ $type = "dhcpServerService" ] || [ $type = "dnsServerService" ] || [ $type = "staticIpManagerService" ] || [ $type = "highAvailabilityService" ] || [ $type = "ldapDirectoryService" ] || [ $type = "ldapReplicationManagerService" ] || [ $type = "timeServerService" ] || [ $type = "radiusServerService" ] || [ $type = "sambaService" ] || [ $type = "sshService" ] || [ $type = "syslogService" ] || [ $type = "usersLogService" ] || [ $type = "pmsClientService" ] ; then
        function_servicecheck
      fi
      if [ $type = "totalConnectedUsers" ] || [ $type = "licensedUsers" ]; then
        function_totalConnected_Licensed
      fi
      if [ $type = "cpuTemperature" ]; then
        function_cpuTemperature
      fi
      if [ $type = "diskTemperature" ]; then
	    function_diskTemperature
      fi
    fi
  fi
fi

echo "something went wrong...."
exit 3
