Creating a Custom Alert

Status
Not open for further replies.
Joined
Mar 31, 2014
Messages
1
There may be other (proper?) ways of doing this. If so, wonderful! If not, this is for you. :)

Would you like to issue your own custom alert (http://doc.freenas.org/index.php/Alert)? Well, the alert scripts are pretty straightforward. Here is a script that watches for the existence of the file /tmp/my_alert. The first line of /tmp/my_alert can optionally set the alert to either WARN or CRIT.

Code:
import os
 
from freenasUI.system.alert import alertPlugins, Alert, BaseAlert
 
 
class MyAlert(BaseAlert):
 
    def run(self):
        my_alert_type = Alert.WARN
        my_alert_msg = "my_alert has triggered an alert"
        if os.path.exists('/tmp/my_alert'):
            with open ("/tmp/my_alert", "r") as alertfile:
                my_alert_data=alertfile.readlines()
 
                if len(my_alert_data) == 1:
                    my_alert_msg = my_alert_data[0].rstrip('\n')
                elif len(my_alert_data) > 1:
                    if my_alert_data[0] == 'WARN\n':
                        my_alert_type = Alert.WARN
                    elif my_alert_data[0] == 'CRIT\n':
                        my_alert_type = Alert.CRIT
                    my_alert_msg = my_alert_data[1].rstrip('\n')
 
                return [
                    Alert(
                        my_alert_type,
                        my_alert_msg
                    ),
                ]
 
alertPlugins.register(MyAlert)


Copy and save to /usr/local/www/freenasUI/system/alertmods/my_alert.py. Don't forget to mount the filesystem for writing first (mount -uw /). I would recommend a reboot after adding the script so that it is registered in the web GUI. A reboot will also return the root filesystem to read-only. Might be other ways to do this, but I found this method the easiest.

Why use this? Well, if you have a cronjob monitoring something specific, it can create the /tmp/my_alert file. Note that CRIT alerts will send emails to the root email account. WARN alerts do not, but the alert icon will blink yellow.

Edit: Title should have been Creating a Custom Alert. Ugh. Need sleep.
Edit 2: Thanks for the edits/fixes! :)
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Fixed the title for you.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
Fixed your link too.
 
Status
Not open for further replies.
Top