Skip to content

Commit

Permalink
adding class to create alerts (voxpupuli#24)
Browse files Browse the repository at this point in the history
* adding class to create alerts

* fix puppet syntax in alert class

* fix puppet syntax in alert class

* fix name Value in Labels
  • Loading branch information
snubba authored and vide committed Jun 22, 2017
1 parent f83f475 commit be89daf
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions manifests/alerts.pp
Original file line number Diff line number Diff line change
@@ -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"),
}
}

}
7 changes: 7 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,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,
Expand Down Expand Up @@ -172,6 +174,11 @@
notify => $notify_service,
config_template => $config_template,
} ->
class { '::prometheus::alerts':
location => $config_dir,
alerts => $alerts,
notify => $notify_service,
} ->
class { '::prometheus::run_service': } ->
anchor {'prometheus_last': }
}
1 change: 1 addition & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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' }
Expand Down
15 changes: 15 additions & 0 deletions templates/alerts.epp
Original file line number Diff line number Diff line change
@@ -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'] %>",
<% } -%>
}
<% } -%>
1 change: 1 addition & 0 deletions tests/init.pp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include prometheus
include prometheus::node_exporter
include prometheus::alert_manager
include prometheus::alerts

0 comments on commit be89daf

Please sign in to comment.