LrcReport 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cd0f3ca8d3a47b2348fab1095dc1afc53c1491d777f68620a894af959a419c80
4
+ data.tar.gz: 616999d192609e127ecfd838ab22dacb6df3c50d7001771a136be9f5ec8bdd04
5
+ SHA512:
6
+ metadata.gz: 1ee3dba435c58e815f1e77119e0f652cc04b196933267c4f656f8e4d8044eba0fc2532d282bbabde90b55de0d2f461f7ccaf0e29e2bcb8eb2dee2009700d9b96
7
+ data.tar.gz: 56a189b0c165d77d07f36899b8882e75dbffdef4d6d40b04e95c5db7577aa5e550d1aff1e553d24aad99a69a22efc65b9195f9bc1d7374d024a9d271962300b3
@@ -0,0 +1,24 @@
1
+ # Copyright (c) LINKBYNET 2017
2
+
3
+ based on chef_handler_foreman gem package (Copyright (c) 2013 Marek Hulan)
4
+
5
+ MIT License
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Description
2
+
3
+ This gem adds Chef report and attributes handlers that send reports to LRC Project.
4
+ LRC mean LinkByNet Reporter for Chef
5
+
6
+ ## Installation
7
+
8
+ Since it's released as a gem you can simply run this command under root
9
+
10
+ ```sh
11
+ chef gem install lrc_handler
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ In /etc/chef/client.rb:
17
+
18
+ ```ruby
19
+ # this adds new functions to chef configuration
20
+ require 'lrc_handler'
21
+ # here you can specify your connection options
22
+ lrc_server_options :url => 'https://lrc.lbn.fr'
23
+ # add following line if you want to upload reports
24
+ lrc_reports_upload true
25
+ # add following line to manage reports verbosity. Allowed values are debug, notice and error
26
+ lrc_reports_level "notice"
27
+ ```
28
+
29
+ ## packaging
30
+
31
+ Edit the `lib\lbn_report_chef\version.rb` with a new version
32
+
33
+ run the following command:
34
+
35
+ ```sh
36
+ gem build lrc_handler.gemspec
37
+ gem push lrc_handler-x.x.x.gem
38
+ ```
39
+
40
+ gem push require that you can login to rubygems.org
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Importing Required libs
4
+ require 'chef'
5
+ require 'chef/handler'
6
+ require 'net/http'
7
+ require 'uri'
8
+
9
+ class Chef
10
+ class Handler
11
+ # Class for Lrc report
12
+ class LrcReport < Chef::Handler
13
+ attr_writer :url, :upload, :level
14
+
15
+ # Initalizing Variables
16
+ def initialize(options = {})
17
+ options[:url] ||= 'https://gate.linkbynet.com/881c1287e9c9e04af8c1f054bb1c40fe6c44625c0d0c89d1509d3bbfd62f7c21/'
18
+ options[:upload] ||= true
19
+ options[:level] ||= 'info'
20
+ options[:ssl_verify] ||= true
21
+ @url = options[:url]
22
+ @upload = options[:upload]
23
+ @level = options[:level]
24
+ @ssl_verify = options[:ssl_verify] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
25
+ end
26
+
27
+ # Defining Function for posting data to LRC
28
+ # Running chef in debug mode (-l debug) will display the POST data to Stdout
29
+ def lrc_request(path, body, method = 'post')
30
+ uri = URI.parse(@url)
31
+ http = Net::HTTP.new(uri.host, uri.port)
32
+ http.use_ssl = true
33
+ http.verify_mode = @ssl_verify
34
+ req = Net::HTTP.const_get(method.capitalize).new("#{uri}#{path}")
35
+ req.add_field('Accept', 'application/json')
36
+ req.add_field('Content-Type', 'application/json')
37
+ req.add_field('User-Agent', "LRC_handler v1.1, NODE:#{body['report']['host']}, chef-client v#{Chef::VERSION}")
38
+ req.body = body.to_json
39
+ response = http.request(req)
40
+ Chef::Log.info("The report API has return: #{response.inspect} from #{uri}#{path}")
41
+ Chef::Log.debug("Report Content: #{body.to_json}")
42
+ end
43
+
44
+ def resources_per_status
45
+ if Chef::Config.why_run
46
+ {
47
+ 'applied' => 0,
48
+ 'restarted' => 0,
49
+ 'failed' => 0,
50
+ 'failed_restarts' => 0,
51
+ 'skipped' => @total_skipped,
52
+ 'pending' => @total_updated + @total_restarted + @total_failed + @total_failed_restart,
53
+ }
54
+ else
55
+ {
56
+ 'applied' => @total_updated,
57
+ 'restarted' => @total_restarted,
58
+ 'failed' => @total_failed,
59
+ 'failed_restarts' => @total_failed_restart,
60
+ 'skipped' => @total_skipped,
61
+ 'pending' => 0,
62
+ }
63
+ end
64
+ end
65
+
66
+ def report
67
+ report = { 'host' => node.name, 'reported_at' => Time.now.utc.to_s }
68
+ report_status = Hash.new(0)
69
+ report_status['failed'] = 1 if failed?
70
+ report_status['applied'] = run_status.updated_resources.count
71
+ report['status'] = resources_per_status
72
+
73
+ # I can't compute much metrics for now
74
+ metrics = {}
75
+ metrics['resources'] = { 'total' => run_status.all_resources.count }
76
+
77
+ times = {}
78
+ run_status.all_resources.each do |resource|
79
+ resource_name = resource.resource_name
80
+ if times[resource_name].nil?
81
+ times[resource_name] = resource.elapsed_time
82
+ else
83
+ times[resource_name] += resource.elapsed_time
84
+ end
85
+ end
86
+ metrics['time'] = times.merge!('total' => run_status.elapsed_time)
87
+ report['metrics'] = metrics
88
+
89
+ logs = []
90
+ run_status.updated_resources.each do |resource|
91
+ l = { 'log' => { 'sources' => {}, 'messages' => {}, 'level' => 'notice' } }
92
+
93
+ case resource.resource_name.to_s
94
+ when 'template', 'cookbook_file'
95
+ message = resource.diff
96
+ when 'package'
97
+ message = "Installed #{resource.package_name} package in #{resource.version}"
98
+ else
99
+ message = resource.action.to_s
100
+ end
101
+ l['log']['messages']['message'] = message
102
+ l['log']['sources']['source'] = [resource.resource_name.to_s, resource.name].join(' ')
103
+ logs << l
104
+ end
105
+
106
+ # I only set failed to 1 if chef run failed
107
+ if failed?
108
+ logs << {
109
+ 'log' => {
110
+ 'sources' => { 'source' => 'chef' },
111
+ 'messages' => { 'message' => run_status.exception },
112
+ 'level' => 'err'
113
+ }
114
+ }
115
+ end
116
+
117
+ report['logs'] = logs
118
+ lrc_request('/report', { 'report' => report })
119
+ end
120
+
121
+ end
122
+ end
123
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: LrcReport
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.10
5
+ platform: ruby
6
+ authors:
7
+ - Haïdar Salauroo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.14'
27
+ description: Create and Send Chef execution report to LRC
28
+ email:
29
+ - h.salauroo@linkbynet.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.md
35
+ - README.md
36
+ - lib/chef/handler/lrc_report.rb
37
+ homepage: https://github.com/lostmyname/chef-handler-logstash
38
+ licenses: []
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.1.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Custom Chef Handler for LRC
59
+ test_files: []