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

Commit 5b19112

Browse files
author
Gavin M. Roy
committed
Support MongoDB with per-database authentication (MeetMe#66)
1 parent 8d70b37 commit 5b19112

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

etc/newrelic/newrelic_plugin_agent.cfg

+16-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,23 @@ Application:
3434
# name: hostname
3535
# host: localhost
3636
# port: 27017
37-
# username: user # [OPTIONAL]
38-
# password: pass # [OPTIONAL]
3937
# databases:
40-
# - test
41-
# - yourdbname
38+
# - test
39+
# - yourdbname
40+
41+
#mongodb: # With Authentication
42+
# name: hostname
43+
# host: localhost
44+
# port: 27017
45+
# admin_username: user
46+
# admin_password: pass
47+
# databases:
48+
# test:
49+
# username: user
50+
# password: pass
51+
# yourdbname:
52+
# username: user
53+
# password: pass
4254

4355
#nginx:
4456
# name: hostname

newrelic_plugin_agent/plugins/mongodb.py

+39-5
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,54 @@ def get_and_add_stats(self):
167167
"""Fetch the data from the MongoDB server and add the datapoints
168168
169169
"""
170-
client = self.connect()
171170
databases = self.config.get('databases', list())
171+
if isinstance(databases, list):
172+
self.get_and_add_db_list(databases)
173+
else:
174+
self.get_and_add_db_with_auth(databases)
175+
176+
def get_and_add_db_list(self, databases):
177+
"""Handle the list of databases while supporting authentication for
178+
the admin if needed
179+
180+
:param list databases: The database list
181+
182+
"""
183+
client = self.connect()
172184
for database in databases:
173185
db = client[database]
174-
if self.config.get('username'):
175-
db.authenticate(self.config['username'],
176-
self.config.get('password'))
177186
try:
178-
if database == databases[0]:
187+
if database == database[0]:
188+
if self.config.get('admin_username'):
189+
db.authenticate(self.config['admin_username'],
190+
self.config.get('admin_password'))
179191
self.add_server_datapoints(db.command('serverStatus'))
180192
self.add_datapoints(database, db.command('dbStats'))
181193
except errors.OperationFailure as error:
182194
LOGGER.critical('Could not fetch stats: %s', error)
183195

196+
def get_and_add_db_with_auth(self, databases):
197+
"""Handle the nested database structure with usernnames and password.
198+
199+
:param dict databases: The databases data structure
200+
201+
"""
202+
client = self.connect()
203+
db_names = databases.keys()
204+
for database in db_names:
205+
db = client[database]
206+
try:
207+
if database == db_names[0]:
208+
if self.config.get('admin_username'):
209+
db.authenticate(self.config['admin_username'],
210+
self.config.get('admin_password'))
211+
self.add_server_datapoints(db.command('serverStatus'))
212+
if 'username' in databases[database]:
213+
db.authenticate(databases[database]['username'],
214+
databases[database].get('password'))
215+
except errors.OperationFailure as error:
216+
LOGGER.critical('Could not fetch stats: %s', error)
217+
184218
def poll(self):
185219
LOGGER.info('Polling MongoDB at %(host)s:%(port)i', self.config)
186220
start_time = time.time()

0 commit comments

Comments
 (0)