This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
hipchat
executable file
·111 lines (97 loc) · 3.12 KB
/
hipchat
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
#!/bin/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
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.
OPTIONS:
-h Show this message
-t <token> API token
-r <room id> Room ID
-f <from name> From name
-c <color> Message color (yellow, red, green, purple 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)
EOF
}
# Include hipchat defaults if available
test -f /etc/hipchat && . /etc/hipchat
TOKEN=${HIPCHAT_TOKEN:-}
ROOM_ID=${HIPCHAT_ROOM_ID:-}
FROM=${HIPCHAT_FROM:-}
COLOR=${HIPCHAT_COLOR:-}
FORMAT=${HIPCHAT_FORMAT:-html}
MESSAGE=${HIPCHAT_MESSAGE:-html}
NOTIFY=${HIPCHAT_NOTIFY:-0}
HOST=${HIPCHAT_HOST:-api.hipchat.com}
LEVEL=${HIPCHAT_LEVEL:-}
while getopts “ht:r:f:c:m:o:i:l:n” 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;;
[?]) usage; exit;;
esac
done
# check for required args
if [[ -z $TOKEN ]] || [[ -z $ROOM_ID ]] || [[ -z $FROM ]]; then
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}" | sed "s/$/\<br\>/")
fi
# replace bare URLs with real hyperlinks
INPUT=$(echo -n "${INPUT}" | perl -p -e "s/(?<!href=\"|href=')((?:https?|ftp|mailto)\:\/\/[^ \n]*)/\<a href=\"\1\"\>\1\<\/a>/g")
# urlencode with perl
INPUT=$(echo -n "${INPUT}" | perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg')
# do the curl
curl -sS \
-d "auth_token=$TOKEN&room_id=$ROOM_ID&from=$FROM&color=$COLOR&message_format=$FORMAT&message=$INPUT¬ify=$NOTIFY" \
https://$HOST/v1/rooms/message