Skip to content

Commit

Permalink
collectd cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
jimneath committed Oct 29, 2012
1 parent 85d63a2 commit 35ffb04
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
121 changes: 121 additions & 0 deletions cookbooks/collectd/libraries/collectd.rb
Original file line number Original file line Diff line number Diff line change
@@ -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)
61 changes: 61 additions & 0 deletions cookbooks/collectd/readme.md
Original file line number Original file line Diff line number Diff line change
@@ -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
```
12 changes: 12 additions & 0 deletions cookbooks/collectd/recipes/default.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions cookbooks/main/recipes/default.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# uncomment to turn on thinking sphinx/ultra sphinx. Remember to edit cookbooks/sphinx/recipes/default.rb first! # uncomment to turn on thinking sphinx/ultra sphinx. Remember to edit cookbooks/sphinx/recipes/default.rb first!
# require_recipe "sphinx" # require_recipe "sphinx"


# uncomment to use the collectd recipe. See cookbooks/collectd/readme.md for documentation.
# require_recipe "collectd"

#uncomment to turn on memcached #uncomment to turn on memcached
# require_recipe "memcached" # require_recipe "memcached"


Expand Down

0 comments on commit 35ffb04

Please sign in to comment.