Send Alerts from the Command Line to Discord
This is a quick and functional way to get scripts to alert a group or user over a mobile device; Discord is free and the app is available on many platforms. For the purposes of getting alerts about services and monitoring conditions, Discord is a great medium.
In this post, a Linux server is setup to securely make alerts to Discord, which can be read using the Discord app, on your mobile device or desktop computer.
0 . Latest updates and prerequisites.
2023.09.15 – First draft, Ubuntu 22.04 Server.
2024.11.21 – Updated.
Check back for updates. Leave a comment if the post is missing some detail.
Based on where the alert script will run, sudo access may or may not be required. A basic Discord account is free and required, so is basic Internet access.
1 . Create a Discord account and server.
Go to discord.com and create a free account. Make sure to setup 2-factor-authentication while there; Discord is a popular gaming platform and intrusion attempts happen often, here.
Once there, “create a server”.
Once the server is created, there should be a default text channel called “#general”, a good idea is to create a new text channel just for this project. In this case, a channel called “#automatons” is created.
The channel must be set to private.
In the channel settings, under integrations, there is a Webhook section. Create a new Webhook and select “Copy Webhook URL”. This is an API key of sorts that has authority in this channel. Do not share it. Save it someplace for the next steps.
The Webhook will look something like this:
https://discord.com/api/webhooks/11525228959595252368/PaKsnnjvb-qpN7N1rhkuVNsdfgsdg
Other cosmetic changes do not matter, the only important part for this post is the existence of the channel and the related webhook.
2 . Check command line functionality.
There are many guides on posting to Discord from the command-line, however, at its most basic, all that is required is a curl POST statement which defines a message in JSON format, to the webhook URL. A message can be sent with a line, like this, from the shell:
curl -H "Content-Type:application/json" -d '{"username": "server30", "content": "Posted Via Command line"}' https://discord.com/api/webhooks/1152187184116408380/ZOSUZPKjnbx-n5CjzOyVExsuV3M
The message block has to be formatted well and as can be seen, the username is free text or it defaults to the username setting in the Discord integrations page. After some experimenting, its possible to post a message from the command-line:
Now that it works, in the next steps some basic monitoring and alerting will be setup.
3 . Create a shell script.
The webhook is the key to post to the channel and should be hidden.
Create a file called “discord.conf” with an editor in /etc or ~/ :
admin@server40:~$ sudo nano /etc/discord.conf
The file contains only the webhook and in the future, maybe some other restricted variables:
webhook="https://discord.com/api/webhooks/1152522872586252368/PaKsnnjvb-N7nXvcjy"
If a user or service wants to send a message to that Discord channel, they or one of their groups needs at least read access to this file. Lock the file down so that only the allowed user can read it:
sudo chmod 440 /etc/discord.conf
Now, if a regular user tries to view the file, access should be denied. This is to prevent anybody posting to the channel and while this security precaution is not required, it is highly recommended:
user@server40:~$ cat /etc/discord.conf
cat: /etc/discord.conf: Permission denied
Now a simple BASH script can be created that accepts only two parameters; the username and the message:
sudo mkdir -p /opt/discord
sudo nano /opt/discord/discord.sh
Notice the script calls the conf file for the webhook variable. In BASH, the first parameter is “$1” and the second, “$2”, etc. The backslashes are preserving the formatting of the JSON file, while enclosed in double quotes:
#!/bin/bash
. /etc/discord.conf
curl -H "Content-Type:application/json" -d "{\"username\": \"$1\", \"content\": \"$2\"}" $webhook
Set the script to executable:
sudo chmod +x /opt/discord/discord.sh
Run this script with something simple, but make sure to do so as a user with permissions to the conf file and the script file:
admin@server40:~$ sudo /opt/discord/discord.sh "server03" "HDD03 has error!"
And the output on Discord should be similar to this:
With the Discord app on mobiles, a user or group can receive messages from services.
In the next steps, a simple alert for when the server is restarted and a scheduled check for disk space, is set up.
4 . Automate an alert.
Crontab is included in almost every Linux distribution in some form or another. This is a scheduler that runs script regularly or at some event, such as at startup, with the time set to “@reboot”, for example. To send an alert at startup, with a short delay to wait for network services to come up is possible. Start the crontab editor:
sudo crontab -e
Add a line this like this, which introduces a 20 second pause before sending an alert:
@reboot sleep 20 && /opt/discord/discord.sh "$(hostname)" "Starting up..."
Once after a reboot, plus about 20 seconds, Discord will get the message:
Another useful feature of this kind of automation is to report on progress of tasks, availability of resources, etc. Here is a simple way to report disk space, everyday at 0900 from crontab:
0 9 * * * /opt/discord/discord.sh "$(hostname)" "Disk space on / is $(df -kh / | tail -n1 | awk '{print $4}')"
And every day at 0900, Discord will have a message:
And since the discord.sh script runs with just those two parameters, a more complicated solution is possible, scheduled or not, the discord.sh can be called from anywhere, too, such as Zabbix, PRTG or other third party applications that allow a script to be run as part of any other action or activity.
5 . Supporting this blog.
This type of content takes a lot of effort to write. Each post is drafted, researched, then tested multiple times, even a simple step or detail might take more than a few hours to go from the idea to a published blog post. This post was not generated with AI.
Did you notice there are no adverts on this site?
If you feel I have saved you some time, you can support me by;
- hosting with DigitalOcean, like I do – click here to go to DigitalOcean.
- buying me a beer through PayPal – click here to go to PayPal.
©horsefreeglue.com, 2024. Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given.