Skip to content
This repository was archived by the owner on Jun 22, 2022. It is now read-only.

Commit 839d86d

Browse files
author
Michael Conigliaro
committed
fix mongodb ratio reporting bug, trivial updates to diskfree and tokyo_tyrant modules
1 parent 82972aa commit 839d86d

3 files changed

Lines changed: 118 additions & 89 deletions

File tree

diskfree/python_modules/diskfree.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
23

34

45
import os
56

67

7-
name_prefix = 'disk_free_'
8-
params = {
8+
NAME_PREFIX = 'disk_free_'
9+
PARAMS = {
910
'mounts' : '/proc/mounts'
1011
}
1112

1213

13-
# return a value for the requested metric
1414
def metric_handler(name):
15+
"""Return a value for the requested metric"""
1516

1617
# parse path from name
17-
if name == name_prefix + 'rootfs':
18+
if name == NAME_PREFIX + 'rootfs':
1819
path = '/'
1920
else:
20-
path = '/' + name.replace(name_prefix, '').replace('_', '/')
21+
path = '/' + name.replace(NAME_PREFIX, '').replace('_', '/')
2122

2223
# get fs stats
2324
try:
@@ -28,18 +29,18 @@ def metric_handler(name):
2829
return (disk.f_bavail * disk.f_frsize) / 1024000000
2930

3031

31-
# initialize metric descriptors
3232
def metric_init(lparams):
33+
"""Initialize metric descriptors"""
3334

34-
global params
35+
global PARAMS
3536

3637
# set parameters
3738
for key in lparams:
38-
params[key] = lparams[key]
39+
PARAMS[key] = lparams[key]
3940

4041
# read mounts file
4142
try:
42-
f = open(params['mounts'])
43+
f = open(PARAMS['mounts'])
4344
except IOError:
4445
return 0
4546

@@ -56,7 +57,7 @@ def metric_init(lparams):
5657
path_key = mount_info[1][1:].replace('/', '_')
5758

5859
descriptors.append({
59-
'name': name_prefix + path_key,
60+
'name': NAME_PREFIX + path_key,
6061
'call_back': metric_handler,
6162
'time_max': 60,
6263
'value_type': 'uint',
@@ -70,8 +71,9 @@ def metric_init(lparams):
7071
return descriptors
7172

7273

73-
# cleanup
7474
def metric_cleanup():
75+
"""Cleanup"""
76+
7577
pass
7678

7779

mongodb/python_modules/mongodb.py

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
23

34

45
import json
@@ -8,20 +9,21 @@
89
import time
910

1011

11-
name_prefix = 'mongodb_'
12-
params = {
12+
NAME_PREFIX = 'mongodb_'
13+
PARAMS = {
1314
'stats_command' : 'mongo --quiet --eval "printjson(db.serverStatus())"'
1415
}
15-
metrics = {
16+
METRICS = {
1617
'time' : 0,
1718
'values' : {}
1819
}
19-
delta_metrics = {}
20-
metrics_cache_max = 1
20+
DELTA_METRICS = {}
21+
METRICS_CACHE_MAX = 1
2122

2223

23-
# flatten a dict (i.e. dict['a']['b']['c'] => dict['a_b_c'])
2424
def flatten(d, pre = '', sep = '_'):
25+
"""Flatten a dict (i.e. dict['a']['b']['c'] => dict['a_b_c'])"""
26+
2527
new_d = {}
2628
for k,v in d.items():
2729
if type(v) == dict:
@@ -31,15 +33,15 @@ def flatten(d, pre = '', sep = '_'):
3133
return new_d
3234

3335

34-
# return all metrics
3536
def get_metrics():
37+
"""Return all metrics"""
3638

37-
global metrics
39+
global METRICS
3840

39-
if (time.time() - metrics['time']) > metrics_cache_max:
41+
if (time.time() - METRICS['time']) > METRICS_CACHE_MAX:
4042

4143
# get raw metric data
42-
io = os.popen(params['stats_command'])
44+
io = os.popen(PARAMS['stats_command'])
4345

4446
# clean up
4547
metrics_str = ''.join(io.readlines()).strip() # convert to string
@@ -49,66 +51,88 @@ def get_metrics():
4951
fresh_metrics = flatten(json.loads(metrics_str))
5052

5153
# update cache
52-
metrics['time'] = time.time()
54+
METRICS['time'] = time.time()
5355
for name,value in fresh_metrics.items():
54-
metrics['values'][name] = value
56+
METRICS['values'][name] = value
5557

56-
return metrics
58+
return METRICS
5759

5860

59-
# return a value for the requested metric
6061
def get_value(name):
62+
"""Return a value for the requested metric"""
6163

6264
metrics = get_metrics()
6365

6466
try:
65-
name = name[len(name_prefix):] # remove prefix from name
67+
name = name[len(NAME_PREFIX):] # remove prefix from name
6668
result = metrics['values'][name]
6769
except KeyError:
6870
result = 0
6971

7072
return result
7173

7274

73-
# return change over time for the requested metric
7475
def get_delta(name):
76+
"""Return change over time for the requested metric"""
7577

76-
global delta_metrics
78+
global DELTA_METRICS
7779

7880
# get current metrics
7981
curr_metrics = get_metrics()
8082

8183
# get delta
8284
try:
83-
name = name[len(name_prefix):] # remove prefix from name
84-
delta = (curr_metrics['values'][name] - delta_metrics[name]['value'])/(curr_metrics['time'] - delta_metrics[name]['time'])
85+
name = name[len(NAME_PREFIX):] # remove prefix from name
86+
delta = (curr_metrics['values'][name] - DELTA_METRICS[name]['value'])/(curr_metrics['time'] - DELTA_METRICS[name]['time'])
8587
except KeyError:
8688
delta = 0
8789

8890
# update last metrics
89-
delta_metrics[name] = {
91+
DELTA_METRICS[name] = {
9092
'value' : get_metrics()['values'][name],
9193
'time' : get_metrics()['time']
9294
}
9395

9496
return delta
9597

9698

97-
# initialize metric descriptors
99+
def get_globalLock_ratio(name):
100+
"""Return the global lock ratio"""
101+
102+
try:
103+
result = get_delta(NAME_PREFIX + 'globalLock_lockTime') / get_delta(NAME_PREFIX + 'globalLock_totalTime') * 100
104+
except ZeroDivisionError:
105+
result = 0
106+
107+
return result
108+
109+
110+
def indexCounters_btree_missRatio(name):
111+
"""Return the btree miss ratio"""
112+
113+
try:
114+
result = get_delta(NAME_PREFIX + 'indexCounters_btree_misses') / get_delta(NAME_PREFIX + 'indexCounters_btree_accesses') * 100
115+
except ZeroDivisionError:
116+
result = 0
117+
118+
return result
119+
120+
98121
def metric_init(lparams):
122+
"""Initialize metric descriptors"""
99123

100-
global params
124+
global PARAMS
101125

102126
# set parameters
103127
for key in lparams:
104-
params[key] = lparams[key]
128+
PARAMS[key] = lparams[key]
105129

106130
# define descriptors
107131
time_max = 60
108132
groups = 'mongodb'
109133
descriptors = [
110134
{
111-
'name': name_prefix + 'opcounters_insert',
135+
'name': NAME_PREFIX + 'opcounters_insert',
112136
'call_back': get_delta,
113137
'time_max': time_max,
114138
'value_type': 'float',
@@ -119,7 +143,7 @@ def metric_init(lparams):
119143
'groups': groups
120144
},
121145
{
122-
'name': name_prefix + 'opcounters_query',
146+
'name': NAME_PREFIX + 'opcounters_query',
123147
'call_back': get_delta,
124148
'time_max': time_max,
125149
'value_type': 'float',
@@ -130,7 +154,7 @@ def metric_init(lparams):
130154
'groups': groups
131155
},
132156
{
133-
'name': name_prefix + 'opcounters_update',
157+
'name': NAME_PREFIX + 'opcounters_update',
134158
'call_back': get_delta,
135159
'time_max': time_max,
136160
'value_type': 'float',
@@ -141,7 +165,7 @@ def metric_init(lparams):
141165
'groups': groups
142166
},
143167
{
144-
'name': name_prefix + 'opcounters_delete',
168+
'name': NAME_PREFIX + 'opcounters_delete',
145169
'call_back': get_delta,
146170
'time_max': time_max,
147171
'value_type': 'float',
@@ -152,7 +176,7 @@ def metric_init(lparams):
152176
'groups': groups
153177
},
154178
{
155-
'name': name_prefix + 'opcounters_getmore',
179+
'name': NAME_PREFIX + 'opcounters_getmore',
156180
'call_back': get_delta,
157181
'time_max': time_max,
158182
'value_type': 'float',
@@ -163,7 +187,7 @@ def metric_init(lparams):
163187
'groups': groups
164188
},
165189
{
166-
'name': name_prefix + 'opcounters_command',
190+
'name': NAME_PREFIX + 'opcounters_command',
167191
'call_back': get_delta,
168192
'time_max': time_max,
169193
'value_type': 'float',
@@ -174,7 +198,7 @@ def metric_init(lparams):
174198
'groups': groups
175199
},
176200
{
177-
'name': name_prefix + 'backgroundFlushing_flushes',
201+
'name': NAME_PREFIX + 'backgroundFlushing_flushes',
178202
'call_back': get_delta,
179203
'time_max': time_max,
180204
'value_type': 'float',
@@ -185,7 +209,7 @@ def metric_init(lparams):
185209
'groups': groups
186210
},
187211
{
188-
'name': name_prefix + 'mem_mapped',
212+
'name': NAME_PREFIX + 'mem_mapped',
189213
'call_back': get_value,
190214
'time_max': time_max,
191215
'value_type': 'uint',
@@ -196,7 +220,7 @@ def metric_init(lparams):
196220
'groups': groups
197221
},
198222
{
199-
'name': name_prefix + 'mem_virtual',
223+
'name': NAME_PREFIX + 'mem_virtual',
200224
'call_back': get_value,
201225
'time_max': time_max,
202226
'value_type': 'uint',
@@ -207,7 +231,7 @@ def metric_init(lparams):
207231
'groups': groups
208232
},
209233
{
210-
'name': name_prefix + 'mem_resident',
234+
'name': NAME_PREFIX + 'mem_resident',
211235
'call_back': get_value,
212236
'time_max': time_max,
213237
'value_type': 'uint',
@@ -218,7 +242,7 @@ def metric_init(lparams):
218242
'groups': groups
219243
},
220244
{
221-
'name': name_prefix + 'extra_info_page_faults',
245+
'name': NAME_PREFIX + 'extra_info_page_faults',
222246
'call_back': get_delta,
223247
'time_max': time_max,
224248
'value_type': 'float',
@@ -229,8 +253,8 @@ def metric_init(lparams):
229253
'groups': groups
230254
},
231255
{
232-
'name': name_prefix + 'globalLock_ratio',
233-
'call_back': get_value,
256+
'name': NAME_PREFIX + 'globalLock_ratio',
257+
'call_back': get_globalLock_ratio,
234258
'time_max': time_max,
235259
'value_type': 'float',
236260
'units': '%',
@@ -240,8 +264,8 @@ def metric_init(lparams):
240264
'groups': groups
241265
},
242266
{
243-
'name': name_prefix + 'indexCounters_btree_missRatio',
244-
'call_back': get_value,
267+
'name': NAME_PREFIX + 'indexCounters_btree_missRatio',
268+
'call_back': indexCounters_btree_missRatio,
245269
'time_max': time_max,
246270
'value_type': 'float',
247271
'units': '%',
@@ -251,7 +275,7 @@ def metric_init(lparams):
251275
'groups': groups
252276
},
253277
{
254-
'name': name_prefix + 'globalLock_currentQueue_total',
278+
'name': NAME_PREFIX + 'globalLock_currentQueue_total',
255279
'call_back': get_value,
256280
'time_max': time_max,
257281
'value_type': 'uint',
@@ -262,7 +286,7 @@ def metric_init(lparams):
262286
'groups': groups
263287
},
264288
{
265-
'name': name_prefix + 'globalLock_currentQueue_readers',
289+
'name': NAME_PREFIX + 'globalLock_currentQueue_readers',
266290
'call_back': get_value,
267291
'time_max': time_max,
268292
'value_type': 'uint',
@@ -273,7 +297,7 @@ def metric_init(lparams):
273297
'groups': groups
274298
},
275299
{
276-
'name': name_prefix + 'globalLock_currentQueue_writers',
300+
'name': NAME_PREFIX + 'globalLock_currentQueue_writers',
277301
'call_back': get_value,
278302
'time_max': time_max,
279303
'value_type': 'uint',
@@ -284,7 +308,7 @@ def metric_init(lparams):
284308
'groups': groups
285309
},
286310
{
287-
'name': name_prefix + 'connections_current',
311+
'name': NAME_PREFIX + 'connections_current',
288312
'call_back': get_value,
289313
'time_max': time_max,
290314
'value_type': 'uint',
@@ -299,8 +323,9 @@ def metric_init(lparams):
299323
return descriptors
300324

301325

302-
# cleanup
303326
def metric_cleanup():
327+
"""Cleanup"""
328+
304329
pass
305330

306331

0 commit comments

Comments
 (0)