Skip to content
Browse files

15367 - mongodb should only discover recently seen machines

Support setting a maximum age for a node in the registration database
after which they will be considered invalid and not discoverable using
the plugin.registration.criticalage setting
  • Loading branch information...
1 parent 0b32601 commit 3a6fc55fd88db6ef8cb0e41087ab1cdd0e2e0c51 @ripienaar ripienaar committed Jul 4, 2012
Showing with 17 additions and 16 deletions.
  1. +17 −16 agent/registration-mongodb/discovery/mongo.rb
View
33 agent/registration-mongodb/discovery/mongo.rb
@@ -10,6 +10,7 @@ def discover(filter, timeout, limit=0, client=nil)
mongohost = config.pluginconf["registration.mongohost"] || "localhost"
mongodb = config.pluginconf["registration.mongodb"] || "puppet"
collection = config.pluginconf["registration.collection"] || "nodes"
+ newerthan = Time.now.to_i - Integer(config.pluginconf["registration.criticalage"] || 3600)
dbh = ::Mongo::Connection.new(mongohost).db(mongodb)
coll = dbh.collection(collection)
@@ -19,16 +20,16 @@ def discover(filter, timeout, limit=0, client=nil)
filter.keys.each do |key|
case key
when "fact"
- fact_search(filter["fact"], coll, found, client.options[:collective])
+ fact_search(filter["fact"], coll, found, client.options[:collective], newerthan)
when "cf_class"
- class_search(filter["cf_class"], coll, found, client.options[:collective])
+ class_search(filter["cf_class"], coll, found, client.options[:collective], newerthan)
when "agent"
- agent_search(filter["agent"], coll, found, client.options[:collective])
+ agent_search(filter["agent"], coll, found, client.options[:collective], newerthan)
when "identity"
- identity_search(filter["identity"], coll, found, client.options[:collective])
+ identity_search(filter["identity"], coll, found, client.options[:collective], newerthan)
end
end
@@ -37,7 +38,7 @@ def discover(filter, timeout, limit=0, client=nil)
found.inject(found[0]){|x, y| x & y}
end
- def fact_search(filter, collection, found, collective)
+ def fact_search(filter, collection, found, collective, newerthan)
filter.each do |f|
fact = f[:fact]
value = f[:value]
@@ -47,15 +48,15 @@ def fact_search(filter, collection, found, collective)
when "==", "=~"
query = {"facts.#{fact}" => regexy_string(value), 'collectives' => collective}
when "<="
- query = {"facts.#{fact}" => {"$lte" => regexy_string(value)}, 'collectives' => collective}
+ query = {"facts.#{fact}" => {"$lte" => regexy_string(value)}, 'collectives' => collective, 'lastseen' => {"$gte" => newerthan}}
when ">="
- query = {"facts.#{fact}" => {"$gte" => regexy_string(value)}, 'collectives' => collective}
+ query = {"facts.#{fact}" => {"$gte" => regexy_string(value)}, 'collectives' => collective, 'lastseen' => {"$gte" => newerthan}}
when "<"
- query = {"facts.#{fact}" => {"$lt" => regexy_string(value)}, 'collectives' => collective}
+ query = {"facts.#{fact}" => {"$lt" => regexy_string(value)}, 'collectives' => collective, 'lastseen' => {"$gte" => newerthan}}
when ">"
- query = {"facts.#{fact}" => {"$gt" => regexy_string(value)}, 'collectives' => collective}
+ query = {"facts.#{fact}" => {"$gt" => regexy_string(value)}, 'collectives' => collective, 'lastseen' => {"$gte" => newerthan}}
when "!="
- query = {"facts.#{fact}" => {"$ne" => regexy_string(value)}, 'collectives' => collective}
+ query = {"facts.#{fact}" => {"$ne" => regexy_string(value)}, 'collectives' => collective, 'lastseen' => {"$gte" => newerthan}}
else
raise "Cannot perform %s matches for facts using the mongo discovery method" % f[:operator]
end
@@ -66,25 +67,25 @@ def fact_search(filter, collection, found, collective)
end
end
- def class_search(filter, collection, found, collective)
+ def class_search(filter, collection, found, collective, newerthan)
return if filter.empty?
matcher = filter.map {|klass| regexy_string(klass)}.uniq
- found << collection.find({'classes' => {"$all" => matcher}, 'collectives' => collective}, :fields => ["identity"]).map{|n| n["identity"]}
+ found << collection.find({'classes' => {"$all" => matcher}, 'collectives' => collective, 'lastseen' => {"$gte" => newerthan}}, :fields => ["identity"]).map{|n| n["identity"]}
end
- def agent_search(filter, collection, found, collective)
+ def agent_search(filter, collection, found, collective, newerthan)
return if filter.empty?
matcher = filter.map {|agent| regexy_string(agent)}.uniq
- found << collection.find({'agentlist' => {"$all" => matcher}, 'collectives' => collective}, :fields => ["identity"]).map{|n| n["identity"]}
+ found << collection.find({'agentlist' => {"$all" => matcher}, 'collectives' => collective, 'lastseen' => {"$gte" => newerthan}}, :fields => ["identity"]).map{|n| n["identity"]}
end
- def identity_search(filter, collection, found, collective)
+ def identity_search(filter, collection, found, collective, newerthan)
return if filter.empty?
matcher = filter.map {|identity| regexy_string(identity)}
- found << collection.find({'identity' => {"$in" => matcher}, 'collectives' => collective}, :fields => ["identity"]).map{|n| n["identity"]}
+ found << collection.find({'identity' => {"$in" => matcher}, 'collectives' => collective, 'lastseen' => {"$gte" => newerthan}}, :fields => ["identity"]).map{|n| n["identity"]}
end
def regexy_string(string)

0 comments on commit 3a6fc55

Please sign in to comment.
Something went wrong with that request. Please try again.