1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to: Send Growl notifications from router (Python)

Discussion in 'Tomato Firmware' started by philess, May 6, 2013.

  1. philess

    philess Networkin' Nut Member

    What is Growl?

    Growl is a desktop notification app displaying Balloon-popups with information about
    various events. Growl is mostly home on Mac´s but there is also a Windows version.
    Growl can tell you when a download has finished, a new email has arrived and much more.
    It can also forward messages over network from one computer to another, so you
    can have your downloads running on one computer in the bedroom, while you are browsing
    the web on another computer in the living room, receiving Growl messages if a download
    has finished. You could also forward them as push-notification to your iPhone using "Boxcar".

    More info & Mac app download: http://growl.info
    Growl for Windows: http://www.growlforwindows.com
    Growl for Linux: https://github.com/mattn/growl-for-linux


    [​IMG]

    Display style can be customized in the desktop app, also options like display-duration
    and which corner of your desktop it should appear and much more. These screenshots
    are just very basic examples.

    I think this is a nice alternative or addition to email notifications from scripts running on Tomato.
    I will not get into too much detail about Growl itself here (assuming most people who
    read the topic title already know Growl) and instead focus on the simple steps to
    make it possible to have your Tomato router send you notifications.


    Requirements:
    - Python 2.5 or higher (tested with 2.5 and 2.7)
    - SSH & (S)FTP access to your router

    If you arent already, connect to your router over SSH.

    I am assuming you are doing this install from your /tmp directory.
    Code:
    cd /tmp
    Download & install setuptools
    (Check your Python version with "python -V". If you are using a different
    version than 2.7 in this example, get the download link for the setuptools
    from http://pypi.python.org and proceed accordingly)
    Code:
    wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
    chmod +x setuptools-0.6c11-py2.7.egg
    ./setuptools-0.6c11-py2.7.egg
    After installation has completed successfully:
    Code:
    rm -rf setuptools-0.6c11-py2.7.egg

    Download & install pip
    Code:
    wget http://pypi.python.org/packages/source/p/pip/pip-1.2.tar.gz
    tar xvzf pip-1.2.tar.gz
    rm -rf pip-1.2.tar.gz
    cd pip-1.2
    python setup.py install
    After installation has completed successfully:
    Code:
    cd ..
    rm -rf pip-1.2/

    Download & install gntp
    Download the .ZIP from here: https://github.com/kfdm/gntp
    (Doesnt seem to be possible with wget@tomato, download it on
    your computer and upload the .zip to /tmp on your router)

    Code:
    unzip gntp-master.zip
    rm -rf gntp-master.zip
    cd gntp-master
    python setup.py install
    After installation has completed successfully:
    Code:
    cd ..
    rm -rf gntp-master/

    Create the following files, change the mentioned lines to your own setup
    (If you are using Windows, pay attention to CR/LF, i recommend using Notepad++ or UltraEdit)


    growl-reg.py
    Code:
    import gntp.notifier
     
    growl = gntp.notifier.GrowlNotifier(
            applicationName = "Tomato",
            notifications = ["Info"],
            defaultNotifications = ["Info"],
            hostname = "192.168.1.100",
            password = "password"
    )
    growl.register()
    
    Required: Change the IP to the IP of your computer that should later receive the notifications.
    Required: Change the password to the Growl-password you have set on your computer.
    Optional: applicationName is how the App will show up towards the receiver.
    Optional: notifications is the category it should register, it is fine to just have one.
    Optional: defaultNotifications is the default category to use for messages.


    growl.py
    Code:
    #!/opt/bin/python
     
    import gntp.notifier
    import sys
     
    # USAGE: python growl.py <IP-or-HOSTNAME> <TITLE> <TEXT>
     
    # these settings must be the same as used previously
    # when registering with growl-reg.py
    # if you dont want to use IP from commandline,
    # replace sys.argv[1], with your IP
    growl = gntp.notifier.GrowlNotifier(
            applicationName = "Tomato",
            notifications = ["Info"],
            defaultNotifications = ["Info"],
            hostname = sys.argv[1],
            password = "password"
    )
     
    # Send message
    growl.notify(
            noteType = "Info",
            # priority can be used in the client to handle
            # messages differently, otherwise can be ignored
            priority = 1,
            # title is the message title (subject/header)
            title = sys.argv[2],
            # description is the message text (body)
            description = sys.argv[3],
            # If sticky is True, the message will stay onscreen until
            # you click on it. Can be useful for very important messages.
            sticky = False,
            #
            # optional:
            # provide callback url when growl message is clicked
            # example: open browser & view TomatoUSB webinterface
            # callback = "http://192.168.1.1",
            #
            # optional:
            # send along a icon for the mesage
            #
            # note: i never got this working with a local file, only this example:
            # icon = "http://png-4.findicons.com/files/icons/2332/super_mono/64/wireless_router.png",
    )
    
    Required: Edit the top part exactly the same as in growl-reg.py!
    The lower part can be ignored, but it is explained what each setting does.

    Optional step:
    I have two files to call from scripts. One sends standard messages that will disappear after
    a few seconds. The other is for important events (High CPU load, Temperature, etc) and will
    send sticky messages that will stay onscreen until you click on them.
    If you want this too, duplicate the created growl.py from above as growl-sticky.py
    but change the setting to "sticky = True,".


    Upload the created growl-reg.py and growl.py to your router, for example to /opt


    Registering

    This is only required ONCE.
    Or if you want to use different applicationName or categories for the messages.
    Code:
    python /opt/growl-reg.py
    On your computer there should be a notification "Application Tomato has registered"
    Now you can send notifications using growl.py to that computer.




    Sending notifications

    Code:
    python /opt/growl.py 192.168.1.100 "Hello" "Hello World"
    Should give you a notification with the title "Hello" and the text "Hello World" on your receiving computer.

    [​IMG]

    From now on you can send these messages from your scripts or WebUI.



    For example in Webui, Administration/Scripts/WAN UP:
    Code:
    python /opt/growl.py 192.168.1.100 "WAN UP" "WAN is up with IP $(nvram get wan_ipaddr)"
    [​IMG]

    Or if you have certain things being done from Scheduler:
    Code:
    echo "15" > /proc/net/expire_early
    python /opt/growl.py 192.168.1.100 "Conntrack" "Idle connections have been dropped."

    Or whenever someone connects to your OpenVPN Tomato server:
    Add to OpenVPN custom configuration:
    Code:
    script-security 3 system
    client-connect /opt/connect.sh
    client-disconnect /opt/disconnect.sh
    connect.sh
    Code:
    #!/bin/sh
     
    python /opt/growl/growl.py 192.168.1.100 OpenVPN "$common_name connected from $untrusted_ip"
     
    exit 0
    disconnect.sh
    Code:
    #!/bin/sh
     
    python /opt/growl/growl.py 192.168.1.100 OpenVPN "$common_name disconnected"
     
    exit 0
    (Dont forget to "chmod +x connect.sh")


    I hope this can be useful or atleast fun to play with for someone. Enjoy.
    Any input is very welcome!
     
    blackwind likes this.
  2. occamsrazor

    occamsrazor Network Guru Member

    This will take some time for me to get my head around the installation process, but looks great. Growl is a super system....
     
  3. philess

    philess Networkin' Nut Member

    It may look complicated because i wanted to make this how-to pretty much fool-proof, but it really isnt.
    Its just installing setuptools.egg, pip (python package manager), the gntp-master and then registering
    from growl-reg.py. I usually get through all steps in way less than 10 minutes.
     
  4. MercuryV

    MercuryV Networkin' Nut Member

  5. philess

    philess Networkin' Nut Member

    True, but when i tried some months ago i couldnt get it working on Tomato.
     
  6. MercuryV

    MercuryV Networkin' Nut Member

    What's about python script on router as sender + Linux port of Growl on desktop as receiver?
     
    philess likes this.
  7. philess

    philess Networkin' Nut Member

    Oh yes, true. I didnt think about that :) Thank you.
     
  8. Sean Rhodes

    Sean Rhodes Networkin' Nut Member

    Hi philess,

    How do you add multiple recipients? Do you have to send growl-reg.py for each ip address on the network, or can you have multiple hostname IP addresses?

    I sent the growl-reg.py from my iMac and got a return notification from the router OK, but I also have a mac-mini and windows pc both running growl as well. They all see each other through the growl network, but only my iMac receives notification.

    My assumption was they all should receive the notification?
     
  9. ospfv3

    ospfv3 New Member Member

    Hi philess,

    thanks for your awesome guide! I finally can get notified by my pfsense router, if the cpu gets hot. I tried php-growl and net:smtp:tls email notifications before, but could get nothing to work. Your guide just works and is easy to understand. Two thumps up.
     

Share This Page