Send Custom (email) Notifications from scripts running on a Synology

I use a custom script on my Synology DS209+ (DSM 4.2) DS713+ (DSM 5.1) to backup the databases and was recently asked how to notify the administrator about the success or the failure of that script. The easiest solution is to use the native Notification mechanism of the DSM. It notifies not only by email but also with popup messages, SMS, Skype, etc....

If you are using DSM 6.x, look at the new version of this post, located here.

Click to Read More

First, create the texts for the Notifications to be sent:

  1. Open a dos command prompt
  2. Execute: telnet <YourSynoHostname>
  3. Login as 'root'
  4. Use the password of your Administrator
  5. Execute: cd /usr/syno/synoman/webman/texts/
  6. Execute: ls
  7. Now you see a list of folders for each language supported by your Synology. Assuming that your DSM is configured to run in English (the default), we will use here after the folder 'enu' (but read the notice at the end of this post if you want to configure Notifications for another language)
  8. Execute: cp enu/mails /volume1/web/
    1. I presume here that the web station is enabled, otherwise, copy the file 'mails' in any other shared folder.
    2. This file contains the Notification messages used by the Synology.
  9. Open now this file 'mails' with notepad++: \\<yourSynoHostname>\web\mails
  10. Add two new "tags" at the end of the file. E.g.: "MySqlBkpError" and "MySqlBkpOK" (see bellow for details to possibly be used for those tags)
  11. Save the changes.
  12. Back to the telnet console, execute: cp /volume1/web/mails enu/mails
[MySqlBkpError] Category: Backup/Restore,Important
Title: Database backup failed
Subject: Database backup on %HOSTNAME% has failed

Dear User,

Database backup on %HOSTNAME% has failed.

[MySqlBkpOK] Category: Backup/Restore
Title: Database backup succeeded
Subject: Database backup on %HOSTNAME% has succeeded

Dear User,

Database backup on %HOSTNAME% has successfully completed.

Next, complete your script to notify the Administrator about the Success or Failure of its last action:

  1. Edit your script with notepad++ once it has been copied in a shared folder of your Synology as made here above with the file 'mails'.
  2. After the action to be reported, add the code here bellow.
  3. Save your changes and copy the file back to its original location

if [ $? -eq 0 ] then /usr/syno/bin/synonotify MySqlBkpOK
else /usr/syno/bin/synonotify MySqlBkpError
fi

