Skip to content

Instantly share code, notes, and snippets.

@hkennyv
Last active August 5, 2019 17:52
Show Gist options
  • Save hkennyv/4dd25369c69ebef4ab5a27da55d46b9f to your computer and use it in GitHub Desktop.
Save hkennyv/4dd25369c69ebef4ab5a27da55d46b9f to your computer and use it in GitHub Desktop.
slack-logger

slack-logger

A script used to tail a logfile and post newlines to a slack webhook. Original script taken from this Postman Blog. In practice, I expect that it will be modified slightly (it'd be nice to know the name of the file it's tailing).

Usage

Download or copy the slack-logger.sh file.

Make the shell script executable using:

chmod +x slack-logger.sh

Run the shell script, passing in the name of the file you wish to tail and the Slack webhook URL.

./slack-logger.sh nginx.log https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXX

Bonus

Additionally, you can make it so that the script only posts lines with a specific keyword in it by passing a 3rd argument:

./slack-logger.sh nginx.log https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXX err
#!/bin/bash
tail -n0 -F "$1" | while read LINE; do
(echo "$LINE" | grep -e "$3") && curl -X POST --silent --data-urlencode \
"payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment