@@ -167,20 +167,54 @@ def get_and_add_stats(self):
167
167
"""Fetch the data from the MongoDB server and add the datapoints
168
168
169
169
"""
170
- client = self .connect ()
171
170
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 ()
172
184
for database in databases :
173
185
db = client [database ]
174
- if self .config .get ('username' ):
175
- db .authenticate (self .config ['username' ],
176
- self .config .get ('password' ))
177
186
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' ))
179
191
self .add_server_datapoints (db .command ('serverStatus' ))
180
192
self .add_datapoints (database , db .command ('dbStats' ))
181
193
except errors .OperationFailure as error :
182
194
LOGGER .critical ('Could not fetch stats: %s' , error )
183
195
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
+
184
218
def poll (self ):
185
219
LOGGER .info ('Polling MongoDB at %(host)s:%(port)i' , self .config )
186
220
start_time = time .time ()
0 commit comments