Created
December 12, 2016 05:22
-
-
Save istepanov/a1d706fb48138795c0ab0d7e50f39bbc to your computer and use it in GitHub Desktop.
Simple ping watchdog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import datetime | |
address = '8.8.8.8' | |
ping_timeout = 5 | |
attempts = 4 | |
log_file = '/var/log/ping_test.log' | |
failed_attempts = 0 | |
for i in range(0, attempts): | |
result = os.system('/bin/ping -c 1 -w %i %s > /dev/null 2>&1' % (ping_timeout, address)) | |
if result != 0: | |
failed_attempts += 1 | |
if failed_attempts == attempts: | |
with open(log_file, 'a') as logfile: | |
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M') | |
logfile.write("%s 'ping %s' failed attempts: %i. Rebooting...\n" % (now, address, failed_attempts)) | |
os.system('/sbin/reboot') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment