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.



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

def initialize(conf, log_level)
  @logger = Logger.new(STDOUT, level: log_level, formatter: proc {|severity, datetime, progname, msg|
    "#{datetime}: [Diagutils] [#{severity}] #{msg}\n"
  })
  @time_format = conf[:time]
  @basedir = conf[:basedir]
  @workdir = conf[:workdir]
  @outdir = conf[:outdir]     

  @tdenv = get_tdenv()
  @tdconf = @tdenv['FLUENT_CONF'].split('/')[-1]
  @tdconf_path = @tdenv['FLUENT_CONF'].gsub(@tdconf,'')
  @tdlog =  @tdenv['TD_AGENT_LOG_FILE'].split('/')[-1]
  @tdlog_path = @tdenv['TD_AGENT_LOG_FILE'].gsub(@tdlog,'')

  @osenv = get_osenv()
  @oslog_path = '/var/log/'
  @oslog = 'messages'
  @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("    td-agent conf path = #{@tdconf_path}")
  @logger.info("    td-agent conf file = #{@tdconf}")
  @logger.info("    td-agent log path = #{@tdlog_path}")
  @logger.info("    td-agent log = #{@tdlog}")
end

Instance Method Details

#collect_meminfoObject



134
135
136
137
138
139
140
141
# File 'lib/fluent/diagtool/collectutils.rb', line 134

def collect_meminfo()
  output = @outdir+'/meminfo.output'
  stdout, stderr, status = Open3.capture3("cat /proc/meminfo")
  File.open(output, 'w') do |f|
    f.puts(stdout)
  end
  return output
end

#collect_netstat_planObject



143
144
145
146
147
148
149
150
# File 'lib/fluent/diagtool/collectutils.rb', line 143

def collect_netstat_plan()
  output = @outdir+'/netstat_plan.output'
  stdout, stderr, status = Open3.capture3("netstat -plan")
  File.open(output, 'w') do |f|
    f.puts(stdout)
  end
  return output
end

#collect_netstat_sObject



152
153
154
155
156
157
158
159
# File 'lib/fluent/diagtool/collectutils.rb', line 152

def collect_netstat_s()
  output = @outdir+'/netstat_s.output'
  stdout, stderr, status = Open3.capture3("netstat -s")
  File.open(output, 'w') do |f|
    f.puts(stdout)
  end
  return output
end

#collect_ntp(command) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fluent/diagtool/collectutils.rb', line 161

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
  return output
end

#collect_oslogObject



110
111
112
113
114
# File 'lib/fluent/diagtool/collectutils.rb', line 110

def collect_oslog()
  FileUtils.mkdir_p(@workdir+@oslog_path)
  FileUtils.cp(@oslog_path+@oslog, @workdir+@oslog_path)
  return @workdir+@oslog_path+@oslog
end

#collect_ps_eoObject



125
126
127
128
129
130
131
132
# File 'lib/fluent/diagtool/collectutils.rb', line 125

def collect_ps_eo()
  output = @outdir+'/ps_eo.output'
  stdout, stderr, status = Open3.capture3("ps -eo pid,ppid,stime,time,%mem,%cpu,cmd")
  File.open(output, 'w') do |f|
    f.puts(stdout)
  end
  return output
end

#collect_sysctlObject



104
105
106
107
108
# File 'lib/fluent/diagtool/collectutils.rb', line 104

def collect_sysctl()
  FileUtils.mkdir_p(@workdir+@sysctl_path)
  FileUtils.cp(@sysctl_path+@sysctl, @workdir+@sysctl_path)
  return @workdir+@sysctl_path+@sysctl
end

#collect_tdconfObject



92
93
94
95
96
# File 'lib/fluent/diagtool/collectutils.rb', line 92

def collect_tdconf()
  FileUtils.mkdir_p(@workdir+@tdconf_path)
  FileUtils.cp(@tdconf_path+@tdconf, @workdir+@tdconf_path)
  return @workdir+@tdconf_path+@tdconf
end

#collect_tdgemsObject



173
174
175
176
177
178
179
180
# File 'lib/fluent/diagtool/collectutils.rb', line 173

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

#collect_tdlogObject



98
99
100
101
102
# File 'lib/fluent/diagtool/collectutils.rb', line 98

def collect_tdlog()
  FileUtils.mkdir_p(@workdir+@tdlog_path)
  FileUtils.cp_r(@tdlog_path, @workdir+@oslog_path)
  return Dir.glob(@workdir+@tdlog_path+@tdlog+'*')
end

#collect_ulimitObject



116
117
118
119
120
121
122
123
# File 'lib/fluent/diagtool/collectutils.rb', line 116

def collect_ulimit()
  output = @outdir+'/ulimit_n.output'
  stdout, stderr, status = Open3.capture3("ulimit -n")
  File.open(output, 'w') do |f|
    f.puts(stdout)
  end
  return output
end

#compress_outputObject



182
183
184
185
186
187
# File 'lib/fluent/diagtool/collectutils.rb', line 182

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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluent/diagtool/collectutils.rb', line 80

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

#get_osenvObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fluent/diagtool/collectutils.rb', line 53

def get_osenv()
  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
  }
  File.open(@workdir+'/os_env.output', 'w') do |f|
    f.puts(stdout)
  end
  return os_dict
end

#get_tdenvObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/diagtool/collectutils.rb', line 66

def get_tdenv()
  stdout, stderr, status = Open3.capture3('systemctl cat td-agent')
  env_dict = {}
    File.open(@workdir+'/td-agent_env.output', 'w') do |f|
      f.puts(stdout)
    end
    stdout.split().each do | l |
    if l.include?('Environment')
      env_dict[l.split('=')[1]] = l.split('=')[2]
    end
  end
  return env_dict
end