Skip to main content

power cycle device from a Raspberry Pi when network connectivity lost

Project description

pi-power-relay

power cycle a device from a Raspberry Pi when network connectivity lost

Description

If host(s) on an outside network are not reachable via ping, then reset a device, possibly a cable-modem, that is known to be flakey, by power cycling it via a power relay.

This is intended to be run on a Raspberry Pi, using an 'IoT Power Relay version 2' from Digital Loggers, to use GPIO to power-cycle a device. The power relay is plugged into a UPS, while the Raspberry Pi is plugged into the 'always ON' power port and the cable-modem is plugged into the 'normally ON' power port. The program, when detecting a loss of network connectivity, will drop power on the 'normally ON' port, and bring it back after a short period.

The power relay product used by this program can be seen at: https://dlidirect.com/products/iot-power-relay

This is typically run from the crontab every couple of minutes.

If run on something other than a Raspberry Pi, it will assume it is a development or test box and will NOT attempt to do the actual shutdown, but will still do everything else.

Example usages

% pi-power-relay --hosts 208.67.222.222,8.8.8.8 

This will ping the hosts 208.67.222.222 and 8.8.8.8. If either of them responds back correctly, then the program will exit with no action.

% pi-power-relay --maint 01:20-01:40

This will prevent a reset of the device if the network goes away from 1:20am to 1:40am during a known network maintenance period.

% pi-power-relay --device-name 'cable-modem' --logfile /var/log/power-relay

This will log any resets to the log file /var/log/power-relay and will use the device name of 'cable-modem' in the log entry.

% pi-power-relay --delay-exit 180

The reason for this option is that if you do not use the --quiet option, then an informational message is printed out before the program exits. But if you are relying on output from the crontab to then be sent to you on some other machine via e-mail, it will fail if you do not have a full blown mailer set up and the DNS lookup for the destination will fail because the device (cable-modem?) has not recovered yet. So, this delay will give the device time to recover, before you spit out the message and then exit. The default is no delay.

There is a help option. For eg:

% pi-power-relay --help

usage: pi-power-relay [options]*
    [-d|--debug]               debugging output
    [-e|--delay-exit num]      delay time before output and exit (0 secs)
    [-f|--force-reset]         reset now and quit, despite state or lock
    [-h|--help]                print this help info
    [-l|--logfile string]      log filename (none by default)
    [-m|--maint HH:MM-HH:MM]*  Maintenance time to NOT reset (none by default)
    [-p|--pin num ]            GPIO pin number (25)
    [-q|--quiet]               supress informational messages
    [-r|--reset-time num]      wait time between GPIO state change (15 secs)
    [-t|--tries num]           max number of ping attempts per host (3)
    [-w|--wait-time num]       reset wait time after previous reset (600 secs)
    [-x|--ping-timeout num]    wait time for ping to time out (2 secs)
    [-D|--device-name string]  name of thing being reset for log (device)
    [-H|--hosts string(s)]     comma-delimited hosts to ping (8.8.4.4,8.8.8.8)
    [-L|--lockfile string]     lock-file (/tmp/pi-power-relay--reset-time)
    [-V|--version]             print version of this program (2.0.2) 

Python 2

The code will work with both Python2 and Python3, but the packaging and distribution of this code is strongly tied to Python3. If you are using an ancient Python2 system, see the file README.install-python2 included in the downloadable source code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pi_power_relay_moxad-2.0.2.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

pi_power_relay_moxad-2.0.2-py2.py3-none-any.whl (22.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pi_power_relay_moxad-2.0.2.tar.gz.

File metadata

  • Download URL: pi_power_relay_moxad-2.0.2.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pi_power_relay_moxad-2.0.2.tar.gz
Algorithm Hash digest
SHA256 62564f71550874055cdb039d7c24f1ba7941a8b02f9b54a08ddf04b8f7049ab0
MD5 4d9429147d16e449ae050e56211e4e18
BLAKE2b-256 28f7cde0c3380406bd17ce9e88e43f16821ba77f140ba994aca7716023f25ae3

See more details on using hashes here.

File details

Details for the file pi_power_relay_moxad-2.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pi_power_relay_moxad-2.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5cf0fb26ef5a778248b7a503d05c73d7b0a1dff2c08991dfa6469a182db20077
MD5 75dfba32fe384b0d12ec95e82826155c
BLAKE2b-256 c97fca0ee970595132fb67ab27185ef03861b27f112cf7ab706cae483607687c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page