Added linux_has_systemd_service(): check if systemd unit file is installed

This commit is contained in:
patvdv 2018-11-18 21:25:49 +01:00
parent bd271a5345
commit 18aa1be5da
7 changed files with 100 additions and 14 deletions

View File

@ -212,6 +212,24 @@ print ${_HAS_NM}
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: linux_has_systemd_service()
# DOES: check if a systemd service is present (unit file)
# EXPECTS: name of service [string]
# OUTPUTS: 0=not installed; 1=installed
# RETURNS: 0=success; 1=error
# REQUIRES: n/a
function linux_has_systemd_service
{
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _RC=0
systemctl list-unit-files 2>/dev/null | grep -c "^{$1}" 2>/dev/null
_RC=$?
return ${_RC}
}
#******************************************************************************
# END of script
#******************************************************************************

View File

@ -26,6 +26,7 @@
# @(#) 2017-05-08: fix fall-back for sysv->pgrep [Patrick Van der Veken]
# @(#) 2017-05-17: removed _MSG dupe [Patrick Van der Veken]
# @(#) 2018-05-21: STDERR fixes [Patrick Van der Veken]
# @(#) 2018-11-18: add linux_has_systemd_service() [Patrick Van der Veken]
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
@ -36,7 +37,7 @@ function check_linux_httpd_status
# ------------------------- CONFIGURATION starts here -------------------------
typeset _HTTPD_INIT_SCRIPT="/etc/init.d/httpd"
typeset _HTTPD_SYSTEMD_SERVICE="httpd.service"
typeset _VERSION="2018-05-21" # YYYY-MM-DD
typeset _VERSION="2018-11-18" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
@ -45,6 +46,7 @@ typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}"
typeset _ARGS=$(data_space2comma "$*")
typeset _ARG=""
typeset _CHECK_SYSTEMD_SERVICE=0
typeset _HTTPD_BIN=""
typeset _MSG=""
typeset _STC=0
@ -65,7 +67,14 @@ done
linux_get_init
case "${LINUX_INIT}" in
'systemd')
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_HTTPD_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_HTTPD_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_HTTPD_SYSTEMD_SERVICE}}"
_RC=1
fi
;;
'upstart')
warn "code for upstart managed systems not implemented, NOOP"

View File

@ -25,6 +25,7 @@
# @(#) 2013-12-01: initial version [Patrick Van der Veken]
# @(#) 2017-05-08: fix fall-back for sysv->pgrep [Patrick Van der Veken]
# @(#) 2018-05-21: STDERR fixes [Patrick Van der Veken]
# @(#) 2018-11-18: add linux_has_systemd_service() [Patrick Van der Veken]
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
@ -33,7 +34,7 @@
function check_linux_named_status
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _VERSION="2018-05-21" # YYYY-MM-DD
typeset _VERSION="2018-11-18" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
@ -44,6 +45,7 @@ typeset _ARGS=$(data_space2comma "$*")
typeset _ARG=""
typeset _MSG=""
typeset _STC=0
typeset _CHECK_SYSTEMD_SERVICE=0
typeset _NAMED_CHECKCONF_BIN=""
typeset _NAMED_INIT_SCRIPT=""
typeset _NAMED_SYSTEMD_SERVICE=""
@ -76,7 +78,14 @@ esac
linux_get_init
case "${LINUX_INIT}" in
'systemd')
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_NAMED_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_NAMED_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_NAMED_SYSTEMD_SERVICE}}"
_RC=1
fi
;;
'upstart')
warn "code for upstart managed systems not implemented, NOOP"

View File

@ -27,6 +27,7 @@
# @(#) 2018-10-28: fixed (linter) errors [Patrick Van der Veken]
# @(#) 2018-10-31: added support for chronyd, --dump-logs
# @(#) & --log-healthy [Patrick Van der Veken]
# @(#) 2018-11-18: add linux_has_systemd_service() [Patrick Van der Veken]
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#------------------------------------------------------------------------------
@ -40,7 +41,7 @@ typeset _CHRONY_INIT_SCRIPT="/etc/init.d/chrony"
typeset _NTPD_INIT_SCRIPT="/etc/init.d/ntpd"
typeset _CHRONYD_SYSTEMD_SERVICE="chronyd.service"
typeset _NTPD_SYSTEMD_SERVICE="ntpd.service"
typeset _VERSION="2018-10-31" # YYYY-MM-DD
typeset _VERSION="2018-11-18" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
typeset _CHRONYC_BIN="/bin/chronyc"
typeset _NTPQ_BIN="/usr/sbin/ntpq"
@ -55,6 +56,7 @@ typeset _MSG=""
typeset _STC=0
typeset _CFG_HEALTHY=""
typeset _LOG_HEALTHY=0
typeset _CHECK_SYSTEMD_SERVICE=0
typeset _CURR_OFFSET=0
typeset _MAX_OFFSET=0
typeset _NTP_PEER=""
@ -134,10 +136,24 @@ linux_get_init
case "${LINUX_INIT}" in
'systemd')
if (( _USE_CHRONYD > 0 ))
then
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_CHRONYD_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_CHRONYD_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_CHRONYD_SYSTEMD_SERVICE}}"
_RC=1
fi
else
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_NTPD_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_NTPD_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_NTPD_SYSTEMD_SERVICE}}"
_RC=1
fi
fi
;;
'upstart')

