Skip to content

Instantly share code, notes, and snippets.

@adrianlzt
Created July 17, 2014 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianlzt/b9605efeab711ea2e1bc to your computer and use it in GitHub Desktop.
Save adrianlzt/b9605efeab711ea2e1bc to your computer and use it in GitHub Desktop.
#
# Collectd check should be defined:
# Name: collectd
# Puppet type: monitorizacion::checks::collectd
# Example: {"Plugin":"processes","Process":"sshd","Threshold":[{"Instance":"sshd","Type":[{"Name":"ps_count","DataSource":"processes","FailureMin":1}]}]}
#
#{
# "Plugin": "processes",
# "Process": "sshd",
# "Threshold": [
# {
# "Instance": "sshd",
# "Type": [
# {
# "DataSource": "processes",
# "FailureMin": 1,
# "Name": "ps_count"
# }
# ]
# }
# ]
#}
#
# Debemos definir el plugin que queramos usar.
# El resto de parametros se meteran dentro de la seccion <Plugin> como "clave valor"
# Si definimos una seccion Threshold, se generara tambien la configuracion necesaria para generara la estructura para el plugin threshold.
# Creando un Type por cada elemento del array Type y metiendo los parametros restantes dentro de este type.
#
# http://collectd.org/documentation/manpages/collectd-threshold.5.shtml
#
# Un mismo manifest para todos los checks disponibles.
# La idea es que todos los checks envien informacion al servidor graphite
# Los checks se configurarán de manera pasiva, usando los thresholds de Collectd.
# Como definir esos thresholds puede ser un poco dificil.
# La instalación de collectd en Ubuntu son 150MB
# En redhat esta mas paquetizada
#
define monitorizacion::checks::collectd (
$params = undef,
$vip_name = undef,
$initial_state = undef,
$max_check_attempts = undef,
$retry_interval = undef,
$check_interval = undef,
$active_checks_enabled = undef,
$passive_checks_enabled = undef,
$check_freshness = undef,
$freshness_threshold = undef,
$obsess_over_service = undef,
$retain_status_information = undef,
$retain_nonstatus_information = undef,
$is_volatile = undef,
$sudo = undef,
$command = undef,
$cmd_args = undef,
$eventhandler = undef,
)
{
include monitorizacion::params
include monitorizacion::checks::collectd_common
if empty($params) {
fail("Collectd check must have plugin defined")
}
# Parse JSON data
# Different copy of the values to generate the collectd conf
$template_var = parsejson($params)
$params_parsed = parsejson($params)
$plugin = $params_parsed["Plugin"]
$threshold = $params_parsed["Threshold"]
if !$plugin {
fail("Plugin must be defined")
}
file { "${monitorizacion::checks::collectd_common::conf_dir}/${plugin}_${name}.conf":
ensure => file,
content => template("${module_name}/collectd_plugin.conf.erb"),
owner => root,
group => root,
mode => 0640,
notify => Service['collectd'],
}
if $threshold {
# Set a unique identifier to avoid duplicate resources with threshold_to_check function
$null = inline_template("<% @threshold.map {|t| t[\"id\"] = \"${monitorizacion::params::resourcehost}.${name}.${plugin}\" } %>")
$freshness_default_value = hiera('freshness_default_value',120)
threshold_to_check { $threshold:
plugin => $plugin,
vip_name => $vip_name,
initial_state => inline_template("<%= @initial_state || 'o' %>"),
max_check_attempts => 1,
active_checks_enabled => 0,
passive_checks_enabled => 1,
check_freshness => inline_template("<%= @check_freshness || '1' %>"),
freshness_threshold => inline_template("<%= @freshness_threshold || @freshness_default_value %>"),
obsess_over_service => $obsess_over_service,
retain_status_information => $retain_status_information,
retain_nonstatus_information => $retain_nonstatus_information,
is_volatile => inline_template("<%= @is_volatile || '1' %>"),
command => "check_dummy!0!\"Reset to OK state automatically after ${freshness_default_value}s without new data\"",
# $command by default is check_nrpe
eventhandler => $eventhandler,
}
}
}
class monitorizacion::checks::collectd_common
{
include monitorizacion::params
$logfile = "/var/log/collectd.log"
$collectd_dir = "/etc/collectd"
$conf_file = "${collectd_dir}/collectd.conf"
$conf_dir = "${collectd_dir}/collectd.conf.d"
$conf_dir_manual = "${collectd_dir}/collectd.conf.manual"
$base_dir = "/var/lib/collectd"
$pid_file = "/var/run/collectd.pid"
$types_db = "/usr/share/collectd/types.db"
if $::osfamily == "Debian" {
$plugin_dir = "/usr/lib/collectd"
} else { # RedHat
$plugin_dir = "/usr/lib64/collectd"
}
# Solving a dependency problem
if $::osfamily == "RedHat" {
ensure_resource(package, "perl-Compress-Raw-Zlib", {
ensure => "2.052-1.el6.rfx",
before => Package['collectd'],
})
}
ensure_resource(package, "collectd", {
ensure => latest,
})
ensure_resource(file, "/etc/default/collectd", {
ensure => present,
content => template("${module_name}/collectd.$::osfamily.erb"),
owner => 'root',
group => 'root',
mode => '0644',
notify => Service[collectd],
})
ensure_resource(file, $logfile, {
ensure => present,
owner => 'root',
group => 'root',
mode => '0644',
notify => Service[collectd],
})
ensure_resource(file, $conf_dir_manual, {
ensure => directory,
owner => 'root',
group => 'root',
mode => '0750',
notify => Service[collectd],
})
# Only puppetized files
ensure_resource(file, [$collectd_dir, $conf_dir], {
ensure => directory,
owner => 'root',
group => 'nrpe',
recurse => true,
purge => true,
mode => '0750',
notify => Service[collectd],
})
ensure_resource(file, $conf_file, {
ensure => present,
content => template("${module_name}/collectd.conf.erb"),
owner => 'root',
group => 'root',
mode => '0644',
require => File[$collectd_dir],
notify => Service[collectd],
})
ensure_resource(file, "${conf_dir}/graphite.conf", {
ensure => present,
content => template("${module_name}/graphite.conf.erb"),
owner => 'root',
group => 'root',
mode => '0644',
require => File[$conf_dir],
notify => Service[collectd],
})
ensure_resource(file, "${conf_dir}/notification.conf", {
ensure => present,
content => template("${module_name}/notification.conf.erb"),
owner => 'root',
group => 'root',
mode => '0644',
require => File[$conf_dir],
notify => Service[collectd],
})
$collectd_gearman_log = hiera('collectd_gearman_log','/var/log/collectd_gearman.log')
if $collectd_gearman_log {
ensure_resource(file, $collectd_gearman_log, {
ensure => present,
owner => 'nrpe',
group => 'nrpe',
mode => '0644',
})
}
ensure_resource(file, "${collectd_dir}/nagios.yaml", {
ensure => present,
content => template("${module_name}/nagios.yaml.erb"),
owner => 'root',
group => 'nrpe',
mode => '0640',
require => File[$collectd_dir],
})
ensure_resource(service, "collectd", {
ensure => running,
require => Package["collectd"],
})
ensure_resource(package, "send_gearman", {
ensure => latest,
name => $::osfamily ? {
'Debian' => 'mod-gearman-tools',
'RedHat' => 'mod_gearman',
default => fail("Module '${module_name}' is not currently supported on ${::operatingsystem}")
},
})
# Package to send collectd notifications to gearman
ensure_resource(package, "collectd_gearman", {
ensure => latest,
name => $::osfamily ? {
'Debian' => 'collectd_gearman',
'RedHat' => 'rubygem-collectd_gearman',
default => fail("Module '${module_name}' is not currently supported on ${::operatingsystem}")
},
provider => $::osfamily ? {
'Debian' => 'gem',
'RedHat' => 'yum',
default => fail("Module '${module_name}' is not currently supported on ${::operatingsystem}")
},
require => File["${collectd_dir}/nagios.yaml"],
})
}
# Different thresholds could be defined, and could have several different types
define threshold_to_check (
$plugin,
$vip_name = undef,
$initial_state = undef,
$max_check_attempts = undef,
$active_checks_enabled = undef,
$passive_checks_enabled = undef,
$check_freshness = undef,
$freshness_threshold = undef,
$obsess_over_service = undef,
$retain_status_information = undef,
$retain_nonstatus_information = undef,
$is_volatile = undef,
$command = undef,
$eventhandler = undef,
)
{
$instance = $name['Instance']
$type = $name['Type']
$id = $name['id']
# Set a unique identifier to avoid duplicate resources with threshold_to_check function
$null = inline_template("<% @type.map {|t| t[\"id\"] = \"${id}\" } %>")
multiple_type_to_service { $type:
plugin => $plugin,
instance => $instance,
vip_name => $vip_name,
initial_state => $initial_state,
max_check_attempts => $max_check_attempts,
active_checks_enabled => $active_checks_enabled,
passive_checks_enabled => $passive_checks_enabled,
check_freshness => $check_freshness,
freshness_threshold => $freshness_threshold,
obsess_over_service => $obsess_over_service,
retain_status_information => $retain_status_information,
retain_nonstatus_information => $retain_nonstatus_information,
is_volatile => $is_volatile,
command => $command,
eventhandler => $eventhandler,
}
}
# Define one icinga service per Type
define multiple_type_to_service (
$plugin,
$instance,
$vip_name = undef,
$initial_state = undef,
$max_check_attempts = undef,
$active_checks_enabled = undef,
$passive_checks_enabled = undef,
$check_freshness = undef,
$freshness_threshold = undef,
$obsess_over_service = undef,
$retain_status_information = undef,
$retain_nonstatus_information = undef,
$is_volatile = undef,
$command = undef,
$eventhandler = undef,
)
{
$type_name = $name['Name']
$type_instance = $name['Instance']
$data_source = $name['DataSource']
$check_name = inline_template(
"collectd_<%= @plugin %>\
<%- if @instance -%>\
-<%= @instance %>\
<%- end -%>\
.<%= @type_name %>\
<%- if @type_instance -%>\
-<%= @type_instance %>\
<%- end -%>\
<%- if @data_source -%>\
.<%= @data_source %>\
<%- end -%>"
)
monitorizacion::check { "${monitorizacion::params::resourcehost}.${check_name}":
check_name => $check_name,
package => $package_name,
exec => $exec,
vip_name => $vip_name,
initial_state => $initial_state,
max_check_attempts => $max_check_attempts,
active_checks_enabled => $active_checks_enabled,
passive_checks_enabled => $passive_checks_enabled,
check_freshness => $check_freshness,
freshness_threshold => $freshness_threshold,
obsess_over_service => $obsess_over_service,
retain_status_information => $retain_status_information,
retain_nonstatus_information => $retain_nonstatus_information,
is_volatile => $is_volatile,
command => $command,
eventhandler => $eventhandler,
}
}
#
# Collectd check should be defined:
# Name: collectd_nagios
# Puppet type: monitorizacion::checks::collectd_nagios
# Example: {"Plugin":"check_tcp","Args":"-H google.com -p 80","Interval":10,"Threshold":[{"Type":[{"Name":"time","FailureMax":1}]}]}
#
#{
# "Plugin": "check_tcp",
# "Args": "-H google.com -p 80",
# "Interval" : 10,
# "Threshold": [
# {
# "Type": [
# {
# "Name": "time",
# "FailureMax": 1
# }
# ]
# }
# ]
#}
#
# Plugin should be the executable in the /usr/lib[64]/nagios/plugins/ path
# lib -> ubuntu
# lib64 -> RHEL
#
define monitorizacion::checks::collectd_nagios (
$params = undef,
$vip_name = undef,
$initial_state = undef,
$max_check_attempts = undef,
$retry_interval = undef,
$check_interval = undef,
$active_checks_enabled = undef,
$passive_checks_enabled = undef,
$check_freshness = undef,
$freshness_threshold = undef,
$obsess_over_service = undef,
$retain_status_information = undef,
$retain_nonstatus_information = undef,
$is_volatile = undef,
$sudo = undef,
$command = undef,
$cmd_args = undef,
$eventhandler = undef,
)
{
include monitorizacion::checks::collectd_common
$execnagios = "/usr/local/bin/exec-nagios.px"
if empty($params) {
fail("Collectd check must have plugin defined")
}
# Parse JSON data
# Different copy of the values to generate the collectd conf
$template_var = parsejson($params)
$params_parsed = parsejson($params)
$plugin = $params_parsed["Plugin"]
$args = $params_parsed["Args"]
$threshold = $params_parsed["Threshold"]
if !$plugin {
fail("Plugin must be defined")
}
# Program to execute nagios plugins inside collectd
ensure_resource(file, $execnagios, {
source => "puppet:///modules/${module_name}/exec-nagios.px",
owner => root,
group => nrpe,
mode => '0755',
})
$exec_conf_file = "${monitorizacion::checks::collectd_common::conf_dir}/nagios_${plugin}_${name}.cfg"
file { $exec_conf_file:
ensure => file,
content => template("${module_name}/collectd_exec_nagios.conf.erb"),
owner => root,
group => nrpe,
mode => 0640,
}
file { "${monitorizacion::checks::collectd_common::conf_dir}/${plugin}_${name}.conf":
ensure => file,
content => template("${module_name}/collectd_plugin_nagios.conf.erb"),
owner => root,
group => nrpe,
mode => 0640,
notify => Service['collectd'],
}
if $threshold {
# Set a unique identifier to avoid duplicate resources with threshold_to_check function
$null = inline_template("<% @threshold.map {|t| t[\"id\"] = \"${monitorizacion::params::resourcehost}.${name}.${plugin}\" } %>")
$freshness_default_value = hiera('freshness_default_value',120)
threshold_to_check { $threshold:
plugin => "${plugin}_${name}",
vip_name => $vip_name,
initial_state => inline_template("<%= @initial_state || 'o' %>"),
max_check_attempts => 1,
active_checks_enabled => 0,
passive_checks_enabled => 1,
check_freshness => inline_template("<%= @check_freshness || '1' %>"),
freshness_threshold => inline_template("<%= @freshness_threshold || @freshness_default_value %>"),
obsess_over_service => $obsess_over_service,
retain_status_information => $retain_status_information,
retain_nonstatus_information => $retain_nonstatus_information,
is_volatile => inline_template("<%= @is_volatile || '1' %>"),
command => "check_dummy!0!\"Reset to OK state automatically after ${freshness_default_value}s without new data\"",
# $command by default is check_nrpe
eventhandler => $eventhandler,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment