Merge check_nrpe into bkctld check subcommand

This commit is contained in:
Victor LABORIE 2018-05-30 16:37:13 +02:00
parent f6aca343fb
commit dcba6d5634
5 changed files with 32 additions and 44 deletions

1
Vagrantfile vendored
View file

@ -20,7 +20,6 @@ ln -fs /vagrant/tpl /usr/share/bkctld
ln -fs /vagrant/bash_completion /usr/share/bash-completion/completions/bkctld ln -fs /vagrant/bash_completion /usr/share/bash-completion/completions/bkctld
ln -fs /vagrant/bkctld.conf /etc/default/bkctld ln -fs /vagrant/bkctld.conf /etc/default/bkctld
mkdir -p /usr/lib/nagios/plugins/ mkdir -p /usr/lib/nagios/plugins/
ln -fs /vagrant/check_nrpe /usr/lib/nagios/plugins/check_bkctld
SCRIPT SCRIPT
$deps = <<SCRIPT $deps = <<SCRIPT

View file

@ -9,7 +9,7 @@ function _bkctld()
cur=${COMP_WORDS[COMP_CWORD]}; cur=${COMP_WORDS[COMP_CWORD]};
prev=${COMP_WORDS[COMP_CWORD-1]}; prev=${COMP_WORDS[COMP_CWORD-1]};
commands="init update remove start stop reload restart sync status key port ip inc rm" commands="init update remove start stop reload restart sync status key port ip inc rm check"
if [ $COMP_CWORD -eq 1 ]; then if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=($(compgen -W '${commands}' -- ${cur})) COMPREPLY=($(compgen -W '${commands}' -- ${cur}))

29
bkctld
View file

@ -28,6 +28,7 @@ Subcommands:
ip <jailname> [<ip>|all] Set or get allowed(s) ip(s) of <jailname> ip <jailname> [<ip>|all] Set or get allowed(s) ip(s) of <jailname>
inc Make incremental inc of all jails inc Make incremental inc of all jails
rm Remove old incremtal inc of all jails rm Remove old incremtal inc of all jails
check Run check on jails (NRPE output)
EOF EOF
} }
@ -470,6 +471,30 @@ sub_rm() {
rm "${pidfile}" rm "${pidfile}"
} }
sub_check() {
cur_time=$(date "+%s")
return=0
jails=$(ls "${JAILDIR}")
for jail in ${jails}; do
if [ -f "${JAILDIR}/${jail}/var/log/lastlog" ]; then
last_conn=$(stat --format=%Y "${JAILDIR}/${jail}/var/log/lastlog")
date_diff=$(( (cur_time - last_conn) / (60*60) ))
if [ "${date_diff}" -gt "${CRITICAL}" ]; then
echo "CRITICAL - ${jail} - ${date_diff} hours"
return=2
elif [ "${date_diff}" -gt "${WARNING}" ]; then
echo "WARNING - ${jail} - ${date_diff} hours"
[ "${return}" -ne 2 ] && return=1
fi
else
echo "CRITICAL - ${jail} doesn't have lastlog !"
return=2
fi
done
[ "${return}" -eq 0 ] && echo "OK - Nothing to signal"
exit "${return}"
}
## main function : check usage and valid params ## main function : check usage and valid params
main() { main() {
@ -486,6 +511,8 @@ main() {
AUTHORIZED_KEYS="${AUTHORIZED_KEYS:-/root/.ssh/authorized_keys}" AUTHORIZED_KEYS="${AUTHORIZED_KEYS:-/root/.ssh/authorized_keys}"
FIREWALL_RULES="${FIREWALL_RULES:-}" FIREWALL_RULES="${FIREWALL_RULES:-}"
LOGLEVEL="${LOGLEVEL:-6}" LOGLEVEL="${LOGLEVEL:-6}"
CRITICAL="${CRITICAL:-48}"
WARNING="${WARNING:-24}"
BTRFS=$(command -v btrfs) BTRFS=$(command -v btrfs)
mkdir -p "${CONFDIR}" "${JAILDIR}" "${INCDIR}" mkdir -p "${CONFDIR}" "${JAILDIR}" "${INCDIR}"
subcommand="${1:-}" subcommand="${1:-}"
@ -495,7 +522,7 @@ main() {
"" | "-h" | "--help") "" | "-h" | "--help")
usage usage
;; ;;
"inc" | "rm") "inc" | "rm" | "check")
"sub_${subcommand}" "sub_${subcommand}"
;; ;;
"init") "init")

View file

@ -1,38 +0,0 @@
#!/bin/sh
#
# bkctld(8) - NRPE check
#
# Copyright (c) 2017 Victor Laborie <vlaborie@evolix.fr>
#
[ -f /etc/default/bkctld ] && . /etc/default/bkctld
[ -z "$JAILDIR" ] && JAILDIR="/backup/jails"
[ -z "$CRITICAL" ] && CRITICAL=48
[ -z "$WARNING" ] && WARNING=24
cur_time=$(date "+%s")
return=0
jails=$(ls "$JAILDIR")
for jail in $jails; do
if [ -f "$JAILDIR/$jail/var/log/lastlog" ]; then
last_conn=$(stat --format=%Y "$JAILDIR/$jail/var/log/lastlog")
date_diff=$(( ( $cur_time - $last_conn ) / (60*60) ))
if [ "$date_diff" -gt "$CRITICAL" ]; then
echo "CRITICAL - $jail - $date_diff hours"
return=2
elif [ "$date_diff" -gt "$WARNING" ]; then
echo "WARNING - $jail - $date_diff hours"
[ "$return" -ne 2 ] && return=1
fi
else
echo "CRITICAL - $jail doesn't have lastlog !"
return=2
fi
done
[ "$return" -eq 0 ] && echo "OK - Nothing to signal"
exit "$return"

View file

@ -9,18 +9,18 @@ teardown() {
} }
@test "ok" { @test "ok" {
run /usr/lib/nagios/plugins/check_bkctld run bkctld check
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
} }
@test "warning" { @test "warning" {
touch --date="$(date -d -2days)" /backup/jails/*/var/log/lastlog touch --date="$(date -d -2days)" /backup/jails/*/var/log/lastlog
run /usr/lib/nagios/plugins/check_bkctld run bkctld check
[ "$status" -eq 1 ] [ "$status" -eq 1 ]
} }
@test "critical" { @test "critical" {
touch --date="$(date -d -3days)" /backup/jails/*/var/log/lastlog touch --date="$(date -d -3days)" /backup/jails/*/var/log/lastlog
run /usr/lib/nagios/plugins/check_bkctld run bkctld check
[ "$status" -eq 2 ] [ "$status" -eq 2 ]
} }