Skip to content

Commit

Permalink
Merge pull request #211 from coreymbe/PIE-1374-metrics_parsing
Browse files Browse the repository at this point in the history
(PIE-1374) Refactor metrics parsing
  • Loading branch information
coreymbe authored Jan 12, 2024
2 parents 3ca7a4e + 7f47c33 commit 2ca22e2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ All notable changes to this project will be documented in this file. The format

### Fixed

- No longer utilizing `parse_legacy_metrics` function for metrics collected with older versions of `puppet_metrics_collector`. [#211](https://github.com/puppetlabs/puppetlabs-splunk_hec/pull/211)

- False positive when attempting to rescue required facts from an unconfigured `splunk_hec::facts_blocklist`. [#210](https://github.com/puppetlabs/puppetlabs-splunk_hec/pull/210)

- Settings are now removed from `puppet.conf` when `splunk_hec::disabled` is set to **true**. [#205](https://github.com/puppetlabs/puppetlabs-splunk_hec/pull/205)
Expand Down
58 changes: 8 additions & 50 deletions lib/puppet/application/splunk_hec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,14 @@ def upload_report(data, _sourcetype)
end

def main
# This is waiting for > 5.3.0 version of metrics collector
# data = STDIN.lines.map {|l| JSON.parse(l)}
#
# Below works for metrics collection < 5.3.0
begin
datainput = STDIN.read
rescue StandardError => e
Puppet.info 'Unable to parse STDIN, is it text?'
Puppet.info e.message
Puppet.info e.backtrace.inspect
end

data = if datainput.start_with?("{\n")
# Pretty-printed output produced by puppet_metrics_collector
# prior to the 5.3.0 release
parse_legacy_metrics(datainput)
else
parse_metrics(datainput)
data = begin
STDIN.each_line.map { |l| JSON.parse(l) }
rescue StandardError => e
Puppet.info 'Unable to parse json from stdin'
Puppet.info e.message
Puppet.info e.backtrace.inspect

[]
end

sourcetype = options[:sourcetype].to_s
Expand All @@ -84,36 +74,4 @@ def main
upload_report(server, sourcetype) if options[:saved_report]
end
end

def parse_metrics(input)
result = begin
input.lines.map { |l| JSON.parse(l) }
rescue StandardError => e
Puppet.info 'Unable to parse json from stdin'
Puppet.info e.message
Puppet.info e.backtrace.inspect

[]
end

result
end

def parse_legacy_metrics(input)
cleaned = input.gsub("\n}{\n", "\n},{\n")
cleaned = cleaned.insert(0, '[')
cleaned = cleaned.insert(-1, ']')

result = begin
JSON.parse(cleaned)
rescue StandardError => e
Puppet.info 'Unable to parse json from stdin'
Puppet.info e.message
Puppet.info e.backtrace.inspect

[]
end

result
end
end
23 changes: 21 additions & 2 deletions lib/puppet/util/splunk_hec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def create_http(source_type)
ssl_ca = build_ca_store(ssl_ca_file)
http.cert_store = ssl_ca
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
Puppet.warn_once('hec_ssl', 'ssl_ca', message, nil, nil, :info)
Puppet.warn_once('hec_ssl', 'ssl_ca', message, :default, :default, :info)
else
message = "will NOT verify #{splunk_url} SSL identity"
Puppet.warn_once('hec_ssl', 'no_ssl', message, nil, nil, :info)
Puppet.warn_once('hec_ssl', 'no_ssl', message, :default, :default, :info)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
Expand Down Expand Up @@ -164,4 +164,23 @@ def sourcetypetime(time, duration = 0)
total = Time.parse((parsed_time + duration).iso8601(3))
'%10.3f' % total.to_f
end

# Legacy function to parse pretty-printed output produced by puppet_metrics_collector prior to v5.3.0
def parse_legacy_metrics(input)
cleaned = input.gsub("\n}{\n", "\n},{\n")
cleaned = cleaned.insert(0, '[')
cleaned = cleaned.insert(-1, ']')

result = begin
JSON.parse(cleaned)
rescue StandardError => e
Puppet.info 'Unable to parse json from stdin'
Puppet.info e.message
Puppet.info e.backtrace.inspect

[]
end

result
end
end
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": [
{
"name": "puppetlabs-puppet_metrics_collector",
"version_requirement": ">= 6.0.0 < 9.0"
"version_requirement": ">= 6.0.0 < 9.0.0"
},
{
"name": "puppetlabs-pe_event_forwarding",
Expand Down

0 comments on commit 2ca22e2

Please sign in to comment.