chef-handler-splunkstorm 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/chef/handler/splunkstorm.rb +118 -0
- metadata +6 -6
- data/lib/chef/handler/splunkstorm_handler.rb +0 -111
@@ -0,0 +1,118 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Chef Exception & Reporting Handler for Splunk Storm.
|
3
|
+
#
|
4
|
+
# Author:: Greg Albrecht <mailto:gba@splunk.com>
|
5
|
+
# Copyright:: Copyright 2012 Splunk, Inc.
|
6
|
+
# License:: Apache License 2.0.
|
7
|
+
#
|
8
|
+
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'chef/handler'
|
12
|
+
require 'json'
|
13
|
+
require 'rest-client'
|
14
|
+
|
15
|
+
|
16
|
+
class Chef
|
17
|
+
class Handler
|
18
|
+
|
19
|
+
# Chef Exception & Reporting Handler for Splunk Storm.
|
20
|
+
#
|
21
|
+
# Extracts several keys from the run_status Hash, converts them to JSON, and
|
22
|
+
# POSTs them to Splunk Storm as an event for later searching & reporting.
|
23
|
+
#
|
24
|
+
# = Usage
|
25
|
+
# 1. Create a Splunk Storm account: https://www.splunkstorm.com
|
26
|
+
# 2. Retrieve your Access Token & Project ID: http://docs.splunk.com/Documentation/Storm/latest/User/UseStormsRESTAPI
|
27
|
+
# 3. Add the chef_handler Cookbook to your Run List.
|
28
|
+
# 4. Add a chef_handler Resource to one of your existing Recipes.
|
29
|
+
#
|
30
|
+
# = Example
|
31
|
+
# Here's an example +chef_handler+ Resource given you've retrieved your
|
32
|
+
# Access Token as *ACCESS_TOKEN* and Project ID as *PROJECT_ID*:
|
33
|
+
#
|
34
|
+
# include_recipe 'chef_handler'
|
35
|
+
#
|
36
|
+
# gem_package('chef-handler-splunkstorm'){action :nothing}.run_action(:install)
|
37
|
+
#
|
38
|
+
# chef_handler 'Chef::Handler::SplunkStorm' do
|
39
|
+
# action :enable
|
40
|
+
# arguments ['ACCESS_TOKEN', 'PROJECT_ID']
|
41
|
+
# source File.join(Gem.all_load_paths.grep(/chef-handler-splunkstorm/).first,
|
42
|
+
# 'chef', 'handler', 'splunkstorm.rb')
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# = Requirements
|
46
|
+
# * +json+ Gem: http://flori.github.com/json/
|
47
|
+
# * +rest-client+ Gem: https://github.com/archiloque/rest-client
|
48
|
+
#
|
49
|
+
# = Links
|
50
|
+
# * Internal Splunk JIRA: http://jira.splunk.com/browse/STORM-3796
|
51
|
+
# * Opscode JIRA: http://tickets.opscode.com/browse/COOK-1208
|
52
|
+
#
|
53
|
+
class SplunkStorm < Chef::Handler
|
54
|
+
SPLUNKSTORM_URL = 'https://api.splunkstorm.com'
|
55
|
+
API_ENDPOINT = '/1/inputs/http'
|
56
|
+
|
57
|
+
# * *Args*:
|
58
|
+
# - +access_token+ -> A Splunk Storm Access Token.
|
59
|
+
# - +project_id+ -> A Splunk Storm Project ID.
|
60
|
+
#
|
61
|
+
def initialize(access_token, project_id)
|
62
|
+
@access_token = access_token
|
63
|
+
@project_id = project_id
|
64
|
+
end
|
65
|
+
|
66
|
+
# Reports Chef's +run_status+ metrics and results to Splunk Storm.
|
67
|
+
def report_metrics
|
68
|
+
event_metadata = {
|
69
|
+
:sourcetype => 'generic_single_line',
|
70
|
+
:host => node.hostname,
|
71
|
+
:project => @project_id}
|
72
|
+
|
73
|
+
# We're creating a new Hash b/c 'node' and 'all_resources' in run_status
|
74
|
+
# are just TOO large.
|
75
|
+
status_event = {
|
76
|
+
:failed => run_status.failed?,
|
77
|
+
:start_time => run_status.start_time,
|
78
|
+
:end_time => run_status.end_time,
|
79
|
+
:elapsed_time => run_status.elapsed_time,
|
80
|
+
:exception => run_status.formatted_exception}
|
81
|
+
|
82
|
+
api_params = event_metadata.collect{ |k,v| [k, v].join('=') }.join('&')
|
83
|
+
url_params = URI.escape(api_params)
|
84
|
+
endpoint_path = [API_ENDPOINT, url_params].join('?')
|
85
|
+
|
86
|
+
request = RestClient::Resource.new(
|
87
|
+
SPLUNKSTORM_URL, :user => @access_token, :password => 'x')
|
88
|
+
|
89
|
+
request[endpoint_path].post(status_event.to_json)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Reports Chef's +run_status+ exception and backtrace to Splunk Storm.
|
93
|
+
def report_exception
|
94
|
+
event_metadata = {
|
95
|
+
:sourcetype => 'storm_multi_line',
|
96
|
+
:host => node.hostname,
|
97
|
+
:project => @project_id}
|
98
|
+
|
99
|
+
api_params = event_metadata.collect{ |k,v| [k, v].join('=') }.join('&')
|
100
|
+
url_params = URI.escape(api_params)
|
101
|
+
endpoint_path = [API_ENDPOINT, url_params].join('?')
|
102
|
+
|
103
|
+
request = RestClient::Resource.new(
|
104
|
+
SPLUNKSTORM_URL, :user => @access_token, :password => 'x')
|
105
|
+
|
106
|
+
request[endpoint_path].post(Array(run_status.backtrace).join("\n"))
|
107
|
+
end
|
108
|
+
|
109
|
+
# Proxies calls for +report_metrics+ and, on failure, +report_exception+.
|
110
|
+
def report
|
111
|
+
report_metrics
|
112
|
+
if run_status.failed?
|
113
|
+
report_exception
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chef-handler-splunkstorm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Greg Albrecht
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05-
|
18
|
+
date: 2012-05-11 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: json
|
@@ -56,8 +56,8 @@ extra_rdoc_files: []
|
|
56
56
|
files:
|
57
57
|
- LICENSE
|
58
58
|
- README.md
|
59
|
-
- lib/chef/handler/
|
60
|
-
homepage: http://
|
59
|
+
- lib/chef/handler/splunkstorm.rb
|
60
|
+
homepage: http://ampledata.org/splunk_storm_chef_handler.html
|
61
61
|
licenses: []
|
62
62
|
|
63
63
|
post_install_message:
|
@@ -1,111 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Chef Exception & Reporting Handler for Splunk Storm.
|
3
|
-
#
|
4
|
-
# Author:: Greg Albrecht <mailto:gba@splunk.com>
|
5
|
-
# Copyright:: Copyright 2012 Splunk, Inc.
|
6
|
-
# License:: Apache License 2.0.
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
require 'rubygems'
|
11
|
-
require 'json'
|
12
|
-
require 'rest-client'
|
13
|
-
|
14
|
-
|
15
|
-
# Chef Exception & Reporting Handler for Splunk Storm.
|
16
|
-
#
|
17
|
-
# Extracts several keys from the run_status Hash, converts them to JSON, and
|
18
|
-
# POSTs them to Splunk Storm as an event for later searching & reporting.
|
19
|
-
#
|
20
|
-
# = Usage
|
21
|
-
# 1. Create a Splunk Storm account: https://www.splunkstorm.com
|
22
|
-
# 2. Retrieve your Access Token & Project ID: http://docs.splunk.com/Documentation/Storm/latest/User/UseStormsRESTAPI
|
23
|
-
# 3. Add the chef_handler Cookbook to your Run List.
|
24
|
-
# 4. Add a chef_handler Resource to one of your existing Recipes.
|
25
|
-
#
|
26
|
-
# = Example
|
27
|
-
# Here's an example +chef_handler+ Resource given you've retrieved your
|
28
|
-
# Access Token as *ACCESS_TOKEN* and Project ID as *PROJECT_ID*:
|
29
|
-
#
|
30
|
-
# include_recipe 'chef_handler'
|
31
|
-
#
|
32
|
-
# gem_package 'chef-handler-splunkstorm'
|
33
|
-
#
|
34
|
-
# chef_handler 'SplunkStormHandler' do
|
35
|
-
# action :enable
|
36
|
-
# arguments ['ACCESS_TOKEN', 'PROJECT_ID']
|
37
|
-
# source 'chef/handler/splunkstorm_handler'
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# = Requirements
|
41
|
-
# * +json+ Gem: http://flori.github.com/json/
|
42
|
-
# * +rest-client+ Gem: https://github.com/archiloque/rest-client
|
43
|
-
#
|
44
|
-
# = Links
|
45
|
-
# * Internal Splunk JIRA: http://jira.splunk.com/browse/STORM-3796
|
46
|
-
# * Opscode JIRA: http://tickets.opscode.com/browse/COOK-1208
|
47
|
-
#
|
48
|
-
class SplunkStormHandler < Chef::Handler
|
49
|
-
SPLUNKSTORM_URL = 'https://api.splunkstorm.com'
|
50
|
-
API_ENDPOINT = '/1/inputs/http'
|
51
|
-
|
52
|
-
# * *Args*:
|
53
|
-
# - +access_token+ -> A Splunk Storm Access Token.
|
54
|
-
# - +project_id+ -> A Splunk Storm Project ID.
|
55
|
-
#
|
56
|
-
def initialize(access_token, project_id)
|
57
|
-
@access_token = access_token
|
58
|
-
@project_id = project_id
|
59
|
-
end
|
60
|
-
|
61
|
-
# Reports Chef's +run_status+ metrics and results to Splunk Storm.
|
62
|
-
def report_metrics
|
63
|
-
event_metadata = {
|
64
|
-
:sourcetype => 'generic_single_line',
|
65
|
-
:host => node.hostname,
|
66
|
-
:project => @project_id}
|
67
|
-
|
68
|
-
# We're creating a new Hash b/c 'node' and 'all_resources' in run_status
|
69
|
-
# are just TOO large.
|
70
|
-
status_event = {
|
71
|
-
:failed => run_status.failed?,
|
72
|
-
:start_time => run_status.start_time,
|
73
|
-
:end_time => run_status.end_time,
|
74
|
-
:elapsed_time => run_status.elapsed_time,
|
75
|
-
:exception => run_status.formatted_exception}
|
76
|
-
|
77
|
-
api_params = event_metadata.collect{ |k,v| [k, v].join('=') }.join('&')
|
78
|
-
url_params = URI.escape(api_params)
|
79
|
-
endpoint_path = [API_ENDPOINT, url_params].join('?')
|
80
|
-
|
81
|
-
request = RestClient::Resource.new(
|
82
|
-
SPLUNKSTORM_URL, :user => @access_token, :password => 'x')
|
83
|
-
|
84
|
-
request[endpoint_path].post(status_event.to_json)
|
85
|
-
end
|
86
|
-
|
87
|
-
# Reports Chef's +run_status+ exception and backtrace to Splunk Storm.
|
88
|
-
def report_exception
|
89
|
-
event_metadata = {
|
90
|
-
:sourcetype => 'storm_multi_line',
|
91
|
-
:host => node.hostname,
|
92
|
-
:project => @project_id}
|
93
|
-
|
94
|
-
api_params = event_metadata.collect{ |k,v| [k, v].join('=') }.join('&')
|
95
|
-
url_params = URI.escape(api_params)
|
96
|
-
endpoint_path = [API_ENDPOINT, url_params].join('?')
|
97
|
-
|
98
|
-
request = RestClient::Resource.new(
|
99
|
-
SPLUNKSTORM_URL, :user => @access_token, :password => 'x')
|
100
|
-
|
101
|
-
request[endpoint_path].post(Array(run_status.backtrace).join("\n"))
|
102
|
-
end
|
103
|
-
|
104
|
-
# Proxies calls for +report_metrics+ and, on failure, +report_exception+.
|
105
|
-
def report
|
106
|
-
report_metrics
|
107
|
-
if run_status.failed?
|
108
|
-
report_exception
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|