#!/bin/bash

############################################################################### # # ./hipchat_room_message # # A script for sending a system message to a room. # # Docs: github.com/hipchat/hipchat-cli # # Usage: # cat message.txt | ./hipchat_room_message -t <token> -r 1234 -f “System” # echo -e “Newnline” | ./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
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/()/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&notify=$NOTIFY" \
https://$HOST/v1/rooms/message