From 1fabed0b471dd4dae9e034dd45afe9660c84ca42 Mon Sep 17 00:00:00 2001 From: Kenji Okimoto Date: Thu, 23 Aug 2018 17:09:13 +0900 Subject: [PATCH] Add Fluentd::Setting::Config#{filters,labels,group_by_label} Signed-off-by: Kenji Okimoto --- app/models/fluentd/setting/config.rb | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/models/fluentd/setting/config.rb b/app/models/fluentd/setting/config.rb index 09a7678..b01089d 100644 --- a/app/models/fluentd/setting/config.rb +++ b/app/models/fluentd/setting/config.rb @@ -21,12 +21,48 @@ class Fluentd end end + def filters + elements.find_all do |elm| + elm.name == "filter" + end + end + def matches elements.find_all do |elm| elm.name == "match" end end + def labels + elements.find_all do |elm| + elm.name == "label" + end + end + + def group_by_label + hash = Hash.new{|h, k| h[k] = {} } + sources.each do |source| + label = source["@label"] || source["label"] + if label + hash[label][:sources] = [source] + else + hash["ROOT"][:sources] = [source] + end + end + hash["ROOT"][:filters] = filters unless filters.empty? + hash["ROOT"][:matches] = matches unless matches.empty? + + labels.each do |label| + hash[label.arg][:filters] = label.elements.find_all do |e| + e.name == "filter" + end + hash[label.arg][:matches] = label.elements.find_all do |e| + e.name == "match" + end + end + hash + end + def write_to_file return unless Fluentd.instance Fluentd.instance.agent.config_write formatted