Accepting JSON requests over the web

This function is achieved by the input plugin. Logstash has support for the http input plugin, which does precisely that. It builds an HTTP interface using different types of payloads that can be submitted to Logstash as an input.

The relevant part from logstash_sensor_data_http.conf, which has the input filter, is as follows:

input {
http {
id => "sensor_data_http_input"
}
}

Here, the id field is a string that can uniquely identify this input filter later in the file if needed. We will not need to reference this name in the file; we just choose the name sensor_data_http_input.

The reference documentation of the HTTP input plugin is available athttps://www.elastic.co/guide/en/logstash/current/plugins-inputs-http.html. In this instance, since we are using the default configuration of the http input plugin, we have just specified id. We should secure this HTTP endpoint as it will be exposed over the internet to allow sensors to send data from anywhere. We can configure user and password parameters to protect this endpoint with the desired username and password, as follows:

input {
http {
id => "sensor_data_http_input"
user => "sensor_data"
password => "sensor_data"
}
}

When Logstash is started with this input plugin, it starts an HTTP server on port 8080, which is secured using basic authentication with the given username and password. We can send a request to this Logstash pipeline using a curl command, as follows:

curl -XPOST -u sensor_data:sensor_data --header "Content-Type: application/json" "http://localhost:8080/" -d '{"sensor_id":1,"time":1512102540000,"reading":16.24}'

Let's see how we will enrich the JSON payload with the metadata we have in MySQL.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset