How to Send a Message to Slack From Bash Script

As most of the organizations using Slack for communication between teams. So it’s great tool to monitor infra alerts with slack as well. In this tutorial I am going to setup mysql alerts to slack . Slack has an API that allows you to automate a lot of interactions with your workspace. You can access this API from anywhere that allows you to make HTTP requests like POST and GET, which includes bash scripts using the curl utility, as well as most scripting languages. The curl utility is installed by default on nearly all Unix distros

Navigate to Slack’s API portal  and create a new Slack app. Give this app a username, select the workspace it will belong to, and hit “Create App.” Please create account on slack first.

After that click on Incoming webhooks.

By default Incoming Webhooks are disabled. We need to enable it.

After that click on Add New Webhook To Workspace

Now select your slack channel where you want to send alerts.

I have created channel for alerts. So I will send alerts to this channel.

Now copy webhook url and paste it to your script with custom message that you want to send.

curl -X POST -H ‘Content-type: application/json’ –data ‘{“text”:” Mysql was down. Now, started again “}’ yourwebhookurl

We are using curl to hit the api. Please change Mysql was down. Now, started again to your custom message and replace yourwebhookurl to your webhook url.

I have setted up script to monitor mysql service at every minute. So it will send alert to my slack channel whenever it will find mysql service down.

C:\Users\Devops-Admin\Documents\New folder\Untitled.png

I have setted up below script in cron to monitor mysql service.

#!/bin/bash

##########

# Config #

##########

mysql_daemon=’mysqld’

pgrep=’/usr/bin/pgrep’

mysql_start=’sudo service mysql start’

fail_msg=”MySQL is down in $(hostname).”

##########

# Script #

##########

#look up process

$pgrep $mysql_daemon > /dev/null

if [ $? -ne 0 ]; then

echo $fail_msg

curl -X POST -H ‘Content-type: application/json’ –data ‘{“text”:”Mysql was down. Now, started again”}’ https://hooks.slack.com/services/000000000000000000000000000000000000000

$mysql_start

fi

Now I am going to stop mysql service.

Now cronjob is checking mysql service at every minute. So it will check and start mysql service.

Once Mysql service is started. I will get notification in slack.

All Done !

 

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

View all posts by AmritMatti →

Leave a Reply

Your email address will not be published. Required fields are marked *