Created
May 24, 2016 12:27
-
-
Save tracyhatemice/10d08cb8c1b9036cec04a1944b5435ec to your computer and use it in GitHub Desktop.
for network connectivity check
This file contains 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 | |
# -*- coding: utf-8 -*- | |
import sys | |
import logging | |
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) | |
from scapy.all import traceroute | |
import fcntl, sys | |
target = ["8.8.8.8"] | |
logdir = "app_heartbeat.log" | |
pid_file = '/var/run/eventlistener.pid' | |
def main(): | |
logger = logging.getLogger('app_heartbeat') | |
hdlr = logging.FileHandler(logdir) | |
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') | |
hdlr.setFormatter(formatter) | |
logger.addHandler(hdlr) | |
logger.setLevel(logging.INFO) | |
fp = open(pid_file, 'w') | |
try: | |
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
except IOError: | |
# another instance is running | |
sys.exit(0) | |
try: | |
result, unans = traceroute(target, maxttl=6, verbose=False) | |
if not result: | |
print ("restricted") | |
logger.info("Network down, restarting app...") | |
os.system("supervisorctl restart app") | |
exit() | |
else: | |
print ("connected") | |
logger.info("Network up.") | |
exit() | |
except : | |
pass | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment