-
Notifications
You must be signed in to change notification settings - Fork 1
/
status_apache.py
executable file
·253 lines (230 loc) · 8.66 KB
/
status_apache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/python
""" Fetch Apache stats via mod_status and send to Zabbix
By Paulson McIntyre
Patches by:
Zach Bailey <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import urllib
from optparse import OptionParser
import os
from tempfile import mkstemp
import StringIO
import csv
class ErrorSendingValues(RuntimeError):
""" An error occured while sending the values to the Zabbix
server using zabbix_sender.
"""
def fetchURL(url, user = None, passwd = None):
""" Return the data from a URL """
if user and passwd:
parts = url.split('://')
url = parts[0] + "://" + user + ":" + passwd + "@" + parts[1]
conn = urllib.urlopen(url)
try:
data = conn.read()
finally:
conn.close()
return data
def sendValues(filepath, zabbixserver = "localhost", zabbixport = 10051, senderloc = "zabbix_sender", host = "localhost"):
os.system("cat %s" % (filepath))
cmd = "%s --zabbix-server '%s' --port '%s' --input-file '%s' --host '%s' -vv" % (senderloc, zabbixserver, zabbixport, filepath, host)
print cmd
r = os.system(cmd)
if r != 0:
raise ErrorSendingValues, "An error occured sending the values to the server"
def clean(string, chars):
for i in chars:
string = string.replace(i, '')
return string
def parse(data):
""" Parse the CSV file into a dict of data
"""
mapping = {
"_":"Waiting for Connection",
"S":"Starting up",
"R":"Reading Request",
"W":"Sending Reply",
"K":"Keepalive (read)",
"D":"DNS Lookup",
"C":"Closing connection",
"L":"Logging",
"G":"Gracefully finishing",
"I":"Idle cleanup of worker",
".":"Open slot with no current process",
}
# Clean out certian chars
replace = '() '
csvobj = csv.reader(StringIO.StringIO(data), delimiter = ":", skipinitialspace = True)
ret = {}
for (key, val) in csvobj:
if key == 'Scoreboard':
sb = {
"Waiting for Connection":0,
"Starting up":0,
"Reading Request":0,
"Sending Reply":0,
"Keepalive (read)":0,
"DNS Lookup":0,
"Closing connection":0,
"Logging":0,
"Gracefully finishing":0,
"Idle cleanup of worker":0,
"Open slot with no current process":0,
}
for i in val:
sb[mapping[i]] += 1
ret[key] = sb
else:
ret[key] = val
ret2 = {}
for (key, val) in ret.items():
if key == "Scoreboard":
for (key, val) in val.items():
ret2[clean(key, replace)] = val
else:
ret2[clean(key, replace)] = val
print ret2
return ret2
if __name__ == "__main__":
parser = OptionParser(
usage = "%prog [-z <Zabbix hostname or IP>] [-o <Apache hostname or IP>]",
version = "%prog $Revision$",
prog = "ApacheStatsForZabbix",
description = """This program gathers data from Apache's
built-in status page and sends it to
Zabbix. The data is sent via zabbix_sender.
Author: Paulson McIntyre (GpMidi)
License: GPLv2
""",
)
parser.add_option(
"-l",
"--url",
action = "store",
type = "string",
dest = "url",
default = None,
help = "Override the automatically generated URL with one of your own",
)
parser.add_option(
"-o",
"--host",
action = "store",
type = "string",
dest = "host",
default = "localhost",
help = "Host to connect to. [default: %default]",
)
parser.add_option(
"-p",
"--port",
action = "store",
type = "int",
dest = "port",
default = 80,
help = "Port to connect on. [default: %default]",
)
parser.add_option(
"-r",
"--proto",
action = "store",
type = "string",
dest = "proto",
default = "http",
help = "Protocol to connect on. Can be http or https. [default: %default]",
)
parser.add_option(
"-z",
"--zabbixserver",
action = "store",
type = "string",
dest = "zabbixserver",
default = "localhost",
)
parser.add_option(
"-u",
"--user",
action = "store",
type = "string",
dest = "user",
default = None,
help = "HTTP authentication user to use when connection. [default: None]",
)
parser.add_option(
"-a",
"--passwd",
action = "store",
type = "string",
dest = "passwd",
default = None,
help = "HTTP authentication password to use when connecting. [default: None]",
)
parser.add_option(
"-s",
"--sender",
action = "store",
type = "string",
dest = "senderloc",
default = "/usr/bin/zabbix_sender",
help = "Location to the zabbix_sender executable. [default: %default]",
)
parser.add_option(
"-q",
"--zabbixport",
action = "store",
type = "int",
dest = "zabbixport",
default = 10051,
help = "Zabbix port to connect to. [default: %default]",
)
parser.add_option(
"-c",
"--zabbixsource",
action = "store",
type = "string",
dest = "zabbixsource",
default = "localhost",
help = "Zabbix host to use when sending values. [default: %default]",
)
(opts, args) = parser.parse_args()
if opts.url and (opts.port != 80 or opts.proto != "http"):
parser.error("Can't specify -u with -p or -r")
if not opts.url:
opts.url = "%s://%s:%s/server-status?auto" % (opts.proto, opts.host, opts.port)
data = fetchURL(opts.url, user = opts.user, passwd = opts.passwd)
try:
(tempfiled, tempfilepath) = mkstemp()
tempfile = open(tempfilepath, 'wb')
except:
parser.error("Error creating temporary file")
try:
try:
data = parse(data = data)
except csv.Error:
parser.error("Error parsing returned data")
try:
for key, val in data.items():
tempfile.write("%s apache.%s %s\n" % (opts.zabbixsource, key, val))
tempfile.close()
except "bogus":
parser.error("Error creating the file to send")
try:
sendValues(filepath = tempfilepath, zabbixserver = opts.zabbixserver, zabbixport = opts.zabbixport, senderloc = opts.senderloc, host = opts.host)
except ErrorSendingValues:
parser.error("An error occurred while sending values to the Zabbix server")
finally:
try:
tempfile.close()
except:
pass
os.remove(tempfilepath)