Send Alerts to Slack with Bash Shell Script

Slack has quickly become one of the standard messaging apps in the Enterprise. Many a companies use slack for communication amongst team members and IT departments like having slack channels for projects or critical system groups. Since most of the people who are supposed to take action on a event are in a slack channel it is sometimes better to have notifications send directly to slack for server events. One of the requirement I had recently was to create server alerts for checking API gateways and if the webservice was found to be down, to send alert to a slack channel. To create such event based notification to slack, first you need to create an app in slack and then a webhook to expose it to the internet. Once that is done the remaining magic is done in plain old shell scripting1. Create an APP in Slack a) Goto https://api.slack.com/slack-apps b) Create an App c) Sign-in with your workspace2. Create the Slack App and a webhook a) Go to incoming webhooks and Activate incoming webhooks You can see the sample of your webhook cURLthen b) Create a channel in your Slack eg : testalerts and assign to step c below c) Add new Webhook to workspacee Post To : #testalerts d) Webhook URLS for Your Workspace

The above shell script checks the header for 200 OK message , if found it sends alert to the channel via the slack webhook. It can be added to crontab to check the status of the webservice every 5 minutes. If the Webservice is down it will send notification to the Slack channel.