Class: LogStash::Codecs::Sflow

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/codecs/sflow.rb

Overview

The “sflow” codec is for decoding sflow v5 flows.

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Sflow

Returns a new instance of Sflow.



31
32
33
34
# File 'lib/logstash/codecs/sflow.rb', line 31

def initialize(params = {})
  super(params)
  @threadsafe = false
end

Instance Method Details

#assign_key_value(event, bindata_kv) ⇒ Object

def initialize



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/logstash/codecs/sflow.rb', line 38

def assign_key_value(event, bindata_kv)
  unless bindata_kv.nil? or bindata_kv.to_s.eql? ''
    bindata_kv.each_pair do |k, v|
      if v.is_a?(BinData::Choice)
        assign_key_value(event, bindata_kv[k])
      else
        unless @removed_field.include? k.to_s or v.is_a?(BinData::Array)
          event["#{k.to_s}"] = v.to_s
        end
      end
    end
  end
end

#common_sflow(event, decoded, sample) ⇒ Object

Parameters:

  • event (Object)
  • decoded (Object)
  • sample (Object)
  • record (Object)


56
57
58
59
60
# File 'lib/logstash/codecs/sflow.rb', line 56

def common_sflow(event, decoded, sample)
  event['agent_ip'] = decoded['agent_ip'].to_s
  assign_key_value(event, decoded)
  assign_key_value(event, sample)
end

#decode(payload) ⇒ Object



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
167
168
169
170
# File 'lib/logstash/codecs/sflow.rb', line 96

def decode(payload)
  header = SFlowHeader.read(payload)
  unless @versions.include?(header.sflow_version)
    @logger.warn("Ignoring Sflow version v#{header.sflow_version}")
    return
  end

  decoded = SFlow.read(payload)

  events = []

  decoded['samples'].each do |sample|
    #Treat case with no flow decoded (Unknown flow)
    if sample['sample_data'].to_s.eql? ''
      @logger.warn("Unknown sample entreprise #{sample['sample_entreprise'].to_s} - format #{sample['sample_format'].to_s}")
      next
    end

    #treat sample flow
    if sample['sample_entreprise'] == 0 && sample['sample_format'] == 1
      # Create the logstash event
      event = LogStash::Event.new({})
      common_sflow(event, decoded, sample)

      sample['sample_data']['records'].each do |record|
        # Ensure that some data exist for the record
        if record['record_data'].to_s.eql? ''
          @logger.warn("Unknown record entreprise #{record['record_entreprise'].to_s}, format #{record['record_format'].to_s}")
          next
        end

        assign_key_value(event, record)

      end
      #compute frame_length_times_sampling_rate
      if event.include?('frame_length') and event.include?('sampling_rate')
        event["frame_length_times_sampling_rate"] = event['frame_length'].to_i * event['sampling_rate'].to_i
      end

      event["sflow_type"] = 'sample'

      #Get interface dfescr if snmp_interface true
      snmp_call(event)

      events.push(event)

      #treat counter flow
    elsif sample['sample_entreprise'] == 0 && sample['sample_format'] == 2
      sample['sample_data']['records'].each do |record|
        # Ensure that some data exist for the record
        if record['record_data'].to_s.eql? ''
          @logger.warn("Unknown record entreprise #{record['record_entreprise'].to_s}, format #{record['record_format'].to_s}")
          next
        end

        # Create the logstash event
        event = LogStash::Event.new({})
        common_sflow(event, decoded, sample)

        assign_key_value(event, record)

        event["sflow_type"] = 'counter'

        #Get interface dfescr if snmp_interface true
        snmp_call(event)

        events.push(event)
      end
    end
  end

  events.each do |event|
    yield event
  end
end

#registerObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/logstash/codecs/sflow.rb', line 80

def register
  require 'logstash/codecs/sflow/datagram'
  require 'logstash/codecs/snmp/interface_resolver'

  # noinspection RubyResolve
  @removed_field = %w(record_length record_count record_entreprise record_format sample_entreprise sample_format
 sample_length sample_count sample_header data storage) | @optional_removed_field

  if @snmp_interface
    @snmp = SNMPInterfaceResolver.new(@snmp_community, @interface_cache_size, @interface_cache_ttl)
  end
end

#snmp_call(event) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/logstash/codecs/sflow.rb', line 62

def snmp_call(event)
  if @snmp_interface
    if event.include?('source_id_index')
      event["source_id_index_descr"] = @snmp.get_interface(event["agent_ip"], event["source_id_index"])
    end
    if event.include?('input_interface')
      event["input_interface_descr"] = @snmp.get_interface(event["agent_ip"], event["input_interface"])
    end
    if event.include?('output_interface')
      event["output_interface_descr"] = @snmp.get_interface(event["agent_ip"], event["output_interface"])
    end
    if event.include?('interface_index')
      event["interface_index_descr"] = @snmp.get_interface(event["agent_ip"], event["interface_index"])
    end
  end
end