Class: MCollective::Facts::Base

### Home Classes Methods

### In Files

- lib/mcollective/facts/base.rb

### Parent

Object

### Methods

- ::inherited
- ::new
- #force_reload?
- #get_fact
- #get_facts
- #has_fact?

### Files

- COPYING
- README
- Rakefile
- PLACEHOLDER
- PLACEHOLDER

### Class/Module Index

Quicksearch

- MCollective
- MCollective::Agent
- MCollective::Agents
- MCollective::Aggregate
- MCollective::Aggregate::Base
- MCollective::Aggregate::Result
- MCollective::Aggregate::Result::Base
- MCollective::Aggregate::Result::CollectionResult
- MCollective::Aggregate::Result::NumericResult
- MCollective::Application
- MCollective::Applications
- MCollective::Client
- MCollective::Config
- MCollective::Connector
- MCollective::Connector::Base
- MCollective::DDL
- MCollective::DDLValidationError
- MCollective::Data
- MCollective::Data::Base
- MCollective::Data::Result
- MCollective::Discovery
- MCollective::Facts
- MCollective::Facts::Base
- MCollective::Generators
- MCollective::Generators::AgentGenerator
- MCollective::Generators::Base
- MCollective::Generators::DataGenerator
- MCollective::InvalidRPCData
- MCollective::Log
- MCollective::Logger
- MCollective::Logger::Base
- MCollective::Logger::Console_logger
- MCollective::Logger::File_logger
- MCollective::Logger::Syslog_logger
- MCollective::Matcher
- MCollective::Matcher::Parser
- MCollective::Matcher::Scanner
- MCollective::Message
- MCollective::MissingRPCData
- MCollective::MsgDoesNotMatchRequestID
- MCollective::MsgTTLExpired
- MCollective::NotTargettedAtUs
- MCollective::Optionparser
- MCollective::PluginManager
- MCollective::PluginPackager
- MCollective::PluginPackager::AgentDefinition
- MCollective::PluginPackager::StandardDefinition
- MCollective::RPC
- MCollective::RPC::ActionRunner
- MCollective::RPC::Agent
- MCollective::RPC::Audit
- MCollective::RPC::Client
- MCollective::RPC::Helpers
- MCollective::RPC::Progress
- MCollective::RPC::Reply
- MCollective::RPC::Request
- MCollective::RPC::Result
- MCollective::RPC::Stats
- MCollective::RPCAborted
- MCollective::RPCError
- MCollective::Registration
- MCollective::Registration::Base
- MCollective::Runner
- MCollective::RunnerStats
- MCollective::SSL
- MCollective::Security
- MCollective::Security::Base
- MCollective::SecurityValidationFailed
- MCollective::Shell
- MCollective::UnixDaemon
- MCollective::UnknownRPCAction
- MCollective::UnknownRPCError
- MCollective::Util
- MCollective::WindowsDaemon
- Array
- Dir
- Object
- String
- Symbol

No matching classes.

# MCollective::Facts::Base

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"}

### Public Instance Methods

force_reload?() click to toggle source

Plugins can override this to provide forced fact invalidation

    # File lib/mcollective/facts/base.rb, line 81
    def force_reload?
      false
    end

get_fact(fact=nil) click to toggle source

Returns the value of a single fact

    # File lib/mcollective/facts/base.rb, line 22
    def get_fact(fact=nil)
      config = Config.instance

      cache_time = config.fact_cache_time || 300

      Thread.exclusive do
        begin
          if (Time.now.to_i - @last_facts_load > cache_time.to_i ) || force_reload?
            Log.debug("Resetting facter cache, now: #{Time.now.to_i} last-known-good: #{@last_facts_load}")

            tfacts = load_facts_from_source

            # Force reset to last known good state on empty facts
            raise "Got empty facts" if tfacts.empty?

            @facts.clear

            tfacts.each_pair do |key,value|
              @facts[key.to_s] = value.to_s
            end

            @last_good_facts = @facts.clone
            @last_facts_load = Time.now.to_i
          else
            Log.debug("Using cached facts now: #{Time.now.to_i} last-known-good: #{@last_facts_load}")
          end
        rescue Exception => e
          Log.error("Failed to load facts: #{e.class}: #{e}")

          # Avoid loops where failing fact loads cause huge CPU
          # loops, this way it only retries once every cache_time
          # seconds
          @last_facts_load = Time.now.to_i

          # Revert to last known good state
          @facts = @last_good_facts.clone
        end
      end

      # If you do not supply a specific fact all facts will be returned
      if fact.nil?
        return @facts
      else
        @facts.include?(fact) ? @facts[fact] : nil
      end
    end

get_facts() click to toggle source

Returns all facts

    # File lib/mcollective/facts/base.rb, line 71
    def get_facts
      get_fact(nil)
    end

has_fact?(fact) click to toggle source

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

    # File lib/mcollective/facts/base.rb, line 76
    def has_fact?(fact)
      get_fact(nil).include?(fact)
    end

### Public Class Methods

inherited(klass) click to toggle source

Registers new fact sources into the plugin manager

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

new() click to toggle source

    # File lib/mcollective/facts/base.rb, line 10
    def initialize
      @facts = {}
      @last_good_facts = {}
      @last_facts_load = 0
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.