From 988ac819272bf0e039ce5497b7c837bfd6bec82c Mon Sep 17 00:00:00 2001 From: Thomas Spicer Date: Fri, 21 Oct 2016 18:17:16 -0400 Subject: [PATCH 01/17] fix error with TOKEN test Token test occured before the variable was set which caused an error. Moved test further down --- slack.sh | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/slack.sh b/slack.sh index 856a3eb..4a8e335 100644 --- a/slack.sh +++ b/slack.sh @@ -21,22 +21,6 @@ else fi -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)" - -elif [[ -n ${SLACK_TOKEN} ]]; then - - 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 -fi - # ---------- # Environment # ---------- @@ -148,6 +132,26 @@ while getopts "aA:b:B:c:Chi:I:m:N:p:s:t:T:L:k:u:w" opt; do 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)" + +elif [[ -n ${SLACK_TOKEN} ]]; then + + 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 +fi + # ---------- # Service Specific Configurations # ---------- From 3b2e5c6ffc09dd29563a7d19da9325203bf799fa Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Sat, 22 Oct 2016 15:37:51 +0200 Subject: [PATCH 02/17] Replaced old repo name 'ob_slack' with 'ob_hacky_slack' so /etc/slack.d/monit is fetched correctly --- slack.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slack.sh b/slack.sh index 4a8e335..ab82559 100644 --- a/slack.sh +++ b/slack.sh @@ -164,7 +164,7 @@ 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/openbridge/ob_slack/master/etc/slack.d/${MODE}" --verbose + 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 From 97c7ad748ae63798ec0731d10c7d6bbb6a9ec913 Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Sat, 22 Oct 2016 16:23:49 +0200 Subject: [PATCH 03/17] Added env. argument, remove excessive colon/punctuation for icons --- etc/slack.d/monit | 8 +-- slack.sh | 160 +++++++++++++++++----------------------------- 2 files changed, 63 insertions(+), 105 deletions(-) diff --git a/etc/slack.d/monit b/etc/slack.d/monit index d7f009f..32dfd56 100644 --- a/etc/slack.d/monit +++ b/etc/slack.d/monit @@ -4,10 +4,10 @@ PRETEXT="${MONIT_DATE} | ${MONIT_HOST}: ${MONIT_EVENT}" FALLBACK="${MONIT_DATE} | ${MONIT_HOST}: ${MONIT_EVENT}" - if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) is OK. Setting a default STATUS to OK..." && ICON=${ICON:-':monit-ok:'} && COLOR=${COLOR:-'#36a64f'}; fi - 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 + if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) is OK. Setting a default STATUS to OK..." && ICON=${ICON:-'monit-ok'} && COLOR=${COLOR:-'#36a64f'}; fi + 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 # Save for later # if grep -Fq 'Action done' ${MONIT_DESCRIPTION}; then ICON=${ICON-':cpu:'}; fi diff --git a/slack.sh b/slack.sh index ab82559..705009d 100644 --- a/slack.sh +++ b/slack.sh @@ -6,19 +6,13 @@ # Default WEBHOOK to post messages if [[ -n ${WEBHOOK} ]]; then - echo "INFO: The Slack API WEBHOOK was passed via the command line (-w)" - elif [[ -n ${SLACK_WEBHOOK} ]]; then - echo "INFO: The Slack API TOKEN was set as a system variable" TOKEN=${SLACK_WEBHOOK} - else - echo "INFO: Using default Slack API endpoint to POST messages..." WEBHOOK=${WEBHOOK-'https://hooks.slack.com/services/'} - fi # ---------- @@ -40,22 +34,15 @@ if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) was set to OK..."; IC # 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 "${IPCONFIG} does not exist" + IP=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//') + echo "IP=${IP}" > ${IPCONFIG} else - if test "find '${IPCONFIG}' -mmin +120"; then - - echo "${IPCONFIG} is less than 2 hours old." - + echo "${IPCONFIG} is less than 2 hours old." 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 "IP=${IP}" > ${IPCONFIG} fi fi @@ -64,9 +51,7 @@ source ${IPCONFIG} # ---------- # Commands # ---------- - -function GET_HELP() -{ +function GET_HELP() { echo "Usage: [options]" echo " options:" echo "-a, Attachment Sends a messages as an attachment." @@ -75,6 +60,7 @@ function GET_HELP() echo "-B, Author Icon A URL that displays a small image to the left of the author_name text.(Author name is required)." echo "-c, Channel The location the messages should be delivered." echo "-C, Color This value is used to color the border along the left side of the message attachment." + echo "-e, Environment This value is used to provide the message with an environment identifier." echo "-h, Help Show the command options for Slack." echo "-i, Icon A URL to an image file that will be displayed inside a message attachment." echo "-I, Image Small text used to display the author's name." @@ -88,20 +74,16 @@ function GET_HELP() echo "-k, Token Authenticates the POST to Slack." echo "-u, Username User that posts the message." echo "-w, Webhook The Slack API service endpoint to POST messages." - -exit 1 + exit 1 } # Check if any arguments were passed if [[ $# -eq 0 ]]; then - -GET_HELP -exit 1 - + GET_HELP + exit 1 else while getopts "aA:b:B:c:Chi:I:m:N:p:s:t:T:L:k:u:w" opt; do - case ${opt} in a) ATTACHMENT="true" ;; A) AUTHOR="${OPTARG}" ;; @@ -109,6 +91,7 @@ while getopts "aA:b:B:c:Chi:I:m:N:p:s:t:T:L:k:u:w" opt; do B) AUTHORLINK="${OPTARG}" ;; c) CHANNEL="${OPTARG}" ;; C) COLOR="${OPTARG}" ;; + e) ENV="${OPTARG}" ;; h) GET_HELP ;; i) ICON="${OPTARG}" ;; I) IMAGE="${OPTARG}" ;; @@ -129,24 +112,17 @@ while getopts "aA:b:B:c:Chi:I:m:N:p:s:t:T:L:k:u:w" opt; do 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)" - elif [[ -n ${SLACK_TOKEN} ]]; then - 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 @@ -157,37 +133,26 @@ fi # ---------- # Service specifc configurations are passed using -m # 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/openbridge/ob_hacky_slack/master/etc/slack.d/${MODE}" --verbose + curl -o "${CONFIG}/${MODE}" -z "${CONFIG}/${MODE}" "https://raw.githubusercontent.com/gonace/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 "${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 @@ -195,65 +160,58 @@ else 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 "${ENV}"; echo "INFO: You did not provide any ENVIRONMENT (-e) specified."; ENV="${ENV:-'Production'}" 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'} - fi # ---------- # Send Message # ---------- -function SEND() -{ -# 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\": \"${USERNAME}\",\"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\" \ - }" -fi - -# Send the payload to the Slack API -POST=$(curl -s -S -X POST --data-urlencode "${PAYLOAD}" "${WEBHOOK}${TOKEN}"); +function SEND() { + # 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\": \"${USERNAME}\",\"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\" \ + }" + fi -# 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 + # Send the payload to the 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 } SEND - exit 0 From 86dfd1656fc908679082bdd668d6cd3ab93c1a43 Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Sat, 22 Oct 2016 16:51:32 +0200 Subject: [PATCH 04/17] Fixed ENV variable for munin config --- etc/slack.d/monit | 1 + slack.sh | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/etc/slack.d/monit b/etc/slack.d/monit index 32dfd56..761f20e 100644 --- a/etc/slack.d/monit +++ b/etc/slack.d/monit @@ -3,6 +3,7 @@ TEXT="${MONIT_DESCRIPTION}" PRETEXT="${MONIT_DATE} | ${MONIT_HOST}: ${MONIT_EVENT}" FALLBACK="${MONIT_DATE} | ${MONIT_HOST}: ${MONIT_EVENT}" + ENV="${ENV}" if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) is OK. Setting a default STATUS to OK..." && ICON=${ICON:-'monit-ok'} && COLOR=${COLOR:-'#36a64f'}; fi 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 diff --git a/slack.sh b/slack.sh index 705009d..4b2cb95 100644 --- a/slack.sh +++ b/slack.sh @@ -18,7 +18,6 @@ fi # ---------- # Environment # ---------- - DATE=$(date +"%Y%m%d") HOSTNAME=${hostname-$(hostname -s)} CONFIG="/etc/slack.d" @@ -30,7 +29,8 @@ BIN="/usr/bin/slack" test -L "${APP}"; echo "WARNING: You do not have ${APP} symlinked."; ln ${APP} ${BIN} && chmod +x ${BIN} # Set defaults -if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) was set to OK..."; ICON=${ICON:-':good:'} && COLOR=${COLOR:-'#36a64f'}; fi +if test "${ENV}" = "Production"; then echo "INFO: ENV (-e) was set to Production..."; ENV=${ENV:-'Production'}; fi +if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) was set to OK..."; ICON=${ICON:-'good'} && COLOR=${COLOR:-'#36a64f'}; fi # Check for the IP address every 2 hours. Use cache for anything < 2 hours if [[ ! -f "${IPCONFIG}" ]]; then @@ -83,7 +83,7 @@ if [[ $# -eq 0 ]]; then exit 1 else -while getopts "aA:b:B:c:Chi:I:m:N:p:s:t:T:L:k:u:w" opt; do +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}" ;; @@ -132,11 +132,11 @@ fi # Service Specific Configurations # ---------- # Service specifc configurations are passed using -m -# For example, the include monit config (/etc/slack.d/monit) will leverage mMonit specific environment variables to set message attributes. +# For example, the include monit config (/etc/slack.d/monit) will leverage Monit 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/master/etc/slack.d/${MODE}" --verbose + 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" @@ -156,11 +156,11 @@ else 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 "${ENV}"; echo "INFO: You did not provide any ENVIRONMENT (-e) specified."; ENV="${ENV:-'Production'}" 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'} fi From 77df07a451865ea052608d330329895144e7b6a8 Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Sat, 22 Oct 2016 17:05:44 +0200 Subject: [PATCH 05/17] Fixed ENV variable for munin config --- etc/slack.d/monit | 1 - slack.sh | 9 +++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/etc/slack.d/monit b/etc/slack.d/monit index 761f20e..32dfd56 100644 --- a/etc/slack.d/monit +++ b/etc/slack.d/monit @@ -3,7 +3,6 @@ TEXT="${MONIT_DESCRIPTION}" PRETEXT="${MONIT_DATE} | ${MONIT_HOST}: ${MONIT_EVENT}" FALLBACK="${MONIT_DATE} | ${MONIT_HOST}: ${MONIT_EVENT}" - ENV="${ENV}" if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) is OK. Setting a default STATUS to OK..." && ICON=${ICON:-'monit-ok'} && COLOR=${COLOR:-'#36a64f'}; fi 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 diff --git a/slack.sh b/slack.sh index 4b2cb95..2b4fb64 100644 --- a/slack.sh +++ b/slack.sh @@ -24,12 +24,13 @@ CONFIG="/etc/slack.d" IPCONFIG="/tmp/ip.txt" APP="/usr/local/bin/slack.sh" BIN="/usr/bin/slack" +ENV="Production" # 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 Production..."; ENV=${ENV:-'Production'}; fi +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 # Check for the IP address every 2 hours. Use cache for anything < 2 hours @@ -91,7 +92,11 @@ while getopts "aA:b:B:c:C:e:h:i:I:m:N:p:s:t:T:L:k:u:w" opt; do B) AUTHORLINK="${OPTARG}" ;; c) CHANNEL="${OPTARG}" ;; C) COLOR="${OPTARG}" ;; - e) ENV="${OPTARG}" ;; + e) + if test "${OPTARG}" = "Test"; then ENV="Test"; fi + if test "${OPTARG}" = "Stage"; then ENV="Stage"; fi + if test "${OPTARG}" = "Production"; then ENV="Production"; fi + ;; h) GET_HELP ;; i) ICON="${OPTARG}" ;; I) IMAGE="${OPTARG}" ;; From 523dc9329b58fda8284e813e1fa378b7a3dba36b Mon Sep 17 00:00:00 2001 From: Thomas Spicer Date: Sat, 22 Oct 2016 11:22:26 -0400 Subject: [PATCH 06/17] updates and new dockerfile --- Dockerfile | 9 ++++ slack.sh | 129 +++++++++++++++++++++++++---------------------------- 2 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6c7a852 --- /dev/null +++ b/Dockerfile @@ -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 diff --git a/slack.sh b/slack.sh index 4a8e335..67b73ee 100644 --- a/slack.sh +++ b/slack.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash # ---------- # Connection @@ -29,33 +29,27 @@ 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" +#APP="/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} +#test -L "${APP}"; echo "WARNING: You do not have ${APP} symlinked."; ln ${APP} ${BIN} && chmod +x ${BIN} # Set defaults if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) was set to OK..."; ICON=${ICON:-':good:'} && COLOR=${COLOR:-'#36a64f'}; fi # 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/<.*$//') + 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 @@ -94,42 +88,37 @@ exit 1 # Check if any arguments were passed if [[ $# -eq 0 ]]; then - -GET_HELP -exit 1 - + GET_HELP + exit 1 else - -while getopts "aA:b:B:c:Chi: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}" ;; - 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: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}" ;; + 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 # ---------- @@ -164,22 +153,22 @@ 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/openbridge/ob_slack/master/etc/slack.d/${MODE}" --verbose + 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 + if [[ -z "${MODE}" ]]; then echo "INFO: No Monit variables are present" - else + else - source "${CONFIG}/${MODE}" + source "${CONFIG}/${MODE}" fi else # ---------- - # Tests + # 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 @@ -190,15 +179,18 @@ else 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 "${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'} - fi +# ---------- +# Test Message +# ---------- + +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 + # ---------- # Send Message # ---------- @@ -228,7 +220,7 @@ PAYLOAD="payload={ \ \"title_link\": \"${TITLELINK}\", \ \"text\": \"${TEXT}\", \ \"mrkdwn_in\": [\"text\",\"pretext\",\"fields\"], \ - \"fields\": [{\"title\": \"Status\",\"value\": \"${PRIORITY}\",\"short\": \"true\"}, {\"title\": \"Host\",\"value\": \"${USERNAME}\",\"short\": \"true\"} ], \ + \"fields\": [{\"title\": \"Status\",\"value\": \"${PRIORITY}\",\"short\": \"true\"}, {\"title\": \"Host\",\"value\": \"${IP}\",\"short\": \"true\"} ], \ \"image_url\": \"${IMAGE}\", \ \"thumb_url\": \"${THUMBNAIL}\" \ }]}" @@ -247,13 +239,14 @@ PAYLOAD="payload={ \ 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 Slack API."; fi } SEND -exit 0 +exit 1 From eeeaa618079f6e3a90425edd61441649fd8f274d Mon Sep 17 00:00:00 2001 From: Thomas Spicer Date: Sun, 23 Oct 2016 12:55:05 -0400 Subject: [PATCH 07/17] cleaned up some issues and ENV --- etc/slack.d/monit | 9 ++- slack.sh | 200 +++++++++++++++++++--------------------------- 2 files changed, 88 insertions(+), 121 deletions(-) diff --git a/etc/slack.d/monit b/etc/slack.d/monit index d7f009f..b46b20d 100644 --- a/etc/slack.d/monit +++ b/etc/slack.d/monit @@ -4,10 +4,11 @@ PRETEXT="${MONIT_DATE} | ${MONIT_HOST}: ${MONIT_EVENT}" FALLBACK="${MONIT_DATE} | ${MONIT_HOST}: ${MONIT_EVENT}" - if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) is OK. Setting a default STATUS to OK..." && ICON=${ICON:-':monit-ok:'} && COLOR=${COLOR:-'#36a64f'}; fi - 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 + if test "${PRIORITY}" = "OK"; then echo "INFO: STATUS (-s) is OK. Setting a default STATUS to OK..." && ICON=${ICON:-'monit-ok'} && COLOR=${COLOR:-'#36a64f'}; fi + 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 + # Save for later # if grep -Fq 'Action done' ${MONIT_DESCRIPTION}; then ICON=${ICON-':cpu:'}; fi diff --git a/slack.sh b/slack.sh index 67b73ee..9590b4f 100644 --- a/slack.sh +++ b/slack.sh @@ -3,45 +3,32 @@ # ---------- # Connection # ---------- - # Default WEBHOOK to post messages if [[ -n ${WEBHOOK} ]]; then - echo "INFO: The Slack API WEBHOOK was passed via the command line (-w)" - elif [[ -n ${SLACK_WEBHOOK} ]]; then - echo "INFO: The Slack API TOKEN was set as a system variable" TOKEN=${SLACK_WEBHOOK} - else - echo "INFO: Using default Slack API endpoint to POST messages..." WEBHOOK=${WEBHOOK-'https://hooks.slack.com/services/'} - fi # ---------- # Environment # ---------- - -DATE=$(date +"%Y%m%d") HOSTNAME=${hostname-$(hostname -s)} CONFIG="/etc/slack.d" IPCONFIG="/tmp/ip.txt" -#APP="/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 "${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 "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 @@ -58,31 +45,29 @@ source ${IPCONFIG} # ---------- # Commands # ---------- - function GET_HELP() { - echo "Usage: [options]" - echo " options:" - echo "-a, Attachment Sends a messages as an attachment." - echo "-A, Author Small text used to display the author's name." - echo "-b, Author Link A URL that will hyperlink the author_name text mentioned above. (Author name is required)." - echo "-B, Author Icon A URL that displays a small image to the left of the author_name text.(Author name is required)." - echo "-c, Channel The location the messages should be delivered." - echo "-C, Color This value is used to color the border along the left side of the message attachment." - echo "-h, Help Show the command options for Slack." - echo "-i, Icon A URL to an image file that will be displayed inside a message attachment." - echo "-I, Image Small text used to display the author's name." - echo "-m, Mode Mode toggles application specific behaviors (e.g., preconfigured Monit settings)." - echo "-N, Thumbnail A URL to an image file that will be displayed as a thumbnail on the right side of a message attachment." - echo "-p, Pretext This is optional text that appears above the message attachment block." - echo "-s, Status An optional value that can either be one of ok, info, warn or error." - echo "-t, Text This is the main text in a message attachment, and can contain standard message markup." - echo "-T, Title The title is displayed as larger, bold text near the top of a message attachmen." - echo "-L, Title Link A valid URL in the will ensure the title text will be hyperlinked." - echo "-k, Token Authenticates the POST to Slack." - echo "-u, Username User that posts the message." - echo "-w, Webhook The Slack API service endpoint to POST messages." - + echo "Usage: [options]" + echo " options:" + echo "-a, Attachment Sends a messages as an attachment." + echo "-A, Author Small text used to display the author's name." + echo "-b, Author Link A URL that will hyperlink the author_name text mentioned above. (Author name is required)." + echo "-B, Author Icon A URL that displays a small image to the left of the author_name text.(Author name is required)." + echo "-c, Channel The location the messages should be delivered." + echo "-C, Color This value is used to color the border along the left side of the message attachment." + echo "-h, Help Show the command options for Slack." + echo "-i, Icon A URL to an image file that will be displayed inside a message attachment." + echo "-I, Image Small text used to display the author's name." + echo "-m, Mode Mode toggles application specific behaviors (e.g., preconfigured Monit settings)." + echo "-N, Thumbnail A URL to an image file that will be displayed as a thumbnail on the right side of a message attachment." + echo "-p, Pretext This is optional text that appears above the message attachment block." + echo "-s, Status An optional value that can either be one of ok, info, warn or error." + echo "-t, Text This is the main text in a message attachment, and can contain standard message markup." + echo "-T, Title The title is displayed as larger, bold text near the top of a message attachmen." + echo "-L, Title Link A valid URL in the will ensure the title text will be hyperlinked." + echo "-k, Token Authenticates the POST to Slack." + echo "-u, Username User that posts the message." + echo "-w, Webhook The Slack API service endpoint to POST messages." exit 1 } @@ -91,7 +76,7 @@ if [[ $# -eq 0 ]]; then GET_HELP exit 1 else - while getopts ":a:A:b:B:c:C:h:i:I:m:N:p:s:Z:T:L:k:u:w" opt; do + 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}" ;; @@ -99,6 +84,7 @@ else B) AUTHORLINK="${OPTARG}" ;; c) CHANNEL="${OPTARG}" ;; C) COLOR="${OPTARG}" ;; + e) ENV="${OPTARG}" ;; h) GET_HELP ;; i) ICON="${OPTARG}" ;; I) IMAGE="${OPTARG}" ;; @@ -124,21 +110,16 @@ 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 # ---------- @@ -150,92 +131,79 @@ fi # 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/openbridge/ob_hacky_slack/master/etc/slack.d/${MODE}" --verbose - + 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 + echo "INFO: No Monit variables are present" + else + source "${CONFIG}/${MODE}" + fi else - # ---------- - # 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 - +# ---------- +# 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 # ---------- # 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# - 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\"} ], \ - \"image_url\": \"${IMAGE}\", \ - \"thumb_url\": \"${THUMBNAIL}\" \ -}]}" - + 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\" \ - }" + PAYLOAD="payload={ \ + \"channel\": \"${CHANNEL}\", \ + \"username\": \"${USERNAME}\", \ + \"pretext\": \"${PRETEXT}\", \ + \"color\": \"${COLOR}\", \ + \"icon_emoji\": \":${ICON}:\", \ + \"text\": \"${TEXT}\", \ + \"mrkdwn\": \"true\" \ + }" fi # Send the payload to the Slack API @@ -243,10 +211,8 @@ 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 -if test "${POST}" != ok; then echo "ERROR: The POST to the Slack API failed" && return 1; else echo "OK: Message successfully sent to the Slack API."; fi - +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 1 From 64ce05ef8bababdbefb76c7b78dcda816d7cafff Mon Sep 17 00:00:00 2001 From: Thomas Spicer Date: Sun, 23 Oct 2016 14:46:42 -0400 Subject: [PATCH 08/17] update to reflect Docker addition --- README.md | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a84a3ab..cdfc190 100644 --- a/README.md +++ b/README.md @@ -44,15 +44,6 @@ Hacky Slack requires cURL (https://curl.haxx.se). Most systems have it installed Installation is pretty simple. Just copy the slack.sh to /usr/local/bin. Then chmod +x /usr/local/bin/slack.sh. -Please note that the default config (slack.sh) assumes you are installing slack into /usr/local/bin: - -``` -APP="/usr/local/bin/slack.sh" -BIN="/usr/bin/slack" -``` - -BIN is used to ln to APP to BIN. This will allow you to use slack vs /usr/local/bin/slack.sh - If you decide to copy Hacky Slack to a different APP directory change the settings accordingly. # Using Hacky Slack @@ -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: @@ -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 From d8abf332f959883812333cbff4d3f29d8fd5d592 Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Mon, 24 Oct 2016 07:32:09 +0200 Subject: [PATCH 09/17] Changed so that env. is passed down as user provided --- slack.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/slack.sh b/slack.sh index 2b4fb64..ff4da69 100644 --- a/slack.sh +++ b/slack.sh @@ -24,7 +24,6 @@ CONFIG="/etc/slack.d" IPCONFIG="/tmp/ip.txt" APP="/usr/local/bin/slack.sh" BIN="/usr/bin/slack" -ENV="Production" # 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} @@ -92,11 +91,7 @@ while getopts "aA:b:B:c:C:e:h:i:I:m:N:p:s:t:T:L:k:u:w" opt; do B) AUTHORLINK="${OPTARG}" ;; c) CHANNEL="${OPTARG}" ;; C) COLOR="${OPTARG}" ;; - e) - if test "${OPTARG}" = "Test"; then ENV="Test"; fi - if test "${OPTARG}" = "Stage"; then ENV="Stage"; fi - if test "${OPTARG}" = "Production"; then ENV="Production"; fi - ;; + e) ENV="${OPTARG}" ;; h) GET_HELP ;; i) ICON="${OPTARG}" ;; I) IMAGE="${OPTARG}" ;; From c0918e14467dcea9e82220f83109458c1b4c0f6b Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Mon, 24 Oct 2016 08:14:03 +0200 Subject: [PATCH 10/17] Fixed intent to match the one used in the original project --- slack.sh | 106 +++++++++++++++++++++++++++---------------------------- 1 file changed, 53 insertions(+), 53 deletions(-) mode change 100644 => 100755 slack.sh diff --git a/slack.sh b/slack.sh old mode 100644 new mode 100755 index 6692f34..572621e --- a/slack.sh +++ b/slack.sh @@ -5,13 +5,13 @@ # ---------- # Default WEBHOOK to post messages if [[ -n ${WEBHOOK} ]]; then - echo "INFO: The Slack API WEBHOOK was passed via the command line (-w)" + echo "INFO: The Slack API WEBHOOK was passed via the command line (-w)" elif [[ -n ${SLACK_WEBHOOK} ]]; then - echo "INFO: The Slack API TOKEN was set as a system variable" - TOKEN=${SLACK_WEBHOOK} + echo "INFO: The Slack API TOKEN was set as a system variable" + TOKEN=${SLACK_WEBHOOK} else - echo "INFO: Using default Slack API endpoint to POST messages..." - WEBHOOK=${WEBHOOK-'https://hooks.slack.com/services/'} + echo "INFO: Using default Slack API endpoint to POST messages..." + WEBHOOK=${WEBHOOK-'https://hooks.slack.com/services/'} fi # ---------- @@ -32,12 +32,12 @@ if [[ ! -f "${IPCONFIG}" ]]; then echo "IP=${IP}" > ${IPCONFIG} else 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 "WARNING: ${IPCONFIG} is more than 2 hours old. Updating the IP in ${IPCONFIG}..." - echo "IP=${IP}" > ${IPCONFIG} - fi + 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 "WARNING: ${IPCONFIG} is more than 2 hours old. Updating the IP in ${IPCONFIG}..." + echo "IP=${IP}" > ${IPCONFIG} + fi fi source ${IPCONFIG} @@ -168,48 +168,48 @@ if [[ -z "${ENV+x}" ]]; then echo "INFO: A ENV (-e) was not set. Using the defau # Send Message # ---------- function SEND() { -# 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\" \ - }" - 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 + # 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\" \ + }" + 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 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 } From 9e479c0b5568c9890eb4661b979b7b6d333ac680 Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Mon, 24 Oct 2016 09:51:19 +0200 Subject: [PATCH 11/17] Fixed monit configuration --- etc/slack.d/monit | 3 --- slack.sh | 10 +++++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/etc/slack.d/monit b/etc/slack.d/monit index 44cd461..b46b20d 100644 --- a/etc/slack.d/monit +++ b/etc/slack.d/monit @@ -8,10 +8,7 @@ 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 diff --git a/slack.sh b/slack.sh index 572621e..2a3afe9 100755 --- a/slack.sh +++ b/slack.sh @@ -146,10 +146,10 @@ else # ---------- # 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 "${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 @@ -210,7 +210,7 @@ function SEND() { 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 - 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 + if test "${POST}" != ok; then echo "ERROR: The POST to the Slack API failed (${POST})" && return 1; else echo "OK: Message successfully sent to the channel ${CHANNEL} via the Slack API"; fi } SEND From 46b2e3f4576ead2164c41a66fd5ec24aab13be4c Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Mon, 24 Oct 2016 10:07:17 +0200 Subject: [PATCH 12/17] Fixed help text --- slack.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slack.sh b/slack.sh index 2a3afe9..622cdff 100755 --- a/slack.sh +++ b/slack.sh @@ -62,7 +62,7 @@ function GET_HELP() { echo "-N, Thumbnail A URL to an image file that will be displayed as a thumbnail on the right side of a message attachment." echo "-p, Pretext This is optional text that appears above the message attachment block." echo "-s, Status An optional value that can either be one of ok, info, warn or error." - echo "-t, Text This is the main text in a message attachment, and can contain standard message markup." + echo "-Z, Text This is the main text in a message attachment, and can contain standard message markup." echo "-T, Title The title is displayed as larger, bold text near the top of a message attachmen." echo "-L, Title Link A valid URL in the will ensure the title text will be hyperlinked." echo "-k, Token Authenticates the POST to Slack." @@ -161,7 +161,7 @@ if [[ -z "${TEXT+x}" ]]; then echo "WARNING: You do not have any TEXT (-t) speci 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 "${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 # ---------- From bd10ca999876254d49b2aedf6f183adcedda1a29 Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Mon, 24 Oct 2016 10:38:26 +0200 Subject: [PATCH 13/17] Changed address for mode configurations --- slack.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slack.sh b/slack.sh index 622cdff..cb3e994 100755 --- a/slack.sh +++ b/slack.sh @@ -133,7 +133,7 @@ fi 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/openbridge/ob_hacky_slack/master/etc/slack.d/${MODE}" --verbose + 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 From 54e07b1b19d4e1fa4c62e82bcc5c7f7798865cf3 Mon Sep 17 00:00:00 2001 From: Erik Hennerfors Date: Wed, 26 Oct 2016 16:11:41 +0200 Subject: [PATCH 14/17] Added support for CHANNEL as system env. --- slack.sh | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/slack.sh b/slack.sh index 2b4fb64..7fc3bd1 100644 --- a/slack.sh +++ b/slack.sh @@ -3,13 +3,12 @@ # ---------- # Connection # ---------- - # Default WEBHOOK to post messages if [[ -n ${WEBHOOK} ]]; then echo "INFO: The Slack API WEBHOOK was passed via the command line (-w)" elif [[ -n ${SLACK_WEBHOOK} ]]; then echo "INFO: The Slack API TOKEN was set as a system variable" - TOKEN=${SLACK_WEBHOOK} + WEBHOOK=${SLACK_WEBHOOK} else echo "INFO: Using default Slack API endpoint to POST messages..." WEBHOOK=${WEBHOOK-'https://hooks.slack.com/services/'} @@ -133,6 +132,20 @@ else exit 1 fi +# ---------- +# Check for CHANNEL +# ---------- +# Default CHANNEL to post messages +if [[ -n ${CHANNEL} ]]; then + echo "INFO: The Slack API TOKEN was passed via the command line (-k)" +elif [[ -n ${SLACK_CHANNEL} ]]; then + echo "INFO: The Slack API TOKEN was set as a system variable" + CHANNEL=${SLACK_CHANNEL} +else + 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 # ---------- From 7de5197059ef1754d0aece28a33b914baa50b4b2 Mon Sep 17 00:00:00 2001 From: Thomas Spicer Date: Wed, 26 Oct 2016 12:22:23 -0400 Subject: [PATCH 15/17] Already CHANNEL check. Sets to #general if null --- slack.sh | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/slack.sh b/slack.sh index aec71be..d859a1a 100755 --- a/slack.sh +++ b/slack.sh @@ -7,8 +7,8 @@ if [[ -n ${WEBHOOK} ]]; then echo "INFO: The Slack API WEBHOOK was passed via the command line (-w)" elif [[ -n ${SLACK_WEBHOOK} ]]; then - echo "INFO: The Slack API TOKEN was set as a system variable" - WEBHOOK=${SLACK_WEBHOOK} + echo "INFO: The Slack API TOKEN was set as a system variable" + WEBHOOK=${SLACK_WEBHOOK} else echo "INFO: Using default Slack API endpoint to POST messages..." WEBHOOK=${WEBHOOK-'https://hooks.slack.com/services/'} @@ -122,21 +122,6 @@ else exit 1 fi - -# ---------- -# Check for CHANNEL -# ---------- -# Default CHANNEL to post messages -if [[ -n ${CHANNEL} ]]; then - echo "INFO: The Slack API TOKEN was passed via the command line (-k)" -elif [[ -n ${SLACK_CHANNEL} ]]; then - echo "INFO: The Slack API TOKEN was set as a system variable" - CHANNEL=${SLACK_CHANNEL} -else - 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 # ---------- @@ -228,4 +213,4 @@ function SEND() { } SEND -exit 1 \ No newline at end of file +exit 1 From 584076b2149b7c0182bdad3397c8ed9129d66a01 Mon Sep 17 00:00:00 2001 From: Thomas Spicer Date: Fri, 28 Oct 2016 09:25:00 -0400 Subject: [PATCH 16/17] comment cleanup --- slack.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slack.sh b/slack.sh index d859a1a..ee32a8c 100755 --- a/slack.sh +++ b/slack.sh @@ -1,7 +1,7 @@ -#!/bin/bash +#!/usr/bin/env bash # ---------- -# Connection +# API Endpoint # ---------- # Default WEBHOOK to post messages if [[ -n ${WEBHOOK} ]]; then @@ -108,7 +108,7 @@ else fi # ---------- -# Check for TOKEN +# Check for Token # ---------- echo "${SLACK_TOKEN}" # Default TOKEN to post messages From 51971ba771219562c53fc1330c4c531b8dd1067e Mon Sep 17 00:00:00 2001 From: Thomas Spicer Date: Tue, 17 Jan 2017 10:49:16 -0500 Subject: [PATCH 17/17] update readme and slack.sh --- README.md | 15 ++++++++------- slack.sh | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cdfc190..f290fb8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Hacky Slack +# Hacky Slack: Slack API Shell Script Hacky Slack is a shell script that will post messages to a Slack webhook API endpoint. @@ -11,7 +11,7 @@ From Slack: Why is it called Hacky Slack? First, this reflects my "hacking" something together that accomplished my goals. Second, I played a ton of Hacky Sack (https://en.wikipedia.org/wiki/Hacky_Sack) when I was a teenager. # Overview -There are two goal of Hacky Slack. The first was to was to create a generic shell client for the Slack messaging API. The second was to take advantage of the Slack messaging interface to allow applications, like Monit, to style its events. In support of both goals Hacky Slack offers customizations for external applications, like Monit, via external config files (See the Monit example). +There are two goal of Hacky Slack. The first was to was to create a generic Slack shell script for the messaging API. The second was to take advantage of the Slack messaging interface to allow applications, like Monit, to style its events. In support of both goals Hacky Slack offers customizations for external applications, like Monit, via external config files (See the Monit example). Also, in support of having more compelling Slack messages, a small collection of icons were created. The icons are meant to provide visual cues to the user so they can more easily identify the context of a message they received in Slack. @@ -32,7 +32,8 @@ As a result going through those two steps, you should get the following: Hacky Slack will default to the Slack API endpoint URL https://hooks.slack.com/services/. However, if you want to use a different one simply pass it via -w "https://whatever.slack.com/provides/" -## Hacky Slack should run in most modern Linux environments. It has been tested in a CentOS 7 Docker container and Mac OS X. However, you will need to make sure a few things are setup in your environment: +## Environment +Hacky Slack should run in most modern Linux environments. It has been tested in a CentOS 7 Docker container and Mac OS X. However, you will need to make sure a few things are setup in your environment: #### cURL @@ -40,7 +41,7 @@ Hacky Slack requires cURL (https://curl.haxx.se). Most systems have it installed # Installation -## slack.sh +## Slack API Shell Script: slack.sh Installation is pretty simple. Just copy the slack.sh to /usr/local/bin. Then chmod +x /usr/local/bin/slack.sh. @@ -52,7 +53,7 @@ Hacky Slack allowed you to pass a variety attributes as defined by the Slack mes ``` -a, Attachment Sends a messages as an attachment." --A, Author Small text used to display the author's name." +-A, Author Display the author's name." -b, Author Link A URL that will hyperlink the author_name text mentioned above. (Author name is required)." -B, Author Icon A URL that displays a small image to the left of the author_name text.(Author name is required)." -c, Channel The location the messages should be delivered. Use # or @ to prefix (#general or @joe)" @@ -65,7 +66,7 @@ Hacky Slack allowed you to pass a variety attributes as defined by the Slack mes -p, Pretext This is optional text that appears above the message attachment block." -s, Status An optional value that can either be one of ok, info, warn or error." -t, Text This is the main text in a message attachment, and can contain standard message markup." --T, Title The title is displayed as larger, bold text near the top of a message attachmen." +-T, Title The title is displayed as larger, bold text near the top of a message attachment." -L, Title Link A valid URL in the will ensure the title text will be hyperlinked." -k, Token Authenticates the POST to Slack." -u, Username User that posts the message." @@ -76,7 +77,7 @@ For more information on the above parameters, please check out the Slack docs: * https://api.slack.com/docs/attachments -# Send A Message +# Send A Message Via Slack API Shell Script 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?" ``` diff --git a/slack.sh b/slack.sh index ee32a8c..fcc04b6 100755 --- a/slack.sh +++ b/slack.sh @@ -213,4 +213,4 @@ function SEND() { } SEND -exit 1 +exit 0