/powerup_checks.py

https://gitlab.com/edgardo001/Python · Python · 98 lines · 72 code · 17 blank · 9 comment · 21 complexity · 877d5e6576996fe47df555caedf4a4f6 MD5 · raw file

  1. # Script Name : powerup_checks.py
  2. # Author : Craig Richards
  3. # Created : 25th June 2013
  4. # Last Modified :
  5. # Version : 1.0
  6. # Modifications :
  7. # Description : Creates an output file by pulling all the servers for the given site from SQLITE database, then goes through the list pinging the servers to see if they are up on the network
  8. import sys # Load the Library Module
  9. import sqlite3 # Load the Library Module
  10. import os # Load the Library Module
  11. import subprocess # Load the Library Module
  12. from time import strftime # Load just the strftime Module from Time
  13. dropbox=os.getenv("dropbox") # Set the variable, by getting the value of the variable from the OS
  14. config=os.getenv("my_config") # Set the variable, by getting the value of the variable from the OS
  15. dbfile=("Databases/jarvis.db") # Set the variable to the database
  16. master_db=os.path.join(dropbox, dbfile) # Create the variable by linking the path and the file
  17. listfile=("startup_list.txt") # File that will hold the servers
  18. serverfile=os.path.join(config,listfile) # Create the variable by linking the path and the file
  19. outputfile=('server_startup_'+strftime("%Y-%m-%d-%H-%M")+'.log')
  20. # Below is the help text
  21. text = '''
  22. You need to pass an argument, the options the script expects is
  23. -site1 For the Servers relating to site1
  24. -site2 For the Servers located in site2'''
  25. def windows(): # This is the function to run if it detects the OS is windows.
  26. f = open(outputfile, 'a') # Open the logfile
  27. for server in open(serverfile,'r'): # Read the list of servers from the list
  28. #ret = subprocess.call("ping -n 3 %s" % server.strip(), shell=True,stdout=open('NUL', 'w'),stderr=subprocess.STDOUT) # Ping the servers in turn
  29. ret = subprocess.call("ping -n 3 %s" % server.strip(),stdout=open('NUL', 'w'),stderr=subprocess.STDOUT) # Ping the servers in turn
  30. if ret == 0: # Depending on the response
  31. f.write ("%s: is alive" % server.strip().ljust(15) + "\n") # Write out to the logfile is the server is up
  32. else:
  33. f.write ("%s: did not respond" % server.strip().ljust(15) + "\n") # Write to the logfile if the server is down
  34. def linux(): # This is the function to run if it detects the OS is nix.
  35. f = open('server_startup_'+strftime("%Y-%m-%d")+'.log', 'a') # Open the logfile
  36. for server in open(serverfile,'r'): # Read the list of servers from the list
  37. ret = subprocess.call("ping -c 3 %s" % server, shell=True,stdout=open('/dev/null', 'w'),stderr=subprocess.STDOUT) # Ping the servers in turn
  38. if ret == 0: # Depending on the response
  39. f.write ("%s: is alive" % server.strip().ljust(15) + "\n") # Write out to the logfile is the server is up
  40. else:
  41. f.write ("%s: did not respond" % server.strip().ljust(15) + "\n") # Write to the logfile if the server is down
  42. def get_servers(query): # Function to get the servers from the database
  43. conn = sqlite3.connect(master_db) # Connect to the database
  44. cursor = conn.cursor() # Create the cursor
  45. cursor.execute('select hostname from tp_servers where location =?',(query,)) # SQL Statement
  46. print ('\nDisplaying Servers for : ' + query + '\n')
  47. while True: # While there are results
  48. row = cursor.fetchone() # Return the results
  49. if row == None:
  50. break
  51. f = open(serverfile, 'a') # Open the serverfile
  52. f.write("%s\n" % str(row[0])) # Write the server out to the file
  53. print row[0] # Display the server to the screen
  54. f.close() # Close the file
  55. def main(): # Main Function
  56. if os.path.exists(serverfile): # Checks to see if there is an existing server file
  57. os.remove(serverfile) # If so remove it
  58. if len(sys.argv) < 2: # Check there is an argument being passed
  59. print text # Display the help text if there isn't one passed
  60. sys.exit() # Exit the script
  61. if '-h' in sys.argv or '--h' in sys.argv or '-help' in sys.argv or '--help' in sys.argv: # If the ask for help
  62. print text # Display the help text if there isn't one passed
  63. sys.exit(0) # Exit the script after displaying help
  64. else:
  65. if sys.argv[1].lower().startswith('-site1'): # If the argument is site1
  66. query = 'site1' # Set the variable to have the value site
  67. elif sys.argv[1].lower().startswith('-site2'): # Else if the variable is bromley
  68. query = 'site2' # Set the variable to have the value bromley
  69. else:
  70. print '\n[-] Unknown option [-] ' + text # If an unknown option is passed, let the user know
  71. sys.exit(0)
  72. get_servers(query) # Call the get servers funtion, with the value from the argument
  73. if os.name == "posix": # If the OS is linux.
  74. linux() # Call the linux function
  75. elif os.name in ("nt", "dos", "ce"): # If the OS is Windows...
  76. windows() # Call the windows function
  77. print ('\n[+] Check the log file ' + outputfile + ' [+]\n') # Display the name of the log
  78. if __name__ == '__main__':
  79. main() # Call the main function