Created
May 8, 2018 13:19
-
-
Save upa/ac01cbc322203404e0ed8f49870e2e1a to your computer and use it in GitHub Desktop.
A Script for Executing Command when Ping is Failed
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 python3 | |
import os | |
import sys | |
import time | |
import subprocess | |
from optparse import OptionParser | |
from logging import getLogger, DEBUG, StreamHandler, Formatter | |
from logging.handlers import SysLogHandler | |
logger = getLogger(__name__) | |
logger.setLevel(DEBUG) | |
stream = StreamHandler() | |
syslog = SysLogHandler(address = "/dev/log") | |
syslog.setFormatter(Formatter("restarter: %(message)s")) | |
logger.addHandler(stream) | |
logger.addHandler(syslog) | |
logger.propagate = False | |
def lets_ping(target, count) : | |
success = None | |
for x in range(count) : | |
try : | |
subprocess.check_call([ "/bin/ping", "-W", "1", "-c", "1", target]) | |
success = True | |
time.sleep(0.2) | |
except Exception as e: | |
logger.error("Ping Failed to %s" % target) | |
logger.error(e) | |
if not success : | |
logger.error("%d pings to %s failed!" % (count, target)) | |
return False | |
else : | |
return True | |
def loop(option) : | |
restart_time = 0 | |
while True : | |
time.sleep(option.interval) | |
if lets_ping(option.target, option.count) : | |
restart_time = 0 | |
else : | |
wait_left = option.wait - (int(time.time() - restart_time)) | |
if restart_time and wait_left > 0 : | |
logger.info("Pings Failed, but in wait time, %d sec left" % | |
wait_left) | |
continue | |
try : | |
logger.error("Start to execute '%s'" % option.cmd) | |
output = subprocess.check_output(option.cmd.split()) | |
logger.info(output) | |
except Exception as e: | |
logger.error("Execution Failed: '%s'" % option.cmd) | |
logger.error(e) | |
restart_time = int(time.time()) | |
def main() : | |
desc = "usage: %prog [options]" | |
parser = OptionParser(desc) | |
parser.add_option( | |
"--cmd", type = "string", default = False, dest = "cmd", | |
help = "command string for when ping failed" | |
) | |
parser.add_option( | |
"--target", type = "string", default = False, dest = "target", | |
help = "ping target" | |
) | |
parser.add_option( | |
"--count", type = "int", default = 5, dest = "count", | |
help = "ping count for one check" | |
) | |
parser.add_option( | |
"--interval", type = "int", default = 120, dest = "interval", | |
help = "check interval (sec)" | |
) | |
parser.add_option( | |
"--wait", type = "int", default = 240, dest = "wait", | |
help = "wait time after restart (sec)" | |
) | |
(option, args) = parser.parse_args() | |
if not option.cmd : | |
print("execution command is required") | |
sys.exit(1) | |
if not option.target : | |
print("ping target is reequired") | |
sys.exit(1) | |
loop(option) | |
if __name__ == "__main__" : | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment