Skip to content

Commit

Permalink
Merge pull request voxpupuli#127 from vrtdev/feature/alerts_prometheus2
Browse files Browse the repository at this point in the history
Feature/alerts prometheus2
  • Loading branch information
bastelfreak authored Jan 4, 2018
2 parents 9847c91 + 29ffbb5 commit 8a8ae7b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 12 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ or simply:
include ::prometheus
```

To add alert rules, add the following to the class prometheus:
To add alert rules, add the following to the class prometheus in case you are using prometheus < 2.0:
```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.' }]}]
```
Expand All @@ -91,6 +91,23 @@ alertrules:

```

When using prometheus >= 2.0, we use the new yaml format (https://prometheus.io/docs/prometheus/2.0/migration/#recording-rules-and-alerts) configuration

```yaml
alerts:
groups:
- name: alert.rules
rules:
- alert: 'InstanceDown'
expr: 'up == 0'
for: '5m'
labels:
'severity': 'page'
annotations:
'summary': 'Instance {{ $labels.instance }} down'
'description': '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.'
```
On the monitored nodes:
```puppet
Expand Down
36 changes: 25 additions & 11 deletions manifests/alerts.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@
# This module manages prometheus alerts for prometheus
#
# [*location*]
# Whether to create the alert file for prometheus
# Where to create the alert file for prometheus
#
# [*alerts*]
# Array of alerts (see README)
# Array (< prometheus 2.0.0) or Hash (>= prometheus 2.0.0) of alerts (see README).
#
class prometheus::alerts (
String $location,
Array $alerts,
Variant[Array,Hash] $alerts,
String $alertfile_name = 'alert.rules'
) inherits prometheus::params {

if $alerts != [] {
file{ "${location}/${alertfile_name}":
ensure => 'file',
owner => $prometheus::user,
group => $prometheus::group,
notify => Class['::prometheus::service_reload'],
content => epp("${module_name}/alerts.epp"),
}
if $alerts != [] and $alerts != {} {
if ( versioncmp($::prometheus::version, '2.0.0') < 0 ){

file { "${location}/${alertfile_name}":
ensure => 'file',
owner => $prometheus::user,
group => $prometheus::group,
notify => Class['::prometheus::service_reload'],
content => epp("${module_name}/alerts.epp"),
}

}
else {

file { "${location}/${alertfile_name}":
ensure => 'file',
owner => $prometheus::user,
group => $prometheus::group,
notify => Class['::prometheus::service_reload'],
content => $alerts.to_yaml,
}

}
}
}
54 changes: 54 additions & 0 deletions spec/classes/prometheus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,60 @@
}
end
end

context 'with alerts configured', alerts: true do
[
{
alerts: [{
'name' => 'alert_name',
'condition' => 'up == 0',
'timeduration' => '5min',
'labels' => [{ 'name' => 'severity', 'content' => 'woops' }],
'annotations' => [{ 'name' => 'summary', 'content' => 'did a woops {{ $labels.instance }}' }]
}]
},
{
version: '2.0.0-rc.1',
alerts: {
'groups' => [{
'name' => 'alert.rules',
rules: [
{
'alert' => 'alert_name',
'expr' => 'up == 0',
'for' => '5min',
'labels' => { 'severity' => 'woops' },
'annotations' => {
'summary' => 'did a woops {{ $labels.instance }}'
}
}
]
}]
}
}
].each do |parameters|
context "with prometheus version #{parameters[:version]}" do
let(:params) do
parameters
end

prom_version = parameters[:version] || '1.5.2'
prom_major = prom_version[0]

it {
is_expected.to compile
}
it {
is_expected.to contain_file('/etc/prometheus/alert.rules').with(
'ensure' => 'file',
'owner' => 'prometheus',
'group' => 'prometheus',
'content' => File.read(fixtures('files', "prometheus#{prom_major}.alert.rules"))
).that_notifies('Class[prometheus::service_reload]')
}
end
end
end
end
end
end
9 changes: 9 additions & 0 deletions spec/fixtures/files/prometheus1.alert.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ALERT alert_name
IF up == 0
FOR 5min
LABELS {
severity = "woops",
}
ANNOTATIONS {
summary = "did a woops {{ $labels.instance }}",
}
11 changes: 11 additions & 0 deletions spec/fixtures/files/prometheus2.alert.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
groups:
- name: alert.rules
rules:
- alert: alert_name
expr: up == 0
for: 5min
labels:
severity: woops
annotations:
summary: did a woops {{ $labels.instance }}

0 comments on commit 8a8ae7b

Please sign in to comment.