Skip to content

Instantly share code, notes, and snippets.

@lidox
Created April 9, 2018 10:55
Show Gist options
  • Save lidox/d0f92c2288345f7a18bcabe87fb43e49 to your computer and use it in GitHub Desktop.
Save lidox/d0f92c2288345f7a18bcabe87fb43e49 to your computer and use it in GitHub Desktop.

Logging Documentation

This document describes how to provide logging information from Cassandra, Millimetric Application and Apache Web Server to our Slack channels.

Apache Web Server

This chapter explains how to stream any log file to Slack.

Prerequisites

Curl

Make sure curl is installed.

sudo apt install curl 

Slack Webhooks

  1. Create a new channel to be used at Slack's overview.
  2. Now create a new Webhook at Slack's configuration.
  3. Copy Webhook-URL. For example:
https://hooks.slack.com/services/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

Stream log file to Slack using curl

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 ";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment