Class: Diagtool::CollectUtils

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

Instance Method Summary collapse

Constructor Details

#initialize(conf, log_level) ⇒ CollectUtils

Returns a new instance of CollectUtils.



25
26
27
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
# File 'lib/fluent/diagtool/collectutils.rb', line 25

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' => ''
  }     
  @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



280
281
282
283
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
# File 'lib/fluent/diagtool/collectutils.rb', line 280

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



317
318
319
320
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
# File 'lib/fluent/diagtool/collectutils.rb', line 317

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



234
235
236
237
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
# File 'lib/fluent/diagtool/collectutils.rb', line 234

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



154
155
156
157
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
# File 'lib/fluent/diagtool/collectutils.rb', line 154

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



110
111
112
113
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
# File 'lib/fluent/diagtool/collectutils.rb', line 110

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fluent/diagtool/collectutils.rb', line 95

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_cmd_output(cmd) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/fluent/diagtool/collectutils.rb', line 388

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_ntp(command) ⇒ Object



377
378
379
380
381
382
383
384
385
386
# File 'lib/fluent/diagtool/collectutils.rb', line 377

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



363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/fluent/diagtool/collectutils.rb', line 363

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fluent/diagtool/collectutils.rb', line 218

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



406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/fluent/diagtool/collectutils.rb', line 406

def collect_tdgems()
  output = @outdir+'/tdgem_list.output'
  command = if @package_name == "fluent-package"
              "fluent-gem"
            else
              "td-agent-gem"
            end
  stdout, stderr, status = Open3.capture3("#{command} list | grep fluent")
  File.open(output, 'w') do |f|
    f.puts(stdout)
  end
  return output
end

#collect_tdlogObject



354
355
356
357
358
359
360
361
# File 'lib/fluent/diagtool/collectutils.rb', line 354

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



420
421
422
423
424
425
# File 'lib/fluent/diagtool/collectutils.rb', line 420

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



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fluent/diagtool/collectutils.rb', line 206

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