Class MCollective::Facts::Base
In: lib/mcollective/facts/base.rb
Parent: Object

A base class for fact providers, to make a new fully functional fact provider inherit from this and simply provide a self.get_facts method that returns a hash like:

 {"foo" => "bar",
  "bar" => "baz"}

Methods

Public Class methods

Registers new fact sources into the plugin manager

[Source]

    # File lib/mcollective/facts/base.rb, line 15
15:             def self.inherited(klass)
16:                 PluginManager << {:type => "facts_plugin", :class => klass.to_s}
17:             end

Public Instance methods

Returns the value of a single fact

[Source]

    # File lib/mcollective/facts/base.rb, line 20
20:             def get_fact(fact=nil)
21:                 config = Config.instance
22:                 logger = Log.instance
23: 
24:                 cache_time = config.fact_cache_time || 300
25: 
26:                 Thread.exclusive do
27:                     begin
28:                         if (Time.now.to_i - @@last_facts_load > cache_time.to_i )
29:                             logger.debug("Resetting facter cache after #{cache_time} seconds, now: #{Time.now.to_i} last-known-good: #{@@last_facts_load}")
30: 
31:                             @@facts = load_facts_from_source
32: 
33:                             # Force reset to last known good state on empty facts
34:                             raise "Got empty facts" if @@facts.empty?
35: 
36:                             @@facts.each_pair do |key,value|
37:                                 @@facts[key.to_s] = value.to_s
38:                             end
39: 
40:                             @@last_good_facts = @@facts.clone
41:                             @@last_facts_load = Time.now.to_i
42:                         else
43:                             logger.debug("Using cached facts now: #{Time.now.to_i} last-known-good: #{@@last_facts_load}")
44:                         end
45:                     rescue Exception => e
46:                         logger.error("Failed to load facts: #{e.class}: #{e}")
47: 
48:                         # Avoid loops where failing fact loads cause huge CPU
49:                         # loops, this way it only retries once every cache_time
50:                         # seconds
51:                         @@last_facts_load = Time.now.to_i
52: 
53:                         # Revert to last known good state
54:                         @@facts = @@last_good_facts.clone
55:                     end
56:                 end
57: 
58: 
59:                 # If you do not supply a specific fact all facts will be returned
60:                 if fact.nil?
61:                     return @@facts
62:                 else
63:                     @@facts.include?(fact) ? @@facts[fact] : nil
64:                 end
65:             end

Returns all facts

[Source]

    # File lib/mcollective/facts/base.rb, line 68
68:             def get_facts
69:                 get_fact(nil)
70:             end

Returns true if we know about a specific fact, false otherwise

[Source]

    # File lib/mcollective/facts/base.rb, line 73
73:             def has_fact?(fact)
74:                 get_fact(fact).nil?
75:             end

[Validate]