Permalink
Browse files

collectd cookbook

  • Loading branch information...
1 parent 85d63a2 commit 35ffb04977f02d3e5b42339f422801dc63308b22 @jimneath jimneath committed Oct 10, 2012
@@ -0,0 +1,121 @@
+require 'chef/resource'
+require 'chef/provider'
+require 'chef/mixin/command'
+require 'chef/log'
+
+class Chef
+ class Resource
+ class Collectd < Chef::Resource
+ if Chef::VERSION == '0.6.0.2'
+ def initialize(name, collection = nil, node = nil)
+ super(name, collection, node)
+ init(name)
+ end
+ else
+ def initialize(name, run_context = nil)
+ super(name, run_context)
+ init(name)
+ end
+ end
+
+ def init(name)
+ @resource_name = :collectd
+ @action = :update
+ @allowed_actions.push(:update)
+
+ @load = nil
+ @root_space = nil
+ @db_space = nil
+ @data_space = nil
+ @mnt_space = nil
+ end
+
+ def load(arg = nil)
+ arg = check_options(arg)
+ set_or_return(:load, arg, :kind_of => [Hash])
+ end
+
+ def root_space(arg = nil)
+ arg = check_options(arg)
+ set_or_return(:root_space, arg, :kind_of => [Hash])
+ end
+
+ def db_space(arg = nil)
+ arg = check_options(arg)
+ set_or_return(:db_space, arg, :kind_of => [Hash])
+ end
+
+ def data_space(arg = nil)
+ arg = check_options(arg)
+ set_or_return(:data_space, arg, :kind_of => [Hash])
+ end
+
+ def mnt_space(arg = nil)
+ arg = check_options(arg)
+ set_or_return(:mnt_space, arg, :kind_of => [Hash])
+ end
+
+ protected
+
+ def check_options(arg = nil)
+ if arg.is_a?(Array)
+ raise ArgumentError unless arg.length == 2
+ arg = { :warning => arg[0], :failure => arg[1] }
+ end
+
+ if arg.is_a?(Hash)
+ raise ArgumentError unless arg.key?(:warning) && arg.key?(:failure)
+
+ arg.each_pair do |key, value|
+ if value.is_a?(String)
+ arg[key] = case value
+ when /^([\d\.]+)kb/i; $1.to_f * 1024
+ when /^([\d\.]+)mb/i; $1.to_f * 1048576
+ when /^([\d\.]+)gb/i; $1.to_f * 1073741824
+ else value
+ end
+ end
+ end
+ end
+
+ arg
+ end
+ end
+ end
+
+ class Provider
+ class Collectd < Chef::Provider
+ include Chef::Mixin::Command
+
+ def load_current_resource
+ true
+ end
+
+ def action_update
+ expressions = []
+
+ %w[load root_space db_space data_space mnt_space].each do |type|
+ if (threshold = @new_resource.send(type))
+ Chef::Log.info "Setting collectd #{type} thresholds (#{threshold.inspect})"
+ plugin = type[/_space$/] ? "df-#{type[/(.*)_space$/, 1]}" : type
+
+ threshold.each_pair do |k, v|
+ k = k.to_s.capitalize
+ expressions << %Q{/Plugin "#{plugin}"/,/Plugin/{s/#{k}Max.*/#{k}Max #{v}/}}
+ end
+ end
+ end
+
+ flags = expressions.map{|e| "-e '#{e}'"}.join(' ')
+ command = "sed -i -r #{flags} /etc/engineyard/collectd.conf && pkill -9 collectd"
+ status = run_command(:command => command)
+
+ if status
+ Chef::Log.info("Ran #{@new_resource} successfully")
+ end
+ end
+ end
+ end
+end
+
+Chef::Platform.platforms[:default].merge!(:collectd => Chef::Provider::Collectd)
@@ -0,0 +1,61 @@
+# Collectd
+
+A chef recipe that allows you to tune Collectd alert levels.
+
+## Example
+
+```ruby
+collectd do
+ load :warning => 4, :failure => 8
+ db_space ['1.3GB', '500MB']
+ data_space [3000000000, 1500000000]
+ root_space :warning => '1245MB', :failure => '500MB'
+ mnt_space :warning => 400000, :failure => 200000
+end
+```
+
+## Options
+
+Below is a list of all available options that can be passed to the `collectd` block:
+
+| option | description |
+|------------|---------------------------------------------------------------------------|
+| load | sets the alerting levels for the CPU load |
+| db_space | sets the alerting levels for the disk space remaining on the /db volume |
+| data_space | sets the alerting levels for the disk space remaining on the /data volume |
+| root_space | sets the alerting levels for the disk space remaining on the / volume |
+| mnt_space | sets the alerting levels for the disk space remaining on the /mnt volume |
+
+## Arguments
+
+Each option can be passed either a `Hash` or an `Array`:
+
+### Hash
+
+If passing in a `Hash`, it must contain a `:warning` and a `:failure` key. eg:
+
+```ruby
+collectd do
+ load :warning => 4, :failure => 8
+end
+```
+
+### Array
+
+If passing in an `Array`, it must contain two items. The first item will be used as the warning and the second as the failure level eg:
+
+```ruby
+collectd do
+ load [4, 8]
+end
+```
+
+### Values
+
+Values can either be a `Numeric` value or a `String`. When using a `String`, you may use units (KB, MB, GB) eg:
+
+```ruby
+collectd do
+ db_space ['1.3GB', '500MB']
+end
+```
@@ -0,0 +1,12 @@
+#
+# Cookbook Name:: collectd
+# Recipe:: default
+#
+
+# collectd do
+# load :warning => 4, :failure => 8
+# db_space ['1.3GB', '500MB']
+# data_space [3000000000, 1500000000]
+# root_space :warning => '1245MB', :failure => '500MB'
+# mnt_space :warning => 400000, :failure => 200000
+# end
@@ -7,6 +7,9 @@
# uncomment to turn on thinking sphinx/ultra sphinx. Remember to edit cookbooks/sphinx/recipes/default.rb first!
# require_recipe "sphinx"
+# uncomment to use the collectd recipe. See cookbooks/collectd/readme.md for documentation.
+# require_recipe "collectd"
+
#uncomment to turn on memcached
# require_recipe "memcached"

0 comments on commit 35ffb04

Please sign in to comment.