lib/kpm/system.rb in kpm-0.7.2 vs lib/kpm/system.rb in kpm-0.8.0

- old
+ new

@@ -1,41 +1,43 @@ +# frozen_string_literal: true + require 'yaml' require_relative 'system_helpers/system_proxy' module KPM class System - MAX_VALUE_COLUMN_WIDTH = 60 DEFAULT_BUNDLE_DIR = Dir['/var' + File::SEPARATOR + 'lib' + File::SEPARATOR + 'killbill' + File::SEPARATOR + 'bundles'][0] || Dir['/var' + File::SEPARATOR + 'tmp' + File::SEPARATOR + 'bundles'][0] DEFAULT_KAUI_SEARCH_BASE_DIR = '**' + File::SEPARATOR + 'kaui' DEFAULT_KILLBILL_SEARCH_BASE_DIR = '**' + File::SEPARATOR + 'ROOT' - def initialize + def initialize(logger) + @logger = logger @formatter = KPM::Formatter.new end def information(bundles_dir = nil, output_as_json = false, config_file = nil, kaui_web_path = nil, killbill_web_path = nil) - set_config(config_file) - killbill_information = show_killbill_information(kaui_web_path,killbill_web_path,output_as_json) + self.config = config_file + killbill_information = show_killbill_information(kaui_web_path, killbill_web_path, output_as_json) java_version = `java -version 2>&1`.split("\n")[0].split('"')[1] environment_information = show_environment_information(java_version, output_as_json) os_information = show_os_information(output_as_json) cpu_information = show_cpu_information(output_as_json) memory_information = show_memory_information(output_as_json) disk_space_information = show_disk_space_information(output_as_json) entropy_available = show_entropy_available(output_as_json) - if not java_version.nil? - command = get_command - java_system_information = show_java_system_information(command,output_as_json) + unless java_version.nil? + command = java_command + java_system_information = show_java_system_information(command, output_as_json) end - plugin_information = show_plugin_information(get_plugin_path || bundles_dir || DEFAULT_BUNDLE_DIR, output_as_json) + plugin_information = show_plugin_information(plugin_path || bundles_dir || DEFAULT_BUNDLE_DIR, output_as_json) - json_data = Hash.new + json_data = {} json_data[:killbill_information] = killbill_information json_data[:environment_information] = environment_information json_data[:os_information] = os_information json_data[:cpu_information] = cpu_information json_data[:memory_information] = memory_information @@ -46,236 +48,217 @@ json_data.to_json end def show_killbill_information(kaui_web_path, killbill_web_path, output_as_json) - kpm_version = KPM::VERSION - kaui_version = get_kaui_version(get_kaui_web_path || kaui_web_path) - killbill_version = get_killbill_version(get_killbill_web_path || killbill_web_path) - kaui_standalone_version = get_kaui_standalone_version(get_kaui_web_path || kaui_web_path) + kaui_version = kaui_version(kaui_web_path || kaui_web_path) + killbill_version = killbill_version(killbill_web_path || killbill_web_path) + kaui_standalone_version = kaui_standalone_version(kaui_web_path || kaui_web_path) - environment = Hash[:kpm => {:system=>'KPM',:version => kpm_version}, - :kaui => {:system=>'Kaui',:version => kaui_version.nil? ? 'not found' : kaui_version}, - :kaui_standalone => {:system=>'Kaui standalone',:version => kaui_standalone_version.nil? ? 'not found' : kaui_standalone_version}, - :killbill => {:system=>'Killbill',:version => killbill_version.nil? ? 'not found' : killbill_version}] + environment = Hash[kpm: { system: 'KPM', version: kpm_version }, + kaui: { system: 'Kaui', version: kaui_version.nil? ? 'not found' : kaui_version }, + kaui_standalone: { system: 'Kaui standalone', version: kaui_standalone_version.nil? ? 'not found' : kaui_standalone_version }, + killbill: { system: 'Killbill', version: killbill_version.nil? ? 'not found' : killbill_version }] - labels = [{:label => :system}, - {:label => :version}] + labels = [{ label: :system }, + { label: :version }] - if not output_as_json - @formatter.format(environment,labels) - end + @formatter.format(environment, labels) unless output_as_json environment end def show_environment_information(java_version, output_as_json) + environment = Hash[ruby: { environment: 'Ruby', version: RUBY_VERSION }, + java: { environment: 'Java', version: java_version.nil? ? 'no version found' : java_version }] - environment = Hash[:ruby => {:environment=>'Ruby',:version => RUBY_VERSION}, - :java => {:environment=>'Java',:version => java_version.nil? ? 'no version found' : java_version}] + labels = [{ label: :environment }, + { label: :version }] - labels = [{:label => :environment}, - {:label => :version}] + @formatter.format(environment, labels) unless output_as_json - if not output_as_json - @formatter.format(environment,labels) - end - environment end def show_cpu_information(output_as_json) - cpu_info = KPM::SystemProxy::CpuInformation.fetch - labels = KPM::SystemProxy::CpuInformation.get_labels + cpu_information = KPM::SystemProxy::CpuInformation.new + cpu_info = cpu_information.cpu_info + labels = cpu_information.labels - unless output_as_json - @formatter.format(cpu_info,labels) - end + @formatter.format(cpu_info, labels) unless output_as_json cpu_info end def show_memory_information(output_as_json) - memory_info = KPM::SystemProxy::MemoryInformation.fetch - labels = KPM::SystemProxy::MemoryInformation.get_labels + memory_information = KPM::SystemProxy::MemoryInformation.new + memory_info = memory_information.memory_info + labels = memory_information.labels - unless output_as_json - @formatter.format(memory_info,labels) - end + @formatter.format(memory_info, labels) unless output_as_json memory_info end def show_disk_space_information(output_as_json) - disk_space_info = KPM::SystemProxy::DiskSpaceInformation.fetch - labels = KPM::SystemProxy::DiskSpaceInformation.get_labels + disk_space_information = KPM::SystemProxy::DiskSpaceInformation.new + disk_space_info = disk_space_information.disk_space_info + labels = disk_space_information.labels - unless output_as_json - @formatter.format(disk_space_info,labels) - end + @formatter.format(disk_space_info, labels) unless output_as_json disk_space_info end def show_entropy_available(output_as_json) - entropy_available = KPM::SystemProxy::EntropyAvailable.fetch - labels = KPM::SystemProxy::EntropyAvailable.get_labels + entropy_information = KPM::SystemProxy::EntropyAvailable.new + entropy_available = entropy_information.entropy_available + labels = entropy_information.labels - unless output_as_json - @formatter.format(entropy_available,labels) - end + @formatter.format(entropy_available, labels) unless output_as_json entropy_available end def show_os_information(output_as_json) - os_information = KPM::SystemProxy::OsInformation.fetch - labels = KPM::SystemProxy::OsInformation.get_labels + os_information = KPM::SystemProxy::OsInformation.new + os_info = os_information.os_info + labels = os_information.labels - unless output_as_json - @formatter.format(os_information,labels) - end + @formatter.format(os_info, labels) unless output_as_json - os_information + os_info end def show_java_system_information(command, output_as_json) - java_system = Hash.new - property_count = 0; + java_system = {} + property_count = 0 last_key = '' `#{command}`.split("\n").each do |prop| + break if prop.to_s.strip.empty? - if prop.to_s.strip.empty? - break; - end - - if property_count > 0 + if property_count.positive? props = prop.split('=') - if (not props[1].nil? && props[1].to_s.strip.size > MAX_VALUE_COLUMN_WIDTH) && output_as_json == false + if !(props[1].nil? && props[1].to_s.strip.size > MAX_VALUE_COLUMN_WIDTH) && output_as_json == false chunks = ".{1,#{MAX_VALUE_COLUMN_WIDTH}}" props[1].to_s.scan(/#{chunks}/).each_with_index do |p, index| - - java_system[property_count] = {:java_property => index.equal?(0) ? props[0] : '', :value => p} + java_system[property_count] = { java_property: index.equal?(0) ? props[0] : '', value: p } property_count += 1 - end elsif output_as_json key = (props[1].nil? ? last_key : props[0]).to_s.strip value = props[1].nil? ? props[0] : props[1] - if java_system.has_key?(key) + if java_system.key?(key) java_system[key][:value] = java_system[key][:value].to_s.concat(' ').concat(value) else - java_system[key] = {:java_property => key, :value => value} + java_system[key] = { java_property: key, value: value } end else - java_system[property_count] = {:java_property => props[1].nil? ? '' : props[0], :value => props[1].nil? ? props[0] : props[1]} + java_system[property_count] = { java_property: props[1].nil? ? '' : props[0], value: props[1].nil? ? props[0] : props[1] } end last_key = props[1].nil? ? last_key : props[0] end property_count += 1 - end - labels = [{:label => :java_property}, - {:label => :value}] + labels = [{ label: :java_property }, + { label: :value }] + @formatter.format(java_system, labels) unless output_as_json - if not output_as_json - @formatter.format(java_system,labels) - end - java_system - end def show_plugin_information(bundles_dir, output_as_json) - if bundles_dir.nil? all_plugins = nil else inspector = KPM::Inspector.new all_plugins = inspector.inspect(bundles_dir) end unless output_as_json - if all_plugins.nil? || all_plugins.size == 0 + if all_plugins.nil? || all_plugins.empty? puts "\e[91;1mNo KB plugin information available\e[0m\n\n" else @formatter.format(all_plugins) end end - if output_as_json && (all_plugins.nil? || all_plugins.size == 0) - all_plugins = 'No KB plugin information available' - end + all_plugins = 'No KB plugin information available' if output_as_json && (all_plugins.nil? || all_plugins.empty?) all_plugins end - def get_kaui_standalone_version(kaui_web_path = nil) + private + + def kaui_standalone_version(kaui_web_path = nil) kaui_search_default_dir = kaui_web_path.nil? ? DEFAULT_KAUI_SEARCH_BASE_DIR : Dir[kaui_web_path][0] return nil if kaui_search_default_dir.nil? - kaui_search_default_dir.gsub!('.war','') + + kaui_search_default_dir.gsub!('.war', '') version = nil yaml_file = kaui_search_default_dir + File::SEPARATOR + 'WEB-INF' + File::SEPARATOR + 'version.yml' unless Dir[yaml_file][0].nil? - yml_data = YAML::load_file(Dir[yaml_file][0]) + yml_data = YAML.load_file(Dir[yaml_file][0]) version = yml_data['version'] end version end - def get_kaui_version(kaui_web_path = nil) + def kaui_version(kaui_web_path = nil) kaui_search_default_dir = kaui_web_path.nil? ? DEFAULT_KAUI_SEARCH_BASE_DIR : Dir[kaui_web_path][0] return nil if kaui_search_default_dir.nil? - kaui_search_default_dir.gsub!('.war','') + + kaui_search_default_dir.gsub!('.war', '') version = nil gemfile = Dir[kaui_search_default_dir + File::SEPARATOR + 'WEB-INF' + File::SEPARATOR + 'Gemfile'] unless gemfile[0].nil? absolute_gemfile_path = File.absolute_path(gemfile[0]) - version = open(absolute_gemfile_path) do |f| + version = File.open(absolute_gemfile_path, 'r') do |f| f.each_line.detect do |line| - if /kaui/.match(line) - version = /(\d+)\.(\d+)\.(\d+)/.match(line) + next unless /kaui/.match(line) - break unless version.nil? - end + version = /(\d+)\.(\d+)\.(\d+)/.match(line) + + break unless version.nil? end version end end version end - def get_killbill_version(killbill_web_path = nil) + def killbill_version(killbill_web_path = nil) killbill_search_default_dir = killbill_web_path.nil? ? DEFAULT_KILLBILL_SEARCH_BASE_DIR : Dir[killbill_web_path][0] return nil if killbill_search_default_dir.nil? - killbill_search_default_dir.gsub!('.war','') - killbill_search_default_dir.gsub!('webapps','**') - file = Dir[killbill_search_default_dir + File::SEPARATOR + 'META-INF' + File::SEPARATOR + '**' + File::SEPARATOR + 'pom.properties'] + killbill_search_default_dir.gsub!('.war', '') + killbill_search_default_dir.gsub!('webapps', '**') + + file = Dir[killbill_search_default_dir + File::SEPARATOR + 'META-INF' + File::SEPARATOR + '**' + File::SEPARATOR + 'pom.properties'] version = nil unless file[0].nil? absolute_file_path = File.absolute_path(file[0]) - version = open(absolute_file_path) do |f| + version = File.open(absolute_file_path, 'r') do |f| f.each_line.detect do |line| version = /(\d+)\.(\d+)\.(\d+)/.match(line) break unless version.nil? end @@ -285,95 +268,81 @@ end version end - def get_command + def java_command command = 'java -XshowSettings:properties -version 2>&1' - apache_tomcat_pid = get_apache_tomcat_pid + apache_tomcat_pid = find_apache_tomcat_pid + @logger.debug("Found Tomcat PID: #{apache_tomcat_pid}") - if not apache_tomcat_pid.nil? - command = "jcmd #{apache_tomcat_pid} VM.system_properties" - end + command = "jcmd #{apache_tomcat_pid} VM.system_properties" unless apache_tomcat_pid.nil? command end - def get_apache_tomcat_pid - apache_tomcat_pid = nil; + def find_apache_tomcat_pid + apache_tomcat_pid = nil `jcmd -l 2>&1`.split("\n").each do |line| - if /org.apache.catalina/.match(line) words = line.split(' ') apache_tomcat_pid = words[0] end - end return apache_tomcat_pid unless apache_tomcat_pid.nil? - jcmd = ( ENV['JAVA_HOME'] || '/**' ) + File::Separator + 'bin' + File::Separator + 'jcmd' + jcmd = (ENV['JAVA_HOME'] || '/**') + File::Separator + 'bin' + File::Separator + 'jcmd' jcmd = Dir[jcmd][0] return nil if jcmd.nil? - apache_tomcat_pid = `#{jcmd} | awk '/org.apache.catalina/' | cut -d ' ' -f 1`.gsub("\n",'') + apache_tomcat_pid = `#{jcmd} | awk '/org.apache.catalina/' | cut -d ' ' -f 1`.gsub("\n", '') return nil if apache_tomcat_pid.nil? || apache_tomcat_pid.empty? apache_tomcat_pid end - def set_config(config_file = nil) + def config=(config_file = nil) @config = nil - if not config_file.nil? - if not Dir[config_file][0].nil? - @config = YAML::load_file(config_file) - end - end + return if config_file.nil? + @config = YAML.load_file(config_file) unless Dir[config_file][0].nil? end - def get_kaui_web_path - kaui_web_path = nil; + def kaui_web_path + kaui_web_path = nil - if not @config.nil? + unless @config.nil? config_kaui = @config['kaui'] - if not config_kaui.nil? - kaui_web_path = Dir[config_kaui['webapp_path']][0] - end + kaui_web_path = Dir[config_kaui['webapp_path']][0] unless config_kaui.nil? end kaui_web_path end - def get_killbill_web_path - killbill_web_path = nil; + def killbill_web_path + killbill_web_path = nil - if not @config.nil? + unless @config.nil? config_killbill = @config['killbill'] - if not config_killbill.nil? - killbill_web_path = Dir[config_killbill['webapp_path']][0] - end + killbill_web_path = Dir[config_killbill['webapp_path']][0] unless config_killbill.nil? end killbill_web_path end - def get_plugin_path - plugin_path = nil; + def plugin_path + plugin_path = nil - if not @config.nil? + unless @config.nil? config_killbill = @config['killbill'] - if not config_killbill.nil? - plugin_path = Dir[config_killbill['plugins_dir']][0] - end + plugin_path = Dir[config_killbill['plugins_dir']][0] unless config_killbill.nil? end plugin_path end - end - -end \ No newline at end of file +end