Showing with 107 additions and 33 deletions.
  1. +7 −0 CHANGELOG.md
  2. +99 −32 files/tk_metrics
  3. +1 −1 metadata.json
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Minor Release 4.4.0

## Improvements
- Allow connecting over http instead of https for PuppetDB
- [PR #37](https://github.com/npwalker/pe_metric_curl_cron_jobs/pull/37)
- In order to use pass `--no-ssl` and `--metrics_port` to the tk_metrics script

# Minor Release 4.3.0

## Improvements
Expand Down
131 changes: 99 additions & 32 deletions files/tk_metrics
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ OptionParser.new do |opts|
opts.on('-p', '--[no-]print', 'Print to stdout') { |p| options[:print] = p }
opts.on('-m [TYPE]', '--metrics_type [TYPE]', 'Type of metric to collect') { |v| options[:metrics_type] = v }
opts.on('-o [DIR]', '--output-dir [DIR]', 'Directory to save output to') { |o| options[:output_dir] = o }
opts.on('--metrics_port [PORT]', 'The port the metrics service runs on') { |port| options[:metrics_port] = port }
opts.on('--[no-]ssl', 'Whether or not to use SSL when gather metrics') { |ssl| options[:ssl] = ssl }
end.parse!

if options[:metrics_type].nil? then
Expand All @@ -24,31 +26,50 @@ end
METRICS_TYPE = options[:metrics_type]
config = YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)),"#{METRICS_TYPE}_config.yaml"))

def coalesce ( opt1, opt2, default=nil )
if opt1.nil? then
if opt2.nil? then
default
else
opt2
end
else
opt1
end
end

OUTPUT_DIR = options[:output_dir]
HOSTS = config['hosts']
PORT = config['metrics_port']
PORT = coalesce(options[:metrics_port], config['metrics_port'])
METRICS = config['additional_metrics']
CLIENTCERT = config['clientcert']
PE_VERSION = config['pe_version']
SSL = coalesce(options[:ssl], config['ssl'], true)

$error_array = []

def get_endpoint(url)
def setup_connection(url, ssl)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
if ssl then
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

return http,uri
end

def get_endpoint(url,ssl)
http,uri = setup_connection(url,ssl)

data = JSON.parse(http.get(uri.request_uri).body)
rescue Exception => e
$error_array << "#{e}"
data = {}
end

def post_endpoint(url,post_data)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
def post_endpoint(url,post_data,ssl)
http,uri = setup_connection(url,ssl)

request = Net::HTTP::Post.new(uri.request_uri)
request.content_type = 'application/json'
Expand All @@ -60,6 +81,69 @@ rescue Exception => e
data = {}
end

def generate_host_url(host, port, ssl)
if ssl then
protocol = 'https'
else
protocol = 'http'
end

host_url = "#{protocol}://#{host}:#{port}"
end

def get_status_endpoint(host, port, ssl)
host_url = generate_host_url(host, port, ssl)

status_endpoint = "#{host_url}/status/v1/services?level=debug"
status_output = get_endpoint(status_endpoint,ssl)
end

def bulk_retrieve_additional_metrics(host, port, metrics, ssl)
host_url = generate_host_url(host, port, ssl)

post_data = []
metrics.each do |metric|
post_data << metric['url']
end

endpoint = "#{host_url}/metrics/v1/mbeans"
metrics_output = post_endpoint(endpoint, post_data.to_json, ssl)
metrics_array = []

metrics.each_index do |index|
metric_name = metrics[index]['name']
metric_data = metrics_output[index]

metrics_array << { 'name' => metric_name,
'data' => metric_data }
end

return metrics_array
end

def individually_retrieve_additional_metrics(host, port, metrics, ssl)
host_url = generate_host_url(host, port, ssl)

metrics_array = []
metrics.each do |metric|
endpoint = URI.escape("#{host_url}/metrics/v1/mbeans/#{metric['url']}")
metrics_array << { 'name' => metric['name'],
'data' => get_endpoint(endpoint, ssl) }
end

return metrics_array
end

def retrieve_additional_metrics(host,port,metrics,pe_version, ssl)
if Gem::Version.new(pe_version) < Gem::Version.new('2016.2.0') then
metrics_array = individually_retrieve_additional_metrics(host, port, metrics, ssl)
else
metrics_array = bulk_retrieve_additional_metrics(host, port, metrics, ssl)
end

return metrics_array
end

filename = Time.now.utc.strftime('%Y%m%dT%H%M%SZ') + '.json'

HOSTS.each do |host|
Expand All @@ -68,34 +152,17 @@ HOSTS.each do |host|
dataset = {'timestamp' => timestamp.utc.iso8601, 'servers' => {}}
hostkey = host.gsub('.', '-')

host_url = "https://#{host}:#{PORT}"

status_endpoint = "#{host_url}/status/v1/services?level=debug"
status_output = get_endpoint(status_endpoint)
status_output = get_status_endpoint(host, PORT, SSL)
dataset['servers'][hostkey] = {METRICS_TYPE => status_output}

post_data = []

unless METRICS.empty? then
if Gem::Version.new(PE_VERSION) < Gem::Version.new('2016.2.0') then
METRICS.each do |metric|
endpoint = "#{host_url}/metrics/v1/mbeans/#{metric['url']}"
dataset['servers'][hostkey][METRICS_TYPE][metric['name']] = get_endpoint(endpoint)
end
else
METRICS.each do |metric|
post_data << metric['url']
end
metrics_array = retrieve_additional_metrics(host, PORT, METRICS, PE_VERSION, SSL)

endpoint = "#{host_url}/metrics/v1/mbeans"
metrics_output = post_endpoint(endpoint, post_data.to_json)
metrics_array.each do |metric_hash|
metric_name = metric_hash['name']
metric_data = metric_hash['data']

METRICS.each_index do |index|
metric_name = METRICS[index]['name']
metric_data = metrics_output[index]

dataset['servers'][hostkey][METRICS_TYPE][metric_name] = metric_data
end
dataset['servers'][hostkey][METRICS_TYPE][metric_name] = metric_data
end
end

Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "npwalker/pe_metric_curl_cron_jobs",
"version": "4.3.0",
"version": "4.4.0",
"author": "npwalker",
"summary": "A Puppet module for gathering metrics from PE components",
"license": "Apache-2.0",
Expand Down