Now, define those custom Notifications to let them appear in the "Advanced" tab of the "Control Panel" > "Notifications" pane where you will be able to enable "email", "sms", ... for them.

  1. Go back to the telnet console and execute: cp enu/notification_category /volume1/web/
    1. If you want to define those notifications for another language, used the adequate folder instead of 'enu'.
    2. Notice that I presume you are still in the path set previously, i.e.: /usr/syno/synoman/webman/texts/
  2. Edit the file 'notification_category' with notepad++
  3. Look for the following definition in that file: {"group":"Backup/Restore","name":"NetBkpS3OK","title":"Amazon S3 backup completed"}
  4. Insert the following definition next to it (don't forget the coma), save the changes and copy the file back to its original location: cp /volume1/web/notification_category enu/notification_category

,{"group":"Backup/Restore","name":"MySqlBkpOK","title":"Database Backup completed"},{"group":"Backup/Restore","name":"MySqlBkpError","title":"Database Backup failed"}

Finally, check that the "Notifications" are enabled on your Synology and tick options like emails, SMS, ... for the new tags:

  1. In your DSM, go to “Control Panel” > “Notification” > “E-Mail”
  2. Tick the option “Enable e-mail notifications”.
  3. Complete all the fields in that tab. At least: SMTP server, SMTP port, SMTP authentication and Primary email.
  4. Send also a test mail from this tab to verify your settings.
  5. Go next to the "Advanced" tab
  6. Tick the "E-mail" option (and possibly others) for the two new tags which appear now in the list.
    1. If the tags do not appear, close and reopen the control panel.
    2. The tags appear with their title "Database Backup completed" and "Database Backup failed"

And here are the outcomes: a popup message and an email

Database Backup Email

Database Backup Email

Database Backup Custom Notification

Database Backup Custom Notification

Pay attention that the tags are case sensitive ! Also backup the changes you made in the file 'mails' and 'notification_category' as they will be overwritten for sure when you will update the DSM... Finally, delete the files 'mails' and 'notification_category' from your web folder...

Notice: to declare Notifications for other languages, you will apply the tips here above in the files under your own language folder. A reader, Kasak, kindly reported however that he had to declare the 'tags' not only in the 'mails' file under the folder of his language (E.g.: 'fre'), but also under the English (enu) folder.

Notice: as reported by another reader, Pieter Hooftman, you will see your notifications in Control Panel > Notifications > Advanced tab if and only if you filter them on "All Notifications". If you need to filter your custom notifications, you have to add it into each section of the file "notification_category" (there is one section per filter). Concretely, look for each occurrence of {"group":"Backup/Restore","name":"NetBkpS3OK" and insert your in front.

Notice: as reported by k13tas, it's much easier to reuse an existing notification and customize its text via Control Panel > Notification > Advanced. Indeed, customized text are not lost after a DSM update. That being said, once a custom notification added in the file "notification_category", you can also customize its text. Once this is done, the notification will always be available to be used with /usr/syno/bin/synonotify, even after a DSM update, although not visible anymore in the Advanced tab - which is far from ideal.

As creating manually custom notifications is far from ideal, please vote for my feature request on Synology's forumhttp://forum.synology.com/enu/viewtopic.php?f=3&t=92727

48 thoughts on “Send Custom (email) Notifications from scripts running on a Synology

  1. Hello,
    Thanks for the tutorial. it worked all well to me except for the custom notifications that i created are not appearing in advanced tab.
    However i was able to execute /usr/syno/bin/synonotify MyCustomNotifName successfully. (and was able to see on top right corner of DSM home page)
    Can you help me why the notifications are not visible in the advanced tab.
    Below is how my notification_category file looks like
    ,{"group":"Backup/Restore","name":"NetBkpS3OK","title":"Amazon S3 backup completed"},{"group":"Backup/Restore","name":"MySnapsBkupError","title":"My Snaps are backup FAILED"},{"group":"Backup/Restore","name":"MySnapsBkupOk","title":"My Snaps are backup successfully"},{"group":"Backup/Restore","name":"NetLUNBkpError","title":"Network LUN backup failed"},

    Chakri

    • I just noticed that if you did copy/paste the text from my post into notepad++, the double quotes were not pasted as ASCI code 34. Please, double-check all of them in your notification_category file. They should not appear in italic like this: " or ", but they must appear like that: ".

      I have fixed my post so they are now ok. I think that this is your issue.

      Possibly also check with notepad++ that the EOL (end-of-lines) are Unix Format... Menu Edit > EOL Conversion > UNIX/OSX format (possibly swap to Windows and back the UNIX)

  2. Hi there - i am looking for a way to receive notifications when a file changes (or doesn't change) on my Synology NAS. Sometimes my backup software (macrium) doesn't work and I need to receive notice when this happens. If the backup fails, usually Macrium itself will email me but this doesn't always happen and i receive no alert. It might be more consistent if i can figure out a way to get the DS to notify me directly.

    Each computer has it's own share. So i see no reason why i couldn't have a script search for a file at a particular time and alert me if no file from that time point exists. Or something to that effect....
    any ideas?

    • The problem is that Macrium could just "crash" in the middle of the backup (or not complete successfully if there is no space anymore on your NAS) => some files would be backuped and some others not.

      So, the solution you suggest will only be reliable if Macrium can write a kind of "lock" file when the backup is successfully completed. Monitoring the modification date of that "lock" file on your NAS could be used to trigger a notification.

      Assuming that Macrium runs everyday and can write such a "lock" file, use a script that checks if the last change on that "lock" file was made within the last 24 hours. If it's the case, send a "backup successfully completed" notification. The script could look like this:

      #!/bin/bash
      #
      if test `find "/volume1/web/MyMacriumLock.lck" -mmin -1440`
      then /usr/syno/bin/synonotify MySqlBkpOk
      else /usr/syno/bin/synonotify MySqlBkpError
      fi

      You can also use "find" to look for file older than x minutes: find ... -mmin +x
      Or you can look for any file within a folder younger than x minutes: find myFolder -mmin -x
      Or look within a folder for a specific file n younger than x minutes: find myFolder -name n -mmin -x

      NOTICE: in the script above, around the "find" command, you have to use ` instead of '.
      Also, be sure that the file is in ANSI, with UNIX EOL and not Windows. You can do that with notepad++.

      Save the script within some folder like /volume1/MyOwnSripts/MonitorBackup.sh

      And next, go the the Synology "Control Panel" > "Task Scheduler" to create a new task with a "user defined script" = sh /volume1/MyOwnSripts/MonitorBackup.sh

      Use the 'root' account to execute that script, and schedule it to be run once a day every day.

  3. Really great article ! It really is what I looked for.

    Two additional questions:
    - Do you know how to customize/add variable from the text of the email (like %HOSTNAME%) ?
    - Do you know why some categories (like Download stations ones) are not listed in the notification panel and how can I use them?

    Thanks a lot

    • To use variables in your text, look for existing ones in the other messages defined in /usr/syno/synoman/webman/texts/enu/mails. I am using %HOSTNAME% in my message and it is replaced automatically when the notification is sent.

      I didn't try yet to add custom variables... I will possibly investigate later.

      Regarding the notifications from the Download Station, I didn't notice they were indeed not available. The message are not defined in the 'mails' file, so I can only presume that the Download Station is not using exactly the same mechanism... A reason could be that the messages are "possibly" not flexible enough. Notifications from the Download Station contains variables related to the download url, etc...

        • What you could try is to configure the Download Stations to call a script. That script would send your own notifications as explained in my post...

          1) Stop first the DS: /volume1/@appstore/DownloadStation/scripts/S25download.sh stop

          2) Then configure DS to run your script when a download task is done by editing the file /usr/syno/etc/packages/DownloadStation/download/settings.json
          "script-torrent-done-enabled": true,
          "script-torrent-done-filename": "/volume1/YourFolder/YourNotificationScript.sh",

          3) And finally, restart the DS: /volume1/@appstore/DownloadStation/scripts/S25download.sh start

          However, I still don't know how to pass arguments from the DS to your script and from your script to the "synonotify" method.

          • The variables like %PARAM% are private and managed by the Synology and if I believe Google, not a lot of persons know how to set their values...

            So, the best option is maybe to search and replace your own parameters in the file 'mails', within your script, before calling 'synonotify'... Something like this should do the dirty trick (assuming that 'enu' is your language):

            cp /usr/syno/synoman/webman/texts/enu/mails /volume1/YourFolder/
            sed -i "s/%YOUR_OWN_PARAMETER%/your_own_value/g" /usr/syno/synoman/webman/texts/enu/mails
            /usr/syno/bin/synonotify Your_Event
            cp /volume1/YourFolder/mails /usr/syno/synoman/webman/texts/enu/

  4. Hi,
    I try to write a script to backup my database under DSM 4.3. The backup itself works fine. I used this tutorial : http://www.zarafa.com/wiki/index.php/Full_database_backup. As the mail isn't installed on my synology, I am interested in your solution to trigger a notification (ideally with email). So I added the tags in the mails file as you suggest, I also modified the notification_category file, and two new lines appeared in the notification tab in dsm. Unfortunately, I am able to trigger any original notification with synonotify, but not mine. I checked eol characters and quotes, this seems correct. Did you try under DSM 4.3? What could I miss?

    Alternatively I can use synodsmnotify which works but isn't able to send emails.

    Thanks :-)

    • Unfortunatelly, I cannot upgrade my Synology DS209+ to DSM 4.3.
      DSM 4.2 is the last one supported on my model...

      If you run synonotify from a telnet console, do you get any error message ?
      Also, double check that you have added the new event and its message in the 'mails' file under the right language...

      An alternative for you would be to reuse an existing event (one already defined in 'mails' that you don't need) and change its message... By the way, if you try this and don't get a mail with the modified text, it means that you didn't adapt the 'mails' file under the right language.

      • I get no error message when I use synonotify. I modified an existing notification and I received the modified email. Only new notifications cannot be triggered. It seems that both emails and notification_category files are correct. I will probably reuse an existing one.

        Thank you for your tips

        • I can't check right now but when I was using synonotify with an event not defined in the 'mails' file, I was getting an error in the System Logs: "Failed to send email. (Failed to found mail tag).".

          So, possibly run 'synonotify aDummyName', where aDummyName does not exist in 'mails', to see if you get such an error (you should). Next retry 'synonotify YourActualEvent' to confirm that it does not trigger any error. If you have an error, it could be due to a typo mistake (are you using any special characters ?). If you don't have an error, confirm that you have selected the 'mail' option in the "Control Panel > Notification > Advanced tab" and is able to "Edit Message" of your custom event. Also, could you confirm that you not only missing the email but also the notification which is assumed to be displayed via the "notifications icon" in the right-top corner of DSM ?!

          • I found that, to make it work, I need to edit both files from enu and fre folders (my DSM is in french). If tags are missing in files within the enu folder, it doesn't work (Failed to found mail tag in system log).

            Thank you for your support!

          • That's a very interesting finding !
            Un tout grand merci pour avoir rapporté la solution ;)

            I have updated my post to include that information.
            I only wonder if Guido's DSM is configured to run in 'enu' as he didn't report such an issue (and Guido is from 'nld') ?!

  5. Thank you very much for this - I've been looking around for something like this for a while. Nice to see how integrated it is with the notification system.

  6. This didn't work on my DSM 5.0 :(
    Notification fires when I do synonotify X, but notification settings don't show up in advanced tab. So I'm unable to tick email, sms, push etc. It only works for the in-GUI notification.

    • I confirm that I do see my custom notifications in the Advanced Tab of my DSM 5.0.
      Did you check that there is no error logged on your system (Main Menu > Log Center) ?

      Also, are you using the default language (enu) and did you update accordingly enu/mails ? Or is your DSM using another language ?
      Finally, pay attention that when doing a copy/paste of the “group”:”Backup/Restore”, ... suggested in my post, the encoding of the double quotes are possibly not correct. I.e: not ASCII code 34. Retype them manually to be 100% sure. Otherwise, they won't indeed appear in the Advanced Tab.

  7. Good topic, I got a question concerning a standard notification.

    I would like to change the send notification when you change a user's password.

    This notification does not appear in the list of notification (or I do not find it).

    Do you have any idea of which notification must be edited?

    Regards.

    • Hi,

      I presume that indeed, one cannot enable/disable that notification via the Control Panel. But fetch the file 'mails' I mentioned in the post and look for the section [UserChangePassword]. I guess it's the one you want to customize.

  8. Hi,

    I have a question about a simulair thing. I am syncing files with a friend of my. Sometimes we exchange file but we don't talk on a daily base. Even more we sometimes don't talk for 1 or 2 weeks but we exchange files of our common project. Is there a script of some sort that will tell me when an particulair directory changes?

    Thanks in advance for your answers on this matter.

    Cheers,

    Peter

    • Couldn't you just enable the "File Transfer Log" (in Control Panel > File Services > Windows File Service) ?
      You have a Widget that can be enabled in DSM to view the "File Change Log"... You would see there all files changed in Shared Folders...

      Otherwise, you will have to schedule a script, e.g. every 24h, to look for recent changes and notify you by email when there are such changes... The script would look like this:

      #!/bin/sh
      #
      DIR="/volume1/web/"

      COUNT=`find . -mmin -$((24*60)) -type f ( ! -iname "#recycle" ) | wc -l | sed 's/ //g'`
      if [ $COUNT -eq 0 ]
      then echo "No change found"
      else /usr/syno/bin/synonotify FilesWebChanged
      fi

      If I have some free time this weekend, I will fine tune this script as I didn't test it and am quite sure it's not running as-is. Unfortunatelly, Synology run the ash shell by default, which is quite poor compared to the bash shell... and I am really not confortable with this ash shell :(

      Also, as I never found a way to pass parameters to synonotify, the message in "FilesWebChanges" is quite static. It means that basically, you would need one script per directory to be monitored.... Or; an idea would be to generate a report (html page) with all touched files or with only the touched folders, and include an hyperlink to that report in the message...

  9. Thank you so much Valery! I am sure i gonna have a look at both... Or perhaps try to get my programming language at work in the Synology. I let you know how it goes.

    Cheers,

    Peter

  10. Pingback: Using Gmail smtp through VPN (on Synology) | Rifraf's tips

  11. Hi,

    if you use DSM 5 you need to also add a new line to:

    /usr/syno/etc/notification/notification_filter.settings

    For the example you need to add the following line:

    MySqlBkpError="mail"
    MySqlBkpOK="mail"

    • Thx a lot for sharing!

      However, in my case, I didn't need to set that manually. I did it via the "Advanced" tab of the "Control Panel" > "Notification". Doing so, the lines you mentioned are automatically added into /usr/syno/etc/notification/notification_filter.settings.

      I did Google on notification_filter.settings to learn a bit more about that file and noticed that other delivery channels can be manually added with this kind of notation: MySqlBkpError="mail,mobile,sms,skype"

      But again, in my case, those extra delivery channels are added into the file when I enabled them via the "Notification" configuration pane...

  12. DSM 5.0 update 5.
    I have used this guide to add a notification in the System Category. I have noticed this custom notification only shows up in the advanced tab if i set the advanced tab filter to 'system' (drop down box in upper right corner of the advanced window.) If i set the drop down box to 'all messages', the custom notification is not shown in the list of notifications in the system category.

    • I have updated to DSM 5.0-4493 Update 5. As usually after an update, some files are reset.
      This time, only "mails" was reset. "notifications_category" was unchanged.

      Once the tag added back into 'mails', I did refresh DSM (CTRL-F5) in the browser and I confirm that I see indeed the notification "Database backup completed" and "Database backup failed" only with "All Notifications" selected as a filter. I have no idea how filters work if they don't use the "group" name of the notifications defined in the file "notifications_category" ("Backup/Restore" in my case)

      Thx a lot for reporting! I know what to investigate when I'm bored :D

      • Ok... I had some time to look at this issue. In the file "notification_category", there is one section for each filter. In my post, I said to look in that file for the following definition: {“group”:”Backup/Restore”,”name”:”NetBkpS3OK”,”title”:”Amazon S3 backup completed”}.

        By default, we all look for the first occurrence, which is in the section "All" (As you can see at the beginning of the file: {"All":.). So, to see the notification within the other filter, you have to add it next to each occurrence!

  13. Hi there, has anyone found a way to modify the Download Station notification and make it work?
    I have been looking for a workaround for months and haven't found anything yet... this post is the closest to something, but haven't seen anyone actually making it work.
    Would really appreciate it.

    Thanks!

    • What do you mean by "a way to modify"?
      Currently, you can only get an email or a desktop notification on "download task completion"...
      Do you want to change the message of that notification or create new notifications (Ex.: on download started, download failed, ...)?

  14. The problem with this method is that it gets overwritten after each update. To bypass this problem I simply modified one of existing notifications using web interface.
    Control Panel --> Notification --> Advanced. Choose some notification that you normally would never get like 'Amazon S3 restoration failed' (if you don't use amazon S3 ofcourse). Slect that notification and click 'Edit Message'. Edit title and body of the notification. Since it was edited using web interface it won't get overwritten during next update. Now it's time to call this notification from your script:
    /usr/syno/bin/synonotify S3RecoverError
    This way you get custom notifications and no ssh needed at all. And most importantly - you won't gave to do it all over again after next update.
    Hope it helps somebody.

    • That’s a great finding! Especially since Synology is providing more and more frequent updates. I was myself looking how to use the “sed” command in a script to reinsert my custom notifications automatically after each update…

      Starting from your finding, I did however some tests and noticed that you remark is not only true for modifications of native notifications but also for modifications of custom notifications. I.e.: if one has created a custom notification and modified its text via the Advanced tab, the text of the notification will still be available for sending Desktop notification even after a DSM update (even if not declared anymore in the file “mails”) ! The drawback is that it won’t be visible anymore in the Advanced tab because it’s not in the file “notification_category” due to the DSM upgrade… In addition, if I remember well, the “send” options are also reset after an upgrade (Reason why, using your trick, you have to customize an existing notification that has “Email” option checked by default). Conclusion: If custom notifications are not visible anymore in the Advanced tab, one could not re-enable the option to send it by Email – except if editing also the file /usr/syno/etc/notification/notification_filter.settings mentioned by Gagi… So, I definitively has to create a script using sed – or – send a request for the feature “Custom Notification” to Synology

  15. Dang. This is no longer working for me since I upgraded to DSM 5.1. I can run the synonotify command from the command line and the custom notifications work fine, but for some reason it doesn't work from my Transmission script that is called when a torrent is completed. I wonder if they changed synonotify so that only the root user can use it?

    Something else that's weird: the text for the notification messages is no longer being read from the enu/mails file. If I change a notification message using the web interface, the text is NOT updated in the mails file.

    • For sure security have been improved with DSM 5.1. But no idea if there are now restrictions like the one you suggest. I would connect on your NAS via Telnet and the user account running your Transmission script to try manually the command synonotify... Next, if no error message is displayed in the console, look into the "logs" to see if something wrong happened. synonotify logs its own errors there.

      Regarding the text, I noticed recently that if one has created a custom notification and modified its text via the Advanced tab, the text of the notification is still be available even after a DSM update (which usually reset the file 'mails' and 'notification_category') . It means that this text is kept somewhere else. But I didn't investigate further...

  16. Hi,

    Good topic, I got a question concerning a standard notification.

    I would like to receive notification whenever a user upload their file(s) onto the Synology via File Station.

    Any idea on how to find it as I cant locate this type of notification in the list of notifications.

    Do you have any idea of which notification must be edited?

    Thanks!

    Regards,
    Gary

  17. Hi,

    Great work, thank you for your input !

    I'm playing around with a custom rsync script. How come, if I use for example

    /usr/syno/bin/synonotify S3RecoverError

    The different variables in my email are not replaced as expected (%HOSTNAME%, ...).

    Thank you !

  18. Valery Letroye : Thanks for all the details and time invested. I am running DSM 6.0.2-8541 Update 2 on a DS214 for testing purposes.

    I was able to find the "mails" file but I couldn't find the "notification_category". I am looking for it on /usr/syno/synoman/webman/texts/ and even used "find" to no avail.

    Do you know it the location has changed?

    • Indeed, the file is not there anymore... To be honest, I am not using notifications since a while now. But the easiest for you would be to edit an existing notification that is not currently used (Via DSM > Control Panel > Notification > Advanced > Edit Message).

      [EDIT] I have just retested this tutorial on my DSM 6.0 and written a new post:see here

Leave a Reply

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