#!/bin/bash

#
# Check_service
#
# Copyright 2008 IBCL, 2018 ITEG IT-Engineers GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License"), see LICENSE.txt
#

# Please call 
#   check_service --help 
# for usage information


if [ $(echo " $* " |egrep -i -- " (-h|--help) ") ]
then
  echo '
Service check plugin for Nagios/Icinga

Usage: check_service servicename [servicepackage [[--tryrestart]]
       check_service -h|--help
       check_service -v|--version

Options:
servicename
    Name of systemctl services or script in /etc/init.d/
servicepackage
    Name of deb resp. rpm package to get the service version from
-h, --help
    Prints this help text
--tryrestart
    Tries to restart a dead service.

'
  exit 0
fi

echo "$0 called with options $*" >/tmp/check_service_${SERVICENAME}.log

# reading parameters
export SERVICENAME="$1"
shift
export SERVICEPKG="$1"
if [ "${SERVICEPKG}" == "--tryrestart" ] ; then
  SERVICEPKG="$SERVICENAME"
else
  shift
fi
if [ -z "${SERVICEPKG}" ] ; then
  SERVICEPKG="$SERVICENAME"
fi
export CHKOPTIONS="$*"

( echo "Service name:  '${SERVICENAME}'" >>/tmp/check_service.log
  echo "Service pkg:   '${SERVICEPKG}'"  >>/tmp/check_service.log
  echo "Other options: '${CHKOPTIONS}'"  >>/tmp/check_service.log
) >>/tmp/check_service_${SERVICENAME}.log


export RC4OK=0
export RC4WARNING=1
export RC4CRITICAL=2
export RC4UNKNOWN=3


if [ "$SERVICENAME" == "" ]
then
  echo "Service UNKNOWN: No servicename specified."
  echo "Usage: "
  echo "  $0 servicename [--tryrestart] [--help]"
  echo "  $0 [--help]"
  exit $RC4UNKNOWN
fi  

if [ -f /usr/share/sysvinit/inittab ] ; then
  #echo "DEBUG: Detected sysvinit environment"
  if [ ! -x /etc/init.d/$SERVICENAME ] ; then
    echo "ERROR: Service ${SERVICENAME} not found. Detected sysvinit-core but no file /etc/init.d/${SERVICENAME}"
    exit $RC4CRITICAL
  fi
else
  export SYSTEMCTLBIN="systemctl"
  if [ -x "/usr/bin/systemctl" ] ; then
    SYSTEMCTLBIN="/usr/bin/systemctl"
    #echo "DEBUG: Detected /usr/bin/systemctl"
  fi
  if [ -x "/bin/systemctl" ] ; then
    SYSTEMCTLBIN="/bin/systemctl"
    #echo "DEBUG: Detected /bin/systemctl"
  fi
  #echo "DEBUG: Assuming systemctl environment"
  SYSCTLSERVICE=$(${SYSTEMCTLBIN} list-units -t service --all ${SERVICENAME}.service 2>&1 |grep ${SERVICENAME}.service)
  if [ -z "${SYSCTLSERVICE}" ] ; then
    echo "ERROR: Service ${SERVICENAME} not found. Detected sysctl but systemctl list-units -t service --all ${SERVICENAME}.service shows nothing."
    exit $RC4CRITICAL
  fi
fi

export STATUSMSG="UNKNOWN"

export SERVICEVERSION="?"
if [ -e "/etc/debian_version" ] ; then
  SERVICEVERSION=$(LANG="C" dpkg -s ${SERVICEPKG} 2>/dev/null |egrep "^Version:" |head -1 |cut -f2- -d' ')
  #echo "DEBUG: Detected version of deb package ${SERVICEPKG} as ${SERVICEVERSION}"
else
  SERVICEVERSION=$(LANG="C" rpm -qi ${SERVICEPKG} 2>/dev/null |egrep "^Version" |cut -f2- -d':' |tr -d ' ')
  #echo "DEBUG: Detected version of rpm package ${SERVICEPKG} as ${SERVICEVERSION}"
fi
#echo "DEBUG: Detected version of package ${SERVICEPKG} as ${SERVICEVERSION}"

export SERVICESTATUSRC="?"

if [ -z "${SYSTEMCTLBIN}" ] ; then
  # init.d
  STATUSMSG=$(/etc/init.d/$SERVICENAME status 2>&1)
  SERVICESTATUSRC=$?
else
  # systemctl
  STATUSMSG=$(${SYSTEMCTLBIN} status ${SERVICENAME}.service 2>&1)
  SERVICESTATUSRC=$?
fi
#echo "DEBUG: Service status RC: ${SERVICESTATUSRC}"

if [ ${SERVICESTATUSRC} == 0 ]
then
  echo "Service ${SERVICENAME} (${SERVICEPKG} ${SERVICEVERSION}) is running."
  #echo "$STATUSMSG"
  exit 0
fi

if [ $(echo " ${CHKOPTIONS} " |egrep -i -- " --tryrestart ") ] ; then
  export RESTARTMSG="?"
  export STATUSMSG2="?"
  export SERVICESTATUSRC2="?"
  if [ -z "${SYSTEMCTLBIN}" ] ; then
    # init.d
    RESTARTMSG=$(sudo /etc/init.d/$SERVICENAME restart 2>&1)
    sleep 1
    STATUSMSG2=$(/etc/init.d/$SERVICENAME status 2>&1)
    SERVICESTATUSRC2=$?
  else
    # systemctl
    RESTARTMSG=$(sudo ${SYSTEMCTLBIN} restart ${SERVICENAME}.service 2>&1)
    sleep 1
    STATUSMSG2=$(${SYSTEMCTLBIN} status ${SERVICENAME}.service 2>&1)
    SERVICESTATUSRC2=$?
  fi
  if [ $SERVICESTATUSRC2 == 0 ] ; then
    echo "Service ${SERVICENAME} (${SERVICEPKG} ${SERVICEVERSION}) is WARNING: Service had to be restarted."
    # echo "$RESTARTMSG" ; echo "$STATUSMSG2"
    exit $RC4WARNING
  else
    echo "Service ${SERVICENAME} (${SERVICEPKG} ${SERVICEVERSION}) is CRITICAL, RESTART FAILED."
    #echo "$RESTARTMSG" ; echo "$STATUSMSG2"
    exit $RC4CRITICAL
  fi
fi

echo "Service ${SERVICENAME} (${SERVICEPKG} ${SERVICEVERSION}) is CRITICAL. No restart was attempted."
# echo "$STATUSMSG"
exit $RC4CRITICAL

