Class: Opstat::Parsers::Master

Inherits:
Object
  • Object
show all
Includes:
Logging, Singleton
Defined in:
lib/opstat-master/parsers.rb

Instance Method Summary collapse

Methods included from Logging

#log_level, #oplogger

Constructor Details

#initializeMaster

Returns a new instance of Master.



6
7
8
# File 'lib/opstat-master/parsers.rb', line 6

def initialize
  @parsers = {}
end

Instance Method Details

#load_parsersObject



9
10
11
# File 'lib/opstat-master/parsers.rb', line 9

def load_parsers
  @parsers = Opstat::Plugins.load_parsers(oplogger)
end

#parse_and_save(params) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/opstat-master/parsers.rb', line 13

def parse_and_save(params)
  plugin = params[:plugin]
  host = params[:host]
  data = params[:plugin_data]['data']
  time = Time.parse(params[:plugin_data]['timestamp'])
  oplogger.info "Saving parsed data collected on #{time} from #{host.id}(plugin:#{plugin[:type]} #{host[:hostname]}) "
  begin
    reports = @parsers[plugin[:type]].parse_data(data: data, time: time)
  #TODO save errors to db
  rescue Exception => e
    oplogger.error "current params #{params}"
	raise e
  end
  if reports.nil? or reports.empty?
    oplogger.warn "no report data parsed - empty report for #{plugin.type}?"
    return
  end
  reports.each do |report|
	default_tags = { :host_id => host.id, :plugin_id => plugin.id, :hostname => host[:hostname] }
	report_data = report.select{|k,v| not k.to_s.starts_with?('OPSTAT_TAG_')}
	report_tags = report.select{|k,v| k.to_s.starts_with?('OPSTAT_TAG_')}
	report_tags ||= {}
	measurement_tags = report_tags.merge(default_tags)
	measurement = { :values => report_data, :timestamp => time.to_i, :tags => measurement_tags, :name => plugin.type }
	Opstat::DB::Influx.instance.write_point(plugin.type, measurement)
  end
end