-
Notifications
You must be signed in to change notification settings - Fork 92
/
hipchat_room_message
executable file
·174 lines (153 loc) · 4.83 KB
/
hipchat_room_message
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
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash
###############################################################################
#
# ./hipchat_room_message
#
# A script for sending a system message to a room.
#
# Docs: http://github.com/hipchat/hipchat-cli
#
# Usage:
# cat message.txt | ./hipchat_room_message -t <token> -r 1234 -f "System"
# echo -e "New\nline" | ./hipchat_room_message -t <token> -r 1234 -f "System"
#
###############################################################################
# exit on failure
set -e
# http://apple.stackexchange.com/questions/68684/why-sh-c-echo-n-1-is-different-from-bash-c-echo-n-1
shopt -u xpg_echo
usage() {
cat << EOF
Usage: $0 -t <token> -r <room id> -f <from name>
This script will read from stdin and send the contents to the given room as
a system message. Or use -i message.
OPTIONS:
-h Show this message
-t <token> API token
-r <room id> Room ID
-f <from name> From name (optional in v2 API)
-c <color> Message color (yellow, red, green, purple, gray
or random - default: yellow)
-m <format> Message format (html or text - default: html)
-i <input> Optional: Input to send to room (default: stdin)
-l <level> Nagios message level (critical, warning, unknown,
ok, down, up). Will override color.
-n Trigger notification for people in the room
-o API host (api.hipchat.com)
-v <version> API version (default: v1)
-k Allow curl to make insecure SSL connections
EOF
}
# Include hipchat defaults if available
test -f /etc/hipchat && . /etc/hipchat
test -f ~/.hipchat && . ~/.hipchat
TOKEN=${HIPCHAT_TOKEN:-}
ROOM_ID=${HIPCHAT_ROOM_ID:-}
FROM=${HIPCHAT_FROM:-}
COLOR=${HIPCHAT_COLOR:-yellow}
FORMAT=${HIPCHAT_FORMAT:-html}
MESSAGE=${HIPCHAT_MESSAGE:-html}
NOTIFY=${HIPCHAT_NOTIFY:-0}
HOST=${HIPCHAT_HOST:-api.hipchat.com}
LEVEL=${HIPCHAT_LEVEL:-}
API=${HIPCHAT_API:-v1}
ALLOW_INSECURE=false
while getopts "ht:r:f:c:m:o:i:l:v:nk" OPTION; do
case $OPTION in
h) usage; exit 1;;
t) TOKEN=$OPTARG;;
r) ROOM_ID=$OPTARG;;
f) FROM=$OPTARG;;
c) COLOR=$OPTARG;;
m) FORMAT=$OPTARG;;
n) NOTIFY=1;;
i) INPUT=$OPTARG;;
l) LEVEL=$OPTARG;;
o) HOST=$OPTARG;;
v) API=$OPTARG;;
k) ALLOW_INSECURE=true;;
[?]) usage; exit;;
esac
done
# check for required args
if [[ -z $TOKEN ]] || [[ -z $ROOM_ID ]] || [[ -z $FROM && $API = "v1" ]]; then
if [[ -z $TOKEN ]]; then
echo "$(basename $0): missing TOKEN"
fi
if [[ -z $ROOM_ID ]]; then
echo "$(basename $0): missing ROOM_ID"
fi
if [[ -z $FROM && $API = "v1" ]]; then
echo "$(basename $0): missing FROM"
fi
usage
exit 1
fi
# nagios levels
if [ ! -z "$LEVEL" ]; then
if [[ $LEVEL == 'CRITICAL' ]] || [[ $LEVEL == 'critical' ]]; then
COLOR="red";
elif [[ $LEVEL == 'WARNING' ]] || [[ $LEVEL == 'warning' ]]; then
COLOR="yellow";
elif [[ $LEVEL == 'UNKNOWN' ]] || [[ $LEVEL == 'unknown' ]]; then
COLOR="gray";
elif [[ $LEVEL == 'OK' ]] || [[ $LEVEL == 'ok' ]]; then
COLOR="green";
elif [[ $LEVEL == 'DOWN' ]] || [[ $LEVEL == 'down' ]]; then
COLOR="red";
elif [[ $LEVEL == 'UP' ]] || [[ $LEVEL == 'up' ]]; then
COLOR="green";
fi
fi
if [ -z "$INPUT" ]; then
# read stdin
INPUT=$(cat)
fi
# replace newlines with XHTML <br>
if [ $FORMAT == 'html' ]; then
INPUT=$(echo -n "${INPUT}" | perl -p -e 's/\n/<br \/>/')
fi
# replace bare URLs with real hyperlinks
INPUT=$(echo -n "${INPUT}" | perl -p -e "s/(?<!href=\"|href='| src=\"| src=')((?:https?|ftp|mailto)\:\/\/[^ \n]*)/\<a href=\"\1\"\>\1\<\/a>/g")
# urlencode with perl
if [ $API == 'v1' ]; then
INPUT=$(echo -n "${INPUT}" | perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg')
fi
# escape special characters in json
if [ $API == 'v2' ]; then
INPUT=$(echo -n "${INPUT}" | sed 's/\\/\\\\/g' | sed 's/"/\\\"/g')
fi
# replace notification boolean from 1/0 to 'true'/'false' for API v2
if [ $API == 'v2' ]; then
if [ $NOTIFY -eq 0 ]; then
NOTIFY='false'
else
NOTIFY='true'
fi
fi
curl_opts="-sS"
if [ $ALLOW_INSECURE == true ]; then
curl_opts+=" --insecure"
fi
# do the curl, using a temporary filename
# (stored in BODY) to avoid long URL errors
BODY=`mktemp`
if [ $API == 'v2' ]; then
(echo "{\"color\":\"$COLOR\", "
if [[ -n "$FROM" ]] ; then
echo "\"from\": \"$FROM\", "
fi
echo "\"message\":\"$INPUT\", \"message_format\":\"$FORMAT\", \"notify\":$NOTIFY}"
) > $BODY
curl ${curl_opts} \
-H 'Content-type: application/json' \
-H "Authorization: Bearer $TOKEN" \
-d @$BODY \
https://$HOST/v2/room/$ROOM_ID/notification
else
echo "auth_token=$TOKEN&room_id=$ROOM_ID&from=$FROM&color=$COLOR&message_format=$FORMAT&message=$INPUT¬ify=$NOTIFY" > $BODY
curl ${curl_opts} \
-d @$BODY \
https://$HOST/v1/rooms/message
fi
rm -f $BODY