summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Pivotto <roidelapluie@inuits.eu>2016-10-21 08:41:32 (GMT)
committerJulien Pivotto <roidelapluie@inuits.eu>2016-10-21 08:41:32 (GMT)
commit5fac613376ac315dcb2b2357a0a5b29e7b0b765f (patch)
treeaeeb1d50723a51780139baa08df729eb32337489
parent5b2be9adcf5f2fc4f467d7b5605db8e95563dc26 (diff)
downloadpuppet-prometheus_reporter-5fac613376ac315dcb2b2357a0a5b29e7b0b765f.zip
puppet-prometheus_reporter-5fac613376ac315dcb2b2357a0a5b29e7b0b765f.tar.gz
puppet-prometheus_reporter-5fac613376ac315dcb2b2357a0a5b29e7b0b765f.tar.bz2
Add the prometheus reporter ruby script
Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
-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