blob: ef12e354922ec0cec1eefe28c2319aef3796aca8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#!/usr/bin/env bash
#
# nagios-force-check(1) -- Force an immediate check of a nominated host or
# service.
#
# $ nac <host>[/<service>]
#
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# Copyright: 2016
#
# Name self
self=nagios-force-check
# Usage printing function
usage() {
printf 'USAGE: %s (-a | <host[/service]> [<host[/service]..])\n' "$self"
}
# Handle options, just -h help at the moment
OPTIND=1
check_all_problems=0
while getopts 'ah' opt ; do
case "$opt" in
a)
check_all_problems=1
;;
h)
usage
exit 0
;;
'?')
usage >&2
exit 1
;;
esac
done
shift "$((OPTIND-1))"
# Bail if -a was selected but there are more arguments
if ((check_all_problems)) && (($#)) ; then
usage >&2
exit 1
# Bail if -a wasn't selected and there are no arguments
elif ! ((check_all_problems)) && ! (($#)) ; then
usage >&2
exit 1
fi
# Define relatively fixed/guaranteed fields for Nagios command; note that the
# comment has a default of 'no comment given'
now=$(date +%s)
cmdfile=${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd}
# Specs are either arguments or all unhandled problems
declare -a specs
if ((check_all_problems)) ; then
while read -r object ; do
specs=("${specs[@]}" "$object")
done < <(nagios-problem-list)
else
specs=("$@")
fi
# Iterate through the specs given and run the force recheck command
for spec in "${specs[@]}" ; do
# Check the host or service exists
if ! nagios-exists "$spec" ; then
printf '%s: Host/service %s does not seem to exist\n' \
"$self" "$spec" >&2
fi
# If a service name is specified after a slash, figure that out
if [[ $spec == */* ]] ; then
host=${spec%/*}
service=${spec##*/}
else
host=$spec
service=
fi
# Write command and print message if it fails; succeed silently
declare -a cmds
cmds=()
if [[ $service ]] ; then
cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_SVC_CHECK;%s;%s;%lu' \
"$now" "$host" "$service" "$now")")
else
cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_HOST_CHECK;%s;%lu' \
"$now" "$host" "$now")")
cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_HOST_SVC_CHECKS;%s;%lu' \
"$now" "$host" "$now")")
fi
# Attempt to write commands to file
for cmd in "${cmds[@]}" ; do
if ! printf '%s\n' "$cmd" >> "$cmdfile" ; then
printf '%s: Failed to write command to file\n' "$self" >&2
exit 1
fi
done
done
|