chef-gelf 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in chef-gelf.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,77 @@
1
+ = DESCRIPTION:
2
+
3
+ Provides a Chef handler which can report run status, including any changes that were made, to a Graylog2 server. In the case of failed runs a backtrace will be included in the details reported.
4
+
5
+ = REQUIREMENTS:
6
+
7
+ * A Graylog2 server running somewhere.
8
+
9
+ = USAGE:
10
+
11
+ This example makes of the chef_handler cookbook, place some thing like this in cookbooks/chef_handler/recipes/gelf.rb and add it to your run list. It also assumes your Graylog2 server has set the attribute rsyslog_server to true.
12
+
13
+ log_server = search(:node, "rsyslog_server:true").first
14
+
15
+ if log_server
16
+ include_recipe "chef_handler::default"
17
+
18
+ gem_package "chef-gelf" do
19
+ action :nothing
20
+ end.run_action(:install)
21
+
22
+ # Make sure the newly installed Gem is loaded.
23
+ Gem.clear_paths
24
+ require 'chef/gelf'
25
+
26
+ chef_handler "Chef::GELF::Handler" do
27
+ source "chef/gelf"
28
+ arguments({
29
+ :server => log_server['fqdn']
30
+ })
31
+
32
+ supports :exception => true, :report => true
33
+ end.run_action(:enable)
34
+ end
35
+
36
+ Arguments take the form of an options hash, with the following options:
37
+
38
+ * :server - The server to send messages to.
39
+ * :port (12201) - The port to send on.
40
+ * :facility (chef-client) - The facility to report under.
41
+ * :host (node.fqdn) - The host to report messages as coming from.
42
+ * :blacklist ({}) - A hash of cookbooks, resources and actions to ignore in the change list.
43
+
44
+ = BLACKLISTING:
45
+
46
+ Some resources report themselves as having updated on every run even if nothing changed, or are just things you don't care about. To reduce the amount of noise in your logs these can be ignored by providing a blacklist. In this example we don't want to be told about the GELF handler being activated:
47
+
48
+ chef_handler "Chef::GELF::Handler" do
49
+ source "chef/gelf"
50
+ arguments({
51
+ :server => log_server['fqdn'],
52
+ :blacklist => {
53
+ "chef_handler" => {
54
+ "chef_handler" => [ "nothing", "enable" ]
55
+ }
56
+ }
57
+ })
58
+
59
+ supports :exception => true, :report => true
60
+ end.run_action(:enable)
61
+
62
+ = LICENSE and AUTHOR:
63
+
64
+ Author:: Jon Wood (<jon@blankpad.net>)
65
+ Copyright:: 2011, Blank Pad Development
66
+
67
+ Licensed under the Apache License, Version 2.0 (the "License");
68
+ you may not use this file except in compliance with the License.
69
+ You may obtain a copy of the License at
70
+
71
+ http://www.apache.org/licenses/LICENSE-2.0
72
+
73
+ Unless required by applicable law or agreed to in writing, software
74
+ distributed under the License is distributed on an "AS IS" BASIS,
75
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
76
+ See the License for the specific language governing permissions and
77
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/chef-gelf.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "chef/gelf/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "chef-gelf"
7
+ s.version = Chef::GELF::VERSION
8
+ s.authors = ["Jon Wood"]
9
+ s.email = ["jon@blankpad.net"]
10
+ s.homepage = "https://github.com/jellybob/chef-gelf"
11
+ s.summary = %q{Provides a Chef handler which reports run failures and changes to a Graylog2 server.}
12
+ s.description = File.read("README.rdoc")
13
+
14
+ s.rubyforge_project = "chef-gelf"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "gelf"
22
+ s.add_dependency "chef"
23
+ end
data/lib/chef/gelf.rb ADDED
@@ -0,0 +1,75 @@
1
+ require "chef/gelf/version"
2
+ require 'gelf'
3
+ require 'chef/log'
4
+
5
+ class Chef
6
+ module GELF
7
+ class Handler < Chef::Handler
8
+ attr_reader :notifier
9
+ attr_reader :options
10
+
11
+ def options=(value = {})
12
+ @options = { :port => 12201, :facility => "chef_client", :blacklist => {}, :host => nil }.merge(value)
13
+ end
14
+
15
+ def initialize(options = {})
16
+ self.options = options
17
+
18
+ Chef::Log.debug "Initialised GELF handler for gelf://#{self.options[:server]}:#{self.options[:port]}/#{self.options[:facility]}"
19
+ @notifier = ::GELF::Notifier.new(self.options[:server], self.options[:port], 'WAN', :facility => self.options[:facility])
20
+ end
21
+
22
+ def report
23
+ Chef::Log.debug "Reporting #{run_status.inspect}"
24
+ if run_status.failed?
25
+ Chef::Log.debug "Notifying Graylog server of failure."
26
+ @notifier.notify!(:short_message => "Chef run failed on #{@notifier.host}. Updated #{changes[:count]} resources.",
27
+ :full_message => run_status.formatted_exception + "\n" + Array(backtrace).join("\n") + changes[:message],
28
+ :level => ::GELF::Levels::FATAL,
29
+ :host => host_name)
30
+ else
31
+ Chef::Log.debug "Notifying Graylog server of success."
32
+ @notifier.notify!(:short_message => "Chef run completed on #{node.name} in #{elapsed_time}. Updated #{changes[:count]} resources.",
33
+ :full_message => changes[:message],
34
+ :level => ::GELF::Levels::INFO,
35
+ :host => host_name)
36
+ end
37
+ end
38
+
39
+ protected
40
+ def host_name
41
+ options[:host] || node[:fqdn]
42
+ end
43
+
44
+ def changes
45
+ @changes unless @changes.nil?
46
+
47
+ lines = sanitised_changes.collect do |resource|
48
+ "recipe[#{resource.cookbook_name}::#{resource.recipe_name}] ran '#{resource.action}' on #{resource.resource_name} '#{resource.name}'"
49
+ end
50
+
51
+ count = lines.size
52
+
53
+ message = if count > 0
54
+ "Updated #{count} resources:\n\n#{lines.join("\n")}"
55
+ else
56
+ "No changes made."
57
+ end
58
+
59
+ @changes = { :lines => lines, :count => count, :message => message }
60
+ end
61
+
62
+ def sanitised_changes
63
+ run_status.updated_resources.reject do |updated|
64
+ options[:blacklist].each do |cookbook, resources|
65
+ resources.each do |resource, actions|
66
+ updated.cookbook_name == cookbook &&
67
+ updated.resource_name == resource &&
68
+ actions.collect { |a| a.to_s }.include?(updated.action.to_s)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ class Chef
2
+ module GELF
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-gelf
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Jon Wood
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-06 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: gelf
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: chef
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: |
38
+ = DESCRIPTION:
39
+
40
+ Provides a Chef handler which can report run status, including any changes that were made, to a Graylog2 server. In the case of failed runs a backtrace will be included in the details reported.
41
+
42
+ = REQUIREMENTS:
43
+
44
+ * A Graylog2 server running somewhere.
45
+
46
+ = USAGE:
47
+
48
+ This example makes of the chef_handler cookbook, place some thing like this in cookbooks/chef_handler/recipes/gelf.rb and add it to your run list. It also assumes your Graylog2 server has set the attribute rsyslog_server to true.
49
+
50
+ log_server = search(:node, "rsyslog_server:true").first
51
+
52
+ if log_server
53
+ include_recipe "chef_handler::default"
54
+
55
+ gem_package "chef-gelf" do
56
+ action :nothing
57
+ end.run_action(:install)
58
+
59
+ # Make sure the newly installed Gem is loaded.
60
+ Gem.clear_paths
61
+ require 'chef/gelf'
62
+
63
+ chef_handler "Chef::GELF::Handler" do
64
+ source "chef/gelf"
65
+ arguments({
66
+ :server => log_server['fqdn']
67
+ })
68
+
69
+ supports :exception => true, :report => true
70
+ end.run_action(:enable)
71
+ end
72
+
73
+ Arguments take the form of an options hash, with the following options:
74
+
75
+ * :server - The server to send messages to.
76
+ * :port (12201) - The port to send on.
77
+ * :facility (chef-client) - The facility to report under.
78
+ * :host (node.fqdn) - The host to report messages as coming from.
79
+ * :blacklist ({}) - A hash of cookbooks, resources and actions to ignore in the change list.
80
+
81
+ = BLACKLISTING:
82
+
83
+ Some resources report themselves as having updated on every run even if nothing changed, or are just things you don't care about. To reduce the amount of noise in your logs these can be ignored by providing a blacklist. In this example we don't want to be told about the GELF handler being activated:
84
+
85
+ chef_handler "Chef::GELF::Handler" do
86
+ source "chef/gelf"
87
+ arguments({
88
+ :server => log_server['fqdn'],
89
+ :blacklist => {
90
+ "chef_handler" => {
91
+ "chef_handler" => [ "nothing", "enable" ]
92
+ }
93
+ }
94
+ })
95
+
96
+ supports :exception => true, :report => true
97
+ end.run_action(:enable)
98
+
99
+ = LICENSE and AUTHOR:
100
+
101
+ Author:: Jon Wood (<jon@blankpad.net>)
102
+ Copyright:: 2011, Blank Pad Development
103
+
104
+ Licensed under the Apache License, Version 2.0 (the "License");
105
+ you may not use this file except in compliance with the License.
106
+ You may obtain a copy of the License at
107
+
108
+ http://www.apache.org/licenses/LICENSE-2.0
109
+
110
+ Unless required by applicable law or agreed to in writing, software
111
+ distributed under the License is distributed on an "AS IS" BASIS,
112
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
113
+ See the License for the specific language governing permissions and
114
+ limitations under the License.
115
+
116
+ email:
117
+ - jon@blankpad.net
118
+ executables: []
119
+
120
+ extensions: []
121
+
122
+ extra_rdoc_files: []
123
+
124
+ files:
125
+ - .gitignore
126
+ - Gemfile
127
+ - README.rdoc
128
+ - Rakefile
129
+ - chef-gelf.gemspec
130
+ - lib/chef/gelf.rb
131
+ - lib/chef/gelf/version.rb
132
+ homepage: https://github.com/jellybob/chef-gelf
133
+ licenses: []
134
+
135
+ post_install_message:
136
+ rdoc_options: []
137
+
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: "0"
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: "0"
152
+ requirements: []
153
+
154
+ rubyforge_project: chef-gelf
155
+ rubygems_version: 1.7.0
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: Provides a Chef handler which reports run failures and changes to a Graylog2 server.
159
+ test_files: []
160
+
161
+ has_rdoc: