forked from appuio/nagios-plugins-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging-wrapper
executable file
·94 lines (73 loc) · 1.84 KB
/
logging-wrapper
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
#!/bin/bash
set -e -u -o pipefail
. /usr/lib/nagios-plugins-openshift/utils
usage() {
echo "Usage: $0 [-Z] <statusdir> <cmd> [args...]"
echo
echo 'Parameters:'
echo '-Z Sleep up to 10 minutes before running command'
echo 'statusdir Directory for storing status'
echo 'cmd Command to execute'
echo 'args... Arguments for command'
}
if [[ "$#" -lt 2 ]]; then
usage >&2
exit 1
fi
if [[ "$1" = -Z ]]; then
shift
delay=600
else
delay=
fi
statusdir="$1"; shift
cmd=( "$@" )
# Make status directory into absolute path (necessary for multilog)
if ! statusdir=$(readlink -f "$statusdir") || \
[[ ! -d "$statusdir" ]]; then
echo "\"$statusdir\" is not a directory" >&2
exit 1
fi
logdir="$statusdir/log"
statusfile="$statusdir/status.txt"
lockfile="$statusdir/lock"
lock_timeout=10
if [[ -n "$delay" ]]; then
sleep $((RANDOM % delay)) || :
fi
exec 200>"$lockfile"
if ! flock --exclusive --timeout "$lock_timeout" 200; then
echo "Lock \"$lockfile\" can not be acquired" >&2
exit 1
fi
tmpdir=$(mktemp -d "$statusdir/.tmp.XXXXXXXX")
trap 'rm -rf "$tmpdir"' EXIT
if ! [[ -d "$logdir" ]]; then
mkdir -m 0700 "$logdir"
fi
# The logs contain access tokens
chmod 0700 "$logdir"
# Redirect stdout/stderr
exec > >(multilog t n10 s$(( 1024 * 1024 )) "$logdir") 2>&1
# Write log messages to stderr (redirected to multilog) and redirect
# stderr of command to logfile
if "${cmd[@]}" --nagios-output "$tmpdir/output.txt"; then
exitcode=0
else
exitcode=$?
fi
{
echo "X-Status: $exitcode"
echo
if [[ -e "$tmpdir/output.txt" ]]; then
cat "$tmpdir/output.txt"
fi
if [[ "$exitcode" > 0 ]]; then
echo
echo "Logs are in \"$logdir\" on \"$(hostname -f || :)\"." \
"Finished at $(date +'%F %T%z')."
fi
} > "$tmpdir/status.txt"
mv "$tmpdir/status.txt" "$statusfile"
exit "$exitcode"
# vim: set sw=2 sts=2 et :