From 6d5ef3ee815c498da9059eafb29e8cefe868d54d Mon Sep 17 00:00:00 2001 From: Fabian Fischer Date: Thu, 10 Oct 2024 11:18:13 +0200 Subject: [PATCH 1/2] Add Alerting and contact points --- README.md | 89 +++++++++++++++++++++++++++++- manifests/config.pp | 41 ++++++++++++++ manifests/init.pp | 21 +++++++ templates/pcontact_points.yaml.epp | 3 + 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 templates/pcontact_points.yaml.epp diff --git a/README.md b/README.md index 47d3decb..a031e36d 100644 --- a/README.md +++ b/README.md @@ -416,6 +416,28 @@ which is not part of grafana's syntax. This option will be extracted from the hash, and used to "source" a directory of dashboards. See **Advanced Usage** for details. +#### `provisioning_alerting_source` + +A Source path to an alerting folder. This will be used to "source" a directory +of alerts. All YAML and JSON Files in this Folder +will be copied to the grafana provisioning directory (default: /etc/grafana/provisioning/alerting). +See **Advanced Usage** for details. + +See [provisioning grafana alerting](https://grafana.com/docs/grafana/latest/alerting/set-up/provision-alerting-resources/file-provisioning/) +for details and example config files. + +##### `provisioning_contact_points` + +A Hash which is converted to YAML for grafana to provision data +sources. See [provisioning grafana alerting](https://grafana.com/docs/grafana/latest/alerting/set-up/provision-alerting-resources/file-provisioning/ for +details and example config file. Requires grafana > v5.0.0. + +This is very useful with Hiera as you can provide a yaml +hash/dictionary which will effectively 'passthrough' to grafana. See +**Advanced Usage** for examples. + +You also have the option to use **provisioning_alerting_source** to "source" it with the directory of alerts. + #### `provisioning_dashboards_file` A String that is used as the target file name for the dashabords @@ -430,6 +452,18 @@ provisioning file. This way the module can be used to generate placeholder files so password can be sepecified in a different iteration, avoiding them to be put in the module code. +#### `provisioning_contact_points_file` + +A String that is used as the target file name for the contact point +provisioning file. This way the module can be used to generate placeholder +files so password can be sepecified in a different iteration, avoiding them +to be put in the module code. + +#### `provisioning_alerting_dir` + +A String to the alerting provisioning directory. This way the module can copy the +alerts and contact points into the right path. + ##### `rpm_iteration` Used when installing Grafana from package ('package' or 'repo' install methods) @@ -1013,7 +1047,8 @@ provisioning](http://docs.grafana.org/administration/provisioning/). This module will provision grafana by placing yaml files into `/etc/grafana/provisioning/datasources` and -`/etc/grafana/provisioning/dashboards` by default. +`/etc/grafana/provisioning/dashboards` and +`/etc/grafana/provisioning/alerting` by default. ##### Example datasource @@ -1158,6 +1193,58 @@ Dashboards known not to be "provisionable": * [HTTP Services Status](https://grafana.com/dashboards/4859) +#### Example Alert + +You need to set +`grafana::provisioning_alerting_source: puppet:///path/to/alerting` in puppet. +This is the the path to "source" the alert YAMLs or JSONs. + +See [grafana import alert rules](https://grafana.com/docs/grafana/latest/alerting/set-up/provision-alerting-resources/file-provisioning/#import-alert-rules) for examples of alert rule YAMLs. + +#### Example Contact point + +A puppet hash example for Prometheus. The module will place the hash +as a yaml file into `/etc/gafana/provisioning/alerting/puppetprovisioned-contact-points.yaml`. + +```puppet +class { 'grafana': + provisioning_contact_points => { + apiVersion => 1, + contactPoints => [ + { + orgId => 1, + name => 'cp_1' + receivers => { + uid => 'first_uid', + type => 'prometheus-alertmanager', + disableResolveMessage => false, + settings => { + url => 'http://test:9000' + }, + }, + }, + ], + } +} +``` + +Here is the same configuration example as a hiera hash. + +```yaml +grafana::provisioning_dashboards: + apiVersion: 1 + contactPoints: + - orgId: 1 + name: cp_1 + receivers: + - uid: first_uid + type: prometheus-alertmanager + disableResolveMessage: false + settings: + url: http://test:9000 +``` + + ## Tasks ### `change_grafana_admin_password` diff --git a/manifests/config.pp b/manifests/config.pp index 0b6673e7..61ad8647 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -182,5 +182,46 @@ notify => Class['grafana::service'], } } + + # --alerts-- + if ( $grafana::provisioning_alerting_source != undef ) { + file { $grafana::provisioning_alerting_dir: + ensure => directory, + owner => 'grafana', + group => 'grafana', + mode => '0640', + recurse => true, + sourceselect => 'all', + purge => true, + source => $grafana::provisioning_alerting_source, + notify => Class['grafana::service'], + } + } + + # --contact_points-- + $pcontact_points = $grafana::provisioning_contact_points + if (length($pcontact_points) >= 1) { + # template uses: + # - pcontact_points + if (!defined(File[$grafana::provisioning_alerting_dir])) { + file { $grafana::provisioning_alerting_dir: + ensure => directory, + owner => 'grafana', + group => 'grafana', + mode => '0750', + } + } + + file { $grafana::provisioning_contact_points_file: + ensure => file, + owner => 'grafana', + group => 'grafana', + mode => '0640', + show_diff => false, + content => epp('grafana/pcontact_points.yaml.epp'), + require => File[$grafana::provisioning_alerting_dir], + notify => Class['grafana::service'], + } + } } } diff --git a/manifests/init.pp b/manifests/init.pp index bd54a72e..2b3a5238 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -81,6 +81,15 @@ # required. Will be converted into YAML and used by granfana to # configure datasources. # +# @param provisioning_contact_points +# Contact points to provision into grafana, grafana > v5.0.0 +# required. Will be converted into YAML and used by granfana to +# configure contact_points. +# +# @param provisioning_alerting_source +# Alerts to provision into grafana, grafana > v5.0.0 +# required. Source Path to alert YAMLs and JSONs to copy. +# # @param provisioning_dashboards_file # Fully qualified path to place the provisioning file # for dashboards, only used if provisioning_dashboards is specified. @@ -89,6 +98,14 @@ # Fully qualified path to place the provisioning file # for datasources, only used if provisioning_datasources is specified. # +# @param provisioning_contact_points_file +# Fully qualified path to place the provisioning file +# for contact points, only used if provisioning_contact_points is specified. +# +# @param provisioning_alerting_dir +# Fully qualified path to place the provisioning file +# for alerts, only used if provisioning_alerting is specified. +# # @param create_subdirs_provisioning # If true puppet will create any # subdirectories in the given path when provisioning dashboards. @@ -170,9 +187,13 @@ Hash $plugins = {}, Hash $provisioning_dashboards = {}, Hash $provisioning_datasources = {}, + Hash $provisioning_contact_points = {}, + Optional[Stdlib::Filesource] $provisioning_alerting_source = undef, Stdlib::Absolutepath $provisioning_dir = '/etc/grafana/provisioning', Stdlib::Absolutepath $provisioning_dashboards_file = "${provisioning_dir}/dashboards/puppetprovisioned.yaml", Stdlib::Absolutepath $provisioning_datasources_file = "${provisioning_dir}/datasources/puppetprovisioned.yaml", + Stdlib::Absolutepath $provisioning_alerting_dir = "${provisioning_dir}/alerting", + Stdlib::Absolutepath $provisioning_contact_points_file = "${provisioning_dir}/alerting/puppetprovisioned-contact-points.yaml", Boolean $create_subdirs_provisioning = false, Optional[Hash] $sysconfig = undef, Hash[String[1], Hash] $ldap_servers = {}, diff --git a/templates/pcontact_points.yaml.epp b/templates/pcontact_points.yaml.epp new file mode 100644 index 00000000..bad503bd --- /dev/null +++ b/templates/pcontact_points.yaml.epp @@ -0,0 +1,3 @@ +# This file is managed by Puppet, any changes will be overwritten + +<%= stdlib::to_yaml($grafana::config::pcontact_points) -%> From 5a1ce0856035d72ea608343f4329084e566270d9 Mon Sep 17 00:00:00 2001 From: Fabian Fischer Date: Thu, 10 Oct 2024 11:25:04 +0200 Subject: [PATCH 2/2] Readme fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a031e36d..e38f0934 100644 --- a/README.md +++ b/README.md @@ -429,7 +429,7 @@ for details and example config files. ##### `provisioning_contact_points` A Hash which is converted to YAML for grafana to provision data -sources. See [provisioning grafana alerting](https://grafana.com/docs/grafana/latest/alerting/set-up/provision-alerting-resources/file-provisioning/ for +sources. See [provisioning grafana alerting](https://grafana.com/docs/grafana/latest/alerting/set-up/provision-alerting-resources/file-provisioning/) for details and example config file. Requires grafana > v5.0.0. This is very useful with Hiera as you can provide a yaml