#!/usr/bin/env ruby require 'optparse' require_relative "./../lib/prometheus_exporter" require_relative "./../lib/prometheus_exporter/server" def run options = {} custom_collector_filename = nil custom_type_collectors_filenames = [] OptionParser.new do |opt| opt.banner = "Usage: prometheus_exporter [options]" opt.on('-p', '--port INTEGER', Integer, "Port exporter should listen on (default: #{PrometheusExporter::DEFAULT_PORT})") do |o| options[:port] = o.to_i end opt.on('--prefix METRIC_PREFIX', String, "Prefix to apply to all metrics (default: #{PrometheusExporter::DEFAULT_PREFIX})") do |o| options[:prefix] = o.to_s end opt.on('-c', '--collector FILE', String, "(optional) Custom collector to run") do |o| custom_collector_filename = o.to_s end opt.on('-a', '--type-collector FILE', String, "(optional) Custom type collectors to run in main collector") do |o| custom_type_collectors_filenames << o end opt.on('-v', '--verbose') do |o| options[:verbose] = true end end.parse! if custom_collector_filename eval File.read(custom_collector_filename), nil, File.expand_path(custom_collector_filename) found = false base_klass = PrometheusExporter::Server::CollectorBase ObjectSpace.each_object(Class) do |klass| if klass < base_klass && klass != base_klass options[:collector_class] = klass end end if !found STDERR.puts "[Error] Can not find a class inheriting off PrometheusExporter::Server::CollectorBase" exit 1 end end if custom_type_collectors_filenames.length > 0 custom_type_collectors_filenames.each do |t| eval File.read(t), nil, File.expand_path(t) end ObjectSpace.each_object(Class) do |klass| if klass < PrometheusExporter::Server::TypeCollector options[:type_collectors] ||= [] options[:type_collectors] << klass end end end runner = PrometheusExporter::Server::Runner.new(options) puts "#{Time.now} Starting prometheus exporter on port #{runner.port}" runner.start sleep end run