This document describes how to provide logging information from Cassandra, Millimetric Application and Apache Web Server to our Slack channels.
This chapter explains how to stream any log file to Slack.
Make sure curl
is installed.
sudo apt install curl
- Create a new channel to be used at Slack's overview.
- Now create a new Webhook at Slack's configuration.
- Copy Webhook-URL. For example:
https://hooks.slack.com/services/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
The following piece of code tails a file and for every new line added to the file, streams it to a slack incoming webhook using curl.
#!/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
To use this script, simply pass the path to the log file and a webhook url to this script.
./tail-slack.sh "file.log" "https://hooks.slack.com/services/...";
For example run like this:
sh tail-slack.sh "/var/log/apache2/access.log" "https://hooks.slack.com/services/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";
If you want this script to keep running even after you have logged out of your SSH terminal, use nohup
command.
You can even make this script send the line to slack only when a particular keyword is found by sending the keyword as a third parameter.
./tail-slack.sh "/var/log/ngix/access.log" "https://hooks.slack.com/services/..." " ERROR ";