Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
448 changes: 448 additions & 0 deletions cookbooks/collectd-custom/README.md

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions cookbooks/collectd-custom/attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#
# Cookbook Name:: collectd-custom
# Attributes:: default
#

# default attributes for all platforms
default['collectd-custom']['service_name'] = 'collectd-custom'
default['collectd-custom']['version'] = "5.4.1"
default['collectd-custom']['source_url'] = "http://fossies.org/linux/privat/collectd-#{node['collectd-custom']['version']}.tar.gz"
default['collectd-custom']['checksum'] = "a90fe6cc53b76b7bdd56dc57950d90787cb9c96e"
default['collectd-custom']['dir'] = "/var/lib/collectd-custom"
default['collectd-custom']['pid_file'] = "/var/run/collectd/custom.pid"
default['collectd-custom']['interval'] = 30
default['collectd-custom']['read_threads'] = 5
default['collectd-custom']['hostname'] = nil
default['collectd-custom']['packages'] = ['dev-libs/libgcrypt','sys-devel/libtool']
default['collectd-custom']['user'] = 'root'

# redis plugin requires credis, so provide those details
default['collectd-custom']['credis']['version'] = '0.2.3'
default['collectd-custom']['credis']['source_url'] = "http://credis.googlecode.com/files/credis-#{node['collectd-custom']['credis']['version']}.tar.gz"
default['collectd-custom']['credis']['checksum'] = "052ad7ebedf86ef3825a3863cf802baf289a624b"

# graphite configuration if you want to report there
default['collectd-custom']['graphite']['host'] = nil
default['collectd-custom']['graphite']['port'] = 2003
default['collectd-custom']['graphite']['extra_config'] = {
'Protocol' => 'tcp',
'Prefix' => 'collectd.'
}

# librato configuration if you want to report there
default['collectd-custom']['librato']['src_dir'] = '/var/lib/collectd-librato'
default['collectd-custom']['librato']['repo'] = 'https://github.com/librato/collectd-librato.git'
default['collectd-custom']['librato']['version'] = '0.0.10'

# provide your librato credentials:
default['collectd-custom']['librato']['email'] = nil
default['collectd-custom']['librato']['api_token'] = nil
default['collectd-custom']['librato']['extra_config'] = {
# Config for the librato collection plugin
# For full reference see https://github.com/librato/collectd-librato/blob/master/README.md
'TypesDB' => "#{node['collectd-custom']['dir']}/share/collectd/types.db",
'LowercaseMetricNames' => true
}

# plugins to automatically include
default['collectd-custom']['plugins'] = {
'logfile' => { 'config' => {
'LogLevel' => 'info',
'File' => '/var/log/collectd-custom.log',
'Timestamp' => true
} },
'cpu' => {},
'load' => {},
'swap' => {},
'memory' => {},
'disk' => { 'config' => {
'Disk' => '/^xv/',
'IgnoreSelected' => false
} },
'interface' => { 'config' => {
'Interface' => 'eth0',
'IgnoreSelected' => false
} }

# Maybe you want to log to syslog instead?
# 'syslog' => { 'config' => {
# 'LogLevel' => 'info'
# } },

# You can track all memcached instances if you want, or override
# on your memcached instance if you have one.
# 'memcached' => { 'config' => {
# 'Host' => '127.0.0.1', 'Port' => '11211'
# } },
}
39 changes: 39 additions & 0 deletions cookbooks/collectd-custom/definitions/collectd_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# Cookbook Name:: collectd-custom
# Definitions:: collectd_plugin
#

define :collectd_plugin, :options => {}, :template => nil, :cookbook => nil do
template "/data/collectd.d/plugin-#{params[:name]}.conf" do
owner node['collectd-custom']['user']
group node['collectd-custom']['user']
mode 0644
if params[:template].nil?
source "plugin.conf.erb"
cookbook params[:cookbook] || 'collectd-custom'
else
source params[:template]
cookbook params[:cookbook]
end
variables({
:name => params[:name],
:options => params[:options]
})
notifies :restart, "service[#{node['collectd-custom']['service_name']}]"
end
end

