Skip to content

Instantly share code, notes, and snippets.

@tkishel
Last active April 15, 2020 18:36
Show Gist options
  • Save tkishel/403a21005d66959da7ced33e2959e927 to your computer and use it in GitHub Desktop.
Save tkishel/403a21005d66959da7ced33e2959e927 to your computer and use it in GitHub Desktop.
Extract current (startup and running) tunable configuration of PE Services from a Support Script
#!/usr/bin/env ruby
# Change into the Support Script output directory, and execute this script.
# Or, pass the directory as the parameter.
require 'json'
# Convert JAVA_ARGS string to a Hash.
def java_args_to_hash(s)
xms = '?'
xmx = '?'
xms_matches = s.match(/\-Xms(\d+\w{1})/)
xmx_matches = s.match(/\-Xmx(\d+\w{1})/)
xms = xms_matches[1] if xms_matches
xmx = xmx_matches[1] if xmx_matches
{ 'Xms' => xms, 'Xmx' => xmx }
end
# Convert a string to a unit of memory, defaults to megabytes.
def string_to_memory(s, default_units = 'm')
return 0 if s.nil?
matches = %r{(\d+)\s*(\w?)}.match(s.to_s)
return 0 if matches.nil?
value = matches[1].to_f
units = matches[2].empty? ? default_units : matches[2].downcase
case units
when 'm' then return value.to_i
when 'g' then return (value * (1 << 10)).to_i
else
0
end
end
# Process command-line parameters.
if ARGV[0]
support_script_directory = ARGV[0]
unless File.directory?(support_script_directory)
puts "Error: not a directory: #{support_script_directory}"
exit 1
end
else
support_script_directory = Dir.pwd
end
# Begin: Support Script Variations: v1 vs v3
if File.file?("#{support_script_directory}/enterprise/puppet_facts.txt")
facts = JSON.parse(File.read("#{support_script_directory}/enterprise/puppet_facts.txt"))
else
facts = JSON.parse(File.read("#{support_script_directory}/system/facter_output.json"))
end
if File.directory?("#{support_script_directory}/enterprise/etc/puppetlabs")
etc_puppetlabs = 'enterprise/etc/puppetlabs'
else
etc_puppetlabs = 'etc/puppetlabs'
end
if File.directory?("#{support_script_directory}/opt/puppetlabs/server/data/postgresql")
pg_data = 'opt/puppetlabs/server/data/postgresql'
else
pg_data = 'enterprise/etc/puppetlabs/postgres'
end
service_init_config = 'etc/default' if File.directory?("#{support_script_directory}/etc/default")
service_init_config = 'etc/sysconfig' if File.directory?("#{support_script_directory}/etc/sysconfig")
service_init_config = 'system/etc' if File.directory?("#{support_script_directory}/system/etc")
service_init_config = 'system/etc/default' if File.directory?("#{support_script_directory}/system/etc/default")
service_init_config = 'system/etc/sysconfig' if File.directory?("#{support_script_directory}/system/etc/sysconfig")
# End: Support Script Variations: v1 vs v3
facts = facts['values'] if facts.key?('values')
fqdn = facts['networking']['fqdn'] || '?'
total_cpu = facts['processors']['count'] || 0
total_ram = facts['memory']['system']['total_bytes'] / 1024 / 1024 || 0
pg_version = facts['pe_postgresql_info']['installed_server_version']
pg_data = "#{pg_data}/#{pg_version}/data"
services = {}
java_services = ['pe-console-services','pe-orchestration-services','pe-puppetdb','pe-puppetserver']
java_services.each do |service|
services[service] = { 'startup_config' => {}, 'running_config' => {} }
end
# Startup Java Services Configuration
java_services.each do |service|
if File.exists?("#{support_script_directory}/#{service_init_config}/#{service}")
conf = File.read("#{support_script_directory}/#{service_init_config}/#{service}")
conf_match = conf.match(/^java_args_to_hash\="(.*)/)
java_args_to_hash_text = conf_match ? conf_match[1] : ''
services[service]['startup_config'] = java_args_to_hash(java_args_to_hash_text)
if service == 'pe-puppetserver' || service == 'pe-orchestration-services'
conf_match = java_args_to_hash_text.match(/ReservedCodeCacheSize\=(\d+\w{1})/)
rccs = conf_match ? conf_match[1] : ''
services[service]['startup_config']['ReservedCodeCacheSize'] = rccs
end
end
end
# Startup PostgreSQL Configuration
# PostgreSQL supports duplicate settings, with the last winning.
postgresql_shared_buffers = 0
postgresql_work_mem = 0
postgresql_conf = "#{support_script_directory}/#{pg_data}/postgresql.conf"
if File.exists?(postgresql_conf)
conf = File.read(postgresql_conf)
conf_scan = conf.scan(/^shared_buffers\s?=\s?(\d+\w+)/)
postgresql_shared_buffers = conf_scan.last ? conf_scan.last[0] : ''
conf_scan = conf.scan(/^work_mem\s?=\s?(\d+\w+)/)
postgresql_work_mem = conf_scan.last ? conf_scan.last[0] : ''
end
# Startup PuppetDB Configuration
puppetdb_command_processing_threads = 0
config_ini = "#{support_script_directory}/#{etc_puppetlabs}/puppetdb/conf.d/config.ini"
if File.exists?(config_ini)
conf = File.read(config_ini)
conf_match = conf.match(/^threads\s?=\s?(\d+)/)
puppetdb_command_processing_threads = conf_match ? conf_match[1] : 0
end
# Startup PuppetServer Configuration
puppetserver_max_active_instances = 0
pe_puppet_server_conf = "#{support_script_directory}/#{etc_puppetlabs}/puppetserver/conf.d/pe-puppet-server.conf"
if File.exists?(pe_puppet_server_conf)
conf = File.read(pe_puppet_server_conf)
conf_match = conf.match(/max-active-instances\s?:\s?(\d+)/)
puppetserver_max_active_instances = conf_match ? conf_match[1] : 0
end
# Running Java Services Configuration
ps_aux = "#{support_script_directory}/system/ps_aux.txt"
processlist = File.read(ps_aux).lines
processlist.each do |process|
if process.match(%r{/var/log/puppetlabs/console-services})
services['pe-console-services']['running_config'] = java_args_to_hash(process)
end
if process.match(%r{/var/log/puppetlabs/orchestration-services})
services['pe-orchestration-services']['running_config'] = java_args_to_hash(process)
end
if process.match(%r{/var/log/puppetlabs/puppetdb})
services['pe-puppetdb']['running_config'] = java_args_to_hash(process)
end
if process.match(%r{/var/log/puppetlabs/puppetserver})
services['pe-puppetserver']['running_config'] = java_args_to_hash(process)
process_match = process.match(/ReservedCodeCacheSize\=(\d+\w{1})/)
rccs = process_match ? process_match[1] : ''
services['pe-puppetserver']['running_config']['ReservedCodeCacheSize'] = rccs
end
end
# System Resources
total_cpu_used = puppetdb_command_processing_threads.to_i + puppetserver_max_active_instances.to_i
total_ram_used = string_to_memory(postgresql_shared_buffers) + string_to_memory(services['pe-puppetdb']['running_config']['Xmx']) + string_to_memory(services['pe-orchestration-services']['running_config']['Xmx']) + string_to_memory(services['pe-console-services']['running_config']['Xmx']) + string_to_memory(services['pe-puppetserver']['running_config']['Xmx']) + string_to_memory(services['pe-puppetserver']['running_config']['ReservedCodeCacheSize'])
# Nodes
active_nodes = 0
active_nodes_source = 'Unknown'
if File.file?("#{support_script_directory}/enterprise/puppetdb_nodes.json")
stats = JSON.parse(File.read("#{support_script_directory}/enterprise/puppetdb_nodes.json"))
active_nodes = stats.count
active_nodes_source = 'enterprise/puppetdb_nodes.json'
end
if File.file?("#{support_script_directory}/enterprise/puppetdb_summary_stats.json") && active_nodes == 0
stats = JSON.parse(File.read("#{support_script_directory}/enterprise/puppetdb_summary_stats.json"))
active_nodes = stats['node_activity'].select {| type | type['active'] == true }['count']
active_nodes_source = 'enterprise/puppetdb_summary_stats.json'
end
if File.file?("#{support_script_directory}/enterprise/certs.txt") && active_nodes == 0
active_nodes = File.open("#{support_script_directory}/enterprise/certs.txt",'r').readlines.size - 1
active_nodes_source = 'enterprise/certs.txt'
end
puts
puts "Server: #{fqdn}"
puts "Resources Available: #{total_cpu} CPU / #{total_ram} MB RAM"
puts "Resources Used : #{total_cpu_used} CPU / #{total_ram_used} MB RAM"
puts
puts '#########################################################################'
puts
puts "Startup Config:"
puts
puts "PostgreSQL Shared Buffers: #{postgresql_shared_buffers}"
puts "PostgreSQL Work Memory: #{postgresql_work_mem}"
puts
puts "PuppetDB Command Processing Threads: #{puppetdb_command_processing_threads}"
puts "PuppetServer Max Active Instances: #{puppetserver_max_active_instances}"
puts
puts "PuppetDB: #{services['pe-puppetdb']['startup_config']}"
puts "Orchestrator: #{services['pe-orchestration-services']['startup_config']}"
puts "Console: #{services['pe-console-services']['startup_config']}"
puts "PuppetServer: #{services['pe-puppetserver']['startup_config']}"
puts
puts '#########################################################################'
puts
puts "Running Config:"
puts
puts "PuppetDB: #{services['pe-puppetdb']['running_config']}"
puts "Orchestrator: #{services['pe-orchestration-services']['running_config']}"
puts "Console: #{services['pe-console-services']['running_config']}"
puts "PuppetServer: #{services['pe-puppetserver']['running_config']}"
puts
puts '#########################################################################'
puts
puts "Statistics:"
puts
puts "Active Nodes: #{active_nodes} (from '#{active_nodes_source}')"
puts
@tkishel
Copy link
Author

tkishel commented Sep 30, 2019

Server: master.example.com
Resources Available: 4 CPU / 7821 MB RAM
Resources Used     : 2 CPU / 4979 MB RAM

#########################################################################

Startup Config:

PostgreSQL Shared Buffers:           883MB
PostgreSQL Work Memory:              4MB

PuppetDB Command Processing Threads: 2
PuppetServer Max Active Instances:   0

PuppetDB:      {}
Orchestrator:  {}
Console:       {}
PuppetServer:  {}

#########################################################################

Running Config:

PuppetDB:      {"Xms"=>"512m", "Xmx"=>"512m"}
Orchestrator:  {"Xms"=>"768m", "Xmx"=>"768m"}
Console:       {"Xms"=>"256m", "Xmx"=>"256m"}
PuppetServer:  {"Xms"=>"2048m", "Xmx"=>"2048m", "ReservedCodeCacheSize"=>"512m"}

#########################################################################

Statistics:

Active Nodes: 1005 (from 'enterprise/puppetdb_nodes.json')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment