From 3b4b6a38d9436c1c1dd7a5a0b3ffd733cfd1e22e Mon Sep 17 00:00:00 2001 From: Vezpi Date: Sun, 1 Mar 2020 14:31:07 +0100 Subject: [PATCH] added check_systemd.sh --- check_systemd.sh | 365 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100755 check_systemd.sh diff --git a/check_systemd.sh b/check_systemd.sh new file mode 100755 index 0000000..ee9609c --- /dev/null +++ b/check_systemd.sh @@ -0,0 +1,365 @@ +#!/bin/bash + +#---------------------------------------------------------------------------------------------------- +#~ Nom : check_systemd +#~ Description : Plugin Nagios pour tester si un service est démarré/activé/arrété +#~ Auteur : Etienne Girault +#~ +#~ Usage : [-h] [-D] [-V] +#~ +#~ Options : -h, --help ; Affiche l'aide +#~ Options : -V, --version ; Affiche la version +#~ Options : -D, --debug ; Mode debug +#~ Options : -n ; Nom de l'ensemble de service +#~ Options : -c ; Service critique, retour critique si service stop, warning si disable +#~ Options : -w ; Service warning, retour warning si service stop +#~ Options : -s ; Service non nécessaire, retourne toujours ok + +#---------------------------------------------------------------------------------------------------- +# Déclaration des variables +#---------------------------------------------------------------------------------------------------- +script_name="$(grep '^#~ Nom' "$0" | sed 's/^.*: //')" +script_version="$(grep '^#~ v' "$0" | tail -1 | sed -e 's/^#~ //' -e 's/ *\-.*$//')" + +PLUGINDIR=/usr/local/nagios/libexec +. $PLUGINDIR/utils.sh + +compteur_service=0 +declare -A services_status +declare -A critical_report +declare -A warning_report +declare -A silent_report +declare -A running_report + + +#---------------------------------------------------------------------------------------------------- +# Déclaraition des fonctions +#---------------------------------------------------------------------------------------------------- +print_script_name_version() { echo "$script_name $script_version" && sed -n '/^#~ Version/,$p' "$0" | grep '^#~' | sed 's/^#~ //'; } +print_script_description() { grep '^#~ Description' "$0" | sed 's/^.*: //'; } +print_script_usage() { grep '^#~ Usage' "$0" | sed -e 's/^.*: //' -e '1 s/^/Usage: '"$script_name"' /' -e '2,$ s/^/'"$(printf '%*s' "$(echo "Usage: $script_name" | wc -c)")"'/'; } +print_script_options() { echo "Options" && grep '^#~ Options' "$0" | sed -e 's/^.*://' -e 's/^ --/ --/' | column -ts \;; } +print_script_help() { echo "$script_name $script_version" && print_script_description && print_script_usage && print_script_options; } + +# Vérifie l'état actuel, ne peut qu'agraver l'état +# $1 : Etat +change_state() { + case $1 in + $STATE_CRITICAL) current_state=$1;; + $STATE_WARNING) [[ "$current_state" = "$STATE_CRITICAL" ]] && return || current_state=$1;; + $STATE_UNKNOWN) [[ "$current_state" = "$STATE_CRITICAL" || "$current_state" = "$STATE_WARNING" ]] && return || current_state=$1;; + $STATE_OK) [[ "$current_state" = "$STATE_CRITICAL" || "$current_state" = "$STATE_WARNING" || "$current_state" = "$STATE_UNKNOWN" ]] && return || current_state=$1;; + esac +} + +# Ajoute l'etat du service dans le tableau de la liste des service +# $1 : numéro du service +# $2 : nom du service +# $3 : criticité du service +# $4 : etat du service (0=running, 1=not running, 2=disable, 3=does not exist) +service_report () { + services_status[$1]=$2 + case $3 in + "critical") + case $4 in + 0) running_report[$1]=$2 + change_state $STATE_OK;; + 1|3) critical_report[$1,0]=$2 + critical_report[$1,1]=$4 + change_state $STATE_CRITICAL;; + 2) warning_report[$1,0]=$2 + warning_report[$1,1]=$4 + change_state $STATE_WARNING;; + esac;; + "warning") + case $4 in + 0|2) running_report[$1]=$2 + change_state $STATE_OK;; + 1|3) warning_report[$1,0]=$2 + warning_report[$1,1]=$4 + change_state $STATE_WARNING;; + esac;; + "silent") + if [ $4 = 0 ]; then + running_report[$1]=$2 + else + silent_report[$1,0]=$2 + silent_report[$1,1]=$4 + fi + change_state $STATE_OK;; + esac +} + +# Reception de signaux d'interruptions +trap 'exit 1' INT TERM + +#---------------------------------------------------------------------------------------------------- +# Gestion des options +#---------------------------------------------------------------------------------------------------- +while getopts ":-:hVDn:c:w:s:" OPTION; do + case $OPTION in + h) print_script_help; exit;; + V) print_script_name_version; exit;; + D) set -x;; + n) namegroup=$OPTARG;; + c) critical_service="${critical_service} $OPTARG";; + w) warning_service="${warning_service} $OPTARG";; + s) silent_service="${silent_service} $OPTARG";; + :) ;; + -) case $OPTARG in + help) print_script_help; exit;; + version) print_script_name_version; exit;; + debug) set -x;; + esac ;; + *) ;; + esac +done +shift $((OPTIND-1)) + +#---------------------------------------------------------------------------------------------------- +# Début du script +#---------------------------------------------------------------------------------------------------- + +# if [[ $# -ne 1 ]]; then +# echo "Usage: ${0##*/} " +# exit $STATE_UNKNOWN +# fi + +if [[ "$critical_service" ]]; then + + for service in $critical_service; do + + status=$(systemctl is-enabled $service 2>/dev/null) + return_code=$? + + if [[ -z "$status" ]]; then + service_report "$compteur_service" "$service" "critical" 3 + else + if systemctl --quiet is-active $service; then + if [[ $return_code -ne 0 ]]; then + service_report "$compteur_service" "$service" "critical" 2 + else + service_report "$compteur_service" "$service" "critical" 0 + fi + else + service_report "$compteur_service" "$service" "critical" 1 + fi + fi + ((compteur_service++)) + done +fi + +if [[ "$warning_service" ]]; then + + for service in $warning_service; do + status=$(systemctl is-enabled $service 2>/dev/null) + return_code=$? + + if [[ -z "$status" ]]; then + service_report "$compteur_service" "$service" "warning" 3 + else + if systemctl --quiet is-active $service; then + if [[ $return_code -ne 0 ]]; then + service_report "$compteur_service" "$service" "warning" 2 + else + service_report "$compteur_service" "$service" "warning" 0 + fi + else + service_report "$compteur_service" "$service" "warning" 1 + fi + fi + ((compteur_service++)) + done +fi + +if [[ "$silent_service" ]]; then + + for service in $silent_service; do + status=$(systemctl is-enabled $service 2>/dev/null) + return_code=$? + + if [[ -z "$status" ]]; then + service_report "$compteur_service" "$service" "silent" 3 + else + if systemctl --quiet is-active $service; then + if [[ $return_code -ne 0 ]]; then + service_report "$compteur_service" "$service" "silent" 2 + else + service_report "$compteur_service" "$service" "silent" 0 + fi + else + service_report "$compteur_service" "$service" "silent" 1 + fi + fi + ((compteur_service++)) + done +fi + +case $current_state in + $STATE_CRITICAL) + for (( service_number=0; service_number<=$compteur_service-1; service_number++ )); do + if [[ ${critical_report[$service_number,0]} != "" ]]; then + case ${critical_report[$service_number,1]} in + 1) case $crit_not_running in + "") crit_not_running_information="${critical_report[$service_number,0]}";; + 1) crit_not_running_information="$crit_not_running_information and ${critical_report[$service_number,0]}";; + *) crit_not_running_information="${critical_report[$service_number,0]}, $crit_not_running_information";; + esac + ((crit_not_running++));; # crit not running + 3) case $crit_not_exist in + "") crit_not_exist_information="${critical_report[$service_number,0]}";; + 1) crit_not_exist_information="$crit_not_exist_information and ${critical_report[$service_number,0]}";; + *) crit_not_exist_information="${critical_report[$service_number,0]}, $crit_not_exist_information";; + esac + ((crit_not_exist++));; # crit not exist + esac + fi + done + if [[ $crit_not_running ]]; then + if [[ $crit_not_running = 1 ]]; then + crit_not_running_information="$crit_not_running_information service is not running" + else + crit_not_running_information="$crit_not_running_information services are not running" + fi + status_information="- $crit_not_running_information" + fi + if [[ $crit_not_exist ]]; then + if [[ $crit_not_exist = 1 ]]; then + crit_not_exist_information="$crit_not_exist_information service does not exist" + else + crit_not_exist_information="$crit_not_exist_information services do not exist" + fi + status_information="- $crit_not_exist_information $status_information" + fi + status_information="ERROR $status_information";; + $STATE_WARNING) + for (( service_number=0; service_number<=$compteur_service-1; service_number++ )); do + if [[ ${warning_report[$service_number,0]} != "" ]]; then + case ${warning_report[$service_number,1]} in + 1) case $warn_not_running in + "") warn_not_running_information="${warning_report[$service_number,0]}";; + 1) warn_not_running_information="$warn_not_running_information and ${warning_report[$service_number,0]}";; + *) warn_not_running_information="${warning_report[$service_number,0]}, $warn_not_running_information";; + esac + ((warn_not_running++));; # warn not running + 2) case $crit_disable in + "") crit_disable_information="${warning_report[$service_number,0]}";; + 1) crit_disable_information="$crit_disable_information and ${warning_report[$service_number,0]}";; + *) crit_disable_information="${warning_report[$service_number,0]}, $crit_disable_information";; + esac + ((crit_disable++));; # crit disable + 3) case $warn_not_exist in + "") warn_not_exist_information="${warning_report[$service_number,0]}";; + 1) warn_not_exist_information="$warn_not_exist_information and ${warning_report[$service_number,0]}";; + *) warn_not_exist_information="${warning_report[$service_number,0]}, $warn_not_exist_information";; + esac + ((warn_not_exist++));; # warn not exist + esac + fi + done + if [[ $warn_not_running ]]; then + if [[ $warn_not_running = 1 ]]; then + warn_not_running_information="$warn_not_running_information service is not running" + else + warn_not_running_information="$warn_not_running_information services are not running" + fi + status_information="- $warn_not_running_information" + fi + if [[ $crit_disable ]]; then + if [[ $crit_disable = 1 ]]; then + crit_disable_information="$crit_disable_information service is disabled" + else + crit_disable_information="$crit_disable_information services are disabled" + fi + status_information="- $crit_disable_information $status_information" + fi + if [[ $warn_not_exist ]]; then + if [[ $warn_not_exist = 1 ]]; then + warn_not_exist_information="$warn_not_exist_information service does not exist" + else + warn_not_exist_information="$warn_not_exist_information services do not exist" + fi + status_information="- $warn_not_exist_information $status_information" + fi + status_information="WARN $status_information";; + $STATE_OK) + for (( service_number=0; service_number<=$compteur_service-1; service_number++ )); do + if [[ ${silent_report[$service_number,0]} != "" ]]; then + case ${silent_report[$service_number,1]} in + 1) case $silent_not_running in + "") silent_not_running_information="${silent_report[$service_number,0]}";; + 1) silent_not_running_information="$silent_not_running_information and ${silent_report[$service_number,0]}";; + *) silent_not_running_information="${silent_report[$service_number,0]}, $silent_not_running_information";; + esac + ((silent_not_running++));; # silent not running + 2) case $silent_disable in + "") silent_disable_information="${silent_report[$service_number,0]}";; + 1) silent_disable_information="$silent_disable_information and ${silent_report[$service_number,0]}";; + *) silent_disable_information="${silent_report[$service_number,0]}, $silent_disable_information";; + esac + ((silent_disable++));; # silent disable + 3) case $silent_not_exist in + "") silent_not_exist_information="${silent_report[$service_number,0]}";; + 1) silent_not_exist_information="$silent_not_exist_information and ${silent_report[$service_number,0]}";; + *) silent_not_exist_information="${silent_report[$service_number,0]}, $silent_not_exist_information";; + esac + ((silent_not_exist++));; # silent not exist + esac + fi + if [[ ${running_report[$service_number]} != "" ]]; then + case $running_service in + "") running_service_information="${running_report[$service_number]}";; + 1) running_service_information="$running_service_information and ${running_report[$service_number]}";; + *) running_service_information="${running_report[$service_number]}, $running_service_information";; + esac + ((running_service++)) + fi + done + if [[ $silent_not_running ]]; then + if [[ $silent_not_running = 1 ]]; then + silent_not_running_information="$silent_not_running_information service is not running" + else + silent_not_running_information="$silent_not_running_information services are not running" + fi + status_information="- $silent_not_running_information" + fi + if [[ $silent_disable ]]; then + if [[ $silent_disable = 1 ]]; then + silent_disable_information="$silent_disable_information service is disabled" + else + silent_disable_information="$silent_disable_information services are disabled" + fi + status_information="- $silent_disable_information $status_information" + fi + if [[ $silent_not_exist ]]; then + if [[ $silent_not_exist = 1 ]]; then + silent_not_exist_information="$silent_not_exist_information service does not exist" + else + silent_not_exist_information="$silent_not_exist_information services do not exist" + fi + status_information="- $silent_not_exist_information $status_information" + fi + if [[ $running_service ]]; then + if [[ $running_service = 1 ]]; then + running_service_information="$running_service_information service is running" + else + running_service_information="$running_service_information services are running" + fi + status_information="- $running_service_information $status_information" + fi + + status_information="OK $status_information";; +esac + +echo $status_information +exit $current_state + +#---------------------------------------------------------------------------------------------------- +# Fin du script +#---------------------------------------------------------------------------------------------------- + +#~ Version changelog +#~ v0.1 - Basé sur check_systemd_service.sh de Mohamed El Morabity +#~ v0.2 - Possibilité de gérer plusieurs services avec differents niveaux de criticité + + \ No newline at end of file