diff --git a/README.md b/README.md index d1ae85025..e562856e6 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ This module automates the install and configuration of Prometheus monitoring too * Optionally installs a user to run it under * Installs a configuration file for prometheus daemon (/etc/prometheus/prometheus.yaml) or for alertmanager (/etc/prometheus/alert.rules) * Manages the services via upstart, sysv, or systemd +* Optionally creates alert rules ## Usage @@ -51,6 +52,32 @@ or simply: include ::prometheus ``` +To add alert rules, add the following to the class prometheus: +```puppet + alerts => [{ 'name' => 'InstanceDown', 'condition' => 'up == 0', 'timeduration' => '5m', labels => [{ 'name' => 'severity', 'content' => 'page'}], 'annotations' => [{ 'name' => 'summary', content => 'Instance {{ $labels.instance }} down'}, {'name' => 'description', content => '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.' }]}] +``` + +or in hiera: +```yaml +alertrules: + - + name: 'InstanceDown' + condition: 'up == 0' + timeduration: '5m' + labels: + - + name: 'severity' + content: 'critical' + annotations: + - + name: 'summary' + content: 'Instance {{ $labels.instance }} down' + - + name: 'description' + content: '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.' + +``` + On the monitored nodes: ```puppet diff --git a/manifests/alerts.pp b/manifests/alerts.pp new file mode 100644 index 000000000..db4ea4833 --- /dev/null +++ b/manifests/alerts.pp @@ -0,0 +1,26 @@ +# Class: prometheus::alerts +# +# This module manages prometheus alerts for prometheus +# +# [*location*] +# Whether to create the alert file for prometheus +# +# [*alerts*] +# Array of alerts (see README) +# +class prometheus::alerts ( + String $location, + Array $alerts, + String $alertfile_name = 'alert.rules' +) inherits prometheus::params { + + if $alerts != [] { + file{ "${location}/${alertfile_name}": + ensure => 'file', + owner => $prometheus::user, + group => $prometheus::group, + content => epp("${module_name}/alerts.epp"), + } + } + +} diff --git a/manifests/init.pp b/manifests/init.pp index a2ebb3005..dd8fcb9b7 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -128,6 +128,8 @@ $global_config = $::prometheus::params::global_config, $rule_files = $::prometheus::params::rule_files, $scrape_configs = $::prometheus::params::scrape_configs, + $alerts = $::prometheus::params::alerts + ) inherits prometheus::params { if( versioncmp($::prometheus::version, '1.0.0') == -1 ){ $real_download_url = pick($download_url, @@ -163,6 +165,11 @@ purge => $purge_config_dir, notify => $notify_service, } -> + class { '::prometheus::alerts': + location => $config_dir, + alerts => $alerts, + notify => $notify_service, + } -> class { '::prometheus::run_service': } -> anchor {'prometheus_last': } } diff --git a/manifests/params.pp b/manifests/params.pp index d72ff7cb0..fb23e1363 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -37,6 +37,7 @@ $global_config = { 'scrape_interval'=> '15s', 'evaluation_interval'=> '15s', 'external_labels'=> { 'monitor'=>'master'}} $rule_files = [ "${config_dir}/alert.rules" ] $scrape_configs = [ { 'job_name'=> 'prometheus', 'scrape_interval'=> '10s', 'scrape_timeout'=> '10s', 'static_configs'=> [ { 'targets'=> [ 'localhost:9090' ], 'labels'=> { 'alias'=> 'Prometheus'} } ] } ] + $alerts = [] case $::architecture { 'x86_64', 'amd64': { $arch = 'amd64' } 'i386': { $arch = '386' } diff --git a/templates/alerts.epp b/templates/alerts.epp new file mode 100644 index 000000000..ba0816c38 --- /dev/null +++ b/templates/alerts.epp @@ -0,0 +1,15 @@ +<% $prometheus::alerts::alerts.each |$alert| { -%> +ALERT <%= $alert['name'] %> + IF <%= $alert['condition'] %> + FOR <%= $alert['timeduration'] %> + LABELS { + <% $alert['labels'].each |$label| { -%> + <%= $label['name'] %> = "<%= $label['content'] %>", + <% } -%> + } + ANNOTATIONS { + <% $alert['annotations'].each |$annotation| { -%> + <%= $annotation['name'] %> = "<%= $annotation['content'] %>", + <% } -%> + } +<% } -%> diff --git a/tests/init.pp b/tests/init.pp index b66d01c12..4ab89c60e 100644 --- a/tests/init.pp +++ b/tests/init.pp @@ -1,3 +1,4 @@ include prometheus include prometheus::node_exporter include prometheus::alert_manager +include prometheus::alerts