]>
Commit | Line | Data |
---|---|---|
a8f35baf CMB |
1 | #!/usr/bin/python |
2 | ||
3 | # We need python's URL getting functionality | |
4 | import urllib | |
5 | ||
6 | # Set a sane default timeout (for all socket connections unfortunately...) | |
7 | import socket | |
8 | socket.setdefaulttimeout(3) | |
9 | ||
10 | # for CSV support | |
11 | import csv | |
12 | ||
13 | # We ned sys in order to grab the argument to the script (which is the IP to connect to) | |
14 | import sys | |
15 | ||
16 | # We need os to delete files | |
17 | import os | |
18 | ||
19 | # random for the temporary file created | |
20 | import random | |
21 | ||
22 | # The web server IP to query | |
23 | WebServer = sys.argv[1] | |
24 | ||
25 | # Web server Port is the second argument | |
26 | Port = sys.argv[2] | |
27 | ||
28 | # Metric the user is asking for | |
29 | RequestedMetric = sys.argv[3] | |
30 | ||
31 | # URL is hard coded to /server-status?auto but could be different in the future if needed | |
32 | URL = "/server-status?auto" | |
33 | ||
34 | ############### Function to fire off an HTTP request to the web server, throw an exception gracefully | |
35 | ############### and print FAIL if a connection can't be made | |
36 | def getURL(WebServer,Port,URL): | |
37 | try: | |
38 | # Setup connection string | |
39 | ConnectionString = ("http://%s:%s%s") % (WebServer, Port, URL) | |
40 | ||
41 | conn = urllib.urlopen(ConnectionString) | |
42 | URLresponse = conn.read() | |
43 | ||
44 | # Clean up the connection | |
45 | conn.close() | |
46 | ||
47 | # The response to the function is the output of the URL called | |
48 | return URLresponse | |
49 | ||
50 | # Catch all exceptions | |
51 | except: | |
52 | print "Error getting URL" | |
53 | ||
54 | ######################################################################## | |
55 | ### This function deals with "ordinary" metrics, such as CPULoad etc ### | |
56 | ######################################################################## | |
57 | def GetMetric(RequestedMetric): | |
58 | if RequestedMetric == "TotalAccesses": | |
59 | return ServerStatusOutput[0][1] | |
60 | ||
61 | if RequestedMetric == "TotalkBytes": | |
62 | return ServerStatusOutput[1][1] | |
63 | ||
64 | if RequestedMetric == "CPULoad": | |
65 | return ServerStatusOutput[2][1] | |
66 | ||
67 | if RequestedMetric == "ReqPerSec": | |
68 | return ServerStatusOutput[4][1] | |
69 | ||
70 | if RequestedMetric == "BytesPerSec": | |
71 | return ServerStatusOutput[5][1] | |
72 | ||
73 | if RequestedMetric == "BytesPerReq": | |
74 | return ServerStatusOutput[6][1] | |
75 | ||
76 | if RequestedMetric == "BusyWorkers": | |
77 | return ServerStatusOutput[7][1] | |
78 | ||
79 | if RequestedMetric == "IdleWorkers": | |
80 | return ServerStatusOutput[8][1] | |
81 | ||
82 | ################################################################### | |
83 | ### This function deals with specifically the Apache scoreboard ### | |
84 | ################################################################### | |
85 | ||
86 | # function to count the metric requested... used in every if statement | |
87 | def GetScoreboardMetric(RequestedMetric): | |
88 | # initialize counter variable | |
89 | RequestedMetricCount = 0 | |
90 | ||
91 | # iterate over the ScoreBoard part and count the number of the requested metric | |
92 | for CountMetric in ServerStatusOutput[9][1]: | |
93 | if CountMetric == RequestedMetric: | |
94 | RequestedMetricCount = RequestedMetricCount + 1 | |
95 | return RequestedMetricCount | |
96 | ||
97 | # Scoreboard Key: | |
98 | # "_" Waiting for Connection, | |
99 | # "S" Starting up, | |
100 | # "R" Reading Request, | |
101 | # "W" Sending Reply, | |
102 | # "K" Keepalive (read), | |
103 | # "D" DNS Lookup, | |
104 | # "C" Closing connection, | |
105 | # "L" Logging, | |
106 | # "G" Gracefully finishing, | |
107 | # "I" Idle cleanup of worker, | |
108 | # "." Open slot with no current process | |
109 | ||
110 | ####################### End of function definitions ############################ | |
111 | ||
112 | #### Main body of script - Apache status is parsed and split into a list #### | |
113 | RandomInt = random.randint(1, 1000) | |
114 | TemporaryFileName = ("/tmp/%s.txt") % RandomInt | |
115 | TemporaryFile = open(TemporaryFileName, 'a') | |
116 | TemporaryFile.write(getURL(WebServer,Port,URL)) | |
117 | ||
118 | # Parse the CSV file we just wrote | |
119 | CSVReader = csv.reader(open(TemporaryFileName, "rb"), delimiter = ":", skipinitialspace=True) | |
120 | ||
121 | # Close the file handle and delete the file, to clean up because we're done with it at this point | |
122 | TemporaryFile.close() | |
123 | os.remove(TemporaryFileName) | |
124 | ||
125 | # Turn the split CSV into a two dimensional list | |
126 | ServerStatusOutput = [] | |
127 | for Metric in CSVReader: | |
128 | ServerStatusOutput.append(Metric) | |
129 | ||
130 | # if the last argument to the script is more than two characters, this means an "ordinary" metric was asked for | |
131 | if len(RequestedMetric) > 2: | |
132 | print GetMetric(RequestedMetric) | |
133 | ||
134 | # Otherwise print the Scoreboard specific metric | |
135 | else: | |
136 | print GetScoreboardMetric(RequestedMetric) |