From 283a1e6101ddf4deb9c9467677641b56dcf71dab Mon Sep 17 00:00:00 2001 From: Patrick Van der Veken Date: Sat, 21 Apr 2018 13:32:29 +0200 Subject: [PATCH] Added check_linux_ntp_status --- build/linux/SPECS/hc-linux-platform.spec | 6 + configs/etc/check_linux_ntp_status.conf.dist | 16 ++ .../platform/linux/check_linux_ntp_status.sh | 195 ++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 configs/etc/check_linux_ntp_status.conf.dist create mode 100644 sources/lib/platform/linux/check_linux_ntp_status.sh diff --git a/build/linux/SPECS/hc-linux-platform.spec b/build/linux/SPECS/hc-linux-platform.spec index 2ea4ada..46cac65 100644 --- a/build/linux/SPECS/hc-linux-platform.spec +++ b/build/linux/SPECS/hc-linux-platform.spec @@ -34,6 +34,7 @@ cp ../SOURCES/lib/platform/linux/check_linux_fs_mounts.sh $RPM_BUILD_ROOT/opt/hc cp ../SOURCES/lib/platform/linux/check_linux_burp_status.sh $RPM_BUILD_ROOT/opt/hc/lib/platform/linux/check_linux_burp_status.sh cp ../SOURCES/lib/platform/linux/check_linux_httpd_status.sh $RPM_BUILD_ROOT/opt/hc/lib/platform/linux/check_linux_httpd_status.sh cp ../SOURCES/lib/platform/linux/check_linux_named_status.sh $RPM_BUILD_ROOT/opt/hc/lib/platform/linux/check_linux_named_status.sh +cp ../SOURCES/lib/platform/linux/check_linux_ntp_status.sh $RPM_BUILD_ROOT/opt/hc/lib/platform/linux/check_linux_ntp_status.sh cp ../SOURCES/lib/platform/linux/check_linux_postfix_status.sh $RPM_BUILD_ROOT/opt/hc/lib/platform/linux/check_linux_postfix_status.sh cp ../SOURCES/lib/platform/linux/check_linux_samba_status.sh $RPM_BUILD_ROOT/opt/hc/lib/platform/linux/check_linux_samba_status.sh cp ../SOURCES/lib/platform/linux/check_linux_shorewall_status.sh $RPM_BUILD_ROOT/opt/hc/lib/platform/linux/check_linux_shorewall_status.sh @@ -58,6 +59,7 @@ cp ../SOURCES/etc/check_linux_hpasmcli.conf.dist $RPM_BUILD_ROOT/etc/opt/hc/chec cp ../SOURCES/etc/check_linux_hpacucli.conf.dist $RPM_BUILD_ROOT/etc/opt/hc/check_linux_hpacucli.conf.dist cp ../SOURCES/etc/check_linux_hplog.conf.dist $RPM_BUILD_ROOT/etc/opt/hc/check_linux_hplog.conf.dist cp ../SOURCES/etc/check_linux_hpssacli.conf.dist $RPM_BUILD_ROOT/etc/opt/hc/check_linux_hpssacli.conf.dist +cp ../SOURCES/etc/check_linux_ntp_status.conf.dist $RPM_BUILD_ROOT/etc/opt/hc/check_linux_ntp_status.conf.dist cp ../SOURCES/etc/check_linux_root_crontab.conf.dist $RPM_BUILD_ROOT/etc/opt/hc/check_linux_root_crontab.conf.dist cp ../SOURCES/etc/check_linux_sg_cluster_config.conf.dist $RPM_BUILD_ROOT/etc/opt/hc/check_linux_sg_cluster_config.conf.dist cp ../SOURCES/etc/check_linux_sg_cluster_status.conf.dist $RPM_BUILD_ROOT/etc/opt/hc/check_linux_sg_cluster_status.conf.dist @@ -111,6 +113,7 @@ echo "INFO: finished post-uninstall script" %attr(755, root, root) /opt/hc/lib/platform/linux/check_linux_burp_status.sh %attr(755, root, root) /opt/hc/lib/platform/linux/check_linux_httpd_status.sh %attr(755, root, root) /opt/hc/lib/platform/linux/check_linux_named_status.sh +%attr(755, root, root) /opt/hc/lib/platform/linux/check_linux_ntp_status.sh %attr(755, root, root) /opt/hc/lib/platform/linux/check_linux_postfix_status.sh %attr(755, root, root) /opt/hc/lib/platform/linux/check_linux_samba_status.sh %attr(755, root, root) /opt/hc/lib/platform/linux/check_linux_shorewall_status.sh @@ -135,6 +138,7 @@ echo "INFO: finished post-uninstall script" %attr(644, root, root) /etc/opt/hc/check_linux_hpacucli.conf.dist %attr(644, root, root) /etc/opt/hc/check_linux_hplog.conf.dist %attr(644, root, root) /etc/opt/hc/check_linux_hpssacli.conf.dist +%attr(644, root, root) /etc/opt/hc/check_linux_ntp_status.conf.dist %attr(644, root, root) /etc/opt/hc/check_linux_root_crontab.conf.dist %attr(644, root, root) /etc/opt/hc/check_linux_sg_cluster_config.conf.dist %attr(644, root, root) /etc/opt/hc/check_linux_sg_cluster_status.conf.dist @@ -147,6 +151,8 @@ echo "INFO: finished post-uninstall script" %attr(644, root, root) /etc/opt/hc/core/templates/mail_body.tpl-check_linux_root_crontab %changelog +* Sat Apr 21 2018 - 0.1.0 +- Added check_linux_ntp_status * Thu May 18 2017 - 0.0.9 - Added check_linux_file_change * Thu May 04 2017 - 0.0.9 diff --git a/configs/etc/check_linux_ntp_status.conf.dist b/configs/etc/check_linux_ntp_status.conf.dist new file mode 100644 index 0000000..a86e111 --- /dev/null +++ b/configs/etc/check_linux_ntp_status.conf.dist @@ -0,0 +1,16 @@ +#****************************************************************************** +# @(#) check_linux_ntp_status.conf.dist +#****************************************************************************** +# This is a configuration file for the check_linux_ntp_status HC plugin. +# All lines starting with a '#' are comment lines. +# [default: indicates hardcoded script values if no value is defined here] +#****************************************************************************** + +# maximum allowed offset (in milliseconds) +# [default: 500] +max_offset=500 + + +#****************************************************************************** +# End of FILE +#****************************************************************************** \ No newline at end of file diff --git a/sources/lib/platform/linux/check_linux_ntp_status.sh b/sources/lib/platform/linux/check_linux_ntp_status.sh new file mode 100644 index 0000000..7414ce0 --- /dev/null +++ b/sources/lib/platform/linux/check_linux_ntp_status.sh @@ -0,0 +1,195 @@ +#!/usr/bin/env ksh +#------------------------------------------------------------------------------ +# @(#) check_linux_ntp_status +#------------------------------------------------------------------------------ +# @(#) Copyright (C) 2016 by KUDOS BVBA (info@kudos.be). All rights reserved. +# +# This program is a free software; you can redistribute it and/or modify +# it under the same terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# 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 +#------------------------------------------------------------------------------ +# +# DOCUMENTATION (MAIN) +# ----------------------------------------------------------------------------- +# @(#) MAIN: check_linux_ntp_status +# DOES: see _show_usage() +# EXPECTS: see _show_usage() +# REQUIRES: data_space2comma(), init_hc(), log_hc(), warn() +# +# @(#) HISTORY: +# @(#) 2018-03-20: initial version [Patrick Van der Veken] +# ----------------------------------------------------------------------------- +# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING! +#------------------------------------------------------------------------------ + +# ----------------------------------------------------------------------------- +function check_linux_ntp_status +{ +# ------------------------- CONFIGURATION starts here ------------------------- +typeset _CONFIG_FILE="${CONFIG_DIR}/$0.conf" +typeset _NTPD_INIT_SCRIPT="/etc/init.d/ntpd" +typeset _NTPD_SYSTEMD_SERVICE="ntpd.service" +typeset _VERSION="2018-03-20" # YYYY-MM-DD +typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match +typeset _NTPQ_BIN="/usr/sbin/ntpq" +# ------------------------- CONFIGURATION ends here --------------------------- + +# set defaults +(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set ${DEBUG_OPTS} +init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}" +typeset _ARGS=$(data_space2comma "$*") +typeset _ARG="" +typeset _MSG="" +typeset _STC=0 +typeset _CURR_OFFSET=0 +typeset _MAX_OFFSET=0 +typeset _NTP_PEER="" + +# handle arguments (originally comma-separated) +for _ARG in ${_ARGS} +do + case "${_ARG}" in + help) + _show_usage $0 ${_VERSION} ${_CONFIG_FILE} && return 0 + ;; + esac +done + +# handle config file +[[ -n "${ARG_CONFIG_FILE}" ]] && _CONFIG_FILE="${ARG_CONFIG_FILE}" +if [[ ! -r ${_CONFIG_FILE} ]] +then + warn "unable to read configuration file at ${_CONFIG_FILE}" + return 1 +fi +# read required config values +_MAX_OFFSET=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config 'max_offset') +if [[ -z "${_MAX_OFFSET}" ]] +then + # default + _MAX_OFFSET=500 +fi + +#------------------------------------------------------------------------------ +# check ntp service +linux_get_init +case "${LINUX_INIT}" in + 'systemd') + systemctl --quiet is-active ${_NTPD_SYSTEMD_SERVICE} || _STC=1 + ;; + 'upstart') + warn "code for upstart managed systems not implemented, NOOP" + _RC=1 + ;; + 'sysv') + # check running SysV + if [[ -x ${_NTPD_INIT_SCRIPT} ]] + then + if (( $(${_NTPD_INIT_SCRIPT} status 2>>${HC_STDERR_LOG} | grep -c -i 'is running') == 0 )) + then + _STC=1 + fi + else + warn "sysv init script not found {${_NTPD_INIT_SCRIPT}}" + _RC=1 + fi + ;; + *) + _RC=1 + ;; +esac + +# evaluate results +case ${_STC} in + 0) + _MSG="postfix is running" + ;; + 1) + _MSG="postfix is not running" + ;; + *) + _MSG="could not determine status of postfix" + ;; +esac +log_hc "$0" ${_STC} "${_MSG}" + +#------------------------------------------------------------------------------ +# check ntpq results +_STC=0 +if [[ ! -x ${_NTPQ_BIN} ]] +then + warn "${_NTPQ_BIN} is not installed here" + return 1 +else + ${_NTPQ_BIN} -np 2>>${HC_STDERR_LOG} >>${HC_STDOUT_LOG} + # RC is always 0 +fi + +# 1) active server +_NTP_PEER="$(grep -E -e '^\*' 2>/dev/null ${HC_STDOUT_LOG} | awk '{ print $1 }')" +case ${_NTP_PEER} in + \*127.127.1.0*) + _MSG="NTP is synchronizing against its internal clock" + _STC=1 + ;; + \*[0-9]*) + # some valid server + _MSG="NTP is synchronizing against ${_NTP_PEER##*\*}" + ;; + *) + _MSG="NTP is not synchronizing or NTP daemon is not running" + _STC=1 + ;; +esac +log_hc "$0" ${_STC} "${_MSG}" + +# 2) offset value +if (( _STC == 0 )) +then + _CURR_OFFSET="$(grep -E -e '^\*' 2>/dev/null ${HC_STDOUT_LOG} | awk '{ print $9 }')" + case ${_CURR_OFFSET} in + +([-0-9])*(.)*([0-9])) + # numeric, OK (negatives are OK too!) + if (( $(awk -v c="${_CURR_OFFSET}" -v m="${_MAX_OFFSET}" 'BEGIN { print (c>m) }') != 0 )) + then + _MSG="NTP offset of ${_CURR_OFFSET} is bigger than the configured maximum of ${_MAX_OFFSET}" + _STC=1 + else + _MSG="NTP offset of ${_CURR_OFFSET} is within the acceptable range" + fi + log_hc "$0" ${_STC} "${_MSG}" + ;; + *) + # not numeric + warn "invalid offset value of ${_CURR_OFFSET} found for ${NTP_PEER}?" + return 1 + ;; + esac +fi + +return 0 +} + +# ----------------------------------------------------------------------------- +function _show_usage +{ +cat <<- EOT +NAME : $1 +VERSION : $2 +CONFIG : $3 +PURPOSE : Checks the status of NTP service & synchronization + +EOT + +return 0 +} + + +#------------------------------------------------------------------------------ +# END of script +#------------------------------------------------------------------------------