summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Meusel <tim@bastelfreak.de>2016-10-21 08:54:54 (GMT)
committerGitHub <noreply@github.com>2016-10-21 08:54:54 (GMT)
commit85b098e437b33f4a5577026ac1c4291fa026fb87 (patch)
treee8b4d0291fb3bd7cd88dae87131305aa2e700d80
parentb54e37c52ee31db2acebee7f41b3901c7e76fe0b (diff)
parent5fac613376ac315dcb2b2357a0a5b29e7b0b765f (diff)
downloadpuppet-prometheus_reporter-85b098e437b33f4a5577026ac1c4291fa026fb87.zip
puppet-prometheus_reporter-85b098e437b33f4a5577026ac1c4291fa026fb87.tar.gz
puppet-prometheus_reporter-85b098e437b33f4a5577026ac1c4291fa026fb87.tar.bz2
Merge pull request #4 from roidelapluie/reporter
Add the prometheus reporter ruby script
-rw-r--r--lib/puppet/reports/prometheus.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/puppet/reports/prometheus.rb b/lib/puppet/reports/prometheus.rb
new file mode 100644
index 0000000..ba70415
--- /dev/null
+++ b/lib/puppet/reports/prometheus.rb
@@ -0,0 +1,61 @@
+require 'puppet'
+require 'puppet/util'
+require 'yaml'
+require 'fileutils'
+
+# Required for strftime(%Q)
+require 'date'
+
+Puppet::Reports.register_report(:prometheus) do
+ # Source: evenup/evenup-graphite_reporter code base
+ # lib/puppet/reports/graphite.rb
+ configfile = File.join([File.dirname(Puppet.settings[:config]),
+ 'prometheus.yaml'])
+ unless File.exist?(configfile)
+ raise(Puppet::ParseError, "Prometheus report config file #{configfile} not readable")
+ end
+
+ config = YAML.load_file(configfile)
+
+ TEXTFILE_DIRECTORY = config['textfile_directory']
+ REPORT_FILENAME = config['report_filename']
+
+ if TEXTFILE_DIRECTORY.nil?
+ raise(Puppet::ParseError, "#{configfile}: textfile_directory is not set.")
+ end
+
+ unless REPORT_FILENAME.nil? or REPORT_FILENAME.end_with? '.prom'
+ raise(Puppet::ParseError, "#{configfile}: report_filename does not ends with .prom")
+ end
+
+ def process
+ now = DateTime.now.new_offset(0)
+
+ if REPORT_FILENAME.nil?
+ name = host + '.prom'
+ file = File.join(TEXTFILE_DIRECTORY, name)
+ else
+ file = File.join(TEXTFILE_DIRECTORY, REPORT_FILENAME)
+ end
+
+ common_values = {
+ transaction_uuid: self.transaction_uuid,
+ host: self.host,
+ }.reduce('') {
+ |values, extra| values + ",#{extra[0].to_s}=\"#{extra[1].to_s}\""
+ }
+
+ epochtime = now.strftime('%Q')
+ File.open(file, 'w') do |file|
+ unless metrics.empty? or metrics['events'].nil?
+ metrics.each do |metric, data|
+ data.values.each do |val|
+ file.write("puppet_report_#{metric}{name=\"#{val[1]}\"#{common_values}} #{val[2]}\n")
+ end
+ end
+ end
+
+ file.write("puppet_report{host=\"#{host}\",kind=\"#{kind}\",version=\"#{configuration_version}\"#{common_values}} #{epochtime}\n")
+ end
+ end
+end