Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tokumx] Handle int64 and replica connections #2390

Merged
merged 1 commit into from Apr 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 34 additions & 13 deletions checks.d/tokumx.py
Expand Up @@ -3,6 +3,7 @@
import types

# 3p
import bson
from pymongo import (
MongoClient,
ReadPreference,
Expand Down Expand Up @@ -251,12 +252,7 @@ def get_state_description(state):
'host': hostname
})

def _get_connection(self, instance):
if 'server' not in instance:
raise Exception("Missing 'server' in tokumx config")

server = instance['server']

def _get_ssl_params(self, instance):
ssl_params = {
'ssl': instance.get('ssl', None),
'ssl_keyfile': instance.get('ssl_keyfile', None),
Expand All @@ -269,6 +265,16 @@ def _get_connection(self, instance):
if param is None:
del ssl_params[key]

return ssl_params

def _get_connection(self, instance, read_preference=None):
if 'server' not in instance:
raise Exception("Missing 'server' in tokumx config")

server = instance['server']

ssl_params = self._get_ssl_params(instance)

tags = instance.get('tags', [])
tags.append('server:%s' % server)
# de-dupe tags to avoid a memory leak
Expand Down Expand Up @@ -302,7 +308,13 @@ def _get_connection(self, instance):
self.log.debug("TokuMX: cannot extract username and password from config %s" % server)
do_auth = False
try:
conn = MongoClient(server, socketTimeoutMS=DEFAULT_TIMEOUT*1000, **ssl_params)
if read_preference:
conn = MongoClient(server,
socketTimeoutMS=DEFAULT_TIMEOUT*1000,
read_preference=ReadPreference.SECONDARY,
**ssl_params)
else:
conn = MongoClient(server, socketTimeoutMS=DEFAULT_TIMEOUT*1000, **ssl_params)
db = conn[db_name]
except Exception:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=service_check_tags)
Expand All @@ -318,7 +330,7 @@ def _get_connection(self, instance):

return server, conn, db, tags

def _get_replica_metrics(self, conn, tags, server, status):
def _get_replica_metrics(self, instance, conn, db, tags, server, status):
try:
data = {}

Expand Down Expand Up @@ -355,7 +367,10 @@ def _get_replica_metrics(self, conn, tags, server, status):
tags.append('role:primary')
else:
tags.append('role:secondary')
conn.read_preference = ReadPreference.SECONDARY
self.log.debug("Current replSet member is secondary. "
"Creating new connection to set read_preference to secondary.")
# need a new connection to deal with replica sets
server, conn, db, _ = self._get_connection(instance, read_preference=ReadPreference.SECONDARY)

data['state'] = replSet['myState']
self.check_last_state(data['state'], server, self.agentConfig)
Expand All @@ -366,6 +381,8 @@ def _get_replica_metrics(self, conn, tags, server, status):
else:
raise e

return conn, db

def submit_idx_rate(self, metric_name, value, tags, key):
if key not in self.idx_rates:
local_rate = LocalRate(self, metric_name, tags)
Expand Down Expand Up @@ -393,13 +410,13 @@ def collect_mongos(self, server, conn, db, tags):
self.gauge('tokumx.sharding.chunks', doc['count'], tags=chunk_tags)


def collect_metrics(self, server, conn, db, tags):
def collect_metrics(self, instance, server, conn, db, tags):
status = db["$cmd"].find_one({"serverStatus": 1})
status['stats'] = db.command('dbstats')

# Handle replica data, if any
# See http://www.mongodb.org/display/DOCS/Replica+Set+Commands#ReplicaSetCommands-replSetGetStatus
self._get_replica_metrics(conn, tags, server, status)
conn, db = self._get_replica_metrics(instance, conn, db, tags, server, status)

for dbname in conn.database_names():
db_tags = list(tags)
Expand Down Expand Up @@ -453,7 +470,11 @@ def collect_metrics(self, server, conn, db, tags):
continue

# value is now status[x][y][z]
assert type(value) in (types.IntType, types.LongType, types.FloatType)
if type(value) == bson.int64.Int64:
value = long(value)
else:
if type(value) not in (types.IntType, types.LongType, types.FloatType):
self.log.warning("Value found that is not of type int, int64,long, or float")

# Check if metric is a gauge or rate
if m in self.GAUGES:
Expand All @@ -470,4 +491,4 @@ def check(self, instance):
self.collect_mongos(server, conn, db, tags)

else:
self.collect_metrics(server, conn, db, tags)
self.collect_metrics(instance, server, conn, db, tags)
2 changes: 2 additions & 0 deletions conf.d/tokumx.yaml.example
@@ -1,6 +1,8 @@
init_config:

instances:
# Specify the MongoDB URI, with database to use for reporting (defaults to "admin")
# E.g. mongodb://datadog:LnCbkX4uhpuLHSUrcayEoAZA@localhost:27017/my-db
- server: mongodb://localhost:27017
# tags:
# - optional_tag1
Expand Down