how to send update notifications from a linux server to a slack channel
INSPIRATION
When I first set up my Ghost blog, I configured the host server to use the unattended-upgrades
package to automatically install any security updates daily.
The unattended-upgrades
package was configured to send email notifications to root@localhost
whenever the update process resulted in a change. The Mutt email client was also installed on the server to allow me to read the emails. However, this meant that I needed to log in to the server in order to read the notifications. I needed another option.
IDEATION
What if I could configure the server to also send a short summary of the update report to a Slack channel? That way I would be instantly notified if any updates were applied and if the updates were successful, without me having to directly log in to the server each time.
As it turns out, the process of sending a short notification message to a Slack channel is fairly simple, if we use incoming webhooks. And, the process is outlined below.
IMPLEMENTATION
1. Create Slack Workspace and Channel
2. Create Incoming Webhook
- See Getting started with Incoming Webhooks, Steps #1 through #3.
3. Configure Linux Server to Send Notifications to Slack
-
Create a new non-privileged Linux user with the
adduser
command and follow the prompts.adduser notifyuser
-
Edit the postfix configuration file and set the
default_privs
parameter to your new user. This avoids thePermission Denied
error when executing the bash script, since the command executes with the privileges ofdefault_privs
and by default the local delivery agent uses thenobody
user for delivery to an external file or command.# open postfix configuration file sudo nano /etc/postfix/main.cf # set default_privs parameter default_privs = notifyuser # reload the configuration file sudo systemctl restart postfix
-
Edit the
aliases
file to add a mail alias for the new user, and also add an external command to pipe any messages for the new alias to a script.# open the aliases file sudo nano /etc/aliases # add the below alias and external command notifyuser: "|/home/notifyuser/bin/notifyslack.sh" # rebuild the database for the mail aliases file sudo newaliases
-
Update the
unattended-upgrades
package configuration file to also send updates to the new user (in addition toroot
).# open the configuration file sudo nano /etc/apt/apt.conf.d/50unattended-upgrades # update the below line to include the new user Unattended-Upgrade::Mail "root@localhost,notifyuser@localhost";
-
Create a bash script to handle posting messages to Slack using the incoming webhook created in Step #2 above. Ensure that the owner and group for both the user's private bin directory and the bash script file are also set to the new user.
# create bash script file in user's private bin sudo mkdir /home/notifyuser/bin sudo nano /home/notifyuser/bin/notifyslack.sh # update owner and group for private bin and bash file sudo chown notifyuser:notifyuser /home/notifyuser/bin sudo chown notifyuser:notifyuser /home/notifyuser/bin/notifyslack.sh
Add the below script to the bash file. These commands will format and send the notification. Replace
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
with your own incoming webhook URL from Slack.#!/bin/bash # Slack Message MESSAGE=$(sed -n -e '7'p -e '16,19'p $1) # Slack Webhook WEBHOOK_URL="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" # POST request to Slack webhook with subject from unattended-upgrade notification curl -X POST -H 'Content-type: application/json' --data '{"text":"'"${MESSAGE}"'"}' $WEBHOOK_URL
Update the file permissions to allow execution of the bash script.
sudo chmod +x /home/notifyuser/bin/notifyslack.sh
-
Test the configuration to ensure it works.
# perform a test run with the verbose and extra debug output options sudo unattended-upgrade -v -d
If everything is setup correctly then you should receive Slack messages after each change.
Sources:
M. Vogt. Ubuntu Manpage Repository: unattended-upgrade. (2019). Accessed: Feb. 19, 2023. [Online]. Available: https://manpages.ubuntu.com/manpages/jammy/man8/unattended-upgrade.8.html
"Postfix Configuration Parameters," postfix.org. https://www.postfix.org/postconf.5.html (accessed Feb. 19, 2023).
"Postfix: permission denied error while the incoming mail reading from python script," stackoverflow.com. https://stackoverflow.com/questions/61503015/postfix-permission-denied-error-while-the-incoming-mail-reading-from-python-scr (accessed Feb. 19, 2023).
X. Decuyper, Sending Fail2ban (and other) notifications to a Slack chat channel. savjee.be. https://savjee.be/blog/Sending-fail2ban-notifications-and-others-to-slack-chat-channel/ (accessed Feb. 19, 2023).