Skip to content

Commit

Permalink
fix bugs when using MongoDB 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouruisong committed Aug 18, 2017
1 parent 6b3ca3b commit e1df940
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions mongodb/python_modules/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

NAME_PREFIX = 'mongodb_'
PARAMS = {
'server_status' : '~/mongodb-osx-x86_64-1.8.1/bin/mongo --host mongodb04.example.com --port 27018 --quiet --eval "printjson(db.serverStatus())"',
'rs_status' : '~/mongodb-osx-x86_64-1.8.1/bin/mongo --host mongodb04.example.com --port 27018 --quiet --eval "printjson(rs.status())"'
'server_status' : 'mongo --quiet --eval "printjson(db.serverStatus())"',
'rs_status' : 'mongo --quiet --eval "printjson(rs.status())"'
}
METRICS = {
'time' : 0,
Expand Down Expand Up @@ -74,14 +74,16 @@ def get_metrics():
# clean up
metrics_str = ''.join(io.readlines()).strip() # convert to string
metrics_str = re.sub('\w+\((.*)\)', r"\1", metrics_str) # remove functions

metrics_str = metrics_str.replace(''', 1,''',''',''')

# convert to flattened dict
try:
if status_type == 'server_status':
metrics.update(flatten(json.loads(metrics_str)))
else:
metrics.update(flatten(json.loads(metrics_str), pre='%s_' % status_type))
except ValueError:
except ValueError,e:
print e
metrics = {}

# update cache
Expand All @@ -96,10 +98,8 @@ def get_metrics():

def get_value(name):
"""Return a value for the requested metric"""

# get metrics
metrics = get_metrics()[0]

# get value
name = name[len(NAME_PREFIX):] # remove prefix from name
try:
Expand All @@ -120,11 +120,11 @@ def get_rate(name):
name = name[len(NAME_PREFIX):] # remove prefix from name

try:
rate = float(curr_metrics['data'][name] - last_metrics['data'][name]) / \
float(curr_metrics['time'] - last_metrics['time'])
rate = (float(curr_metrics['data'][name]) - float(last_metrics['data'][name])) / \
(float(curr_metrics['time']) - float(last_metrics['time']))
if rate < 0:
rate = float(0)
except StandardError:
except StandardError,e:
rate = float(0)

return rate
Expand Down

0 comments on commit e1df940

Please sign in to comment.