Schedule a WOL magic packet based on UPS status

Discussion in 'Tomato Firmware' started by Aquafire, Dec 29, 2010.

  1. Aquafire

    Aquafire LI Guru Member


    I am running Linksys WRTSL54GS on Tomato Firmware v1.27.8747 ND USB.

    I have a APC SmartUPS-1500i with network management card 9617 connected to the router with a fixed IP address.

    A desktop Dell Optiplex 755 is connected to the router. The BIOS settings on the desktop is to "Turn On when A/C power returns".

    When the power goes out the desktop runs on battery for a fixed duration and then turns off (graceful shutdown via APC Network Chute software), while the UPS keeps running with A/C power outlets tunred on. If the power does not returns till the UPS battery dies off, then the UPS goes to sleep and turns off all the A/C power outlets (built on the back of UPS). When A/C main power is restored , the UPS turns on and based on the BIOS settings the desktop is turned on as well.

    Now sometimes, the UPS shuts off the desktop PC (while UPS A/C power outlets are still on), and before a complete battery drain, the main A/C power is restored. In this specific scenario, since there was no power cutt-off to the desktop PC, it never senses the A/C power return and as a results stays in the shut-off state.

    Now the only way to remotely turn on a shut off PC is via the Wake on LAN (magic packet) function in the tomato interface, which I have to do manually sitting on a remote location.

    Based on the above scenario, can I schedule a (cron) job in the tomato interface to periodically query the UPS state. If the UPS is running on battery , then do nothing, but if the UPS is running on A/C main power , then every "x" time interval automatically send the WOL magic packet to all the devices connected (or all devices entered in fixed host list).

    This way it would make sure that if A/C main power is restored then the desktop will keep running (or will be turned on after certain time period).

    I guess there is the 'apcupsd' package available on IPKG. However I need help how to install it in the tomato firmware and how to write the intended script.

    Hope I was able to explain and make sense.

    Thanks for reading and replying.
  2. rs232

    rs232 Network Guru Member

    I see what you're trying to do and I have a similar scenario with the difference that the UPS is plugged via USB to an ESXi host where a vm running fedora 12 run acpupsd to handle events.

    Having said that I don't see any problem scheduling a WOL every e.f. hour for all the hosts. It's a tiny packet and will not affect machine powered on.

    The way I handle this issue myself is as follow:
    I don't use the crontable but the init script instead.
    I have my tomato router NOT plugged to the UPS. So when power goes down so the router does as well. With this in mind all I had to do is adding the following 2 lines in the init script:

    sleep 300
    /usr/bin/ether-wake 00:00:00:00:00:00 
    NOTE: 300 in this case is greater than the minimum battery time left in the UPS when graceful shutdown is triggered.
    So if graceful shutdown is initiated but power is restored before the battery runs out the box still will receive the WOL to power on

  3. Aquafire

    Aquafire LI Guru Member

    Thanks for the explanation.

    But unfortunately it does not addresses my scenario. Where the router is usually always on getting its power via UPS and the UPS is wired network connected to the router.

    How can I query the status of UPS in tomato shell and then based on the response give specific commands (like ether-wake) or schedule a cron job.

    Can anyone else post a more practically applicable solution.

  4. rs232

    rs232 Network Guru Member

    Can I just say that querying the card you generate possibly even more traffic than sending directly the WOL in the crontable?

    Having said that I'm not here to question, if that's the way you prefer: you have wget (to get data from http) and sed/awk/grep to find what you're after. An "if" condition is the final piece of the puzzle

    That's the way I would do it any ways

  5. Aquafire

    Aquafire LI Guru Member

    Well in my case the traffic in querying the card status does not really matters. Cause there is not too much of traffic on the network, just the usual email and web browsing. It can be scheduled to check every 5 minutes or 10 minutes maybe so wont be much of an overload.

    In the example you have quoted , the UPS will still turn on the PC if the graceful shut down is initiated and whether the power returns or not retunrs before the main A/C power is restored which is something I do not want.

    Reason being after graceful shutdown, only the PC is shutdown via software shutdown procedure, the power outlets at the back of the UPS are still in ON state. So as soon as a WOL packet is sent, the PC will again try to turn on whether the UPS is running on mains or on battery.

    The power from UPS is totally cut off once it runs out of battery (if the mains do no return in due time). This is when the PC power supply detects a cut-off and restore back signal and via the BIOS control , turns on itself.

    So I do want to query the state of UPS, because after graceful shutdown, if the power is not restored and UPS is still running (on battery) then I DO NOT want the WOL packet to be sent (so to further drain the battery / UPS will again initiate shutdown), I ONLY want the packets to be sent (periodically) when the UPS is running on main A/C power. (this will make sure an off PC will be turn on anyways).

    Hope I am able to explain my preference.
  6. rs232

    rs232 Network Guru Member

    I don't have a network card in my APC but
    according to the product page:

    The AP9617 supports the following protocols:
    If you can get to the APC off e.g. http (that's perhaps the easiest), save the output of the status page somewhere accessible by tomato, parse it and check if the status field. On my apcupsd webpage it says:
    Status: ONLINE 
    now that the is powered by the main, and that info is definitely coming from the APC itself.

    If the AP9617 doesn't have built in webserver (not even for administration) try telnet or ssh, the information available for sure.

    In the cron script:
    main on --> send WOL
    main off --> do not send WOL

    option B)
    I have somewhere an APC masterswitch somewhere I haven't used for ages. I guess the core function is the same as the AP9617. On that device I seem to remember you can specify the default behaviour when the main is back. Perhaps you have a similar option where you can specify to powercycle the router port only (and so force a tomato reboot to take advantage of the init script option)?

  7. Aquafire

    Aquafire LI Guru Member

    Well I know the technical specs of the network card 9617. It does have a webinterface for configuration.

    For Windows the programs like APCUPSd updates the status of UPS (battery info, runtime, etc) using either some special APC control protocol or SNMP commands.

    I guess the cron script in "plain english" is correct :), however I am looking for a more technically applicable way (syntax) of writing it as a cron job in tomato scheduler option.

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice