Skip to content

Instantly share code, notes, and snippets.

@non7top
Created February 12, 2019 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save non7top/6385a4ba4473fc51fef8f3ca504e6334 to your computer and use it in GitHub Desktop.
Save non7top/6385a4ba4473fc51fef8f3ca504e6334 to your computer and use it in GitHub Desktop.
nagios site check, uses curl
#!/bin/bash
exec 2>&1
[ -d /dev/shm -a -w /dev/shm ] && export TMPDIR=/dev/shm
# Parsing Input Parameters {{{
#####################################################################
allparams=$*
g_myname_pid=$$
myparams=""
if [ $# -eq 0 ]; then
__die "No options specified"
fi
for myarg in ${allparams}
do
arg=`printf '%s\n' $myarg | sed 's/^-//'`
case $arg in
u=*) g_url=`echo $arg | sed 's/^u=//g'` ;; # Url
n=*) g_not_present=`echo $arg | sed 's/n=//g'` ;; # Line that shoud not be present
m=*) g_expect_string=`echo $arg | sed 's/m=//g'` ;; # Line that shoud be present, match
user=*) g_user=`echo $arg | sed 's/^user=//g'` ;; # username for basic auth user:password
-) : ;;
*) echo "$0: unrecognized action specified $arg" ; exit 1 ;;
esac
shift
done
### }}}
CONTENT=$( mktemp -t check_http_wget_c.XXXXXX )
LOG=$( mktemp -t check_http_wget_l.XXXXXX )
if [ ! -z $g_user ]; then
GUSER="-u $g_user"
fi
c=$( curl --retry 2 --silent --show-error --location \
--user-agent "non7top.ml" --max-time 30 \
--output $CONTENT --stderr $LOG $GUSER \
-w "%{time_total};%{time_starttransfer};%{response_code}\n" \
$g_url )
res=$?
__time=$(printf "%4.2f\n" $(echo $c | awk -F";" '{print $1}' ) )
PERFDATA="time=${__time}s"
if [ $res -ne 0 ]; then
wget_err=$( cat $LOG )
echo "HTTP ERROR: Could't get $g_url ($wget_err) | $PERFDATA"
exit 2
fi
__code=$(echo $c | awk -F";" '{print $3}')
PERFDATA="$PERFDATA code=$__code"
# Check that string is present
grep -q -i $g_expect_string $CONTENT
res=$?
if [ $res -eq 0 ]; then
echo "HTTP OK: EXPR $g_expect_string FOUND on $g_url in $__time | $PERFDATA"
rm -f $CONTENT $LOG
exit 0
else
echo "HTTP ERROR: EXPR $g_expect_string NOT FOUND on $g_url in $__time | $PERFDATA"
exit 2
fi
echo "HTTP UNKNOWN: Looks like script error"
exit 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment