Class: Diagtool::CollectUtils

Inherits:
Object
  • Object
show all
Includes:
Windows::PlatformSpecificCollectUtils
Defined in:
lib/fluent/diagtool/collectutils.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf, log_level) ⇒ CollectUtils



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fluent/diagtool/collectutils.rb', line 28

def initialize(conf, log_level)
  @logger = Logger.new(STDOUT, level: log_level, formatter: proc {|severity, datetime, progname, msg|
    "#{datetime}: [Collectutils] [#{severity}] #{msg}\n"
  })
  @precheck = conf[:precheck]
  @type = conf[:type]
  @time_format = conf[:time]
  @basedir = conf[:basedir]
  @workdir = conf[:workdir]
  @outdir = conf[:outdir]
  @tdenv = {
    'FLUENT_CONF' => '',
    'TD_AGENT_LOG_FILE' => '',
    'FLUENT_PACKAGE_LOG_FILE' => '',
  }
  @package_name = conf[:package_name]
  @service_name = conf[:service_name]

  case @type
  when 'fluentd'
    _find_fluentd_info()
  when 'fluentbit'
    _find_fluentbit_info()
  end

  if not conf[:tdconf].empty?
    @tdconf = conf[:tdconf].split('/')[-1]
    @tdconf_path = conf[:tdconf].gsub(@tdconf,'')
  elsif
    if not @tdenv['FLUENT_CONF'].empty?
      @tdconf = @tdenv['FLUENT_CONF'].split('/')[-1]
  	  @tdconf_path = @tdenv['FLUENT_CONF'].gsub(@tdconf,'')
	else
	  raise "The path of #{@package_name} configuration file need to be specified."  if conf[:precheck] == false
	end
  end
  if not conf[:tdlog].empty?
    @tdlog = conf[:tdlog].split('/')[-1]
    @tdlog_path = conf[:tdlog].gsub(@tdlog,'')
  elsif
    if not @tdenv['TD_AGENT_LOG_FILE'].empty?
      @tdlog =  @tdenv['TD_AGENT_LOG_FILE'].split('/')[-1]
      @tdlog_path = @tdenv['TD_AGENT_LOG_FILE'].gsub(@tdlog,'')
    elsif not @tdenv['FLUENT_PACKAGE_LOG_FILE'].empty?
      @tdlog =  @tdenv['FLUENT_PACKAGE_LOG_FILE'].split('/')[-1]
      @tdlog_path = @tdenv['FLUENT_PACKAGE_LOG_FILE'].gsub(@tdlog,'')
    else
      case @type
      when 'fluentd'
        raise "The path of #{@package_name} log file need to be specified." if conf[:precheck] == false
      when 'fluentbit'
        @logger.warn("FluentBit logs are redirected to the standard output interface ")
      end
   end
  end 
  @osenv = _find_os_info()
  @oslog_path = '/var/log/'
  @oslog = 'messages'
  @syslog = 'syslog'
  @sysctl_path = '/etc/'
  @sysctl = 'sysctl.conf'

  @logger.info("Loading the environment parameters...")
  @logger.info("    operating system = #{@osenv['Operating System']}")
  @logger.info("    kernel version = #{@osenv['Kernel']}")
  @logger.info("    #{@package_name} conf path = #{@tdconf_path}")
  @logger.info("    #{@package_name} conf file = #{@tdconf}")
  @logger.info("    #{@package_name} log path = #{@tdlog_path}")
  @logger.info("    #{@package_name} log = #{@tdlog}")
end

Instance Method Details

#_collect_tdbit_parser(conf) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/fluent/diagtool/collectutils.rb', line 284