define :collectd_python_plugin, :options => {}, :module => nil, :path => nil do
begin
t = resources(:template => '/data/collectd.d/plugin-python.conf')
rescue ArgumentError,Chef::Exceptions::ResourceNotFound
collectd_plugin "python" do
options :paths => ["#{node['collectd-custom']['dir']}/lib/collectd"], :modules => {}
template 'python_plugin.conf.erb'
cookbook 'collectd-custom'
end
retry
end
t.variables[:options][:paths] << params[:path] unless params[:path].nil?
t.variables[:options][:modules][params[:module] || params[:name]] = params[:options]
end
29 changes: 29 additions & 0 deletions cookbooks/collectd-custom/libraries/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Cookbook Name:: collectd-custom
# Library:: default
#

def collectd_plugin_option_value(option)
return option if option.instance_of?(Fixnum) || option == true || option == false
"\"#{option}\""
end

def collectd_plugin_conf(config, level=0)
conf = []
config.to_hash.each_pair do |key, value|
if value.is_a? Array
value.each do |subvalue|
conf << "#{key} #{collectd_plugin_option_value(subvalue)}"
end
elsif value.is_a? Hash
value.each_pair do |name, suboptions|
conf << "<#{key} \"#{name}\">"
conf << "#{collectd_plugin_conf(suboptions, level+2)}"
conf << "</#{key}>"
end
else
conf << "#{key} #{collectd_plugin_option_value(value)}"
end
end
conf.join("\n" + (" "*level))
end
9 changes: 9 additions & 0 deletions cookbooks/collectd-custom/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name 'collectd-custom'
maintainer 'Stephen Craton'
maintainer_email '[email protected]'
license 'MIT License'
description 'Installs/Configures collectd-custom'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.1.0'

supports 'gentoo'
30 changes: 30 additions & 0 deletions cookbooks/collectd-custom/recipes/credis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Cookbook Name:: collectd-custom
# Recipe:: credis
#
# Installs credis library for use by the redis collectd plugin
#

attributes = node['collectd-custom']['credis']

# Download source
remote_file "#{Chef::Config[:file_cache_path]}/credis-#{attributes['version']}.tar.gz" do
source attributes['source_url']
checksum attributes['checksum']
action :create_if_missing
end

