lrc_report_full 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/lrc_report_full.rb +85 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 883622f28f34424c2860dcdf897dcaf908680131f286688d41be95967276e16c
4
+ data.tar.gz: 007f4d7474d577da2bd5ad7e87a92432843965325be204b1809f703124419c7b
5
+ SHA512:
6
+ metadata.gz: 7b310d2732754ca43a0a657af87eb95debe53cf1e76d3d6018d43755ad533dad43ae5bd24ffc9af350cdbbb0ab9921af3ce30c863de979c7192e372de6acfa5e
7
+ data.tar.gz: f2f887e74a9854e05ca9183c96fe06bf33780e8ced897decb345e2d785ecb0d8832116136c30280d20cc5045057a5eb348bb01cafb28a36bf5e4d0bb8161f547
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Importing Required libs
4
+ require 'chef'
5
+ require 'chef/handler'
6
+ require 'socket'
7
+ require 'timeout'
8
+ require 'net/http'
9
+ require 'ffi_yajl'
10
+
11
+ class Chef
12
+ class Handler
13
+ # Class for Lrc report
14
+ class LrcReportFull < Chef::Handler
15
+ attr_reader :config
16
+
17
+ # Initalizing Variables
18
+ def initialize(config = {})
19
+ @config = config
20
+ @config[:url] ||= 'https://exemple.com'
21
+ @config[:upload] ||= true
22
+ @config[:ssl_verify] ||= true
23
+ @config[:pathSave] ||= "/var/chef/reports"
24
+ @config[:drop_file] ||= true
25
+ @ssl_verify = @config[:ssl_verify] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
26
+ end
27
+
28
+ # Building the report
29
+ def report
30
+ # Report Array in json
31
+ if exception
32
+ Chef::Log.error("Creating JSON exception report")
33
+ else
34
+ Chef::Log.info("Creating JSON run report")
35
+ end
36
+ result = false
37
+ build_report_dir
38
+ savetime = Time.now.strftime("%Y%m%d%H%M%S")
39
+ File.open(File.join(config[:pathSave], "chef-run-report-#{savetime}.json"), "w") do |file|
40
+
41
+ # ensure start time and end time are output in the json properly in the event activesupport happens to be on the system
42
+ run_data = data
43
+ run_data[:start_time] = run_data[:start_time].to_s
44
+ run_data[:end_time] = run_data[:end_time].to_s
45
+ encoder = FFI_Yajl::Encoder.new( :validate_utf8 => false )
46
+ report = encoder.encode( run_data )
47
+ #file.puts Chef::JSONCompat.to_json_pretty(run_data)
48
+ #report = Chef::JSONCompat.to_json_pretty(run_data)
49
+ file.puts report
50
+ report = report.gsub('""', '"NULL"')
51
+ if config[:upload]
52
+ result = lrc_request('/api/v2/report/publish', report)
53
+ end
54
+ end
55
+ if result
56
+ File.delete(config[:pathSave] + "/chef-run-report-#{savetime}.json")
57
+ end
58
+ end
59
+
60
+ def lrc_request(path, body, method = 'post')
61
+ uri = URI.parse(config[:url])
62
+ http = Net::HTTP.new(uri.host, uri.port)
63
+ http.use_ssl = true
64
+ http.verify_mode = @ssl_verify
65
+ req = Net::HTTP.const_get(method.capitalize).new("#{uri}#{path}")
66
+ req.add_field('Accept', 'application/json')
67
+ req.add_field('Content-Type', 'application/json')
68
+ req.add_field('User-Agent', "LRC_handler_report v1.0, NODE:#{body['report']['host']}, chef-client v#{Chef::VERSION}")
69
+ req.body = body
70
+ response = http.request(req)
71
+ Chef::Log.info("The report API has return: #{response.inspect} from #{uri}#{path}")
72
+ Chef::Log.debug("Report Content: #{body}")
73
+ return response.kind_of? Net::HTTPSuccess
74
+ end
75
+
76
+ def build_report_dir
77
+ unless File.exist?(config[:pathSave])
78
+ FileUtils.mkdir_p(config[:pathSave])
79
+ File.chmod(00700, config[:pathSave])
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lrc_report_full
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Eric Mansa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a report handler for chef
14
+ email: e.mansa@linkbynet.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/lrc_report_full.rb
20
+ homepage: https://rubygems.org/gems/lrc_report_full
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.2.22
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: send chef report to our lrc server
43
+ test_files: []