def _collect_tdbit_parser(conf)
  target_dir = @workdir+@tdconf_path
  parser_conf = []
  File.readlines(conf).each do |line|
    if line.strip.start_with?('parsers_file') || line.strip.start_with?('Parsers_File')
      l = line.split()[1]
      if l.start_with?(/\//)  # /tmp/work1/b.conf
        if l.include?('*')
          Dir.glob(l).each { |ll|
            pconf = target_dir + ll.gsub(/\//,'-')
            FileUtils.cp(ll, pconf)
            parser_conf.push(pconf)
          }
        else 
          pconf = target_dir+l.gsub(/\//,'-')
          FileUtils.cp(l, pconf)
          parser_conf.push(pconf)
        end
      else
        l = l.gsub('./','') if l.include?('./')
        if l.include?('*')
          Dir.glob(@tdconf_path+f).each{ |ll|
            pconf = target_dir + ll.gsub(@tdconf_path,'').gsub(/\//,'-')
            FileUtils.cp(ll, pconf)
            parser_conf.push(pconf)
          }
        else
          pconf = target_dir+l.gsub(/\//,'-')
          FileUtils.cp(@tdconf_path+l, pconf)
          parser_conf.push(pconf)
        end
      end
    end
  end  
  return parser_conf
end

#_collect_tdbit_plugins(conf) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/fluent/diagtool/collectutils.rb', line 321

def _collect_tdbit_plugins(conf)
  target_dir = @workdir+@tdconf_path
  plugins_conf = []
  File.readlines(conf).each do |line|
    if line.strip.start_with?('plugins_file') || line.strip.start_with?('Plugins_File')
      l = line.split()[1]
      if l.start_with?(/\//)  # /tmp/work1/b.conf
        if l.include?('*')
          Dir.glob(l).each { |ll|
            pconf = target_dir + ll.gsub(/\//,'-')
            FileUtils.cp(ll, pconf)
            plugins_conf.push(pconf)
          }
        else 
          pconf = target_dir+l.gsub(/\//,'-')
          FileUtils.cp(l, pconf)
          plugins_conf.push(pconf)
        end
      else
        l = l.gsub('./','') if l.include?('./')
        if l.include?('*')
          Dir.glob(@tdconf_path+f).each{ |ll|
            pconf = target_dir + ll.gsub(@tdconf_path,'').gsub(/\//,'-')
            FileUtils.cp(ll, pconf)
            plugins_conf.push(pconf)
          }
        else
          pconf = target_dir+l.gsub(/\//,'-')
          FileUtils.cp(@tdconf_path+l, pconf)
          plugins_conf.push(pconf)
        end
      end
    end
  end
  return plugins_conf
end

#_collect_tdconf_include(conf) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/fluent/diagtool/collectutils.rb', line 238

def _collect_tdconf_include(conf)
  target_dir = @workdir+@tdconf_path
  inc_list = []
  File.readlines(conf).each do |line|
    if line.start_with?('@include')
      l = line.split()[1]
      if l.start_with?('http')
        uri = URI(l)
        inc_http = target_dir + 'http' + uri.path.gsub('/','_')
        File.open(inc_http, 'w') do |f|
          f.puts(Net::HTTP.get(uri))
        end
        inc_list.push inc_http
      else
        if l.start_with?('/')  # /tmp/work1/b.conf
          if l.include?('*')
            Dir.glob(l).each { |ll|
              inc_conf = target_dir + ll.gsub(/\//,'-')
              FileUtils.cp(ll, inc_conf)
              inc_list.push inc_conf
            }
          else 
            inc_conf = target_dir+l.gsub(/\//,'-')
            FileUtils.cp(l, inc_conf)
            inc_list.push inc_conf
          end
        else
          l = l.gsub('./','') if l.include?('./')
          if l.include?('*')
            Dir.glob(@tdconf_path+f).each{ |ll|
              inc_conf = target_dir + ll.gsub(@tdconf_path,'').gsub(/\//,'-')
              FileUtils.cp(ll, inc_conf)
              inc_list.push inc_conf
            }
          else
            inc_conf = target_dir+l.gsub(/\//,'-')
            FileUtils.cp(@tdconf_path+l, inc_conf)
            inc_list.push inc_conf
          end
        end
      end
    end
  end
  return inc_list
end

#_find_fluentbit_infoObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/fluent/diagtool/collectutils.rb', line 158

def _find_fluentbit_info()
  ### check if the td-agent-bit is run as daemon
  stdout, stderr, status = Open3.capture3('systemctl cat td-agent-bit')
  if status.success?
    if @precheck == false  # SKip if precheck is true
      File.open(@outdir+'/td-agent-bit_env.output', 'w') do |f|
        f.puts(stdout)
      end
    end
    stdout.split(/\n/).each do | line |
      if line.start_with?("ExecStart")
        cmd = line.split("=")[1]
        i =0
        cmd.split().each do | c |
          case
          when c == "-c"
            @tdenv['FLUENT_CONF'] = cmd.split()[i+1]
          when c == "-l"
            @tdenv['TD_AGENT_LOG_FILE'] = cmd.split()[i+1]
          when c.include?("--conf")
            @tdenv['FLUENT_CONF'] = c.split("=")[1]
          when c.include?("--log")
            @tdenv['TD_AGENT_LOG_FILE'] = c.split("=")[1]
          end
          i+=1
        end
      end
    end
  else
    ### check if the td-agent-bit is not run as daemon or run FluentdBit with customized script 
    stdout, stderr, status = Open3.capture3('ps aux | grep fluent-bit | grep -v ".*\(grep\|diagtool\)"')
    if status.success?
      i = 0
      stdout.split().each do | line |
        case
        when line.include?("--conf")
          @tdenv['FLUENT_CONF'] = line.split("=")[1]
        when line.include?("--log")
          @tdenv['TD_AGENT_LOG_FILE'] = line.split("=")[1]
        when line == "-c"
          @tdenv['FLUENT_CONF'] = stdout.split()[i+1]
        when line == "-l"
          @tdenv['TD_AGENT_LOG_FILE'] = stdout.split()[i+1]
        end
        i+=1
      end
    else
      @logger.warn("No FluentBit daemon or proccess running")
    end
  end
end

#_find_fluentd_infoObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fluent/diagtool/collectutils.rb', line 114

def _find_fluentd_info()
  ### check if the td-agent is run as daemon
  stdout, stderr, status = Open3.capture3("systemctl cat #{@service_name}")
  if status.success?
    if @precheck == false  # SKip if precheck is true
      File.open(@outdir+'/td-agent_env.output', 'w') do |f|
        f.puts(stdout)
      end
    end  
    stdout.split().each do | l |
      if l.include?('Environment')
        @tdenv[l.split('=')[1]] = l.split('=')[2]
      end
    end
  else
    ### check if the td-agent is not run as daemon or run Fluentd with customized script
    stdout, stderr, status = Open3.capture3('ps aux | grep fluentd | grep -v ".*\(grep\|diagtool\)"')
    if status.success?
      line = stdout.split(/\n/)
      line.each do |l|
        cmd = l.split.drop(10)
        i = 0
        if cmd[-1] != '--under-supervisor'
          cmd.each do |c|
            case
            when c == "-c"
              @tdenv['FLUENT_CONF'] = cmd[i+1]
            when c == "-l"
              @tdenv['TD_AGENT_LOG_FILE'] = cmd[i+1]
            when c.include?("--conf")
              @tdenv['FLUENT_CONF'] = c.split("=")[1]
            when c.include?("--log")
              @tdenv['TD_AGENT_LOG_FILE'] = c.split("=")[1]
            end
            i+=1
          end
        end
      end
    else
      @logger.warn("No Fluentd daemon or proccess running") 
    end
  end
end

#_find_os_infoObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fluent/diagtool/collectutils.rb', line 99

def _find_os_info()
  stdout, stderr, status = Open3.capture3('hostnamectl')
  os_dict = {}
  stdout.each_line { |l|
    s = l.split(":")
    os_dict[s[0].chomp.strip] = s[1].chomp.strip
  }
  if @precheck == false  # SKip if precheck is true
    File.open(@outdir+'/os_env.output', 'w') do |f|
      f.puts(stdout)
    end
  end
  return os_dict
end

#collect_bundled_pluginsObject



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/fluent/diagtool/collectutils.rb', line 422

def collect_bundled_plugins
  gemfile_path = if fluent_package?
                   "/opt/fluent/share/Gemfile"
                 else
                   "/opt/td-agent/share/Gemfile"
                 end
  File.read(gemfile_path).lines(chomp: true).grep(/\Agem "fluent-plugin-/).collect do |line|
    gem_name = line[/\Agem "(fluent-plugin-.+?)"/, 1]
    platforms_option = line[/platforms: (\w+)/, 1]
    next gem_name, platforms_option
  end.select do |gem_name, platforms_option|
    platforms_option.nil? || match_platform?(platforms_option)
  end.collect do |gem_name, platforms_option|
    gem_name
  end
end

#collect_cmd_output(cmd) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/fluent/diagtool/collectutils.rb', line 392

def collect_cmd_output(cmd)
  if system(cmd + '> /dev/null 2>&1')
    cmd_name = cmd.gsub(/\s/,'_').gsub(/\//,'-').gsub(',','_')
    output = @outdir+'/'+cmd_name+'.txt'
    stdout, stderr, status = Open3.capture3(cmd)
    if status.success?
      File.open(output, 'w') do |f|
        f.puts(stdout)
      end
    else
      @logger.warn("Command #{cmd} failed due to the following message -  #{stderr.chomp}")
    end
  else
    @logger.warn("Command #{cmd} does not exist -  skip collecting #{cmd} output")
  end
  return output
end

#collect_manually_installed_gems(gemlist_path) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/fluent/diagtool/collectutils.rb', line 439

def collect_manually_installed_gems(gemlist_path)
  installed_gems = File.read(gemlist_path).lines(chomp: true).collect do |line|
    line.split.first
  end
  bundled_plugins = collect_bundled_plugins
  bundled_gem_path = File.join(@outdir, "gem_bundled_list.output")
  File.open(bundled_gem_path, 'w') do |f|
    f.puts(bundled_plugins.join("\n"))
  end
  local_gem_path = File.join(@outdir, "gem_local_list.output")
  # the folloing gems are not collected as bundled plugins
  masked_gems = ["fluentd", "fluent-config-regexp-type", "fluent-logger", "fluent-diagtool"]
  File.open(local_gem_path, 'w') do |f|
    f.puts((installed_gems - bundled_plugins - masked_gems).join("\n"))
  end
  return {:bundled => bundled_gem_path, :local => local_gem_path}
end

#collect_ntp(command) ⇒ Object



381
382
383
384
385
386
387
388
389
390
# File 'lib/fluent/diagtool/collectutils.rb', line 381

def collect_ntp(command)
  output = @outdir+'/ntp_info.output'
  stdout_date, stderr_date, status_date = Open3.capture3("date")
  stdout_ntp, stderr_ntp, status_ntp = Open3.capture3("chronyc sources") if command == "chrony"
  stdout_ntp, stderr_ntp, status_ntp = Open3.capture3("ntpq -p") if command == "ntp"
  File.open(output, 'w') do |f|
    f.puts(stdout_date)
    f.puts(stdout_ntp)
  end
end

#collect_oslogObject



367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/fluent/diagtool/collectutils.rb', line 367

def collect_oslog()
  target_dir = @workdir+@oslog_path
  FileUtils.mkdir_p(target_dir)
  if File.exist? @oslog_path+@oslog
  	FileUtils.cp(@oslog_path+@oslog, target_dir)
  	return target_dir+@oslog
  elsif File.exist? @oslog_path+@syslog
    FileUtils.cp(@oslog_path+@syslog, target_dir)
    return target_dir+@syslog
  else
    @logger.warn("Can not find OS log file in #{@oslog} or #{@syslog}")
  end
end

#collect_tdconfObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/fluent/diagtool/collectutils.rb', line 222

def collect_tdconf()
  target_dir = @workdir+@tdconf_path
  FileUtils.mkdir_p(target_dir)
  FileUtils.cp(@tdconf_path+@tdconf, target_dir)
  conf = @workdir+@tdconf_path+@tdconf
  conf_list = []
  conf_list.push conf
  case @type
  when 'fluentd'
    conf_list = conf_list + _collect_tdconf_include(conf)
  when 'fluentbit'
    conf_list = conf_list + _collect_tdconf_include(conf) + _collect_tdbit_parser(conf) + _collect_tdbit_plugins(conf) 
  end   
  return conf_list
end

#collect_tdgemsObject



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/fluent/diagtool/collectutils.rb', line 457

def collect_tdgems()
  output = @outdir+'/tdgem_list.output'
  command = if fluent_package?
              "fluent-gem"
            else
              "td-agent-gem"
            end
  stdout, stderr, status = Open3.capture3("#{command} list")
  gems = stdout.each_line(chomp: true).select do |line|
    line.include?("fluent")
  end.collect do |line|
    line.split.first
  end
  File.open(output, 'w') do |f|
    f.puts(gems.join("\n"))
  end
  return output
end

#collect_tdlogObject



358
359
360
361
362
363
364
365
# File 'lib/fluent/diagtool/collectutils.rb', line 358

def collect_tdlog()
  target_dir = @workdir+@tdlog_path
  FileUtils.mkdir_p(target_dir)
  Dir.glob(@tdlog_path+@tdlog+'*').each{ |f| 
    FileUtils.cp(f, target_dir)
  }
  return Dir.glob(target_dir+@tdlog+'*')
end

#compress_outputObject



476
477
478
479
480
481
# File 'lib/fluent/diagtool/collectutils.rb', line 476

def compress_output()
  Dir.chdir(@basedir)
  tar_file = 'diagout-'+@time_format+'.tar.gz'
  stdout, stderr, status = Open3.capture3("tar cvfz #{tar_file} #{@time_format}")
  return @basedir + '/' + tar_file
end

#export_envObject



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/fluent/diagtool/collectutils.rb', line 210

def export_env()
  env = {
    :os => @osenv['Operating System'],
    :kernel => @osenv['Kernel'],
    :tdconf => @tdconf,
    :tdconf_path => @tdconf_path,
    :tdlog => @tdlog,
    :tdlog_path => @tdlog_path
  }
  return env
end

#fluent_package?Boolean



410
411
412
# File 'lib/fluent/diagtool/collectutils.rb', line 410

def fluent_package?
  @package_name == "fluent-package"
end

#match_platform?(platforms_option) ⇒ Boolean



414
415
416
417
418
419
420
# File 'lib/fluent/diagtool/collectutils.rb', line 414

def match_platform?(platforms_option)
  if Diagtool.windows?
    platforms_option == "windows_platforms"
  else
    platforms_option == "not_windows_platforms"
  end
end