# Compile
bash "install-credis" do
cwd Chef::Config[:file_cache_path]
code <<-EOH
[ -d credis-#{attributes['version']}/ ] || mkdir credis-#{attributes['version']}/
tar -xzf credis-#{attributes['version']}.tar.gz -C credis-#{attributes['version']}/ --strip 1
cd credis-#{attributes['version']}
make
cp -f libcredis.so /usr/lib
cp -f libcredis.a /usr/include
cp -f credis.h /usr/include
EOH
not_if "[ -f /usr/lib/libcredis.so ]"
end
129 changes: 129 additions & 0 deletions cookbooks/collectd-custom/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#
# Cookbook Name:: collectd-custom
# Recipe:: default
#

attributes = node['collectd-custom']
service = "service[#{attributes['service_name']}]"
config_opts = []

# Emerge required packages
attributes['packages'].each do |pkg|
package pkg do
action :install
end
end

# Download source
remote_file "#{Chef::Config[:file_cache_path]}/collectd-#{attributes['version']}.tar.gz" do
source attributes['source_url']
checksum attributes['checksum']
action :create_if_missing
end

# prefix
config_opts << "--prefix=#{attributes['dir']}"

# platform specific
case node['platform_family']
when 'gentoo'
config_opts << "--without-included-ltdl"
config_opts << "--with-libiptc=no" unless attributes['plugins'].include? 'iptables'
end

# Compile
bash "install-collectd" do
cwd Chef::Config[:file_cache_path]
code <<-EOH
[ -d collectd-#{attributes['version']}/ ] || mkdir collectd-#{attributes['version']}/
tar -xzf collectd-#{attributes['version']}.tar.gz -C collectd-#{attributes['version']}/ --strip 1
cd collectd-#{attributes['version']}
export EPYTHON="python2.7"
./configure #{config_opts.join(" ")}
make && make install
EOH
not_if "#{attributes['dir']}/sbin/collectd -h 2>&1 | grep #{attributes['version']}"
end

# Create init.d
template "/etc/init.d/#{attributes['service_name']}" do
owner 'root'
group 'root'
mode 0766
source 'collectd.init.erb'
variables({
:dir => attributes['dir'],
:pid_file => attributes['pid_file'],
:pid_dir => File.dirname(attributes['pid_file']),
:user => attributes['user']
})
notifies :restart, "service[#{attributes['service_name']}]"
end

# Create custom config directory under EY mount
directory "/data/collectd.d" do
owner attributes['user']
group attributes['user']
mode 0755
action :create
end

link "#{attributes['dir']}/etc/conf.d" do
to "/data/collectd.d"
end

# Create the logfile if that's configured
if attributes['plugins'].keys.include?('logfile')
logfile_config = attributes['plugins']['logfile']['config']
logfile_dir = File.dirname(logfile_config['File'])

directory logfile_dir do
owner attributes['user']
group attributes['user']
mode 0755
recursive true
not_if "[ -d #{logfile_dir} ]"
end

file logfile_config['File'] do
owner attributes['user']
group attributes['user']
mode 0644
action :create_if_missing
end

template "/etc/logrotate.d/#{attributes['service_name']}" do
owner 'root'
group 'root'
mode 0644
source 'logrotate.conf.erb'
variables({
:log_file => logfile_config['File'],
:rotate => 7
})
end
end

# Create the base configuration
template "#{attributes['dir']}/etc/collectd.conf" do
mode "0644"
source "collectd.conf.erb"
variables({
:hostname => attributes['hostname'],
:pid_file => attributes['pid_file'],
:dir => attributes['dir'],
:interval => attributes['interval'],
:read_threads => attributes['read_threads'],
:plugins => attributes['plugins']
})
notifies :restart, "service[#{attributes['service_name']}]"
end

# Configure plugins provided by DNA
include_recipe "collectd-custom::plugins"

# Start collectd-custom
service attributes['service_name'] do
supports :status => true, :restart => true
action [ :enable, :start ]
end
20 changes: 20 additions & 0 deletions cookbooks/collectd-custom/recipes/graphite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Cookbook Name:: collectd-custom
# Recipe:: graphite
#
# Installs and configures the WriteGraphite plugin for the collectd-custom process
#

graphite = node['collectd-custom']['graphite']

collectd_plugin "write_graphite" do
opts = {
'Node' => {
'carbon' => { 'Host' => graphite['host'], 'Port' => graphite['port'] }
}
}
opts['Node']['carbon'].merge!(graphite['extra_config']) if graphite['extra_config']
options(opts)

not_if { graphite['host'].nil? }
end
54 changes: 54 additions & 0 deletions cookbooks/collectd-custom/recipes/librato.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# Cookbook Name:: collectd-custom
# Recipe:: librato
#
# Installs the librato plugin for the collectd-custom process
#

attributes = node['collectd-custom']
librato = attributes['librato']
plugin_path = "#{attributes['dir']}/lib/collectd/collectd-librato.py"

directory librato['src_dir'] do
user attributes['user']
group attributes['user']
recursive true
action :create
end

execute "install collectd-librato" do
user "root"
cwd librato['src_dir']
command "git clone #{librato['repo']} ."
not_if { File.exists?("#{librato['src_dir']}/.git/")}
end

execute "checkout specific collectd-librato version" do
cwd librato['src_dir']
command "git checkout v#{librato['version']}"
not_if "git describe | grep v#{librato['version']}"
end

execute "install collectd-librato plugin" do
cwd librato['src_dir']
command "cp -f lib/collectd-librato.py #{plugin_path}"
end

collectd_python_plugin "collectd-librato" do
path plugin_path
opts = {
'APIToken' => librato['api_token'],
'Email' => librato['email']
}

opts['Api'] = librato['api'] if librato['api']
opts.merge!(librato['extra_config']) if librato['extra_config']

options(opts)
end

# Delete legacy file, was moved to simplercpu recipe instead
file "/data/collectd.d/librato-aggregate-cpu.conf" do
action :delete
notifies :restart, "service[#{attributes['service_name']}]"
end
Loading