DEV Community

Cover image for Send alerts to multiple platforms from your scripts
Abdul Khaliq
Abdul Khaliq

Posted on • Updated on

Send alerts to multiple platforms from your scripts

In this article, we will illustrate how to configure and send messages to multiple platforms from your scripts.

A notification can be setup based on success or failure of a task or if you want to have an alert when a certain condition is meet.

This article uses a small binary PingMe to send this notification, you can find more information about it on github.

Lets dig right into it

In this first example we would like to get a notification when any one logs into our server via ssh.
Multiple options are available for messaging platform in this example we will use Pushover service.

  • Download & install

First step is to download install the binary itself on the system which we will use for sending notifications.
We can download the binary from github and extract in the current folder.

Its executable but for good measure we will make it executable and then move the binary in system path, in this case its /usr/local/bin.

App is also available as deb and rpm, for windows users a compatible binary is can be found under release page.

wget -q && tar -xf pingme_Linux_x86_64.tar.gz
chmod +x pingme
sudo mv pingme /usr/local/bin/pingme
Enter fullscreen mode Exit fullscreen mode

Lets create our script and configure notifications

  • Send Alert when any user login via ssh

/etc/profile.d/ is read at every login (for bash shell users). The if statement will only return true if the user has logged in via ssh, which in turn will cause the indented code block to be run.

Create a file with desired name i.e /etc/profile.d/ and paste the following.

pushover_token="<your token here>"
pushover_user="<your user here>"
alert_title="Alert SSH Login: $(date)"
if [ -n "$SSH_CLIENT" ]; then
    PUSHOVER_MESSAGE="ssh login to ${USER}@$(hostname -f) from $(echo $SSH_CLIENT|awk '{print $1}')"
    pingme pushover --msg "$PUSHOVER_MESSAGE" --token $pushover_token --user $pushover_user --title "$alert_title"
Enter fullscreen mode Exit fullscreen mode

Pushover Alert


Send alert on failure or success of a cronjob

We have a cronjob which runs and take backup of our files, we would like to know if this backup has been created successfully
or failed. In this example we are going to send the alert to telegram channel.

  • Cron Job
* * * * * /home/user/scripts/
Enter fullscreen mode Exit fullscreen mode
  • Backup Script
# telegram bot token
token="<token here>"
# Telegram channel
channel="<channel here>"
DATE=$(date +%d-%m-%Y-%T)


# check the exit code and send alert based on that.
if [ $? -eq 0 ]
    title="Success: Backup created successfully"
    /usr/local/bin/pingme telegram --msg "Successfully backed up $BACKUP_FILE_NAME" -t $token -c $channel --title "$title"
    title="Failed: Error creating backup"
    /usr/local/bin/pingme telegram --msg "Unsuccessful backup of $BACKUP_FILE_NAME" -t $token -c $channel --title "$title"

Enter fullscreen mode Exit fullscreen mode

Telegram Alert
alt text

Top comments (0)