forked from ganglia/gmond_python_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquid.py
More file actions
496 lines (463 loc) · 17 KB
/
squid.py
File metadata and controls
496 lines (463 loc) · 17 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
"""
Copyright (c)2012 Daniel Rich <drich@employees.org>
This module will query an squid server via SNMP for metrics
"""
import sys
import os
import re
import time
#import logging
#logging.basicConfig(level=logging.ERROR, format="%(asctime)s - %(name)s - %(levelname)s\t Thread-%(thread)d - %(message)s", filename='/tmp/gmond.log', filemode='w')
#logging.debug('starting up')
last_update = 0
# We get counter values back, so we have to calculate deltas for some stats
squid_stats = {}
squid_stats_last = {}
MIN_UPDATE_INTERVAL = 30 # Minimum update interval in seconds
def collect_stats():
#logging.debug('collect_stats()')
global last_update
global squid_stats, squid_stats_last
now = time.time()
if now - last_update < MIN_UPDATE_INTERVAL:
#logging.debug(' wait ' + str(int(MIN_UPDATE_INTERVAL - (now - last_update))) + ' seconds')
return True
else:
elapsed_time = now - last_update
last_update = now
squid_stats = {}
# Run squidclient mgr:info to get stats
try:
stats = {}
squidclient = os.popen("squidclient mgr:info")
except IOError,e:
#logging.error('error running squidclient')
return False
# Parse output, splitting everything into key/value pairs
rawstats = {}
for stat in squidclient.readlines():
stat = stat.strip()
if stat.find(':') >= 0:
[key,value] = stat.split(':',1)
if value: # Toss things with no value
value = value.lstrip()
rawstats[key] = value
else:
match = re.search("(\d+)\s+(.*)$",stat) # reversed "value key" line
if match:
rawstats[match.group(2)] = match.group(1)
# Use stats_descriptions to convert raw stats to real metrics
for metric in stats_descriptions:
if stats_descriptions[metric].has_key('key'):
if rawstats.has_key(stats_descriptions[metric]['key']):
rawstat = rawstats[stats_descriptions[metric]['key']]
if stats_descriptions[metric].has_key('match'):
match = re.match(stats_descriptions[metric]['match'],rawstat)
if match:
rawstat = match.group(1)
squid_stats[metric] = rawstat
else:
squid_stats[metric] = rawstat
if squid_stats.has_key(metric): # Strip trailing non-num text
if metric != 'cacheVersionId': # version is special case
match = re.match('([0-9.]+)',squid_stats[metric]);
squid_stats[metric] = float(match.group(1))
if stats_descriptions[metric]['type'] == 'integer':
squid_stats[metric] = int(squid_stats[metric])
# Calculate delta for counter stats
if metric in squid_stats_last:
if stats_descriptions[metric]['type'] == 'counter32':
current = squid_stats[metric]
squid_stats[metric] = (squid_stats[metric] - squid_stats_last[metric]) / float(elapsed_time)
squid_stats_last[metric] = current
else:
squid_stats_last[metric] = squid_stats[metric]
else:
if metric in squid_stats:
squid_stats_last[metric] = squid_stats[metric]
#logging.debug('collect_stats done')
#logging.debug('squid_stats: ' + str(squid_stats))
def get_stat(name):
#logging.info("get_stat(%s)" % name)
global squid_stats
ret = collect_stats()
if ret:
if name.startswith('squid_'):
label = name[6:]
else:
lable = name
#logging.debug("fetching %s" % label)
try:
#logging.info("got %4.2f" % squid_stats[label])
return squid_stats[label]
except:
#logging.error("failed to fetch %s" % name)
return 0
else:
return 0
def metric_init(params):
global descriptors
global squid_stats
global stats_descriptions # needed for stats extraction in collect_stat()
#logging.debug("init: " + str(params))
stats_descriptions = dict(
cacheVersionId = {
'description': 'Cache Software Version',
'units': 'N/A',
'type': 'string',
'key': 'Squid Object Cache',
},
cacheSysVMsize = {
'description': 'Storage Mem size in KB',
'units': 'KB',
'type': 'integer',
'key': 'Storage Mem size',
},
cacheMemUsage = {
'description': 'Total memory accounted for KB',
'units': 'KB',
'type': 'integer',
'key': 'Total accounted',
},
cacheSysPageFaults = {
'description': 'Page faults with physical i/o',
'units': 'faults/s',
'type': 'counter32',
'key': 'Page faults with physical i/o',
},
cacheCpuTime = {
'description': 'Amount of cpu seconds consumed',
'units': 'seconds',
'type': 'integer',
'key': 'CPU Time',
},
cacheCpuUsage = {
'description': 'The percentage use of the CPU',
'units': 'percent',
'type': 'float',
'key': 'CPU Usage',
},
cacheCpuUsage_5 = {
'description': 'The percentage use of the CPU - 5 min',
'units': 'percent',
'type': 'float',
'key': 'CPU Usage, 5 minute avg',
},
cacheCpuUsage_60 = {
'description': 'The percentage use of the CPU - 60 min',
'units': 'percent',
'type': 'float',
'key': 'CPU Usage, 60 minute avg',
},
cacheMaxResSize = {
'description': 'Maximum Resident Size in KB',
'units': 'KB',
'type': 'integer',
'key': 'Maximum Resident Size',
},
cacheNumObjCount = {
'description': 'Number of objects stored by the cache',
'units': 'objects',
'type': 'integer',
'key': 'StoreEntries',
},
cacheNumObjCountMemObj = {
'description': 'Number of memobjects stored by the cache',
'units': 'objects',
'type': 'integer',
'key': 'StoreEntries with MemObjects',
},
cacheNumObjCountHot = {
'description': 'Number of hot objects stored by the cache',
'units': 'objects',
'type': 'integer',
'key': 'Hot Object Cache Items',
},
cacheNumObjCountOnDisk = {
'description': 'Number of objects stored by the cache on-disk',
'units': 'objects',
'type': 'integer',
'key': 'on-disk objects',
},
cacheCurrentUnusedFDescrCnt = {
'description': 'Available number of file descriptors',
'units': 'file descriptors',
'type': 'gauge32',
'key': 'Maximum number of file descriptors',
},
cacheCurrentResFileDescrCnt = {
'description': 'Reserved number of file descriptors',
'units': 'file descriptors',
'type': 'gauge32',
'key': 'Reserved number of file descriptors',
},
cacheCurrentFileDescrCnt = {
'description': 'Number of file descriptors in use',
'units': 'file descriptors',
'type': 'gauge32',
'key': 'Number of file desc currently in use',
},
cacheCurrentFileDescrMax = {
'description': 'Highest file descriptors in use',
'units': 'file descriptors',
'type': 'gauge32',
'key': 'Largest file desc currently in use',
},
cacheProtoClientHttpRequests = {
'description': 'Number of HTTP requests received',
'units': 'requests/s',
'type': 'counter32',
'key': 'Number of HTTP requests received'
},
cacheIcpPktsSent = {
'description': 'Number of ICP messages sent',
'units': 'messages/s',
'type': 'counter32',
'key': 'Number of ICP messages sent',
},
cacheIcpPktsRecv = {
'description': 'Number of ICP messages received',
'units': 'messages/s',
'type': 'counter32',
'key': 'Number of ICP messages received',
},
cacheCurrentSwapSize = {
'description': 'Storage Swap size',
'units': 'KB',
'type': 'gauge32',
'key': 'Storage Swap size',
},
cacheClients = {
'description': 'Number of clients accessing cache',
'units': 'clients',
'type': 'gauge32',
'key': 'Number of clients accessing cache',
},
cacheHttpAllSvcTime_5 = {
'description': 'HTTP all service time - 5 min',
'units': 'seconds',
'type': 'float',
'key': 'HTTP Requests (All)',
'match': '([0-9.]+)',
},
cacheHttpAllSvcTime_60 = {
'description': 'HTTP all service time - 60 min',
'units': 'seconds',
'type': 'float',
'key': 'HTTP Requests (All)',
'match': '[0-9.]+\s+([0-9.]+)',
},
cacheHttpMissSvcTime_5 = {
'description': 'HTTP miss service time - 5 min',
'units': 'seconds',
'type': 'float',
'key': 'Cache Misses',
'match': '([0-9.]+)',
},
cacheHttpMissSvcTime_60 = {
'description': 'HTTP miss service time - 60 min',
'units': 'seconds',
'type': 'float',
'key': 'Cache Misses',
'match': '[0-9.]+\s+([0-9.]+)',
},
cacheHttpNmSvcTime_5 = {
'description': 'HTTP hit not-modified service time - 5 min',
'units': 'seconds',
'type': 'float',
'key': 'Not-Modified Replies',
'match': '([0-9.]+)',
},
cacheHttpNmSvcTime_60 = {
'description': 'HTTP hit not-modified service time - 60 min',
'units': 'seconds',
'type': 'float',
'key': 'Not-Modified Replies',
'match': '[0-9.]+\s+([0-9.]+)',
},
cacheHttpHitSvcTime_5 = {
'description': 'HTTP hit service time - 5 min',
'units': 'seconds',
'type': 'float',
'key': 'Cache Hits',
'match': '([0-9.]+)',
},
cacheHttpHitSvcTime_60 = {
'description': 'HTTP hit service time - 60 min',
'units': 'seconds',
'type': 'float',
'key': 'Cache Hits',
'match': '[0-9.]+\s+([0-9.]+)',
},
cacheIcpQuerySvcTime_5 = {
'description': 'ICP query service time - 5 min',
'units': 'seconds',
'type': 'float',
'key': 'ICP Queries',
'match': '([0-9.]+)',
},
cacheIcpQuerySvcTime_60 = {
'description': 'ICP query service time - 60 min',
'units': 'seconds',
'type': 'float',
'key': 'ICP Queries',
'match': '[0-9.]+\s+([0-9.]+)',
},
cacheDnsSvcTime_5 = {
'description': 'DNS service time - 5 min',
'units': 'seconds',
'type': 'float',
'key': 'DNS Lookups',
'match': '([0-9.]+)',
},
cacheDnsSvcTime_60 = {
'description': 'DNS service time - 60 min',
'units': 'seconds',
'type': 'float',
'key': 'DNS Lookups',
'match': '[0-9.]+\s+([0-9.]+)',
},
cacheRequestHitRatio_5 = {
'description': 'Request Hit Ratios - 5 min',
'units': 'percent',
'type': 'float',
'key': 'Request Hit Ratios',
'match': '5min: ([0-9.]+)%',
},
cacheRequestHitRatio_60 = {
'description': 'Request Hit Ratios - 60 min',
'units': 'percent',
'type': 'float',
'key': 'Request Hit Ratios',
'match': '5min: [0-9.]+%,\s+60min: ([0-9.]+)%',
},
cacheRequestByteRatio_5 = {
'description': 'Byte Hit Ratios - 5 min',
'units': 'percent',
'type': 'float',
'key': 'Byte Hit Ratios',
'match': '5min: ([0-9.]+)%',
},
cacheRequestByteRatio_60 = {
'description': 'Byte Hit Ratios - 60 min',
'units': 'percent',
'type': 'float',
'key': 'Byte Hit Ratios',
'match': '5min: [0-9.]+%,\s+60min: ([0-9.]+)%',
},
cacheRequestMemRatio_5 = {
'description': 'Memory Hit Ratios - 5 min',
'units': 'percent',
'type': 'float',
'key': 'Request Memory Hit Ratios',
'match': '5min: ([0-9.]+)%',
},
cacheRequestMemRatio_60 = {
'description': 'Memory Hit Ratios - 60 min',
'units': 'percent',
'type': 'float',
'key': 'Request Memory Hit Ratios',
'match': '5min: [0-9.]+%,\s+60min: ([0-9.]+)%',
},
cacheRequestDiskRatio_5 = {
'description': 'Disk Hit Ratios - 5 min',
'units': 'percent',
'type': 'float',
'key': 'Request Disk Hit Ratios',
'match': '5min: ([0-9.]+)%',
},
cacheRequestDiskRatio_60 = {
'description': 'Disk Hit Ratios - 60 min',
'units': 'percent',
'type': 'float',
'key': 'Request Disk Hit Ratios',
'match': '5min: [0-9.]+%,\s+60min: ([0-9.]+)%',
},
cacheHttpNhSvcTime_5 = {
'description': 'HTTP refresh hit service time - 5 min',
'units': 'seconds',
'type': 'float',
'key': 'Near Hits',
'match': '([0-9.]+)',
},
cacheHttpNhSvcTime_60 = {
'description': 'HTTP refresh hit service time - 60 min',
'units': 'seconds',
'type': 'float',
'key': 'Near Hits',
'match': '[0-9.]+\s+([0-9.]+)',
},
)
descriptors = []
collect_stats()
time.sleep(MIN_UPDATE_INTERVAL)
collect_stats()
for label in stats_descriptions:
if squid_stats.has_key(label):
if stats_descriptions[label]['type'] == 'string':
d= {
'name': 'squid_' + label,
'call_back': get_stat,
'time_max': 60,
'value_type': "string",
'units': '',
'slope': "none",
'format': '%s',
'description': label,
'groups': 'squid',
}
elif stats_descriptions[label]['type'] == 'counter32':
d= {
'name': 'squid_' + label,
'call_back': get_stat,
'time_max': 60,
'value_type': "float",
'units': stats_descriptions[label]['units'],
'slope': "positive",
'format': '%f',
'description': label,
'groups': 'squid',
}
elif stats_descriptions[label]['type'] == 'integer':
d= {
'name': 'squid_' + label,
'call_back': get_stat,
'time_max': 60,
'value_type': "uint",
'units': stats_descriptions[label]['units'],
'slope': "both",
'format': '%u',
'description': label,
'groups': 'squid',
}
else:
d= {
'name': 'squid_' + label,
'call_back': get_stat,
'time_max': 60,
'value_type': "float",
'units': stats_descriptions[label]['units'],
'slope': "both",
'format': '%f',
'description': label,
'groups': 'squid',
}
d.update(stats_descriptions[label])
descriptors.append(d)
#else:
#logging.error("skipped " + label)
return descriptors
def metric_cleanup():
#logging.shutdown()
pass
#This code is for debugging and unit testing
if __name__ == '__main__':
metric_init(None)
for d in descriptors:
v = d['call_back'](d['name'])
if d['value_type'] == 'string':
print 'value for %s is %s %s' % (d['name'], v, d['units'])
elif d['value_type'] == 'uint':
print 'value for %s is %d %s' % (d['name'], v, d['units'])
else:
print 'value for %s is %4.2f %s' % (d['name'], v, d['units'])