The httpjson plugin can collect data from remote URLs which respond with JSON. Then it flattens JSON and finds all numeric values, treating them as floats.
The httpjson plugin collects data from HTTP URLs which respond with JSON. It flattens the JSON and finds all numeric values, treating them as floats.
For example, if you have a service called _mycollector_, which has HTTP endpoint for gathering stats at http://my.service.com/_stats, you would configure the HTTP JSON plugin like this:
### Configuration:
```
```toml
[[inputs.httpjson]]
[[inputs.httpjson]]
name = "mycollector"
## NOTE This plugin only reads numerical measurements, strings and booleans
## will be ignored.
## Name for the service being polled. Will be appended to the name of the
## measurement e.g. "httpjson_webserver_stats".
##
## Deprecated (1.3.0): Use name_override, name_suffix, name_prefix instead.
name="webserver_stats"
## URL of each server in the service's cluster
servers=[
servers=[
"http://my.service.com/_stats"
"http://localhost:9999/stats/",
"http://localhost:9998/stats/",
]
]
## Set response_timeout (default 5 seconds)
# HTTP method to use (case-sensitive)
method = "GET"
# Set response_timeout (default 5 seconds)
response_timeout="5s"
response_timeout="5s"
```
`name` is used as a prefix for the measurements.
`method` specifies HTTP method to use for requests.
## HTTP method to use: GET or POST (case-sensitive)
method="GET"
`response_timeout` specifies timeout to wait to get the response
## Tags to extract from top-level of JSON server response.
# tag_keys = [
# "my_tag_1",
# "my_tag_2"
# ]
You can also specify which keys from server response should be considered tags:
## HTTP Request Parameters (all values must be strings). For "GET" requests, data
## will be included in the query. For "POST" requests, data will be included
## in the request body as "x-www-form-urlencoded".
# [inputs.httpjson.parameters]
# event_type = "cpu_spike"
# threshold = "0.75"
```
## HTTP Request Headers (all values must be strings).
[[inputs.httpjson]]
# [inputs.httpjson.headers]
...
# X-Auth-Token = "my-xauth-token"
# apiVersion = "v1"
tag_keys = [
## Optional SSL Config
"role",
# ssl_ca = "/etc/telegraf/ca.pem"
"version"
# ssl_cert = "/etc/telegraf/cert.pem"
]
# ssl_key = "/etc/telegraf/key.pem"
## Use SSL but skip chain & host verification
# insecure_skip_verify = false
```
```
If the JSON response is an array of objects, then each object will be parsed with the same configuration.
### Measurements & Fields:
You can also specify additional request parameters for the service:
- httpjson
- response_time (float): Response time in seconds
```
Additional fields are dependant on the response of the remote service being polled.
[[inputs.httpjson]]
...
[inputs.httpjson.parameters]
### Tags:
event_type = "cpu_spike"
threshold = "0.75"
```
- All measurements have the following tags:
- server: HTTP origin as defined in configuration as `servers`.
You can also specify additional request header parameters for the service:
Any top level keys listed under `tag_keys` in the configuration are added as tags. Top level keys are defined as keys in the root level of the object in a single object response, or in the root level of each object within an array of objects.
```
[[inputs.httpjson]]
...
[inputs.httpjson.headers]
### Examples Output:
X-Auth-Token = "my-xauth-token"
apiVersion = "v1"
```
# Example:
This plugin understands responses containing a single JSON object, or a JSON Array of Objects.
Let's say that we have a service named "mycollector" configured like this: