Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding class to create alerts #24

Merged
merged 4 commits into from
Dec 13, 2016
Merged
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
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 @@ -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,
Expand Down Expand Up @@ -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': }
}
1 change: 1 addition & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
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