LDShadowLord

Internet Watcher V2

Jul 19th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. #V2 of InternetWatcher - Added file flushing every X runs, to assist in Replication scenarios.
  2. #This scripts automatically polls the Google DNS server every 15 seconds to check that it has internet connectivity - It logs both LIVE and DEAD statuses to a formatted .csv file
  3.  
  4. #Import required libraries
  5. import logging, time, socket, os, sys
  6.  
  7. #Declare Variables
  8. ##Number of runs passed
  9. count = 0
  10. ##Last count where file was flushed
  11. lastCount = 0
  12. ##How many counts until it flushes file
  13. minCounter = 100
  14. ##Max number of iterations before it restarts itself - 5759 is roughly 24 hours
  15. maxCount = 5759
  16.  
  17. #Define logging function
  18. ##file_name is the file name you want to force it to append to
  19. ##mode disables addHandler if anything other than "c"
  20. ##folderpath is the path to folder, leave it empty to put log files in executable folder
  21. def startupTask(file_name="", mode="c", folderpath=""):
  22.  
  23.   ##If the filename is nothing, then prepare a new file filename. Otherwise, the supplied filename should be used
  24.   if file_name == "":
  25.     t_fn = folderpath+time.strftime("%d%m20%y-%H%M%S",time.localtime())+".csv"
  26.   else:
  27.     t_fn = file_name
  28.  
  29.   ##Configure logging in the specific format
  30.   logging.basicConfig(
  31.     filename=t_fn,
  32.     filemode="a",
  33.     level=logging.INFO,
  34.     datefmt="%d %b - %Y : %H:%M:%S",
  35.     format="%(asctime)s , %(levelname)s , %(message)s")
  36.   console = logging.StreamHandler()
  37.   console.setLevel(logging.INFO)
  38.   formatter = logging.Formatter("%(asctime)s , %(levelname)s , %(message)s","%d %b - %Y : %H:%M:%S")
  39.   console.setFormatter(formatter)
  40.  
  41.   ##Add handler if equal to c, otherwise ignore
  42.   if mode == "c":
  43.     logging.getLogger('').addHandler(console)
  44.   else:
  45.     pass
  46.  
  47.   ##Return the file name
  48.   return t_fn
  49.  
  50. #Start logging
  51. cur_fn = startupTask("", "c", "")
  52.  
  53. #Main Script
  54. while True:
  55.   time.sleep(10)
  56.  
  57.   ##If the maximum count has been reached, restart the script
  58.   ##Alternatively, kill the script.
  59.   if count > maxCount:
  60.     logging.shutdown()
  61.     time.sleep(0.2)
  62.     os.execv(sys.executable, ['python'] + sys.argv)
  63.     ###quit()
  64.  
  65.   ##If the appropriate number of counts has passed, flush the log to disk and reopen the file for appending
  66.   elif count == lastCount+minCounter:
  67.     lastCount = count
  68.     logging.shutdown()
  69.     time.sleep(0.2)
  70.     startupTask(cur_fn, "l")
  71.    
  72.   else:
  73.     pass
  74.  
  75.   ##Attempt connection to Google DNS, log as success
  76.   try:
  77.     time.sleep(5)
  78.     count = count + 1
  79.     s = socket.create_connection(("8.8.8.8", 53),5)
  80.     logging.info(str(count)+" , LIVE")
  81.  
  82.   ##If the connection fails, log as failure
  83.   except:
  84.     logging.warn(str(count)+" , DEAD")
Add Comment
Please, Sign In to add comment