Setting up Discord Notifications for OpenMediaVault đź””

23/01/2024 homelab 3 mins read
Table Of Contents

OpenMediaVault (OMV) is a powerful network-attached storage solution that typically relies on email for system notifications. However, many users prefer instant messaging platforms like Discord for monitoring their systems. This guide will walk you through setting up Discord notifications for OpenMediaVault, providing real-time system alerts directly in your Discord server.

Prerequisites

  • A running OpenMediaVault installation
  • Administrative access to your OMV system
  • A Discord server where you have permission to create webhooks
  • Basic familiarity with the Linux command line
  • SSH access to your OpenMediaVault system

Discord Webhook Setup

  1. Open Discord and navigate to your server
  2. Right-click on the desired notification channel
  3. Select “Edit Channel” → “Integrations” → “Create Webhook”
  4. Name your webhook (e.g., “OMV Notifications”)
  5. Copy the webhook URL for later use
  6. Click “Save Changes”

Installation

Create Notification Script

Create a new file for the Discord notification script in OpenMediaVault:

Terminal window
nano /usr/share/openmediavault/notification/sink.d/20discord

Add Script Content

Copy this script into the file, replacing the webhook URL placeholder:

#!/bin/bash
#CREATED BY XMON FOR OpenMediaVault
DISCORD_WEBHOOK='PUT YOUR WEBHOOK LINK HERE'
generate_post_data() {
cat <<EOF
{
"content": "",
"embeds": [{
"title": "${OMV_NOTIFICATION_SUBJECT}",
"description": "$(cat ${OMV_NOTIFICATION_MESSAGE_FILE})",
"color": "45973",
"footer": {
"text": "${OMV_NOTIFICATION_DATE}",
"icon_url": ""
}
}]
}
EOF
}
curl -H "Content-Type: application/json" -X POST -d "$(generate_post_data)" $DISCORD_WEBHOOK

Make Script Executable

After saving the file, make it executable with:

chmod +x /usr/share/openmediavault/notification/sink.d/20discord

Configuration

  1. Log into your OpenMediaVault web interface
  2. Navigate to System → Notification
  3. Keep email notifications enabled but disable authentication
  4. Click “Save”
  5. Test the setup using the “Test” button

Troubleshooting

If you encounter issues, here’s a debugging version of the script that includes logging:

#!/bin/bash
DISCORD_WEBHOOK='YOUR_WEBHOOK_HERE'
LOG_FILE="/var/log/discord_notification.log"
# Log function
log_message() {
echo "$(date): $1" >> $LOG_FILE
}
log_message "Script triggered"
log_message "Subject: ${OMV_NOTIFICATION_SUBJECT}"
log_message "Message file: ${OMV_NOTIFICATION_MESSAGE_FILE}"
# Check variables
if [ -z "$OMV_NOTIFICATION_SUBJECT" ]; then
log_message "ERROR: Subject is empty"
fi
if [ ! -f "$OMV_NOTIFICATION_MESSAGE_FILE" ]; then
log_message "ERROR: Message file does not exist"
fi
generate_post_data() {
cat <<EOF
{
"content": "",
"embeds": [{
"title": "${OMV_NOTIFICATION_SUBJECT}",
"description": "$(cat ${OMV_NOTIFICATION_MESSAGE_FILE})",
"color": "45973",
"footer": {
"text": "${OMV_NOTIFICATION_DATE}",
"icon_url": ""
}
}]
}
EOF
}
# Log the curl attempt
log_message "Attempting to send to Discord"
RESPONSE=$(curl -H "Content-Type: application/json" -X POST -d "$(generate_post_data)" $DISCORD_WEBHOOK 2>&1)
log_message "Curl response: $RESPONSE"

Common Issues

  1. File Permissions: Check permissions with:

    ls -l /usr/share/openmediavault/notification/sink.d/20discord

    Should show: -rwxr-xr-x 1 root root

  2. System Logs: Review notification errors:

    grep -i notification /var/log/syslog
  3. Test Webhook: Verify webhook functionality:

    curl -H "Content-Type: application/json" -X POST -d '{"content":"Test message"}' YOUR_WEBHOOK_URL

Security Notes

  • Keep your webhook URL private and secure
  • Monitor notification logs regularly
  • Consider implementing rate limiting for high-volume notifications
  • Update webhook URL if Discord server configurations change

Customization

The script can be customized by modifying:

  • Embed color (change the “color” value)
  • Footer icon (add an “icon_url”)
  • Message format
  • Additional embed fields

Remember to check the debug log at /var/log/discord_notification.log if you make any modifications to ensure everything works as expected.