]> git.uio.no Git - uio-zabbix.git/blame - query-apachestats.py
Add zabbix autodiscovery script for rabbitmq
[uio-zabbix.git] / query-apachestats.py
CommitLineData
a8f35baf
CMB
1#!/usr/bin/python
2
3# We need python's URL getting functionality
4import urllib
5
6# Set a sane default timeout (for all socket connections unfortunately...)
7import socket
8socket.setdefaulttimeout(3)
9
10# for CSV support
11import csv
12
13# We ned sys in order to grab the argument to the script (which is the IP to connect to)
14import sys
15
16# We need os to delete files
17import os
18
19# random for the temporary file created
20import random
21
22# The web server IP to query
23WebServer = sys.argv[1]
24
25# Web server Port is the second argument
26Port = sys.argv[2]
27
28# Metric the user is asking for
29RequestedMetric = sys.argv[3]
30
31# URL is hard coded to /server-status?auto but could be different in the future if needed
32URL = "/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
36def 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########################################################################
57def 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
87def 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 ####
113RandomInt = random.randint(1, 1000)
114TemporaryFileName = ("/tmp/%s.txt") % RandomInt
115TemporaryFile = open(TemporaryFileName, 'a')
116TemporaryFile.write(getURL(WebServer,Port,URL))
117
118# Parse the CSV file we just wrote
119CSVReader = 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
122TemporaryFile.close()
123os.remove(TemporaryFileName)
124
125# Turn the split CSV into a two dimensional list
126ServerStatusOutput = []
127for 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
131if len(RequestedMetric) > 2:
132 print GetMetric(RequestedMetric)
133
134# Otherwise print the Scoreboard specific metric
135else:
136 print GetScoreboardMetric(RequestedMetric)