-
Notifications
You must be signed in to change notification settings - Fork 3
/
cronitor.sh
165 lines (149 loc) · 3.27 KB
/
cronitor.sh
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/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][-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'
-E: environment
-a: auth key to send for all monitor actions
-s: suppresses output to logger command
-S: suppresses stdout from 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:sSpt:eoa:E:" opt; do
case $opt in
i)
id=$OPTARG
;;
E)
environment=$OPTARG
;;
a)
auth=$OPTARG
;;
s)
silentlog=1
;;
S)
silentstdout=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 "$*"
}
urlencode() {
data="$(curl -s -o /dev/null -w %{url_effective} --get --data-urlencode "$*" "")"
echo "${data##/?}"
return 0
}
callcronitor() {
local mode=${1:-run}
if [ -n "$environment" ]; then
local envstr="env=$environment"
fi
if [ "$mode" == "fail" -a -n "$2" ]; then
shift
local failstr="msg=$(urlencode $*)"
fi
local pingqs=$(join "&" ${auth_arg} ${envstr} ${failstr})
while [ $((curlcount--)) -gt 0 ]; do
local url=cronitor.link/$CRONITOR_ID/$mode?$pingqs
url=${url:0:553}
curl -m$timeout -s --insecure $proto://$url
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 "$silentlog" ]; then
logger -t cronitor "TaskID=$CRONITOR_ID, ExitStatus=$E, ElapsedTimeMS=$timef, Command=$cmd"
fi
if [ -z "$silentstdout" ]; then
echo -n "${output}"
fi
exit $E