Class: Onering::Reporter

Inherits:
Object
  • Object
show all
Extended by:
Util
Defined in:
lib/onering/plugins/reporter.rb

Constant Summary collapse

DEFAULT_PLUGIN_GEMNAMES =
[
  'onering-report-plugins'
]
DEFAULT_PLUGIN_PATH =
[
  '/var/lib/onering/reporter'
]
DEFAULT_FACTER_PATH =
[
  '/etc/facter'
]

Constants included from Util

Util::HTTP_STATUS_CODES

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Util

fact, gem_path, http_status, make_filter

Class Attribute Details

.facter_pathObject (readonly)

Returns the value of attribute facter_path.



28
29
30
# File 'lib/onering/plugins/reporter.rb', line 28

def facter_path
  @facter_path
end

Class Method Details

.add(&block) ⇒ Object



96
97
98
99
100
# File 'lib/onering/plugins/reporter.rb', line 96

def add(&block)
  if block_given?
    instance_eval(&block)
  end
end

.load_pluginsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/onering/plugins/reporter.rb', line 59

def load_plugins()
# load plugins from @path
  @path.compact.each do |root|
    begin
      Dir["#{root}/*"].each do |directory|

      # only process top-level directories
        if File.directory?(directory)
          d = File.basename(directory)

        # allow plugins to be conditionally loaded based on fact values:
        #   default - always load
        #   <fact>-<fact_value> - load if <fact> == <fact_value>
        #
          if d == 'default' or Facter.value(d.split('-',2).first).to_s.downcase.nil_empty == d.split('-',2).last.to_s.downcase.nil_empty
            Dir[File.join(directory, '*.rb')].each do |plugin|
              plugin = File.basename(plugin, '.rb')

              begin
                Timeout.timeout((@options[:plugin_timeout] || 10).to_i) do
                  Onering::Logger.debug("Loading plugin #{plugin}", "Onering::Reporter")
                  require "#{directory}/#{plugin}"
                end
              rescue Timeout::Error => e
                Onering::Logger.warn("Plugin #{plugin} took too long to return, skipping", "Onering::Reporter")
              end
            end
          end
        end
      end
    rescue Exception => e
      Onering::Logger.warn(e.message, e.class.name)
      next
    end
  end
end

.property(name, value = nil) ⇒ Object



102
103
104
# File 'lib/onering/plugins/reporter.rb', line 102

def property(name, value=nil)
  @_report[:properties].set(name.to_s.gsub('.','_'), value) unless value.nil?
end

.reportObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/onering/plugins/reporter.rb', line 111

def report()
  @id = (@options[:id] || Onering::Util.fact('hardwareid', nil))

  if not @id.nil?
    Timeout.timeout((@options[:timeout] || 60).to_i) do
      hostname = (Facter.value('fqdn') rescue %x{hostname -f}.strip.chomp)

      @_report = {
        :id           => @id,
        :name         => hostname,
        :aliases      => @options[:aliases],
        :tags         => @options[:tags],
        :status       => (@options[:status] || 'online'),
        :inventory    => true,
        :properties   => {}
      }

    # loads plugins and populates @_report
      load_plugins

      return @_report.stringify_keys()
    end
  else
    Onering::Logger.fatal!("Cannot generate report without a hardware ID", "Onering::Reporter")
  end

  {}
end

.setup(config = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/onering/plugins/reporter.rb', line 30

def setup(config={})
  @options = config
  @facter_path = DEFAULT_FACTER_PATH

  @path = [*Onering::Config.get('reporter.plugin_path',[])]
  @path += DEFAULT_PLUGIN_PATH

# add gem paths to the @path
  ([*Onering::Config.get('reporter.plugin_gems',[])]+DEFAULT_PLUGIN_GEMNAMES).compact.each do |g|
    begin
      p = File.join(Util.gem_path(g), 'lib')
      @path << File.join(p, 'reporter')
      @facter_path << File.join(p, 'facter')
    rescue Gem::LoadError => e
      Onering::Logger.warn("Error loading gem: #{e.message}", "Onering::Reporter")
      next
    end
  end

  begin
    ENV['FACTERLIB'] = @facter_path.join(':')
    require 'facter'
    Onering::Logger.debug("Facter loaded successfully, FACTERLIB is #{ENV['FACTERLIB']}", "Onering::Reporter")

  rescue LoadError
    Onering::Logger.error("Unable to load Facter library", "Onering::Reporter")
  end
end

.stat(name, value = nil) ⇒ Object



106
107
108
109
# File 'lib/onering/plugins/reporter.rb', line 106

def stat(name, value=nil)
  @_report[:properties][:metrics] ||= {}
  @_report[:properties][:metrics].set(name.to_s.gsub('.','_'), value) unless value.nil?
end