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.



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

def facter_path
  @facter_path
end

Class Method Details

.add(&block) ⇒ Object



84
85
86
87
88
# File 'lib/onering/plugins/reporter.rb', line 84

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

.load_pluginsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/onering/plugins/reporter.rb', line 55

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 |e|
              e = File.basename(e, '.rb')
              require "#{directory}/#{e}"
            end
          end
        end
      end
    rescue Exception => e
      STDERR.puts e.message
      next
    end
  end
end

.property(name, value = nil) ⇒ Object



90
91
92
# File 'lib/onering/plugins/reporter.rb', line 90

def property(name, value=nil)
  @_report[:properties].set(name, value) if value
end

.reportObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/onering/plugins/reporter.rb', line 99

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

  if not @id.nil?
    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()
  else
    raise "Cannot generate report without a hardware ID"
  end

  {}
end

.setup(config = {}) ⇒ Object



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

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

  @path = [*@options[:plugin_path]]
  @path += DEFAULT_PLUGIN_PATH

# add gem paths to the @path
  ([*@options[: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
      next
    end
  end

  begin
    ENV['FACTERLIB'] = @facter_path.join(':')
    require 'facter'
  rescue LoadError
    STDERR.puts("Unable to load Facter library")
  end
end

.stat(name, value = nil) ⇒ Object



94
95
96
97
# File 'lib/onering/plugins/reporter.rb', line 94

def stat(name, value=nil)
  @_report[:properties][:metrics] ||= {}
  @_report[:properties][:metrics].set(name, value) if value
end