Class: Fluent::TextParser::JuniperNaParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/fluent/plugin/parser_juniper_na.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object

This method is called after config_params have read configuration parameters



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fluent/plugin/parser_juniper_na.rb', line 10

def configure(conf)
    super

    ## Check if "output_format" has a valid value
    unless  @output_format.to_s == "structured" ||
            @output_format.to_s == "flat" ||
            @output_format.to_s == "statsd"

        raise ConfigError, "output_format value '#{@output_format}' is not valid. Must be : structured, flat or statsd"
    end
end

#name_flat(res_id) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/fluent/plugin/parser_juniper_na.rb', line 108

def name_flat(res_id)

    res_id.sub!(/^\//, "")

    # ## Replace all / and : per .
    res_id.gsub!('/', '.')
    res_id.gsub!(':', '.')

    return res_id
end

#name_hash(res_id) ⇒ Object



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
# File 'lib/fluent/plugin/parser_juniper_na.rb', line 119

def name_hash(res_id)

    # Parse Res_ID and convert to hash
    # 1- Remove leading /
    # 2- Split String by /
    # 3- for each attribute split by : to extract key / value

    res_id.sub!(/^\//, "")
    attributes = res_id.split("/")

    res_hash = Hash.new
    type = ''

    attributes.each do |attribute|
        key_value = attribute.split(":")
        res_hash[key_value[0].downcase] = key_value[1].downcase

        if key_value[0].downcase != 'device'
            type = type + key_value[0].downcase + '.'
        end
    end

    res_hash['type'] = type

    return res_hash
end

#parse(text) ⇒ Object

This is the main method. The input “text” is the unit of data to be parsed. If this is the in_tail plugin, it would be a line. If this is for in_syslog, it is a single syslog message.



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
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
# File 'lib/fluent/plugin/parser_juniper_na.rb', line 24

def parse(text)

    resources = JSON.parse(text)
    time = Engine.now

    resources.each do |resource|
        resource["Resources"].each do |data|
            name = data["Res_ID"]

            name_h = Hash.new
            type = ''
            if output_format.to_s == 'flat' ||
                output_format.to_s == 'statsd'
                name_gr = name_flat(name)

            elsif output_format.to_s == 'structured'
                name_h = name_hash(name)
                type = name_h['type']

            else
                $log.warn "Output_format '#{output_format.to_s}' not supported."
            end

            ##
            ## Extract data
            ## First check if the value is a hash or not
            ##
            data["Data"].each do |key, value|
                if value.is_a?(Hash)

                    ## If data is within a Hash, extract all values and construct new keys
                    data["Data"][key].each do |key2, value2|
                        sub_type = key + "." + key2

                        record = {}
                        if output_format.to_s == 'flat'
                            full_name = "#{name_gr}.#{sub_type}"
                            record[full_name.downcase]= value2

                        elsif output_format.to_s == 'structured'
                            name_h['type'] = type + sub_type
                            record['value'] = value2
                            record.merge!(name_h)

                        elsif output_format.to_s == 'statsd'
                            full_name = "#{name_gr}.#{sub_type}"
                            record[:statsd_type] = 'gauge'
                            record[:statsd_key] = full_name.downcase
                            record[:statsd_gauge] = value2
                        else
                            $log.warn "Output_format '#{output_format.to_s}' not supported for #{sub_type}"
                        end

                        yield time, record
                    end
                else
                    record = {}
                    sub_type = key

                    if output_format.to_s == 'flat'
                        full_name = "#{name_gr}.#{sub_type}"
                        record[full_name.downcase]= value

                    elsif output_format.to_s == 'structured'
                        name_h['type'] = type + sub_type
                        record['value'] = value
                        record.merge!(name_h)

                    elsif output_format.to_s == 'statsd'
                        full_name = "#{name_gr}.#{sub_type}"
                        record[:statsd_type] = 'gauge'
                        record[:statsd_key] = full_name.downcase
                        record[:statsd_gauge] = value
                    else
                        $log.warn "Output_format '#{output_format.to_s}' not supported for #{sub_type}"
                    end

                    yield time, record
                end
            end
        end
    end
end