Skip to content

Commit

Permalink
Synced with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Hennerfors committed Oct 24, 2016
2 parents d8abf33 + 64ce05e commit caa0cec
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM alpine:latest
RUN apk add --update \
bash \
curl \
&& rm /var/cache/apk/*
COPY slack.sh /slack.sh
RUN chmod +x /slack.sh \
&& touch /tmp/ip.txt \
&& mkdir -p /tmp
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ Hacky Slack requires cURL (https://curl.haxx.se). Most systems have it installed

Installation is pretty simple. Just copy the <code>slack.sh</code> to <code>/usr/local/bin</code>. Then <code>chmod +x /usr/local/bin/slack.sh</code>.

Please note that the default config (slack.sh) assumes you are installing slack into <code>/usr/local/bin</code>:

```
APP="/usr/local/bin/slack.sh"
BIN="/usr/bin/slack"
```

BIN is used to <code>ln</code> to APP to BIN. This will allow you to use <code>slack</code> vs <code>/usr/local/bin/slack.sh</code>

If you decide to copy Hacky Slack to a different APP directory change the settings accordingly.

# Using Hacky Slack
Expand Down Expand Up @@ -89,13 +80,17 @@ For more information on the above parameters, please check out the Slack docs:
The channel is "general" with username "hacky-slack". The icon is "apple" and the author is "apple". The author name is linked to "apple.com" and the text sent in the message is "Where are the new 2016 Macbook models?"

```
slack -c "#general" -u "hacky-slack" -i "apple" -a "Macbook" -b "http://www.apple.com/ -t "Where are the new 2016 Macbook models?"
sh slack.sh -c "#general" -u "hacky-slack" -i "apple" -a "Macbook" -b "http://www.apple.com/ -t "Where are the new 2016 Macbook models?"
```

Here is a sample message and a screenshot of the message with various flags set.

```
slack -a -t "Hello World" -i ":slack:" -T "Titles are awesome" -p "Pretext is so helpful to include" -s "info"
sh slack.sh -a -t "Hello World" -i ":slack:" -T "Titles are awesome" -p "Pretext is so helpful to include" -s "info"
```

```
sh slack.sh -c "#general" -u "steve" -i "dog" -a "Macbook Pro" -B "http://www.apple.com/" -p "Almost" -Z "Where are the new 2016 Macbook models" -s "ok" -T "Wow" -L "https://www.apple.com" -k "213912391-2093-10293ASSJASLKA"
```

Here is the command represented in Slack:
Expand All @@ -105,6 +100,16 @@ Here is the command represented in Slack:

Note: These examples assume you have set your token and webhook endpoint.

# Hack Slack Docker style

There is a `Dockerfile` included which may simplify running the Hacky Slack. This assumes you have Docker installed. Simply build your image by running `docker build -t slack .`

Here is an example of running with Docker:
```
docker run -it slack /bin/bash /slack.sh -c "#general" -u "steve" -i "dog" -a "Macbook Pro" -B "http://www.apple.com/" -p "Almost" -Z "Where are the new 2016 Macbook models" -s "ok" -T "Wow" -L "https://www.apple.com" -k "213912391-2093-10293ASSJASLKA"
```

This is approach consumes a lot more space that the file alone. The total image is < 10MB. However, it might provide more flexibility in some use cases. Your mileage may vary.

# Hacky Slack + Monit

Expand Down
4 changes: 4 additions & 0 deletions etc/slack.d/monit
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
if test "${PRIORITY}" = "INFO"; then echo "INFO: STATUS (-s) is INFO. Setting a default STATUS to INFO..." && ICON=${ICON:-'monit-info'} && COLOR=${COLOR:-'#439FE0'}; fi
if test "${PRIORITY}" = "WARN"; then echo "INFO: STATUS (-s) is WARN. Setting a default STATUS to WARN..." && ICON=${ICON:-'monit-warn'} && COLOR=${COLOR:-'#ed7d21'}; fi
if test "${PRIORITY}" = "ERROR"; then echo "INFO: STATUS (-s) is ERROR. Setting a default STATUS to ERROR..." && ICON=${ICON:-'monit-error'} && COLOR=${COLOR:-'#E21B6C'}; fi
<<<<<<< HEAD
=======

>>>>>>> upstream/develop

# Save for later
# if grep -Fq 'Action done' ${MONIT_DESCRIPTION}; then ICON=${ICON-':cpu:'}; fi
Expand Down
222 changes: 111 additions & 111 deletions slack.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env bash
#!/bin/bash

# ----------
# Connection
# ----------

# Default WEBHOOK to post messages
if [[ -n ${WEBHOOK} ]]; then
echo "INFO: The Slack API WEBHOOK was passed via the command line (-w)"
Expand All @@ -18,31 +17,26 @@ fi
# ----------
# Environment
# ----------
DATE=$(date +"%Y%m%d")
HOSTNAME=${hostname-$(hostname -s)}
CONFIG="/etc/slack.d"
IPCONFIG="/tmp/ip.txt"
APP="/usr/local/bin/slack.sh"
BIN="/usr/bin/slack"

# Set the symlink for the app if it does not exist
test -L "${APP}"; echo "WARNING: You do not have ${APP} symlinked."; ln ${APP} ${BIN} && chmod +x ${BIN}

# Set defaults
if test "${ENV}" != "Production"; then echo "INFO: ENV (-e) was set to ${ENV}..."; ENV=${ENV}; fi
if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) was set to OK..."; ICON=${ICON:-'good'} && COLOR=${COLOR:-'#36a64f'}; fi

# ----------
# IP
# ---------
# Check for the IP address every 2 hours. Use cache for anything < 2 hours
if [[ ! -f "${IPCONFIG}" ]]; then
echo "${IPCONFIG} does not exist"
IP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
echo "IP=${IP}" > ${IPCONFIG}
echo "INFO: ${IPCONFIG} does not exist. Creating..."
IP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
touch ${IPCONFIG}
echo "IP=${IP}" > ${IPCONFIG}
else
if test "find '${IPCONFIG}' -mmin +120"; then
echo "${IPCONFIG} is less than 2 hours old."
if test "find '${IPCONFIG}' -mmin +120"; then
echo "INFO: ${IPCONFIG} is less than 2 hours old. We will use the cached IP in ${IPCONFIG}..."
else
IP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
echo "IP=${IP}" > ${IPCONFIG}
IP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
echo "WARNING: ${IPCONFIG} is more than 2 hours old. Updating the IP in ${IPCONFIG}..."
echo "IP=${IP}" > ${IPCONFIG}
fi
fi

Expand Down Expand Up @@ -82,121 +76,126 @@ if [[ $# -eq 0 ]]; then
GET_HELP
exit 1
else

while getopts "aA:b:B:c:C:e:h:i:I:m:N:p:s:t:T:L:k:u:w" opt; do
case ${opt} in
a) ATTACHMENT="true" ;;
A) AUTHOR="${OPTARG}" ;;
b) AUTHORICON="${OPTARG}" ;;
B) AUTHORLINK="${OPTARG}" ;;
c) CHANNEL="${OPTARG}" ;;
C) COLOR="${OPTARG}" ;;
e) ENV="${OPTARG}" ;;
h) GET_HELP ;;
i) ICON="${OPTARG}" ;;
I) IMAGE="${OPTARG}" ;;
m) MODE="${OPTARG}" ;;
N) THUMBNAIL="${OPTARG}" ;;
p) PRETEXT="${OPTARG}" ;;
s)
if test "${OPTARG}" = "ok"; then PRIORITY="OK"; fi
if test "${OPTARG}" = "info"; then PRIORITY='INFO'; fi
if test "${OPTARG}" = "warn"; then PRIORITY='WARN'; fi
if test "${OPTARG}" = "error"; then PRIORITY='ERROR'; fi
;;
t) TEXT="${OPTARG}" ;;
T) TITLE="${OPTARG}" ;;
L) TITLELINK="${OPTARG}" ;;
k) TOKEN="${OPTARG}" ;;
u) USERNAME="${OPTARG}" ;;
w) WEBHOOK="${OPTARG}" ;;
esac
done
while getopts ":a:A:b:B:c:C:e:h:i:I:m:N:p:s:Z:T:L:k:u:w" opt; do
case ${opt} in
a) ATTACHMENT="true" ;;
A) AUTHOR="${OPTARG}" ;;
b) AUTHORICON="${OPTARG}" ;;
B) AUTHORLINK="${OPTARG}" ;;
c) CHANNEL="${OPTARG}" ;;
C) COLOR="${OPTARG}" ;;
e) ENV="${OPTARG}" ;;
h) GET_HELP ;;
i) ICON="${OPTARG}" ;;
I) IMAGE="${OPTARG}" ;;
m) MODE="${OPTARG}" ;;
N) THUMBNAIL="${OPTARG}" ;;
p) PRETEXT="${OPTARG}" ;;
s)
if test "${OPTARG}" = "ok"; then PRIORITY="OK"; fi
if test "${OPTARG}" = "info"; then PRIORITY='INFO'; fi
if test "${OPTARG}" = "warn"; then PRIORITY='WARN'; fi
if test "${OPTARG}" = "error"; then PRIORITY='ERROR'; fi
;;
Z) TEXT="${OPTARG}" ;;
T) TITLE="${OPTARG}" ;;
L) TITLELINK="${OPTARG}" ;;
k) TOKEN="${OPTARG}" ;;
u) USERNAME="${OPTARG}" ;;
w) WEBHOOK="${OPTARG}" ;;
esac
done
fi

# ----------
# Check for TOKEN
# ----------
echo "${SLACK_TOKEN}"
# Default TOKEN to post messages
if [[ -n ${TOKEN} ]]; then
echo "INFO: The Slack API TOKEN was passed via the command line (-k)"
echo "INFO: The Slack API TOKEN was passed via the command line (-k)"
elif [[ -n ${SLACK_TOKEN} ]]; then
echo "INFO: The Slack API TOKEN was set as a system variable"
TOKEN=${SLACK_TOKEN}
echo "INFO: The Slack API TOKEN was set as a system variable"
TOKEN=${SLACK_TOKEN}
else
echo "ERROR: No Slack API TOKEN was found. Can not proceed with posting messages to the API without one."
exit 1
echo "ERROR: No Slack API TOKEN was found. Can not proceed with posting messages to the API without one."
exit 1
fi


# ----------
# Service Specific Configurations
# ----------
# Service specifc configurations are passed using -m <config>
# For example, the include monit config (/etc/slack.d/monit) will leverage Monit specific environment variables to set message attributes.
# Service specific configurations are passed using -m <config>
# For example, the include monit config (/etc/slack.d/monit) will leverage mMonit specific environment variables to set message attributes.

# We look for this first, if no config exists we use defaults

if [[ -n "${MODE}" ]]; then
test -d "${CONFIG}" && echo "INFO: The ${CONFIG} direcotry exists" || echo "WARNING: The ${CONFIG} direcotry does not exist. Creating..."; mkdir -p ${CONFIG}
curl -o "${CONFIG}/${MODE}" -z "${CONFIG}/${MODE}" "https://raw.githubusercontent.com/gonace/ob_hacky_slack/develop/etc/slack.d/${MODE}" --verbose

if [[ -z "${MODE}" ]]; then
echo "INFO: No Monit variables are present"
else
source "${CONFIG}/${MODE}"
fi
test -d "${CONFIG}" && echo "INFO: The ${CONFIG} direcotry exists" || echo "WARNING: The ${CONFIG} direcotry does not exist. Creating..."; mkdir -p ${CONFIG}
curl -o "${CONFIG}/${MODE}" -z "${CONFIG}/${MODE}" "https://raw.githubusercontent.com/openbridge/ob_hacky_slack/master/etc/slack.d/${MODE}" --verbose
if [[ -z "${MODE}" ]]; then
echo "INFO: No Monit variables are present"
else
source "${CONFIG}/${MODE}"
fi
else
# ----------
# Tests
# ----------
# Certain elements should be part of a message. Rather than simply exit, we post placeholders to highlight the fact the information is missing
# Set stauts attributes
if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) was set to OK..."; ICON=${ICON:-'good'} && COLOR=${COLOR:-'#36a64f'}; fi
if test "${PRIORITY}" = "INFO"; then echo "INFO: STATUS (-s) was set to INFO..."; ICON=${ICON:-'info'} && COLOR=${COLOR:-'#439FE0'}; fi
if test "${PRIORITY}" = "WARN"; then echo "INFO: STATUS (-s) was set to WARN..."; ICON=${ICON:-'warn'} && COLOR=${COLOR:-'#ed7d21'}; fi
if test "${PRIORITY}" = "ERROR"; then echo "INFO: STATUS (-s) was set to ERROR..."; ICON=${ICON:-'error'} && COLOR=${COLOR:-'#E21B6C'}; fi
if test -z "${USERNAME}"; then echo "INFO: A USERNAME (-u) was not specified for this POST to the Slack API. Setting a default username..."; USERNAME="${IP}"; fi

# Set defaults
test -z "${ENV}"; echo "INFO: You did not provide any ENVIRONMENT (-e) specified."; ENV="${ENV:-'Production'}"
test -z "${TEXT}"; echo "WARNING: You do not have any TEXT (-t) specified in the message."; TEXT="${TEXT:-'This message is missing TEXT'}"
test -z "${TITLE}"; echo "WARNING: You do not have a TITLE (-T) specified for the message."; TITLE=${TITLE:-'This message is missing a TITLE'}
test -z "${PRETEXT}"; echo "WARNING: You do not have a PRETEXT (-p) specified for the message."; PRETEXT=${PRETEXT:-'This message is missing a PRETEXT'}
test -z "${CHANNEL}"; echo "WARNING: A CHANNEL (-c) was not set. Using the default CHANNEL..."; CHANNEL=${CHANNEL:-'general'}
test -z "${PRIORITY}"; echo "INFO: STATUS (-s) was not set. Setting a default STATUS to INFO..."; PRIORITY=${PRIORITY:-'INFO'} && ICON=${ICON:-':info:'} && COLOR=${COLOR:-'#439FE0'}

# ----------
# Style Setting
# ----------
# Certain elements should be part of a message. Rather than simply exit, we post placeholders to highlight the fact the information is missing
# Set stauts attributes
if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) was set to OK..."; ICON=${ICON:-':good:'} && COLOR=${COLOR:-'#36a64f'}; fi
if test "${PRIORITY}" = "INFO"; then echo "INFO: STATUS (-s) was set to INFO..."; ICON=${ICON:-':info:'} && COLOR=${COLOR:-'#439FE0'}; fi
if test "${PRIORITY}" = "WARN"; then echo "INFO: STATUS (-s) was set to WARN..."; ICON=${ICON:-':warn:'} && COLOR=${COLOR:-'#ed7d21'}; fi
if test "${PRIORITY}" = "ERROR"; then echo "INFO: STATUS (-s) was set to ERROR..."; ICON=${ICON:-':error:'} && COLOR=${COLOR:-'#E21B6C'}; fi
if test -z "${USERNAME}"; then echo "INFO: A USERNAME (-u) was not specified for this POST to the Slack API. Setting a default username..."; USERNAME="${IP}"; fi
fi

# ----------
# Send Message
# Test Message
# ----------
# We will test for key parts of the message as they should be present
if [[ -z "${TEXT+x}" ]]; then echo "WARNING: You do not have any TEXT (-t) specified in the message."; TEXT="${TEXT:-'This message is missing TEXT'}"; else echo "INFO: TEXT is set to '${TEXT}'"; fi
if [[ -z "${TITLE+x}" ]]; then echo "WARNING: You do not have a TITLE (-T) specified for the message."; TITLE=${TITLE:-'This message is missing a TITLE'}; else echo "INFO: TITLE is set to '${TITLE}'"; fi
if [[ -z "${PRETEXT+x}" ]]; then echo "WARNING: You do not have a PRETEXT (-p) specified for the message."; PRETEXT=${PRETEXT:-'This message is missing a PRETEXT'}; else echo "INFO: PRETEXT is set to '${PRETEXT}'"; fi
if [[ -z "${CHANNEL+x}" ]]; then echo "WARNING: A CHANNEL (-c) was not set. Using the default CHANNEL..."; CHANNEL=${CHANNEL:-'general'}; else echo "INFO: CHANNEL is set to '${CHANNEL}'"; fi
if [[ -z "${PRIORITY+x}" ]]; then echo echo "INFO: STATUS (-s) was not set. Setting a default STATUS to INFO..."; PRIORITY=${PRIORITY:-'INFO'} && ICON=${ICON:-':info:'} && COLOR=${COLOR:-'#439FE0'}; else echo "INFO: PRIORITY is set to '${PRIORITY}'"; fi
if [[ -z "${ENV+x}" ]]; then echo "INFO: A ENV (-e) was not set. Using the default ENV..."; ENV=${ENV:-'Development'}; else echo "INFO: ENV is set to '${ENV}'"; fi

# ----------
# Send Message
# ----------
function SEND() {
# The complete Slack API payload, including attachments#
# The complete Slack API payload, including attachments#
if [[ ${ATTACHMENT} = "true" ]]; then
PAYLOAD="payload={ \
\"channel\": \"${CHANNEL}\", \
\"username\": \"${USERNAME}\", \
\"pretext\": \"${PRETEXT}\", \
\"color\": \"${COLOR}\", \
\"icon_emoji\": \":${ICON}:\", \
\"text\": \"${TEXT}\", \
\"mrkdwn\": \"true\", \
\"attachments\": [{
\"fallback\": \"${FALLBACK}\", \
\"color\": \"${COLOR}\", \
\"pretext\": \"${PRETEXT}\", \
\"author_name\": \"${AUTHOR}\", \
\"author_link\": \"${AUTHORLINK}\", \
\"author_icon\": \"${AUTHORICON}\", \
\"title\": \"${TITLE}\", \
\"title_link\": \"${TITLELINK}\", \
\"text\": \"${TEXT}\", \
\"mrkdwn_in\": [\"text\",\"pretext\",\"fields\"], \
\"fields\": [{\"title\": \"Status\",\"value\": \"${PRIORITY}\",\"short\": \"true\"}, {\"title\": \"Host\",\"value\": \"${IP}\",\"short\": \"true\"}, {\"title\": \"Environment\",\"value\": \"${ENV}\",\"short\": \"true\"} ], \
\"image_url\": \"${IMAGE}\", \
\"thumb_url\": \"${THUMBNAIL}\" \
}]}"
else
PAYLOAD="payload={ \
\"channel\": \"${CHANNEL}\", \
\"username\": \"${USERNAME}\", \
\"pretext\": \"${PRETEXT}\", \
\"color\": \"${COLOR}\", \
\"icon_emoji\": \":${ICON}:\", \
\"text\": \"${TEXT}\", \
\"mrkdwn\": \"true\", \
\"attachments\": [{
\"fallback\": \"${FALLBACK}\", \
\"color\": \"${COLOR}\", \
\"pretext\": \"${PRETEXT}\", \
\"author_name\": \"${AUTHOR}\", \
\"author_link\": \"${AUTHORLINK}\", \
\"author_icon\": \"${AUTHORICON}\", \
\"title\": \"${TITLE}\", \
\"title_link\": \"${TITLELINK}\", \
\"text\": \"${TEXT}\", \
\"mrkdwn_in\": [\"text\",\"pretext\",\"fields\"], \
\"fields\": [{\"title\": \"Status\",\"value\": \"${PRIORITY}\",\"short\": \"true\"}, {\"title\": \"Host\",\"value\": \"${USERNAME}\",\"short\": \"true\"}, {\"title\": \"Environment\",\"value\": \"${ENV}\",\"short\": \"true\"} ], \
\"image_url\": \"${IMAGE}\", \
\"thumb_url\": \"${THUMBNAIL}\" \
}]}"
else
PAYLOAD="payload={ \
\"channel\": \"${CHANNEL}\", \
\"channel\": \"${CHANNEL}\", \
\"username\": \"${USERNAME}\", \
\"pretext\": \"${PRETEXT}\", \
\"color\": \"${COLOR}\", \
Expand All @@ -207,11 +206,12 @@ function SEND() {
fi

# Send the payload to the Slack API
echo "OK: All tests passed, sending message to Slack API..."
POST=$(curl -s -S -X POST --data-urlencode "${PAYLOAD}" "${WEBHOOK}${TOKEN}");

# Check if the message posted to the Slack API. A successful POST should return "ok". Anything other than "ok" indicates an issue
test "${POST}" != ok && echo "ERROR: The POST to the Slack API failed" && return 1
if test "${POST}" != ok; then echo "ERROR: The POST to the Slack API failed" && return 1; else echo "OK: Message successfully sent to the channel ${CHANNEL} via the Slack API"; fi
}

SEND
exit 0
exit 1

0 comments on commit caa0cec

Please sign in to comment.