From 2a0ea3433718141ea5fed9654a10cbb7e7704847 Mon Sep 17 00:00:00 2001 From: Adam Kingsley Date: Sat, 17 Mar 2018 22:05:35 +0100 Subject: [PATCH] Add a method to send a slack message and call it if a webhook url is specified in the /etc/plexupdate.cron.conf file. --- extras/cronwrapper | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/extras/cronwrapper b/extras/cronwrapper index b6b10fa..b976872 100755 --- a/extras/cronwrapper +++ b/extras/cronwrapper @@ -15,6 +15,27 @@ # (typically /var/log/daemon.log or /var/log/syslog) # +sendSlackMessage() { + escapedText=$(cat ${1} | sed 's/"/\"/g' | sed "s/'/\'/g" ) + slackUsername=${SLACK_USERNAME:-plexupdate} + + if [ $RET -eq 10 ] || [ $RET -eq 0 ]; then + color="#36a64f" + else + color="#ad2d2d" + fi + ts=`date +%s` + attachment="{\"color\": \"$color\",\"footer\": \"Plex Update\",\"title\": \"Log File\", \"text\": \"$escapedText\", \"ts\": $ts}" + + if [ ! -z "$SLACK_CHANNEL" ]; then + json="{\"channel\": \"$SLACK_CHANNEL\", \"username\": \"$slackUsername\", \"attachments\": [$attachment]}" + else + json="{\"username\": \"$slackUsername\", \"attachments\": [$attachment]}" + fi + + curl -s -X POST -H 'Content-type: application/json' -d "$json" "$SLACK_WEBHOOK_URL" +} + if [ ! -f /etc/plexupdate.cron.conf ]; then echo "ERROR: You have not configured /etc/plexupdate.cron.conf" >&2 exit 255 @@ -59,6 +80,11 @@ if [ $RET -ne 0 ] ; then # Make sure user gets an email about this (when not success or if user has specified when) cat ${LOGFILE} >&2 + # If there's a slack webhook specified then send a slack message with the log output + if [ ! -z "$SLACK_WEBHOOK_URL" ]; then + sendSlackMessage ${LOGFILE} + fi + # Output will produce a cron email, so we can reset the exit status if [ $RET -eq 10 ]; then RET=0