-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoni.sh
executable file
·74 lines (64 loc) · 1.71 KB
/
moni.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
#!/bin/bash
ACCESS_KEY=""
RECIPIENTS=""
ORIGINATOR=""
MESSAGE=""
SLEEP_TIME_SECONDS=300
MAX_DELTA_SECONDS=1200
ts_last_message=0
function read_config() {
log "Reading configuration at $1"
configuration_path=$1
ACCESS_KEY=`cat $configuration_path | jq ".ACCESS_KEY"`
RECIPIENTS=`cat $configuration_path | jq ".RECIPIENTS"`
ORIGINATOR=`cat $configuration_path | jq ".ORIGINATOR"`
MESSAGE=`cat $configuration_path | jq ".MESSAGE"`
}
function get_date() {
echo "$(date +%d-%m-%Y" "%H:%M:%S)"
}
function log() {
msg=$1
echo "[`get_date`] $msg" | tee -a monish.log
}
function messagebird_send_sms() {
ts=`date +%s`
delta=`expr $ts - $ts_last_message`
log "delta=$delta"
if [[ "$delta" -gt "$MAX_DELTA_SECONDS" ]]; then
resp=`curl --silent -X POST https://rest.messagebird.com/messages \
-H "Authorization: AccessKey $ACCESS_KEY" \
-d "recipients=$RECIPIENTS" \
-d "originator=$ORIGINATOR" \
-d "body=$MESSAGE"`
ts_last_message=`date +%s`
errors=`echo $resp | jq -c .errors`
if [[ $errors == "null" ]]; then
href=`echo $resp | jq .href`
log "Request to messagebird succeeded! $href"
else
log "Request to messagebird failed! $resp"
fi
else
log "$delta seconds from last message, postponing alert..."
sleep $SLEEP_TIME_SECONDS
fi
}
function main() {
read_config "./config.json"
regexp=$1
log "Reading regexp $regexp"
if [ -p /dev/stdin ]; then
while IFS= read line; do
match=$(grep "$regexp" <<< "$line")
if [[ -n $match ]]; then
log "Match found! $match"
messagebird_send_sms
fi
done
else
echo "usage: "
echo "echo \"I bet you ain't match this!\" | ./moni.sh \"*atch\""
fi
}
main "$1"