Created
July 21, 2014 13:35
-
-
Save tomas-edwardsson/80ba82d4caabf15d65f8 to your computer and use it in GitHub Desktop.
Nagios plugin wrapper for long running plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Nagios plugin wrapper for long running plugins | |
# It returns the last run and re-runs the plugin in background caching the results | |
# in /etc/nagios/slowplugins | |
# Examples | |
# /usr/lib64/nagios/plugins/cacheslow /usr/lib64/nagios/plugins/check_package_updates | |
# /usr/lib64/nagios/plugins/cacheslow /usr/lib64/nagios/plugins/check_package_updates -w 3 -c 10 | |
CACHE_DIR="/etc/nagios/slowplugins" | |
main() { | |
if [ ! -e "${CACHE_DIR}" ]; then | |
mkdir -p "${CACHE_DIR}" | |
fi | |
if [ ! -e "${CACHE_DIR}/${program}-output" ]; then | |
echo "First run, caching details" | |
daemonize -l ${CACHE_DIR}/${program}.lock -p ${CACHE_DIR}/${program}.pid $me cache $* | |
exit 3 | |
fi | |
daemonize -l ${CACHE_DIR}/${program}.lock -p ${CACHE_DIR}/${program}.pid $me cache $* || exit 3 | |
cat ${CACHE_DIR}/${program}-output | |
RC=$(cat ${CACHE_DIR}/${program}-rc) | |
exit ${RC} | |
} | |
run_real() { | |
F=$(mktemp) | |
$* > ${F} | |
cat ${F} > ${CACHE_DIR}/${program}-output | |
rm -f ${F} | |
echo $? > ${CACHE_DIR}/${program}-rc | |
} | |
if [ "$1" == "cache" ]; then | |
me=$0 | |
shift | |
program=$(echo $1|sed 's/.*\///g') | |
run_real $* | |
else | |
me=$0 | |
program=$(echo $1|sed 's/.*\///g') | |
main $* | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment