Class: ArgusYml

Inherits:
Object
  • Object
show all
Defined in:
lib/ymlex/argusyml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_hash, product = nil) ⇒ ArgusYml

Returns a new instance of ArgusYml.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ymlex/argusyml.rb', line 149

def initialize filename_or_hash, product = nil
  if filename_or_hash.kind_of? String
    yml = Ymlex.load_file filename_or_hash
  else
    yml = filename_or_hash
  end
  @info_yml = value_to_str yml
  @name = @info_yml["name"]
  @bns = @info_yml["bns"] || []
  node_list = @info_yml["node"] || []
  node_str = ""
  node_list.each {|v| node_str += v + ","}
  @node = node_str.sub /,$/, ""
  @product = product || @info_yml["product"] || @bns.first.sub(/^.*?\./,"").sub(/\..*$/,"") || "Undefined_Product"
  @logs = {}
  @alert = Alert.new @info_yml["contacts"], @info_yml["alert"]
  @service_aggr = []
  @service_rule = []
  @service_alert = [default_alert]
  reset_instance
  trans_ytoj
end

Instance Attribute Details

#alertObject (readonly)

Returns the value of attribute alert.



147
148
149
# File 'lib/ymlex/argusyml.rb', line 147

def alert
  @alert
end

#bnsObject (readonly)

Returns the value of attribute bns.



147
148
149
# File 'lib/ymlex/argusyml.rb', line 147

def bns
  @bns
end

#info_ymlObject (readonly)

Returns the value of attribute info_yml.



147
148
149
# File 'lib/ymlex/argusyml.rb', line 147

def info_yml
  @info_yml
end

#instanceObject (readonly)

Returns the value of attribute instance.



147
148
149
# File 'lib/ymlex/argusyml.rb', line 147

def instance
  @instance
end

#logsObject (readonly)

Returns the value of attribute logs.



147
148
149
# File 'lib/ymlex/argusyml.rb', line 147

def logs
  @logs
end

#nameObject (readonly)

Returns the value of attribute name.



147
148
149
# File 'lib/ymlex/argusyml.rb', line 147

def name
  @name
end

#nodeObject (readonly)

Returns the value of attribute node.



147
148
149
# File 'lib/ymlex/argusyml.rb', line 147

def node
  @node
end

Class Method Details

.process_dir(dir_path, dest_path, product = nil) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/ymlex/argusyml.rb', line 137

def self.process_dir dir_path, dest_path, product = nil
  filelist = `find #{dir_path} -type f -name '*yex'`.split(' ')
  product = File.basename dir_path if !product
  filelist.each do |ymx|
    puts "process #{ymx}"
    ags = ArgusYml.new ymx, product
    ags.dump_json dest_path
  end
end

Instance Method Details

#dump_json(dir_path) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ymlex/argusyml.rb', line 192

def dump_json dir_path
  
  dir = "#{dir_path}/cluster/cluster.#{@name}.#{@product}.all"
  `mkdir -p #{dir}`

  # instance
  filename = "#{dir}/instance"
  new_instance = @instance
  File.open(filename,"w") do |f|
    f.puts JSON.pretty_generate new_instance
  end

  # log
  @logs.each do |log_key, log_value|
    log_name = "#{dir}/#{log_key}.conf"
    File.open(log_name, "w") do |f|
      f.puts JSON.pretty_generate log_value
    end
  end

  # cluster
  aggr_name = "#{dir}/cluster"
  File.open(aggr_name, "w") do |f|
    cluster = { "aggr" => @service_aggr,
                "rule" => @service_rule,
                "alert" => @service_alert,
              }
    cluster["namespace_list"] = @bns if @bns != []
    cluster["service_node"] = @node if @node != ""
    f.puts JSON.pretty_generate cluster
  end
end

#reset_instanceObject



172
173
174
# File 'lib/ymlex/argusyml.rb', line 172

def reset_instance
  @instance = empty
end

#trans_aggr(list) ⇒ Object



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
# File 'lib/ymlex/argusyml.rb', line 250

def trans_aggr list
  list.each do | rule_name, value | 
    @service_aggr << { "items" => value["items"],
                       "types" => value["types"] || "sum",
                     }
    if value["formula"]
      rule_name = "#{@name}_aggr_#{rule_name}"
      alert_name = "default_alert"
      other = default_alert_level
      if value["alert"]
        alt = @alert.get_alert value["alert"]
        alt["name"] = rule_name
        other = alt.delete "other"
        @service_alert << alt
        alert_name = rule_name
      end
      @service_rule << { "name" => rule_name,
                         "formula" => value["formula"],
                         "filter" => value["filter"] || "1/1",
                         "merge_window" => other["merge_window"],
                         "level" => other["level"],
                         "alert" => alert_name }
    end
  end
end

#trans_derived(list) ⇒ Object



246
247
248
# File 'lib/ymlex/argusyml.rb', line 246

def trans_derived list
  @instance["derived"] = list
end

#trans_exec(list) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/ymlex/argusyml.rb', line 276

def trans_exec list
  list.each do | raw_key, raw_value |
    dft_exec_raw = { "cycle" => "60",
                     "method" => "exec",
                   }
    raw_value["name"] = "#{@name}_exec_#{raw_key}"
    @instance["raw"] << dft_exec_raw.merge(raw_value)
  end
end

#trans_log(list) ⇒ Object



307
308
309
310
311
312
313
314
315
316
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
# File 'lib/ymlex/argusyml.rb', line 307

def trans_log list
  list.each do |log_key, log_value|
    raw_name = "#{@name}_log_#{log_key}"
    log_raw = { "name" => raw_name,
                "cycle" => log_value["cycle"] || "60",
                "method" => "noah",
                "target" => "logmon",
                "params" => "${ATTACHMENT_DIR}/#{raw_name}.conf",
              }
    @instance["raw"] << log_raw

    log_conf = { "log_filepath" => log_value["path"],
                 "limit_rate" => "5",
                 "item" => []
               }
    log_value.each do |raw_key, raw_value|
      next if raw_key == "path"
      item_name_prefix = "#{raw_name}_#{raw_key}" 
      item = { "item_name_prefix" => item_name_prefix,
               "cycle" => raw_value["cycle"] || "60",
               "match_str" => raw_value["match_str"],
               "filter_str" => raw_value["filter_str"] || "",
             }
      log_conf["item"] << item
      next unless raw_value["formula"]
      alert_name = "default_alert"
      other = default_alert_level
      if raw_value["alert"]
        alt = @alert.get_alert raw_value["alert"]
        alt["name"] = item_name_prefix
        other = alt.delete "other"
        @instance["alert"] << alt
        alert_name = item_name_prefix
      end
      @instance["rule"] << { "name" => item_name_prefix, 
                             "formula" => raw_value["formula"],
                             "filter" => raw_value["filter"] || "1/1",
                             "merge_window" => other["merge_window"],
                             "level" => other["level"],
                             "alert" => alert_name,
                           }
    end
    @logs[raw_name] = log_conf
  end
end

#trans_other(list) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/ymlex/argusyml.rb', line 286

def trans_other list
  list.each do | rule_key, rule_value |
    rule_name = "#{@name}_other_#{rule_key}"
    alert_name = "default_alert"
    other = default_alert_level
    if rule_value["alert"]
      alt = @alert.get_alert rule_value["alert"]
      alt["name"] = rule_name
      other = alt.delete "other"
      @instance["alert"] << alt
      alert_name = rule_name
    end
    @instance["rule"] << { "name" => rule_name,
                           "formula" => rule_value["formula"],
                           "filter" => rule_value["filter"] || "1/1",
                           "merge_window" => other["merge_window"],
                           "level" => other["level"],
                           "alert" => alert_name }
  end
end

#trans_proc(list) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/ymlex/argusyml.rb', line 353

def trans_proc list
  list.each do |raw_key, raw_value|
    raw_name = "#{@name}_proc_#{raw_key}"
    @instance["raw"] << { "name" => raw_name,
                          "cycle" => raw_value["cycle"]||"10",
                          "method" => "noah",
                          "target" => "procmon",
                          "params" => raw_value["path"] }
    raw_value.each do |rule_key, rule_value|
      next if (rule_key == "path" || !rule_value["formula"])
      rule_name = "#{raw_name}_#{rule_key}"
      alert_name = "default_alert"
      other = default_alert_level
      if rule_value["alert"]
        alt = @alert.get_alert rule_value["alert"]
        alt["name"] = rule_name
        other = alt.delete "other"
        @instance["alert"] << alt
        alert_name = rule_name
      end
      @instance["rule"] << { "name" => rule_name,
                             "formula" => rule_value["formula"],
                             "filter" => rule_value["filter"]||"3/3",
                             "merge_window" => other["merge_window"],
                             "level" => other["level"],
                             "alert" => alert_name }
    end
  end
end

#trans_request(list) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/ymlex/argusyml.rb', line 383

def trans_request list
  list.each do |raw_key, raw_value|
    raw_name = "#{@name}_request_#{raw_key}"
    dft_raw = { "name" => raw_name,
                "cycle" => "60",
                "port" => "8080",
                "protocol" => "tcp",
                "mon_idc" => "local",
                "req_type" => "port",
              }
    alert_name = "default_alert"
    other = default_alert_level
    if raw_value["alert"]
      alt = @alert.get_alert raw_value["alert"]
      alt["name"] = raw_name
      other = alt.delete "other"
      @instance["alert"] << alt
      alert_name = raw_name
      raw_value.delete "alert"
    end
    @instance["request"] << dft_raw.merge(raw_value)
    @instance["rule"] << { "name" => raw_name,
                           "formula" => "#{raw_name} != 'ok'",
                           "filter" => raw_value["filter"]||"3/3",
                           "merge_window" => other["merge_window"],
                           "level" => other["level"],
                           "alert" => alert_name }
  end
end

#trans_ytojObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/ymlex/argusyml.rb', line 225

def trans_ytoj
  @info_yml.each do |key, value| 
    case key
    when "proc"
      trans_proc value
    when "request"
      trans_request value
    when "exec"
      trans_exec value
    when "other"
      trans_other value
    when "log"
      trans_log value
    when "aggr"
      trans_aggr value
    when "derived"
      trans_derived value
    end
  end
end

#value_to_str(input) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ymlex/argusyml.rb', line 176

def value_to_str input
  case 
  when input.kind_of?(Hash)
    input.each do |k,v|
      input[k] = value_to_str v
    end
  when input.kind_of?(Array)
    input.each_index do |i|
      input[i] = value_to_str input[i]
    end
  when input.kind_of?(Fixnum) || input.kind_of?(Float)
    input = input.to_s
  end
  input
end