diff --git a/agent/registration-mongodb/agent/registration.rb b/agent/registration-mongodb/agent/registration.rb index 20b3a8b..226a802 100644 --- a/agent/registration-mongodb/agent/registration.rb +++ b/agent/registration-mongodb/agent/registration.rb @@ -44,13 +44,19 @@ def initialize @config = Config.instance @mongohost = @config.pluginconf["registration.mongohost"] || "localhost" + @mongoport = @config.pluginconf["registration.mongoport"] || "27017" @mongodb = @config.pluginconf["registration.mongodb"] || "puppet" + @mongouser = @config.pluginconf["registration.mongouser"] + @mongopass = @config.pluginconf["registration.mongopass"] @collection = @config.pluginconf["registration.collection"] || "nodes" @yaml_dir = @config.pluginconf["registration.extra_yaml_dir"] || false - Log.instance.debug("Connecting to mongodb @ #{@mongohost} db #{@mongodb} collection #{@collection}") + Log.instance.debug("Connecting to mongodb @ #{@mongohost}:#{@mongoport} db #{@mongodb} collection #{@collection}") - @dbh = Mongo::Connection.new(@mongohost).db(@mongodb) + @dbh = Mongo::Connection.new(@mongohost,@mongoport).db(@mongodb) + unless @mongouser.empty? + @auth = @dbh.authenticate(@mongouser, @mongopass) + end @coll = @dbh.collection(@collection) @coll.create_index("fqdn", {:unique => true, :dropDups => true}) @@ -86,7 +92,11 @@ def handlemsg(msg, connection) before = Time.now.to_f begin doc = @coll.find_and_modify(:query => by_fqdn, :update => {'$set' => req}, :new => true) - doc_id = doc['_id'] + if doc + doc_id = doc['_id'] + else + doc_id = @coll.insert(req, {:safe => true}) + end rescue Mongo::OperationFailure doc_id = @coll.insert(req, {:safe => true}) ensure diff --git a/agent/registration-mongodb/discovery/mongo.rb b/agent/registration-mongodb/discovery/mongo.rb index 415cd39..549a40a 100644 --- a/agent/registration-mongodb/discovery/mongo.rb +++ b/agent/registration-mongodb/discovery/mongo.rb @@ -8,11 +8,17 @@ def discover(filter, timeout, limit=0, client=nil) config = Config.instance mongohost = config.pluginconf["registration.mongohost"] || "localhost" + mongoport = config.pluginconf["registration.mongoport"] || "27017" mongodb = config.pluginconf["registration.mongodb"] || "puppet" + mongouser = config.pluginconf["registration.mongouser"] + mongopass = config.pluginconf["registration.mongopass"] collection = config.pluginconf["registration.collection"] || "nodes" newerthan = Time.now.to_i - Integer(config.pluginconf["registration.criticalage"] || 3600) - dbh = ::Mongo::Connection.new(mongohost).db(mongodb) + dbh = ::Mongo::Connection.new(mongohost,mongoport).db(mongodb) + unless mongouser.empty? + auth = dbh.authenticate(mongouser, mongopass) + end coll = dbh.collection(collection) found = []