Skip to content

Commit 788335b

Browse files
committed
This script will, depending on the arguments supplied will ping the servers associated with that application group.
1 parent 5486857 commit 788335b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

ping_servers.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Script Name : ping_servers.py
2+
# Author : Craig Richards
3+
# Created : 9th May 2012
4+
# Last Modified : 14th May 2012
5+
# Version : 1.1
6+
7+
# Modifications : 1.1 - 14th May 2012 - CR Changed it to use the config directory to store the server files
8+
9+
# Description : This script will, depending on the arguments supplied will ping the servers associated with that application group.
10+
11+
import os # Load the Library Module
12+
import subprocess # Load the Library Module
13+
import sys # Load the Library Module
14+
15+
if '-h' in sys.argv or '--h' in sys.argv or '-help' in sys.argv or '--help' in sys.argv: # Help Menu if called
16+
print '''
17+
You need to supply the application group for the servers you want to ping, i.e.
18+
dms
19+
swaps
20+
21+
Followed by the site i.e.
22+
155
23+
bromley'''
24+
sys.exit(0)
25+
else:
26+
27+
if (len(sys.argv) < 3): # If no arguments are passed,display the help/instructions on how to run the script
28+
sys.exit ('\nYou need to supply the app group. Usage : ' + filename + ' followed by the application group i.e. \n \t dms or \n \t swaps \n then the site i.e. \n \t 155 or \n \t bromley')
29+
30+
appgroup = sys.argv[1] # Set the variable appgroup as the first argument you supply
31+
site = sys.argv[2] # Set the variable site as the second argument you supply
32+
33+
if os.name == "posix": # Check the os, if it's linux then
34+
myping = "ping -c 2 " # This is the ping command
35+
elif os.name in ("nt", "dos", "ce"): # Check the os, if it's windows then
36+
myping = "ping -n 2 " # This is the ping command
37+
38+
if 'dms' in sys.argv: # If the argument passed is dms then
39+
appgroup = 'dms' # Set the variable appgroup to dms
40+
elif 'swaps' in sys.argv: # Else if the argment passed is swaps then
41+
appgroup = 'swaps' # Set the variable appgroup to swaps
42+
43+
if '155' in sys.argv: # If the argument passed is 155 then
44+
site = '155' # Set the variable site to 155
45+
elif 'bromley' in sys.argv: # Else if the argument passed is bromley
46+
site = 'bromley' # Set the variable site to bromley
47+
48+
filename = sys.argv[0] # Sets a variable for the script name
49+
logdir = os.getenv("logs") # Set the variable logdir by getting the OS environment logs
50+
logfile = 'ping_'+appgroup+'_'+site+'.log' # Set the variable logfile, using the arguments passed to create the logfile
51+
logfilename=os.path.join(logdir, logfile) # Set the variable logfilename by joining logdir and logfile together
52+
confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable - 1.2
53+
conffile = (appgroup+'_servers_'+site+'.txt') # Set the variable conffile - 1.2
54+
conffilename=os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together - 1.2
55+
56+
f = open(logfilename, "w") # Open a logfile to write out the output
57+
for server in open(conffilename): # Open the config file and read each line - 1.2
58+
ret = subprocess.call(myping + server, shell=True,stdout=f,stderr=subprocess.STDOUT) # Run the ping command for each server in the list.
59+
if ret == 0: # Depending on the response
60+
f.write (server.strip() + " is alive" + "\n") # Write out that you can receive a reponse
61+
else:
62+
f.write (server.strip() + " did not respond" + "\n") # Write out you can't reach the box
63+
64+
print ("\n\tYou can see the results in the logfile : "+ logfilename); # Show the location of the logfile

0 commit comments

Comments
 (0)