sensu-plugins / sensu-plugin

A framework for writing Sensu plugins & handlers with Ruby.
http://sensuapp.org
MIT License
126 stars 117 forks source link

sensu-handler.rb sends 'http://servername:port' to the API, along with stash request #160

Closed raffraffraff closed 7 years ago

raffraffraff commented 7 years ago

I'm using the latest version of sensu (0.26 git clone) and sensu-plugin 1.4.3. My stashes are not honored. I've done some debugging, and I've found that 'sensu-handler.rb' composes a 'correct' uri for each stash check. I've added a 'put' so it logs it to the sensu-server.log. I took the uri it logged and tested it with curl

curl -X -u user:pass http://127.0.0.1:4567/stashes/silence/server/check

This works. The sensu-api.log tells me that the sensu-handler.rb gets a 404 for the same uri. However, I also noticed that the handler appears to be sending the 'http://127.0.0.1:4567' to the http://127.0.0.1:4567.

sensu-api.log - curl

{
    "timestamp": "2016-10-13T17:21:31.135508+0000",
    "level": "info",
    "message": "api response",
    "request": {
        "remote_address": "127.0.0.1",
        "user_agent": "curl/7.50.3",
        "method": "GET",
        "uri": "/stashes/silence/host/check",
        "query_string": null,
        "body": ""
    },
    "status": 200,
    "content_length": 74
}

sensu-api.log - sensu-handler

{
    "timestamp": "2016-10-13T17:21:34.346704+0000",
    "level": "info",
    "message": "api response",
    "request": {
        "remote_address": "127.0.0.1",
        "user_agent": "Ruby",
        "method": "GET",
        "uri": "http://localhost:4567/stashes/silence/host/check",
        "query_string": null,
        "body": ""
    },
    "status": 404,
    "content_length": 0
}

The only way I can replicate this behavior using curl, is to do this: curl -u user:pass -X GET http://127./0.0.1:4567?http://127.0.0.1:4567/stashes/silence/host/check

My api.json is pretty basic:

{
  "api": {
    "port": 4567,
    "bind": "0.0.0.0",
    "user": "user",
    "password": "pass"
  }
}
cwjohnston commented 7 years ago

@raffraffraff thanks for bringing this to our attention; this appears to be a regression.

raffraffraff commented 7 years ago

So a gem install sensu-plugin -v 1.4.2 gets me out of the soup, for now. Thanks for the very quick response.

betorvs commented 7 years ago

I do this (sensu-plugin-1.4.3/lib/sensu-handler.rb):

136c136
<       req = net_http_req_class(method).new(uri.to_s)
---
>       req = net_http_req_class(method).new(uri)

And my logs changed from this:

{"timestamp":"2016-10-13T16:22:00.926057-0300","level":"info","message":"api response","request":{"remote_address":"127.0.0.1","user_agent":"Ruby","method":"GET","uri":"http://localhost:4567/events/HOSTNAME/fs_writable_tmptmp","query_string":null,"body":""},"status":404,"content_length":0}

To this:

{"timestamp":"2016-10-13T16:24:01.437757-0300","level":"info","message":"api response","request":{"remote_address":"127.0.0.1","user_agent":"Ruby","method":"GET","uri":"/events/HOSTNAME/fs_writable_tmptmp","query_string":null,"body":""},"status":200,"content_length":2127}
cwjohnston commented 7 years ago

As noted in #131 we don't have unit tests for most any aspect of api requests, including stashes. I've started adding a test here https://github.com/sensu-plugins/sensu-plugin/compare/tests/api-request-stash-exists but it is passing on 1.9 where this bug report leads me to believe it should be failing.

Perhaps webmock is hiding the bug? Any thoughts?