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