Class: LogStash::Outputs::MQTT

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/mqtt2.rb

Overview

This is Logstash output plugin for the mqtt.org/[MQTT] protocol.

Features:

  • Publish messages to a topic

  • TSL/SSL connection to MQTT server (optional)

  • Message publishing to a topic

  • QoS levels 0 and 1 (note that QoS 2 is not currently supported due to github.com/njh/ruby-mqtt[ruby-mqtt] library limitations)

  • Fault tolerance for network shortages, however not optimzied for performance since it takes a new connection for each event (or a bunch of events) to be published

  • MQTT protocol version 3.1.0

Example publishing to test.mosquitto.org:

source,ruby

output {

mqtt {
  host => "test.mosquitto.org"
  port => 8883
  topic => "hello"
}

}


Example publishing to aws.amazon.com/iot/[AWS IoT]:

source,ruby

output {

mqtt {
  host => "somehostname.iot.someregion.amazonaws.com"
  port => 8883
  topic => "hello"
  client_id => "clientidfromaws"
  ssl => true
  cert_file => "certificate.pem.crt"
  key_file => "private.pem.key"
  ca_file => "root-CA.crt"
}

}


Topic may also depend on parts of the event using the standard sprintf syntax.

source,ruby

output

mqtt {
  ...
  topic => "something/%{myfield"
}

}


Instance Method Summary collapse

Instance Method Details

#closeObject



160
161
162
# File 'lib/logstash/outputs/mqtt2.rb', line 160

def close
  @closing = true
end

#multi_receive(events) ⇒ Object

def receive



154
155
156
157
158
# File 'lib/logstash/outputs/mqtt2.rb', line 154

def multi_receive(events)
  events.each { |event| @codec.encode(event) }
  # Handle all events at once to prevent taking a new connection for each event
  handle_events
end

#receive(event) ⇒ Object



149
150
151
152
# File 'lib/logstash/outputs/mqtt2.rb', line 149

def receive(event)
  @codec.encode(event)
  handle_events
end

#registerObject



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
# File 'lib/logstash/outputs/mqtt2.rb', line 109

def register
  @options = {
    :host => @host
  }
  if @port
    @options[:port] = @port
  end
  if @client_id
    @options[:client_id] = @client_id
  end
  if @username
    @options[:username] = @username
  end
  if @password
    @options[:password] = @password
  end
  if @ssl
    @options[:ssl] = @ssl
  end
  if @cert_file
    @options[:cert_file] = @cert_file
  end
  if @key_file
    @options[:key_file] = @key_file
  end
  if @ca_file
    @options[:ca_file] = @ca_file
  end
  if @keep_alive
    @options[:keep_alive] = @keep_alive
  end

  # Encode events using the given codec
  # Use an array as a buffer so the multi_receive can handle multiple events with a single connection
  @event_buffer = Array.new
  @codec.on_event do |event, encoded_event|
    @event_buffer.push([event, encoded_event])
  end
end