Class: Proxy::Reports::PuppetProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/smart_proxy_reports/puppet_processor.rb

Constant Summary collapse

YAML_CLEAN =
/!ruby\/object.*$/.freeze
KEYS_TO_COPY =
%w[report_format puppet_version environment metrics].freeze
MAX_EVAL_TIMES =
29

Instance Attribute Summary

Attributes inherited from Processor

#errors, #telemetry

Instance Method Summary collapse

Methods inherited from Processor

#add_keywords, #build_report_root, #debug_payload, #debug_payload?, #errors?, #generated_report_id, #hostname_from_config, #keywords, #log_error, #measure, new_processor, #now_utc, #telemetry_as_string

Constructor Details

#initialize(data, json_body: true) ⇒ PuppetProcessor

Returns a new instance of PuppetProcessor.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 9

def initialize(data, json_body: true)
  super(data, json_body: json_body)
  measure :parse do
    if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.6.0")
      # Ruby 2.5 or older does not have permitted_classes argument available
      @data = YAML.load(data.gsub(YAML_CLEAN, ""))
    else
      @data = YAML.safe_load(data.gsub(YAML_CLEAN, ""), permitted_classes: [Symbol, Time, Date])
    end
  end
  raise("No content") unless @data
  @body = {}
  @evaluation_times = []
  logger.debug "Processing report #{report_id}"
  debug_payload("Input", @data)
end

Instance Method Details

#build_reportObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 121

def build_report
  process
  if debug_payload?
    logger.debug { JSON.pretty_generate(@body) }
  end
  build_report_root(
    format: "puppet",
    version: 1,
    host: @body["host"],
    proxy: @body["proxy"],
    change: @body["summary"]["foreman"]["change"],
    nochange: @body["summary"]["foreman"]["nochange"],
    failure: @body["summary"]["foreman"]["failure"],
    keywords: @body["keywords"],
    body: @body,
  )
end

#processObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 93

def process
  @body["format"] = "puppet"
  @body["id"] = report_id
  @body["host"] = hostname_from_config || @data["host"]
  @body["proxy"] = Proxy::Reports::Plugin.settings.reported_proxy_hostname
  @body["reported_at"] = @data["time"]
  @body["reported_at_proxy"] = now_utc
  KEYS_TO_COPY.each do |key|
    @body[key] = @data[key]
  end
  process_root_keywords
  measure :process_logs do
    @body["logs"] = process_logs
  end
  measure :process_resource_statuses do
    @body["resource_statuses"] = process_resource_statuses
  end
  measure :process_summary do
    @body["summary"] = process_summary
  end
  measure :process_evaluation_times do
    @body["evaluation_times"] = process_evaluation_times
  end
  @body["telemetry"] = telemetry
  @body["keywords"] = keywords
  @body["errors"] = errors if errors?
end

#process_evaluation_timesObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 80

def process_evaluation_times
  @evaluation_times.sort! { |a, b| b[1] <=> a[1] }
  if @evaluation_times.count > MAX_EVAL_TIMES
    others = @evaluation_times[MAX_EVAL_TIMES..@evaluation_times.count - 1].sum { |x| x[1] }
    @evaluation_times = @evaluation_times[0..MAX_EVAL_TIMES - 1]
    @evaluation_times << ["Others", others] if others > 0.0001
  end
  @evaluation_times
rescue StandardError => e
  log_error("Unable to parse evaluation_times", e)
  []
end

#process_logsObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 30

def process_logs
  logs = []
  @data["logs"]&.each do |log|
    logs << [log["level"]&.to_s, log["source"], log["message"]]
  end
  logs
rescue StandardError => e
  log_error("Unable to parse logs", e)
  logs
end

#process_resource_statusesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 60

def process_resource_statuses
  statuses = []
  @data["resource_statuses"]&.each_pair do |key, value|
    statuses << key
    @evaluation_times << [key, value["evaluation_time"]]
    add_keywords("PuppetFailed:#{key}", "PuppetFailed") if value["failed"]
    add_keywords("PuppetFailedToRestart:#{key}", "PuppetFailedToRestart") if value["failed_to_restart"]
    add_keywords("PuppetCorrectiveChange") if value["corrective_change"]
    add_keywords("PuppetSkipped") if value["skipped"]
    add_keywords("PuppetRestarted") if value["restarted"]
    add_keywords("PuppetScheduled") if value["scheduled"]
    add_keywords("PuppetOutOfSync") if value["out_of_sync"]
    add_keywords("PuppetEnvironment:#{@data["environment"]}") if @data["environment"]
  end
  statuses
rescue StandardError => e
  log_error("Unable to parse resource_statuses", e)
  statuses
end

#process_root_keywordsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 41

def process_root_keywords
  status = @data["status"]
  if status == "changed"
    add_keywords("PuppetStatusChanged")
  elsif status == "unchanged"
    add_keywords("PuppetStatusUnchanged")
  elsif status == "failed"
    add_keywords("PuppetStatusFailed")
  end
  if @data["noop"] == "true"
    add_keywords("PuppetNoop")
  end
  if @data["noop_pending"] == "true"
    add_keywords("PuppetNoopPending")
  end
rescue StandardError => e
  log_error("Unable to parse root keywords", e)
end

#report_idObject



26
27
28
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 26

def report_id
  @data["transaction_uuid"] || generated_report_id
end

#spool_reportObject



139
140
141
142
143
144
145
146
# File 'lib/smart_proxy_reports/puppet_processor.rb', line 139

def spool_report
  report_hash = build_report
  debug_payload("Output", report_hash)
  payload = measure :format do
    report_hash.to_json
  end
  SpooledHttpClient.instance.spool(:report, payload)
end