Module: RCS::Diagnosticable
- Defined in:
- lib/rcs-common/diagnosticable.rb
Instance Method Summary collapse
- #change_trace_level(level) ⇒ Object
- #command_output(name) ⇒ Object
- #config_files ⇒ Object
- #execution_directory ⇒ Object
- #get_version_info ⇒ Object
- #grouped_logs(glob: "#{log_path}/*", deepness: 2) ⇒ Object
- #hide_addresses(string) ⇒ Object
- #huge_log?(path) ⇒ Boolean
- #log_path ⇒ Object
- #machine_info ⇒ Object
- #os_lang ⇒ Object
- #pretty_print(hash, out = $stdout) ⇒ Object
- #relevant_logs ⇒ Object
-
#services_state ⇒ Object
Get the state (running, stopped, etc.) and the configuration information of all the Windows services that start with “RCS”.
- #windows? ⇒ Boolean
Instance Method Details
#change_trace_level(level) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/rcs-common/diagnosticable.rb', line 39 def change_trace_level(level) trace_yaml = "#{execution_directory}/config/trace.yaml" raise "Unable to find file #{trace_yaml}" unless File.exists?(trace_yaml) content = File.read(trace_yaml) content.gsub!(/(name\s*:\s*logfile\n\s*level\s*:\s*)[A-Z]+/, '\1'+level.to_s.upcase) File.open(trace_yaml, "wb") { |f| f.write(content) } end |
#command_output(name) ⇒ Object
69 70 71 72 73 |
# File 'lib/rcs-common/diagnosticable.rb', line 69 def command_output(name) cmd = (name =~ /rcs\-/) ? "ruby #{execution_directory}/bin/#{name}" : name output = `#{cmd}` "Output of command #{name}\n#{output}\n\n" end |
#config_files ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/rcs-common/diagnosticable.rb', line 75 def config_files paths = [] %w[VERSION VERSION_BUILD certs/*.crt certs/*.pem certs/*.key *.yaml *.lic *.crt *.pem gapi].each do |glob| paths += Dir["#{execution_directory}/config/#{glob}"] end paths.flatten! paths.uniq! paths end |
#execution_directory ⇒ Object
6 7 8 |
# File 'lib/rcs-common/diagnosticable.rb', line 6 def execution_directory File.(Dir.pwd) end |
#get_version_info ⇒ Object
47 48 49 50 51 |
# File 'lib/rcs-common/diagnosticable.rb', line 47 def get_version_info version = File.read("#{execution_directory}/config/VERSION") build = File.read("#{execution_directory}/config/VERSION_BUILD") return version, build end |
#grouped_logs(glob: "#{log_path}/*", deepness: 2) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rcs-common/diagnosticable.rb', line 14 def grouped_logs(glob: "#{log_path}/*", deepness: 2) groups = Hash.new { |h, k| h[k] = [] } regexp = /(rcs\-.+)\_\d{4}-\d{2}-\d{2}\.log|(mongo..log)/ Dir[glob].each do |path| filename = File.basename(path) group_name = filename.scan(regexp).flatten.compact.first groups[group_name] << path if group_name end groups.keys.each do |group_name| groups[group_name].sort! { |p1, p2| File.mtime(p2).to_i <=> File.mtime(p1).to_i } groups[group_name] = groups[group_name][0..deepness - 1] end groups end |
#hide_addresses(string) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/rcs-common/diagnosticable.rb', line 126 def hide_addresses(string) if .respond_to?(:[]) and [:hide_addresses] mask = "###.###.###.###" string.gsub!(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/, mask) string.gsub!(/([a-zA-Z0-9\-\.]+)\:(4444|443|80|2701\d)/, mask+':\2') end string end |
#huge_log?(path) ⇒ Boolean
122 123 124 |
# File 'lib/rcs-common/diagnosticable.rb', line 122 def huge_log?(path) File.size(path) > 52428800 # 50 megabytes end |
#log_path ⇒ Object
10 11 12 |
# File 'lib/rcs-common/diagnosticable.rb', line 10 def log_path File.("./log", execution_directory) end |
#machine_info ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rcs-common/diagnosticable.rb', line 85 def machine_info hash = {} keys = %w[build_os build_vendor build_cpu build RUBY_VERSION_NAME] hash['rbconfig'] = RbConfig::CONFIG.reject { |key| !keys.include?(key) } hash['os_lang'] = os_lang hash['gem_list'] = `gem list`.split("\n") hash['ENV'] = ENV.reject { |key| !%w[RUBY_VERSION PWD].include?(key) } fs = Sys::Filesystem.stat(windows? ? "#{Dir.pwd[0]+":\\"}" : "/") hash['filesystem'] = { 'path' => fs.path, 'size' => (fs.blocks * fs.block_size).to_s_bytes, 'free' => (fs.blocks_free * fs.block_size).to_s_bytes, } hash end |
#os_lang ⇒ Object
63 64 65 66 67 |
# File 'lib/rcs-common/diagnosticable.rb', line 63 def os_lang return unless windows? result = `reg query \"HKLM\\system\\controlset001\\control\\nls\\language\" /v Installlanguage` result.scan(/REG_SZ\s*(.{4})/).flatten.first end |
#pretty_print(hash, out = $stdout) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/rcs-common/diagnosticable.rb', line 32 def pretty_print(hash, out = $stdout) string = JSON.pretty_generate(hash) string = hide_addresses(string) out.write(string) out.write("\n") end |
#relevant_logs ⇒ Object
53 54 55 56 57 |
# File 'lib/rcs-common/diagnosticable.rb', line 53 def relevant_logs list = grouped_logs.values + grouped_logs(glob: "#{log_path}/err/*", deepness: 7).values list.flatten! list end |
#services_state ⇒ Object
Get the state (running, stopped, etc.) and the configuration information of all the Windows services that start with “RCS”
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rcs-common/diagnosticable.rb', line 105 def services_state states = "" config = "" `sc query state= all`.split("SERVICE_NAME:").each do |info| name = info.scan(/\s*(.+)\n/).flatten.first if name =~ /^rcs/i state = info.scan(/STATE\s+:\s+\d+\s*(.+)\s*/).flatten.first states << "#{name}: #{state}\n" config << `sc qc #{name}`.scan(/SERVICE_NAME:\s*(.*)/m).flatten.last.to_s end end return "#{states}\n#{config}" end |
#windows? ⇒ Boolean
59 60 61 |
# File 'lib/rcs-common/diagnosticable.rb', line 59 def windows? RbConfig::CONFIG['host_os'] =~ /mingw/ end |