add check_needrestart

This commit is contained in:
Ludovic Cartier
2025-09-30 14:52:20 +02:00
parent 59aeb52914
commit 0fc907e562
3 changed files with 47 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
#!/bin/bash
#
# Nagios plugin to check for services needing restart using the 'needrestart' package.
#
# Copyright (c) 2025, GitHub Copilot
#
# This script is free software: you can redistribute it and/or modify
# it under the 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 script 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Nagios exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# Check if needrestart is installed
if ! dpkg -s needrestart >/dev/null 2>&1; then
echo "UNKNOWN: The 'needrestart' package is not installed."
exit $STATE_UNKNOWN
fi
# Run needrestart in batch/list mode and grep for service lines.
# The '|| true' prevents the script from exiting if grep finds no matches.
services_to_restart=$(sudo needrestart -b -l 2>/dev/null | grep '^NEEDRESTART-SVC:' || true)
if [ -z "$services_to_restart" ]; then
echo "OK: No services need to be restarted."
exit $STATE_OK
else
# Count the number of services and format the list for the output.
num_services=$(echo "$services_to_restart" | wc -l)
service_list=$(echo "$services_to_restart" | sed 's/NEEDRESTART-SVC: //' | tr '\n' ' ')
echo "WARNING: $num_services service(s) need to be restarted: $service_list"
exit $STATE_WARNING
fi

View File

@@ -16,6 +16,7 @@ command[check_eth]=/usr/lib/nagios/plugins/check_eth -i {{ ansible_default_ipv4.
command[check_proc_fail2ban]=/usr/lib/nagios/plugins/check_procs -a fail2ban -w 1: -c 1:
command[check_proc_age]=/usr/lib/nagios/plugins/check_proc_age -p <proc> -w 400 -c 600
command[check_systemd_failed]=/usr/lib/nagios/plugins/check_systemd_failed
command[check_needrestart]=/usr/lib/nagios/plugins/check_needrestart
# check_disk_usage
# -w <%>: Warning threshold for block usage.

View File

@@ -1,3 +1,4 @@
nagios ALL=(ALL) NOPASSWD: /usr/lib/nagios/plugins/check_postfix_mailqueue -w {{ nrpe_mailq_warning }} -c {{ nrpe_mailq_critical }}
nagios ALL=(ALL) NOPASSWD: /usr/lib/nagios/plugins/check_exim_mailqueue -w {{ nrpe_mailq_warning }} -c {{ nrpe_mailq_critical }}
nagios ALL=(ALL) NOPASSWD: /usr/lib/nagios/plugins/check_raid
nagios ALL=(ALL) NOPASSWD: /usr/sbin/needrestart -b -l