Create a gist now

Instantly share code, notes, and snippets.

@erchn /cronitor.sh
Last active Mar 17, 2016

What would you like to do?
Cronitor wrapper script for start/stop notifications, shamelessly stolen from the now defunct Proby
#!/bin/bash
#
# This script surrounds the command passed in with start and finish notifications
# to the cronitor monitoring application.
#
# === SETUP
#
# * Make sure the cronitor script is executable.
#
# chmod +x cronitor
#
# === USAGE
#
# see usage() function below
#
#
# === EXAMPLE
#
# CRONITOR_ID=83a8d6c0c103012efde3lk8092kasdf6 /path/to/cronitor 'ls -l | grep foo | cut -f 3 -d " "'
#
# If invoking using cron, your crontab entry may look something like
#
# * * * * * CRONITOR_ID=83a8d6c0 /path/to/cronitor 'ls -l | grep couch | cut -f 3 -d " "'
#
#
# === DEPENDENCIES
#
# * curl
# * http://cronitor.link
#
usage() {
cat <<EOF
$0 [-a][-s] [-i cronitor_id] [-n] [-p] [-t 8] [-e] [-o]
echo "Usage: CRONITOR_ID=<your cronitor id> cronitor [-...] '<command>'"
or: cronitor -i <your cronitor id> [-...] 'command'
-a: auth key to send for all monitor actions
-s: suppresses output to logger command
-p: disable ssl in favor of plain-text
-e: do not sleep a few random seconds at start, reduce spikes locally and at Cronitor
-o: only try curl commands once, even on retryable failures (6, 7, 28, 35), default 3 times
-t: curl timeout in seconds; default 10
EOF
exit 1
}
proto=https
timeout=10
sleep=$[ ( $RANDOM % 10 ) + 1 ]
curlcount=3
while getopts ":i:snd:pt:eoa:" opt; do
case $opt in
i)
id=$OPTARG
;;
a)
auth=$OPTARG
;;
s)
silent=1
;;
p)
proto=http
;;
e)
sleep=0
;;
o)
curlcount=1
;;
t)
timeout=$OPTARG
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
\?)
usage
;;
esac
done
shift $(($OPTIND-1))
[ -n "$id" ] && CRONITOR_ID=$id
[ -z "$CRONITOR_ID" ] && usage
[ -n "$auth" ] && auth_arg="auth_key=$auth"
join() {
local IFS="$1"
shift
echo "$*"
}
callcronitor() {
local mode=${1:-run}
[ "$mode" == "fail" -a -n "$2" ] && local failstr="msg=$2"
local pingqs=$(join "&" $auth_arg $failstr)
while [ $((curlcount--)) -gt 0 ]; do
curl -m$timeout -s --insecure $proto://cronitor.link/$CRONITOR_ID/$mode?$pingqs
local e=$?
[ $e -eq 6 ] && continue
[ $e -eq 7 ] && continue
[ $e -eq 28 ] && continue
[ $e -eq 35 ] && continue
break
done
return $e
}
# sleep skew
sleep $sleep
# begin
callcronitor
time1=$(date +%s%3N)
cmd="$@"
output=$(bash -c "$@")
E=$?
time2=$(date +%s%3N)
timef=$(($time2 - $time1))
# does this task use the retry wrapper?
retry=$(echo $cmd | grep -o retry.sh)
if [ $E -ne 0 ]; then
mode="fail"
fail_str="$output"
fi
callcronitor ${mode:-complete} $fail_str
if [ -z "$silent" ]; then
logger -t cronitor "TaskID=$CRONITOR_ID, ExitStatus=$E, ElapsedTimeMS=$timef, Command=$cmd"
fi
exit $E

thbar commented Feb 19, 2016

Thanks for sharing this - useful as I'm migrating from Proby to Cronitor!

This script doesn't properly handle stderr which has spaces in it from the wrapped script. It also eats stdout, which may be a feature or a bug.
See https://gist.github.com/asheetz2000/6838f446cd28ad77222d for a version which addresses both issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment