Class: Diagtool::DiagUtils

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ DiagUtils

Returns a new instance of DiagUtils.



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

def initialize(params)
  time = Time.new
  @time_format = time.strftime("%Y%m%d%0k%M%0S")
  @conf = parse_diagconf(params)
  @conf[:time] = @time_format
  @conf[:workdir] = @conf[:basedir] + '/' + @time_format
  @conf[:outdir] = @conf[:workdir] + '/output'
  
  FileUtils.mkdir_p(@conf[:workdir])
  FileUtils.mkdir_p(@conf[:outdir])
  
  diaglog = @conf[:workdir] + '/diagtool.output'
  @masklog = './mask_' + @time_format + '.json'
  @logger = Logger.new(STDOUT, formatter: proc {|severity, datetime, progname, msg|
    "#{datetime}: [Diagtool] [#{severity}] #{msg}\n"
  })
  @logger_file = Logger.new(diaglog, formatter: proc {|severity, datetime, progname, msg|
    "#{datetime}: [Diagtool] [#{severity}] #{msg}\n"
  })
  diaglogger_info("Parsing command options...")
  diaglogger_info("   Option : Output directory = #{@conf[:basedir]}")
  diaglogger_info("   Option : Mask = #{@conf[:mask]}")
  diaglogger_info("   Option : Word list = #{@conf[:words]}")
  diaglogger_info("   Option : Hash Seed = #{@conf[:seed]}")
end

Instance Method Details

#diaglogger_debug(str) ⇒ Object



206
207
208
209
# File 'lib/fluent/diagtool/diagutils.rb', line 206

def diaglogger_debug(str)
  @logger.debug(str)
  @logger_file.debug(str)
end

#diaglogger_error(str) ⇒ Object



221
222
223
224
# File 'lib/fluent/diagtool/diagutils.rb', line 221

def diaglogger_error(str)
  @logger.error(str)
  @logger_file.error(str)
end

#diaglogger_info(str) ⇒ Object



211
212
213
214
# File 'lib/fluent/diagtool/diagutils.rb', line 211

def diaglogger_info(str)
  @logger.info(str)
  @logger_file.info(str)
end

#diaglogger_warn(str) ⇒ Object



216
217
218
219
# File 'lib/fluent/diagtool/diagutils.rb', line 216

def diaglogger_warn(str)
  @logger.warn(str)
  @logger_file.warn(str)
end

#diagtoolObject



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
98
99
100
101
102
103
104
105
106
107
108
109
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fluent/diagtool/diagutils.rb', line 52

def diagtool()
  loglevel = 'WARN'
  diaglogger_info("Initializing parameters...")
  c = CollectUtils.new(@conf, loglevel)
  c_env = c.export_env()
  diaglogger_info("[Collect] Loading the environment parameters...")
  diaglogger_info("[Collect]    operating system = #{c_env[:os]}")
  diaglogger_info("[Collect]    kernel version = #{c_env[:kernel]}")
  diaglogger_info("[Collect]    td-agent conf path = #{c_env[:tdconf_path]}")
  diaglogger_info("[Collect]    td-agent conf file = #{c_env[:tdconf]}")
  diaglogger_info("[Collect]    td-agent log path = #{c_env[:tdlog_path]}")
  diaglogger_info("[Collect]    td-agent log = #{c_env[:tdlog]}")
  m = MaskUtils.new(@conf, loglevel)
  v = ValidUtils.new(loglevel)
      
  diaglogger_info("[Collect] Collecting log files of td-agent...")
  tdlog = c.collect_tdlog()
  diaglogger_info("[Collect] log files of td-agent are stored in #{tdlog}")

  diaglogger_info("[Collect] Collecting config file of td-agent...")
  tdconf = c.collect_tdconf()
  diaglogger_info("[Collect] config file is stored in #{tdconf}")

  diaglogger_info("[Collect] Collecting td-agent gem information...")
  tdgem = c.collect_tdgems()
  diaglogger_info("[Collect] td-agent gem information is stored in #{tdgem}")

  diaglogger_info("[Collect] Collecting config file of OS log...")
  oslog = c.collect_oslog()
  if @conf[:mask] == 'yes'
    diaglogger_info("[Mask] Masking OS log file : #{oslog}...")
    oslog = m.mask_tdlog(oslog, clean = true)
  end
  diaglogger_info("[Collect] config file is stored in #{oslog}")

  diaglogger_info("[Collect] Collecting process information...")
  meminfo = c.collect_ps_eo()
  diaglogger_info("[Collect] process informationis stored in #{meminfo}")

  diaglogger_info("[Collect] Collecting OS memory information...")
  meminfo = c.collect_meminfo()
  diaglogger_info("[Collect] OS memory information is stored in #{meminfo}")

  diaglogger_info("[Collect] Collecting date/time information...")
  if system('which chronyc > /dev/null 2>&1')
    ntp = c.collect_ntp(command="chrony")
  elsif system('which ntpq > /dev/null 2>&1')
    ntp = c.collect_ntp(command="ntp")
  else
    diaglogger_warn("[Collect] chrony/ntp does not exist. skip collectig date/time information")
  end
  diaglogger_info("[Collect] date/time information is stored in #{ntp}")
      
  diaglogger_info("[Collect] Collecting netstat information...")
  if system('which netstat > /dev/null 2>&1')
    netstat_n = c.collect_netstat_plan()
    netstat_s = c.collect_netstat_s()
    if @conf[:mask] == 'yes'
      diaglogger_info("[Mask] Masking netstat file : #{netstat_n}...")
      netstat_n = m.mask_tdlog(netstat_n, clean = true)
    end
    diaglogger_info("[Collect] netstat information is stored in #{netstat_n} and #{netstat_s}")   
  else
    diaglogger_warn("[Collect] netstat does not exist. skip collectig netstat")
  end
  
  diaglogger_info("[Collect] Collecting systctl information...")
  sysctl = c.collect_sysctl()
  diaglogger_info("[Collect] sysctl information is stored in #{sysctl}")
      
  diaglogger_info("[Valid] Validating systctl information...")
  ret, sysctl = v.valid_sysctl(sysctl)
  list =  sysctl.keys
  list.each do |k|
    if sysctl[k]['result'] == 'correct'
      diaglogger_info("[Valid]    Sysctl: #{k} => #{sysctl[k]['value']} is correct (recommendation is #{sysctl[k]['recommend']})")
    elsif sysctl[k]['result'] == 'incorrect'
      diaglogger_warn("[Valid]    Sysctl: #{k} => #{sysctl[k]['value']} is incorrect (recommendation is #{sysctl[k]['recommend']})")
    end
  end

  diaglogger_info("[Collect] Collecting ulimit information...")
  ulimit = c.collect_ulimit()
  diaglogger_info("[Collect] ulimit information is stored in #{ulimit}")

  diaglogger_info("[Valid] Validating ulimit information...")
  ret, rec, val = v.valid_ulimit(ulimit)
  if ret == true
    diaglogger_info("[Valid]    ulimit => #{val} is correct (recommendation is >#{rec})")
  else
    diaglogger_warn("[Valid]    ulimit => #{val} is incorrect (recommendation is >#{rec})")
  end

  if @conf[:mask] == 'yes'
    diaglogger_info("[Mask] Masking td-agent config file : #{tdconf}...")
    m.mask_tdlog(tdconf, clean = true)
    tdlog.each do | file |
      diaglogger_info("[Mask] Masking td-agent log file : #{file}...")
      filename = file.split("/")[-1]
      if filename.include?(".gz")
        m.mask_tdlog_gz(file, clean = true)
      elsif
        m.mask_tdlog(file, clean = true)
      end
    end
  end
  
  if @conf[:mask] == 'yes'
    diaglogger_info("[Mask] Export mask log file : #{@masklog}")
    m.export_masklog(@masklog)
  end
  
  tar_file = c.compress_output()
  diaglogger_info("[Collect] Generate tar file #{tar_file}")
end

#parse_diagconf(params) ⇒ Object



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/diagutils.rb', line 168

def parse_diagconf(params)
  options = {
    :basedir => '', :mask => '', :words => [], :wfile => '', :seed => ''
  }
  if params[:output] != nil
    if Dir.exist?(params[:output])
      options[:basedir] = params[:output]
    else
      raise "output directory '#{basedir}' does not exist"
    end
  else
    raise "output directory '-o' must be specified"
  end
  if params[:mask] == nil
    options[:mask] = 'no'
  else
    if params[:mask] == 'yes' || params[:mask] == 'no'
      options[:mask] = params[:mask]
    else
      raise "invalid arguments '#{params[:mask]}' : input of '-m|--mask' should be 'yes' or 'no'"
    end
  end
  options[:words] = params[:"word-list"] if params[:"word-list"] != nil
  if params[:"word-file"] != nil
    f = params[:"word-file"]
    if File.exist?(f)
      File.readlines(f).each do  |l|
        options[:words].append(l.gsub(/\n/,''))
      end
    else
      raise "#{params[:"word-file"]} : No such file or directory"
    end
  end
  options[:words] = options[:words].uniq 
  options[:seed] = params[:"hash-seed"] if params[:"hash-seed"] != nil
  return options  
end