Giuseppe Lavagetto has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/336851 )

Change subject: prometheus::class_config: allow new selections for prometheus
......................................................................

prometheus::class_config: allow new selections for prometheus

In some cases, we want to create a targets list for all the servers that
include a certain class. This define allows to do that, and even allows
querying by specific parameters of the class.

Change-Id: I765f2dd0a97f7826e679d3da621777b64e7eda03
---
A modules/prometheus/manifests/class_config.pp
A modules/prometheus/templates/class_config.erb
2 files changed, 78 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/51/336851/1

diff --git a/modules/prometheus/manifests/class_config.pp 
b/modules/prometheus/manifests/class_config.pp
new file mode 100644
index 0000000..7b5f734
--- /dev/null
+++ b/modules/prometheus/manifests/class_config.pp
@@ -0,0 +1,48 @@
+# == Define: prometheus::class_config
+#
+# Generate prometheus targets configuration for all servers including a 
specified class.
+# Data is gathered using the puppetdb querying functions
+
+# == Parameters
+# $dest:    The output file where to write the result.
+# $class_name:    Class name to search.
+# $site:    The site to use.
+# $port:    The port to use for the target.
+# $labels:  Labels to attach to every host. 'Cluster' will be added 
automagically as well
+# $class_parameters: hash of parameters for the class
+#
+# == Example
+#
+#  Configuration for varnish_exporter for maps, running on the frontend varnish
+#  instance.
+#
+#  prometheus::class_config{ "redis_${::site}:
+#      dest    => "${targets_path}/redis_${::site}.yaml",
+#      site    => $::site,
+#      class_name => 'redis',
+#      port    => '9331',
+#  }
+
+define prometheus::class_config(
+    $dest,
+    $site,
+    $class_name,
+    $port,
+    $labels = {},
+    $class_parameters = {},
+) {
+    validate_string($dest)
+    validate_string($site)
+    validate_string($class_name)
+    validate_re($port, '^[0-9]+$')
+    validate_hash($labels)
+    validate_hash($class_parameters)
+
+    file { $dest:
+        ensure  => present,
+        owner   => 'root',
+        group   => 'root',
+        mode    => '0444',
+        content => template('prometheus/class_config.erb'),
+    }
+}
diff --git a/modules/prometheus/templates/class_config.erb 
b/modules/prometheus/templates/class_config.erb
new file mode 100644
index 0000000..64f9cea
--- /dev/null
+++ b/modules/prometheus/templates/class_config.erb
@@ -0,0 +1,30 @@
+<%-
+# All labels / targets list
+all = []
+
+# Construct the query parameters for the class
+if @class_params
+  param_query = "{#{@class_params.map{ |k,v| "#{k} = \"#{v}\"" }.join(", ")}}"
+else
+  param_query = ""
+end
+
+# Query PuppetDB for the class/params and group by certname to get a list of 
servers
+# that have the class applied.
+servers = scope.function_query_resources(false, 
"Class['#{@class_name}']#{param_query}", "certname").keys
+
+# Find all servers in the current site, and filter them by the servers list we 
just found
+scope.function_get_clusters([{'site' => @site}]).each do |cluster, val|
+  targets = val[@site].select { |server| servers.include?(server) }
+  if targets.length
+    all.push(
+      {
+        'labels' => @labels.merge({'cluster' => cluster, 'site' => @site}),
+        'targets' => targets.map{|t| "#{t.split('.')[0]}:#{@port}" },
+      }
+    )
+  end  
+end
+-%>
+# This file is managed by puppet
+<%= scope.function_ordered_yaml([all]) %>

-- 
To view, visit https://gerrit.wikimedia.org/r/336851
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I765f2dd0a97f7826e679d3da621777b64e7eda03
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Giuseppe Lavagetto <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to