I am trying to get a config file running using inputs.http and keep getting a 401 Unauthorized from our device. The device type has a requirement that you have to login via post to authenticate before being able to run a get against it. As long as you keep running gets it will not log you out. However, that first post I can’t seem to configure correctly. I am able to run a curl from the same server with success, so shouldn’t be a firewall issue. This is the body after our agent section, wondering if I am missing some type of formatting that would be passing the password or username weirdly? Tried turning on debug = true in the agent and telegraf config but both simply show me the same 401, not the actual data being passed.
[[outputs.file]]
Files to write to, “stdout” is a specially handled file.
files = [“stdout”, “/tmp/metrics.out”]
data_format = “influx”
[[inputs.http]]
urls = [‘device:443/rest/json/login’]
method = “POST”
data_format = “json”
body = “{‘user’:‘user’,‘password’:‘password’}”
timeout = “5s”
data_format = “influx”
insecure_skip_verify = true
Example Code from Vendor doc for API login:
HTTP Request:
Resource URL: /login
HTTP Method: POST
Content type: application/json
Data: {
“user”: “”,
“password”: “”
}
HTTP Response:
HTTP code 200 – for successful login
HTTP code 401 – invalid user name or password
Hi,
It sounds like you need a cookie? In which case you could use the various cookie config options:
## Optional Cookie authentication
# cookie_auth_url = "https://localhost/authMe"
# cookie_auth_method = "POST"
# cookie_auth_username = "username"
# cookie_auth_password = "pa$$word"
# cookie_auth_headers = { Content-Type = "application/json", X-MY-HEADER = "hello" }
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}'
## cookie_auth_renewal not set or set to "0" will auth once and never renew the cookie
# cookie_auth_renewal = "5m"
1 Like
Funny thing, I actually tried that right after posting that question but can’t get it to work either. For some reason it thinks that I do not have all the parameters set. I keep getting:
[telegraf] Error running agent: Error loading config file /etc/telegraf/telegraf.conf: plugin inputs.http: line 30: configuration specified the fields [“cookie_auth_password” “cookie_auth_headers” “cookie_auth_body” “cookie_auth_url” “cookie_auth_method” “cookie_auth_username”], but they weren’t used
Our config has all of it (I thought):
Optional Cookie authentication
cookie_auth_url = “URL”
cookie_auth_method = “POST”
cookie_auth_username = “username”
cookie_auth_password = “password”
cookie_auth_headers = { Content-Type = “application/json” }
cookie_auth_body = ‘{“username”: “username”, “password”: “password”}’
cookie_auth_renewal not set or set to “0” will auth once and never renew the cookie
cookie_auth_renewal = “5m”
Based on that first example code from the vendor I may have to change “username” to “user”, but need to get telegraf to accept what I am passing it first.
What version of telegraf are you using? That error usually means you are running too old of a version to use those config options, are you put the config options in the wrong spot.
./telegraf --version
Telegraf 1.19.1 (git: HEAD 9f8be047)
Gotcha. Is there a specific document or way that upgrading is recommended?
Actually you can ignore that question. I was able to upgrade to 1.30.1 now and instead get a 400 bad request when running the same config.
Sweet just a couple more syntax problems and I am all up and running. Thanks again for the help @jpowers
1 Like