-
Notifications
You must be signed in to change notification settings - Fork 57
/
check_sentry-events.sh
81 lines (69 loc) · 2.29 KB
/
check_sentry-events.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
#!/bin/bash
#honza@inuits.eu
token=''
sentry_url=''
days_ago=1
exit_code=0
extra_config_file=/dev/null
output=''
critical=''
warning=''
usage(){
cat <<EOF
usage: $0 <options>
-u sentry url
-t api token
-d number of the past days to search for events from
-w warning number of events
-c critical number of events
-f extra config file
Extra config file allows to set specific thresholds for individual projects. If no thresholds are set for a project, the defaults set with '-w' and '-c' will be used.
The file should look as follows:
<project name>;<warning threshold>;<critical threshold>
one project per line
EOF
exit 3
}
while getopts 'u:t:d:c:w:f:' OPTION
do
case $OPTION in
u) sentry_url=$OPTARG;;
t) token=$OPTARG;;
d) days_ago=$OPTARG;;
c) critical=$OPTARG;;
w) warning=$OPTARG;;
f) extra_config_file=$OPTARG;;
*) usage;;
esac
done
if [[ -z $sentry_url ]] || [[ -z $token ]] || [[ -z $critical ]] || [[ -z $warning ]]; then
usage
fi
timestamp=$(date +%s --date=" ${days_ago} days ago")
organizations=$(curl --silent -H "Authorization: Bearer ${token}" ${sentry_url}/api/0/organizations/ | jq .[].slug -r)
for organization in $organizations; do
projects=$(curl --silent -H "Authorization: Bearer ${token}" "${sentry_url}/api/0/organizations/${organization}/projects/" | jq .[].slug -r)
for project in $projects; do
_warning=''
_critical=''
events=$(curl --silent -H "Authorization: Bearer ${token}" "${sentry_url}/api/0/projects/${organization}/${project}/stats/?&since=${timestamp}&until=${timestamp_now}" | jq .[][1] | awk '{s+=$1} END {print s}')
output="${output}$project: $events; "
if egrep -q "^${project};[0-9]+;[0-9]+$" $extra_config_file; then
_warning=$(cat $extra_config_file | egrep "^${project};[0-9]+;[0-9]+$" | cut -f2 -d ';')
_critical=$(cat $extra_config_file | egrep "^${project};[0-9]+;[0-9]+$" | cut -f3 -d ';')
if [[ $events -ge $_critical ]]; then
exit_code=2
elif [[ $events -ge $_warning ]]; then
[[ $exit_code -lt 1 ]] && exit_code=1
fi
else
if [[ $events -ge $critical ]]; then
exit_code=2
elif [[ $events -ge $warning ]]; then
[[ $exit_code -lt 1 ]] && exit_code=1
fi
fi
done
done
echo "${output}for the past ${days_ago} days"
exit $exit_code