Skip to content
This repository has been archived by the owner on Jul 11, 2020. It is now read-only.

Commit

Permalink
add librato chef handler
Browse files Browse the repository at this point in the history
dwradcliffe committed Jul 13, 2014

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent f55b253 commit 3493297
Showing 5 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Berksfile.lock
Original file line number Diff line number Diff line change
@@ -218,7 +218,7 @@ GRAPH
rubygems-cache (0.0.1)
memcached (>= 0.0.0)
rubygems (>= 0.0.0)
rubygems-chef (0.0.9)
rubygems-chef (0.0.10)
chef-client (>= 0.0.0)
omnibus_updater (>= 0.0.0)
rubygems-ci (0.0.7)
2 changes: 1 addition & 1 deletion cookbooks/rubygems-chef/metadata.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name 'rubygems-chef'
maintainer 'RubyGems.org ops team'

version '0.0.9'
version '0.0.10'

depends 'chef-client'
depends 'omnibus_updater'
2 changes: 2 additions & 0 deletions cookbooks/rubygems-chef/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
# Recipe:: default
#

include_recipe 'rubygems-chef::librato'

cron 'chef-client' do
action :delete
end
18 changes: 18 additions & 0 deletions cookbooks/rubygems-chef/recipes/librato.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Cookbook Name:: rubygems-chef
# Recipe:: librato
#

include_recipe 'chef-vault'
librato_creds = chef_vault_item('librato', 'credentials')

chef_gem 'chef-handler-librato'

template '/etc/chef/client.d/librato.rb' do
source 'librato.rb'
mode '0644'
variables(
email: librato_creds['email'],
api_key: librato_creds['api_key']
)
end
65 changes: 65 additions & 0 deletions cookbooks/rubygems-chef/templates/default/librato.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Mostly taken from https://github.com/bscott/chef-handler-librato
# and https://github.com/CozyCo/chef-handler-librato

require 'rubygems'
Gem.clear_paths
require 'librato/metrics'
require 'chef'
require 'chef/handler'

class LibratoReporting < Chef::Handler
attr_accessor :email, :api_key

def report
@source = node.name

Librato::Metrics.authenticate @email, @api_key

gauge_metrics = {}
counter_metrics = {}
gauge_metrics[:updated_resources] = run_status.updated_resources.length
gauge_metrics[:all_resources] = run_status.all_resources.length
gauge_metrics[:elapsed_time] = run_status.elapsed_time.to_i

if run_status.success?
counter_metrics[:success] = 1
counter_metrics[:fail] = 0
else
counter_metrics[:success] = 0
counter_metrics[:fail] = 1
end

gauge_metrics.each do |metric, value|
Chef::Log.debug("#{metric} #{value} #{Time.now}")
begin
Librato::Metrics.submit :"chef.#{metric}" => { type: :gauge, value: value, source: @source }
rescue => e
puts "#{e}"
end
end

counter_metrics.each do |metric, value|
Chef::Log.debug("#{metric} #{value} #{Time.now}")
begin
Librato::Metrics.submit :"chef.#{metric}" => { type: :counter, value: value, source: @source }
rescue => e
puts "#{e}"
end
end
begin
Librato::Metrics.annotate 'chef.runs', "Chef run #{Time.now}",
source: @source,
description: "updated #{run_status.updated_resources.length} resources",
start_time: run_status.start_time.to_i,
end_time: run_status.end_time.to_i
rescue => e
puts "#{e}"
end
end
end

librato_handler = LibratoReporting.new
librato_handler.email = '<%= @email %>'
librato_handler.api_key = '<%= @api_key %>'
report_handlers << librato_handler
exception_handlers << librato_handler

0 comments on commit 3493297

Please sign in to comment.