View File

@ -26,6 +26,7 @@
# @(#) 2017-05-08: suppress errors on postfix call + fix fall-back
# @(#) for sysv->pgrep[Patrick Van der Veken]
# @(#) 2018-05-21: STDERR fixes [Patrick Van der Veken]
# @(#) 2018-11-18: add linux_has_systemd_service() [Patrick Van der Veken]
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
@ -36,7 +37,7 @@ function check_linux_postfix_status
# ------------------------- CONFIGURATION starts here -------------------------
typeset _POSTFIX_INIT_SCRIPT="/etc/init.d/postfix"
typeset _POSTFIX_SYSTEMD_SERVICE="postfix.service"
typeset _VERSION="2018-05-21" # YYYY-MM-DD
typeset _VERSION="2018-11-18" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
@ -49,6 +50,7 @@ typeset _POSTFIX_BIN=""
typeset _MSG=""
typeset _STC=0
typeset _RC=0
typeset _CHECK_SYSTEMD_SERVICE=0
# handle arguments (originally comma-separated)
for _ARG in ${_ARGS}
@ -64,7 +66,14 @@ done
linux_get_init
case "${LINUX_INIT}" in
'systemd')
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_POSTFIX_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_POSTFIX_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_POSTFIX_SYSTEMD_SERVICE}}"
_RC=1
fi
;;
'upstart')
warn "code for upstart managed systems not implemented, NOOP"

View File

@ -27,6 +27,7 @@
# @(#) 2017-05-08: fix fall-back for sysv->pgrep [Patrick Van der Veken]
# @(#) 2017-07-23: fix for systemd service names [Patrick Van der Veken]
# @(#) 2018-05-21: STDERR fixes [Patrick Van der Veken]
# @(#) 2018-11-18: add linux_has_systemd_service() [Patrick Van der Veken]
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
@ -38,7 +39,7 @@ function check_linux_samba_status
typeset _SMB_INIT_SCRIPT="/etc/init.d/samba"
typeset _SMB_SYSTEMD_SERVICE="smb.service"
typeset _NMB_SYSTEMD_SERVICE="nmb.service"
typeset _VERSION="2018-05-21" # YYYY-MM-DD
typeset _VERSION="2018-11-18" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
@ -50,6 +51,7 @@ typeset _ARG=""
typeset _MSG=""
typeset _STC=0
typeset _RC=0
typeset _CHECK_SYSTEMD_SERVICE=0
# handle arguments (originally comma-separated)
for _ARG in ${_ARGS}
@ -66,9 +68,23 @@ linux_get_init
case "${LINUX_INIT}" in
'systemd')
# check running NMB
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_NMB_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_NMB_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_NMB_SYSTEMD_SERVICE}}"
_RC=1
fi
# check running SMB
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_SMB_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_SMB_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=$(( _STC + 2 ))
else
warn "systemd unit file not found {${_SMB_SYSTEMD_SERVICE}}"
_RC=1
fi
;;
'upstart')
warn "code for upstart managed systems not implemented, NOOP"

View File

@ -26,6 +26,7 @@
# @(#) 2017-05-05: fix fall-back for sysv->pgrep [Patrick Van der Veken]
# @(#) 2017-05-08: set init/systemd based on distro [Patrick Van der Veken]
# @(#) 2018-05-21: STDERR fixes [Patrick Van der Veken]
# @(#) 2018-11-18: add linux_has_systemd_service() [Patrick Van der Veken]
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
@ -34,7 +35,7 @@
function check_linux_sshd_status
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _VERSION="2018-05-21" # YYYY-MM-DD
typeset _VERSION="2018-11-18" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
@ -46,6 +47,7 @@ typeset _ARG=""
typeset _MSG=""
typeset _SSHD_INIT_SCRIPT=""
typeset _SSHD_SYSTEMD_SERVICE=""
typeset _CHECK_SYSTEMD_SERVICE=0
typeset _STC=0
typeset _RC=0
@ -75,7 +77,14 @@ esac
linux_get_init
case "${LINUX_INIT}" in
'systemd')
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_SSHD_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_SSHD_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_SSHD_SYSTEMD_SERVICE}}"
_RC=1
fi
;;
'upstart')
warn "code for upstart managed systems not implemented, NOOP"