diff --git a/README.md b/README.md index 425ce21..b98272c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ The plugin has some configuration options even though none are mandatory. This i * Host - hostname or IP address of the mongodb server defaults to 127.0.0.1 * Port - the port of the mongodb server defaults to 27017 * Database - the databases you want to monitor defaults to "admin". You can provide more than one database. Note that the first database _must_ be "admin", as it is used to perform a serverStatus() + "admin" "*" will monitor all databases The following is an example Collectd configuration for this plugin: diff --git a/mongodb.py b/mongodb.py index 7ef82de..8049595 100644 --- a/mongodb.py +++ b/mongodb.py @@ -1,6 +1,8 @@ # # Plugin to collectd statistics from MongoDB # +# source: https://github.com/sebest/collectd-mongodb +# import collectd from pymongo import MongoClient @@ -14,7 +16,7 @@ def __init__(self): self.plugin_name = "mongo" self.mongo_host = "127.0.0.1" self.mongo_port = 27017 - self.mongo_db = ["admin", ] + self.mongo_db = ["admin", "*"] self.mongo_user = None self.mongo_password = None @@ -141,6 +143,19 @@ def config(self, obj): else: collectd.warning("mongodb plugin: Unkown configuration key %s" % node.key) + if '*' in self.mongo_db: + collectd.warning("auto detecting databases.....") + con = MongoClient(host=self.mongo_host, port=self.mongo_port, read_preference=ReadPreference.SECONDARY) + db = con[self.mongo_db[0]] + if self.mongo_user and self.mongo_password: + db.authenticate(self.mongo_user, self.mongo_password) + db_list=db.command("listDatabases") + database_list=[] + for d in db_list["databases"]: + collectd.warning("db: %s" % str(d['name'])) + database_list.append(d['name']) + self.mongo_db=database_list + mongodb = MongoDB() collectd.register_read(mongodb.get_db_and_collection_stats) collectd.register_config(mongodb.config)