Debian Codegraph @

gmond/python_modules/apache_status/apache_status.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import os
5 import threading
6 import time
7 import urllib2
8 import traceback
9 import re
10 import copy
11 import sys
12 import socket
13
14 if sys.version_info < (2, 6):
15 socket.setdefaulttimeout(2)
16
17 # global to store state for "total accesses"
18 METRICS = {
19 'time': 0,
20 'data': {}
21 }
22
23 LAST_METRICS = copy.deepcopy(METRICS)
24 METRICS_CACHE_MAX = 5
25
26 #Metric prefix
27 NAME_PREFIX = "ap_"
28 SSL_NAME_PREFIX = "apssl_"
29
30 SERVER_STATUS_URL = ""
31
32 descriptors = list()
33 Desc_Skel = {}
34 Scoreboard = {
35 NAME_PREFIX + 'waiting' : {'key': '_', 'desc': 'Waiting for Connection'},
36 NAME_PREFIX + 'starting' : {'key': 'S', 'desc': 'Starting up'},
37 NAME_PREFIX + 'reading_request' : {'key': 'R', 'desc': 'Reading Request'},
38 NAME_PREFIX + 'sending_reply' : {'key': 'W', 'desc': 'Sending Reply'},
39 NAME_PREFIX + 'keepalive' : {'key': 'K', 'desc': 'Keepalive (read)'},
40 NAME_PREFIX + 'dns_lookup' : {'key': 'D', 'desc': 'DNS Lookup'},
41 NAME_PREFIX + 'closing' : {'key': 'C', 'desc': 'Closing connection'},
42 NAME_PREFIX + 'logging' : {'key': 'L', 'desc': 'Logging'},
43 NAME_PREFIX + 'gracefully_fin' : {'key': 'G', 'desc': 'Gracefully finishing'},
44 NAME_PREFIX + 'idle' : {'key': 'I', 'desc': 'Idle cleanup of worker'},
45 NAME_PREFIX + 'open_slot' : {'key': '.', 'desc': 'Open slot with no current process'},
46 }
47 Scoreboard_bykey = dict([(v["key"], k) for (k, v) in Scoreboard.iteritems()])
48
49 SSL_REGEX = re.compile('^(cache type:) (.*)(<b>)(?P<shared_mem>[0-9]+)(</b> bytes, current sessions: <b>)(?P<current_sessions>[0-9]+)(</b><br>subcaches: <b>)(?P<num_subcaches>[0-9]+)(</b>, indexes per subcache: <b>)(?P<indexes_per_subcache>[0-9]+)(</b><br>)(.*)(<br>index usage: <b>)(?P<index_usage>[0-9]+)(%</b>, cache usage: <b>)(?P<cache_usage>[0-9]+)(%</b><br>total sessions stored since starting: <b>)(?P<sessions_stored>[0-9]+)(</b><br>total sessions expired since starting: <b>)(?P<sessions_expired>[0-9]+)(</b><br>total \(pre-expiry\) sessions scrolled out of the cache: <b>)(?P<sessions_scrolled_outof_cache>[0-9]+)(</b><br>total retrieves since starting: <b>)(?P<retrieves_hit>[0-9]+)(</b> hit, <b>)(?P<retrieves_miss>[0-9]+)(</b> miss<br>total removes since starting: <b>)(?P<removes_hit>[0-9]+)(</b> hit, <b>)(?P<removes_miss>[0-9]+)')
50 # Good for Apache 2.2
51 #SSL_REGEX = re.compile('^(cache type:) (.*)(<b>)(?P<shared_mem>[0-9]+)(</b> bytes, current sessions: <b>)(?P<current_sessions>[0-9]+)(</b><br>subcaches: <b>)(?P<num_subcaches>[0-9]+)(</b>, indexes per subcache: <b>)(?P<indexes_per_subcache>[0-9]+)(</b><br>index usage: <b>)(?P<index_usage>[0-9]+)(%</b>, cache usage: <b>)(?P<cache_usage>[0-9]+)(%</b><br>total sessions stored since starting: <b>)(?P<sessions_stored>[0-9]+)(</b><br>total sessions expired since starting: <b>)(?P<sessions_expired>[0-9]+)(</b><br>total \(pre-expiry\) sessions scrolled out of the cache: <b>)(?P<sessions_scrolled_outof_cache>[0-9]+)(</b><br>total retrieves since starting: <b>)(?P<retrieves_hit>[0-9]+)(</b> hit, <b>)(?P<retrieves_miss>[0-9]+)(</b> miss<br>total removes since starting: <b>)(?P<removes_hit>[0-9]+)(</b> hit, <b>)(?P<removes_miss>[0-9]+)')
52
53
54 Metric_Map = {
55 'Uptime' : NAME_PREFIX + "uptime",
56 'IdleWorkers' : NAME_PREFIX + "idle_workers",
57 'BusyWorkers' : NAME_PREFIX + "busy_workers",
58 'Total kBytes' : NAME_PREFIX + "bytes",
59 'CPULoad' : NAME_PREFIX + "cpuload",
60 "Total Accesses" : NAME_PREFIX + "rps"
61 }
62
63
64 def get_metrics():
65
66 global METRICS, LAST_METRICS, SERVER_STATUS_URL, COLLECT_SSL
67
68 if (time.time() - METRICS['time']) > METRICS_CACHE_MAX:
69
70 metrics = dict([(k, 0) for k in Scoreboard.keys()])
71
72 # This is the short server-status. Lacks SSL metrics
73 try:
74 req = urllib2.Request(SERVER_STATUS_URL + "?auto")
75
76 # Download the status file
77 if sys.version_info < (2, 6):
78 res = urllib2.urlopen(req)
79 else:
80 res = urllib2.urlopen(req, timeout=2)
81
82 for line in res:
83 split_line = line.rstrip().split(": ")
84 long_metric_name = split_line[0]
85 if long_metric_name == "Scoreboard":
86 for sck in split_line[1]:
87 metrics[ Scoreboard_bykey[sck] ] += 1
88 else:
89 if long_metric_name in Metric_Map:
90 metric_name = Metric_Map[long_metric_name]
91 else:
92 metric_name = long_metric_name
93 metrics[metric_name] = split_line[1]
94
95 except urllib2.URLError:
96 traceback.print_exc()
97
98 # If we are collecting SSL metrics we'll do
99 if COLLECT_SSL:
100
101 try:
102 req2 = urllib2.Request(SERVER_STATUS_URL)
103
104 # Download the status file
105 if sys.version_info < (2, 6):
106 res = urllib2.urlopen(req2)
107 else:
108 res = urllib2.urlopen(req2, timeout=2)
109
110 for line in res:
111 regMatch = SSL_REGEX.match(line)
112 if regMatch:
113 linebits = regMatch.groupdict()
114 for key in linebits:
115 #print SSL_NAME_PREFIX + key + "=" + linebits[key]
116 metrics[SSL_NAME_PREFIX + key] = linebits[key]
117
118 except urllib2.URLError:
119 traceback.print_exc()
120
121 LAST_METRICS = copy.deepcopy(METRICS)
122 METRICS = {
123 'time': time.time(),
124 'data': metrics
125 }
126
127 return [METRICS, LAST_METRICS]
128
129
130 def get_value(name):
131 """Return a value for the requested metric"""
132
133 metrics = get_metrics()[0]
134
135 try:
136 result = metrics['data'][name]
137 except StandardError:
138 result = 0
139
140 return result
141
142
143 def get_delta(name):
144 """Return change over time for the requested metric"""
145
146 # get metrics
147 [curr_metrics, last_metrics] = get_metrics()
148
149 # If it's ap_bytes metric multiply result by 1024
150 if name == NAME_PREFIX + "bytes":
151 multiplier = 1024
152 else:
153 multiplier = 1
154
155 try:
156 delta = multiplier * (float(curr_metrics['data'][name]) - float(last_metrics['data'][name])) / (curr_metrics['time'] - last_metrics['time'])
157 if delta < 0:
158 print name + " is less 0"
159 delta = 0
160 except KeyError:
161 delta = 0.0
162
163 return delta
164
165
166 def create_desc(prop):
167 d = Desc_Skel.copy()
168 for k, v in prop.iteritems():
169 d[k] = v
170 return d
171
172
173 def metric_init(params):
174 global descriptors, Desc_Skel, SERVER_STATUS_URL, COLLECT_SSL
175
176 print '[apache_status] Received the following parameters'
177 print params
178
179 if "metric_group" not in params:
180 params["metric_group"] = "apache"
181
182 Desc_Skel = {
183 'name' : 'XXX',
184 'call_back' : get_value,
185 'time_max' : 60,
186 'value_type' : 'uint',
187 'units' : 'proc',
188 'slope' : 'both',
189 'format' : '%d',
190 'description' : 'XXX',
191 'groups' : params["metric_group"],
192 }
193
194 if "refresh_rate" not in params:
195 params["refresh_rate"] = 15
196
197 if "url" not in params:
198 params["url"] = "http://localhost:7070/server-status"
199
200 if "collect_ssl" not in params:
201 params["collect_ssl"] = False
202
203 SERVER_STATUS_URL = params["url"]
204 COLLECT_SSL = params["collect_ssl"]
205
206 # IP:HOSTNAME
207 if "spoof_host" in params:
208 Desc_Skel["spoof_host"] = params["spoof_host"]
209
210 descriptors.append(create_desc({
211 "name" : NAME_PREFIX + "rps",
212 "value_type" : "float",
213 "units" : "req/sec",
214 "call_back" : get_delta,
215 "format" : "%.3f",
216 "description": "request per second",
217 }))
218
219 descriptors.append(create_desc({
220 "name" : NAME_PREFIX + "bytes",
221 "value_type" : "float",
222 "units" : "bytes/sec",
223 "call_back" : get_delta,
224 "format" : "%.3f",
225 "description": "bytes transferred per second",
226 }))
227
228 descriptors.append(create_desc({
229 "name" : NAME_PREFIX + "cpuload",
230 "value_type" : "float",
231 "units" : "pct",
232 "format" : "%.6f",
233 "call_back" : get_value,
234 "description": "Pct of time CPU utilized",
235 }))
236
237 descriptors.append(create_desc({
238 "name" : NAME_PREFIX + "busy_workers",
239 "value_type" : "uint",
240 "units" : "threads",
241 "format" : "%u",
242 "call_back" : get_value,
243 "description": "Busy threads",
244 }))
245
246 descriptors.append(create_desc({
247 "name" : NAME_PREFIX + "idle_workers",
248 "value_type" : "uint",
249 "units" : "threads",
250 "format" : "%u",
251 "call_back" : get_value,
252 "description": "Idle threads",
253 }))
254
255 descriptors.append(create_desc({
256 "name" : NAME_PREFIX + "uptime",
257 "value_type" : "uint",
258 "units" : "seconds",
259 "format" : "%u",
260 "call_back" : get_value,
261 "description": "Uptime",
262 }))
263
264 for k, v in Scoreboard.iteritems():
265 descriptors.append(create_desc({
266 "name" : k,
267 "call_back" : get_value,
268 "description" : v["desc"],
269 }))
270
271 ##########################################################################
272 # SSL metrics
273 ##########################################################################
274 if params['collect_ssl']:
275
276 descriptors.append(create_desc({
277 "name" : SSL_NAME_PREFIX + "shared_mem",
278 "value_type" : "float",
279 "units" : "bytes",
280 "format" : "%.3f",
281 "call_back" : get_value,
282 "description": "Shared memory",
283 }))
284
285 descriptors.append(create_desc({
286 "name" : SSL_NAME_PREFIX + "current_sessions",
287 "value_type" : "uint",
288 "units" : "sessions",
289 "format" : "%u",
290 "call_back" : get_value,
291 "description": "Current sessions",
292 }))
293
294 descriptors.append(create_desc({
295 "name" : SSL_NAME_PREFIX + "num_subcaches",
296 "value_type" : "uint",
297 "units" : "subcaches",
298 "format" : "%u",
299 "call_back" : get_value,
300 "description": "Number of subcaches",
301 }))
302
303 descriptors.append(create_desc({
304 "name" : SSL_NAME_PREFIX + "indexes_per_subcache",
305 "value_type" : "float",
306 "units" : "indexes",
307 "format" : "%.3f",
308 "call_back" : get_value,
309 "description": "Subcaches",
310 }))
311
312 descriptors.append(create_desc({
313 "name" : SSL_NAME_PREFIX + "index_usage",
314 "value_type" : "float",
315 "units" : "pct",
316 "format" : "%.3f",
317 "call_back" : get_value,
318 "description": "Index usage",
319 }))
320
321 descriptors.append(create_desc({
322 "name" : SSL_NAME_PREFIX + "cache_usage",
323 "value_type" : "float",
324 "units" : "pct",
325 "format" : "%.3f",
326 "call_back" : get_value,
327 "description": "Cache usage",
328 }))
329
330 descriptors.append(create_desc({
331 "name" : SSL_NAME_PREFIX + "sessions_stored",
332 "value_type" : "float",
333 "units" : "sessions/sec",
334 "format" : "%.3f",
335 "call_back" : get_delta,
336 "description": "Sessions stored",
337 }))
338
339 descriptors.append(create_desc({
340 "name" : SSL_NAME_PREFIX + "sessions_expired",
341 "value_type" : "float",
342 "units" : "sessions/sec",
343 "format" : "%.3f",
344 "call_back" : get_delta,
345 "description": "Sessions expired",
346 }))
347
348 descriptors.append(create_desc({
349 "name" : SSL_NAME_PREFIX + "retrieves_hit",
350 "value_type" : "float",
351 "units" : "retrieves/sec",
352 "format" : "%.3f",
353 "call_back" : get_delta,
354 "description": "Retrieves Hit",
355 }))
356
357 descriptors.append(create_desc({
358 "name" : SSL_NAME_PREFIX + "retrieves_miss",
359 "value_type" : "float",
360 "units" : "retrieves/sec",
361 "format" : "%.3f",
362 "call_back" : get_delta,
363 "description": "Retrieves Miss",
364 }))
365
366 descriptors.append(create_desc({
367 "name" : SSL_NAME_PREFIX + "removes_hit",
368 "value_type" : "float",
369 "units" : "removes/sec",
370 "format" : "%.3f",
371 "call_back" : get_delta,
372 "description": "Removes Hit",
373 }))
374
375 descriptors.append(create_desc({
376 "name" : SSL_NAME_PREFIX + "removes_miss",
377 "value_type" : "float",
378 "units" : "removes/sec",
379 "format" : "%.3f",
380 "call_back" : get_delta,
381 "description": "Removes Miss",
382 }))
383
384 return descriptors
385
386
387 def metric_cleanup():
388 '''Clean up the metric module.'''
389 pass
390
391
392 if __name__ == '__main__':
393 try:
394 params = {
395 'url' : 'http://localhost:7070/server-status',
396 'collect_ssl' : False
397 }
398 metric_init(params)
399 while True:
400 for d in descriptors:
401 v = d['call_back'](d['name'])
402 if d['name'] == NAME_PREFIX + "rps":
403 print 'value for %s is %.4f' % (d['name'], v)
404 else:
405 print 'value for %s is %s' % (d['name'], v)
406 time.sleep(15)
407 except KeyboardInterrupt:
408 os._exit(1)