Class: Chef::Handler::Opsmatic
- Inherits:
-
Chef::Handler
- Object
- Chef::Handler
- Chef::Handler::Opsmatic
- Defined in:
- lib/chef/handler/opsmatic.rb,
lib/chef/handler/opsmatic_version.rb
Constant Summary collapse
- VERSION =
"0.0.20"
Instance Method Summary collapse
- #check_proxy(proxy) ⇒ Object
-
#collect_resources(all_resources) ⇒ Object
collects up details on file resources managed by chef on the host and writes the list to a directory for the opsmatic-agent to consume to hint at interesting files the agent can watch.
-
#initialize(config = {}) ⇒ Opsmatic
constructor
A new instance of Opsmatic.
-
#report ⇒ Object
prepares a report of the current chef run.
-
#submit(event) ⇒ Object
submit report to the opsmatic collector.
- #write_metadata ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Opsmatic
Returns a new instance of Opsmatic.
13 14 15 16 17 |
# File 'lib/chef/handler/opsmatic.rb', line 13 def initialize(config = {}) @config = config @config[:agent_dir] ||= "/var/db/opsmatic-agent" @watch_files = {} end |
Instance Method Details
#check_proxy(proxy) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/chef/handler/opsmatic.rb', line 121 def check_proxy(proxy) proxy_value = ENV[proxy] return if proxy_value.nil? || proxy_value.empty? begin proxy_uri = URI.parse(proxy_value) if not proxy_uri.host Chef::Log.warn("#{proxy} set but could not parse URI") return nil end rescue URI::InvalidURIError Chef::Log.warn("#{proxy} set with invalid URI") return nil end return proxy_uri end |
#collect_resources(all_resources) ⇒ Object
collects up details on file resources managed by chef on the host and writes the list to a directory for the opsmatic-agent to consume to hint at interesting files the agent can watch
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/chef/handler/opsmatic.rb', line 84 def collect_resources(all_resources) return unless File.directory?(@config[:agent_dir]) # if the chef run didn't even make it far enough to report resources # we don't want to do anything return if all_resources.nil? or all_resources.empty? all_resources.each do |resource| case resource when Chef::Resource::File @watch_files[resource.path] = true when Chef::Resource::CookbookFile @watch_files[resource.path] = true when Chef::Resource::Template @watch_files[resource.path] = true when Chef::Resource::RemoteFile @watch_files[resource.path] = true end end begin data_dir = "#{@config[:agent_dir]}/external.d" if not File.directory?(data_dir) Dir.mkdir(data_dir) end File.open("#{data_dir}/chef_resources.json", "w") do |f| watchlist = [] @watch_files.keys.each do |k| watchlist << { "path" => k } end f.write({ "files" => watchlist }.to_json) end rescue Exception => msg Chef::Log.warn("Unable to save opsmatic agent file watch list: #{msg}") end end |
#report ⇒ Object
prepares a report of the current chef run
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/chef/handler/opsmatic.rb', line 20 def report if @config[:integration_token].nil? || @config[:integration_token].empty? Chef::Log.warn("Opsmatic integraton integration_token missing, report handler disabled") return end if run_status.success? resource_count = run_status.updated_resources.count resource_word = (resource_count == 1) ? "resource" : "resources" summary = "Chef updated #{resource_count} #{resource_word}" else summary = "Chef run failed" end opsmatic_event = { :timestamp => run_status.end_time.to_i, :source => 'chef_raw', :category => 'automation', :type => 'cm/chef', :summary => summary, :scopes => { :hostname => node.fqdn }, :data => { :status => run_status.success? ? "success" : "failure", :start_time => run_status.start_time.to_i, :end_time => run_status.end_time.to_i, :duration => run_status.elapsed_time, :updated_resources => [] } } if not run_status.updated_resources.nil? run_status.updated_resources.each do |resource| detail = { :cookbook_name => resource.cookbook_name, :recipe_name => resource.recipe_name, :action => resource.action, :name => resource.name, :resource_name => resource.resource_name } opsmatic_event[:data][:updated_resources] << detail end end # if there's an exception include details in event if !run_status.exception.nil? clean_exception = run_status.formatted_exception.encode('UTF-8', {:invalid => :replace, :undef => :replace, :replace => '?'}) opsmatic_event[:data][:exception] = clean_exception end # analyze and collect any potentially monitorable resources collect_resources run_status.all_resources # submit our event submit opsmatic_event # write the node attributes and cookbook versions to json files for the agent end |
#submit(event) ⇒ Object
submit report to the opsmatic collector
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/chef/handler/opsmatic.rb', line 138 def submit(event) Chef::Log.info("Posting chef run report to Opsmatic") url = URI.parse(@config[:collector_url]) qs = url.query.nil? ? [] : url.query.split("&") qs << "token=#{@config[:integration_token]}" url.query = qs.join("&") proxy_uri = nil %w(HTTPS_PROXY https_proxy HTTP_PROXY http_proxy).each do |proxy| proxy_uri = check_proxy(proxy) break if proxy_uri end if proxy_uri p_user, p_pass = proxy_uri.userinfo.split(/:/) if proxy_uri.userinfo http = Net::HTTP.new(url.host, url.port, proxy_uri.host, proxy_uri.port, p_user, p_pass) else http = Net::HTTP.new(url.host, url.port) end timeout = @config[:timeout] if timeout.nil? timeout = 5 else timeout = Integer(timeout) end http.open_timeout = timeout http.read_timeout = timeout http.use_ssl = (url.scheme == 'https') if not @config[:ssl_peer_verify] # TODO: need to work out how to correctly find CA's on all platforms http.verify_mode = OpenSSL::SSL::VERIFY_NONE end request = Net::HTTP::Post.new(url.request_uri) request["Content-Type"] = "application/json" request["User-Agent"] = "Opsmatic Chef Handler #{Chef::Handler::Opsmatic::VERSION}" request.body = event.to_json begin response = http.request(request) if response.code != "202" Chef::Log.warn("Got a #{response.code} from Opsmatic event service, chef run wasn't recorded") Chef::Log.info(response.body) end rescue Timeout::Error Chef::Log.warn("Timed out connecting to Opsmatic event service, chef run wasn't recorded") rescue Exception => msg Chef::Log.warn("An unhandled execption occured while posting event to Opsmatic event service: #{msg}") end end |
#write_metadata ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/chef/handler/opsmatic.rb', line 194 def () ext_dir = "#{@config[:agent_dir]}/user_data/metadata" unless File.exists?(ext_dir) FileUtils.mkdir_p(ext_dir) end begin node_json = Chef::JSONCompat.to_json_pretty(data[:node]) node_hash = JSON.parse(node_json) node_hash.delete("automatic") node_hash["opsmatic_event_category"] = "automation" attribute_json = node_hash.to_json rescue Exception => msg Chef::Log.warn("An unhandled execption while preparing to write node data: #{msg}") return end begin filename = File.join(ext_dir, "chef_attributes.json") File.open(filename, "w") do |file| file.puts attribute_json end rescue Exception => msg Chef::Log.warn("An unhandled execption while writing to #{filename}: #{msg}") return end begin cookbook_hash = {} run_context.cookbook_collection.keys.each do |name| cookbook_hash[name] = run_context.cookbook_collection[name].version end cookbook_hash["opsmatic_event_category"] = "automation" rescue Exception => msg Chef::Log.warn("An unhandled execption while preparing to write cookbook data: #{msg}") return end begin filename = File.join(ext_dir, "chef_cookbooks.json") File.open(filename, "w") do |file| file.puts cookbook_hash.to_json end rescue Exception => msg Chef::Log.warn("An unhandled execption while writing to #{filename}: #{msg}") return end end |