Giter Site home page Giter Site logo

check_http_json's Introduction

check_http_json.rb

This is a plugin for Nagios that will parse JSON from an HTTP response. It is written in Ruby and is known to function in versions 1.8.7, 1.9.3, and 2.4.0.

Usage

Usage: ./check_http_json.rb -u <URI> -e <element> -w <warn> -c <crit>
    -h, --help                       Help info.
    -v, --verbose                    Additional human output.
    -u, --uri URI                    Target URI. Incompatible with -f.
        --user USERNAME              HTTP basic authentication username.
        --pass PASSWORD              HTTP basic authentication password.
        --headers HEADERS            Comma-separated list of HTTP headers to include (ex. HOST:somehost,AUTH:letmein).
        --status_level STRING        Comma-separated list of HTTP status codes and their associated Nagios alert levels (ex. 301:1,404:2).
        --status_level_default VALUE The default return code for unexpected HTTP status codes. Defaults to 1.
    -f, --file PATH                  Target file. Incompatible with -u.
    -e, --element ELEMENT...         Desired element (ex. foo=>bar=>ish is foo.bar.ish). Repeatable argument.
    -E, --element_regex REGEX        Desired element expressed as regular expression.
        --element_regex_global       Check all occurring matches. -E is required.
    -d, --delimiter CHARACTER        Element delimiter (default is period).
    -w, --warn VALUE                 Warning threshold (integer or Nagios threshold ranges format).
    -c, --crit VALUE                 Critical threshold (integer or Nagios threshold ranges format).
    -r, --result STRING              Expected string result. No need for -w or -c.
    -R, --result_regex REGEX         Expected string result expressed as regular expression. No need for -w or -c.
    -W, --result_warn STRING         Warning if element is [string]. -C is required.
    -U, --result_unknown STRING      Unknown if element is [string]. -C is required.
    -C, --result_crit STRING         Critical if element is [string]. -W is required.
        --result_warn_regex REGEX    Warning if element matches REGEX. --result_crit_regex is required.
        --result_unknown_regex REGEX Unknown if element matches REGEX. --result_crit_regex is required.
        --result_crit_regex REGEX    Critical if element matches REGEX. --result_warn_regex is required.
    -p, --perf ELEMENT               Output additional fields (performance metrics).
        --perf_splitter CHARACTER    Additional fields delimiter (default is comma).
        --output_alt_pipe CHARACTER  Specify a character to replace reserved pipes in the output. Default: !
        --perf_regex REGEX           Output additional fields expressed as regular expression.
        --perf_regex_global          Check all occurring matches. --perf-regex is required.
    -t, --timeout SECONDS            Wait before HTTP timeout.

The --warn and --crit arguments conform to the Nagios threshold format guidelines.

If a simple result of either string or regular expression (-r or -R) is specified:

  • A match is OK and anything else is CRIT.
  • The warn / crit thresholds will be ignored.

If the warn and crit results (-W and -C) or regular expressions (--result_warn_regex and --result_crit_regex) are specified:

  • A match is WARN or CRIT and anything else is OK.
  • The warn / crit thresholds will be ignored.

Note that (-r or -R), (-W and -C), and (--result_warn_regex and --result_crit_regex) are mutually exclusive.

Note also that the response must be pure JSON. Bad things happen if this isn't the case.

Implementation

How you choose to implement the plugin is up to you. Here are some suggestions:

given string element, check string result

define command {
    command_name    check_http_json-string
    command_line    /etc/nagios3/plugins/check_http_json.rb -u 'http://$HOSTNAME$:$ARG1$/$ARG2$' -e '$ARG3$' -r '$ARG4$'
}
define service {
    service_description     elasticsearch-cluster-status
    check_command           check_http_json-string!9200!_cluster/health!status!green
}

wildly generic

define command {
    command_name    check_http_json
    command_line    /etc/nagios3/plugins/check_http_json.rb -u 'http://$HOSTNAME$:$ARG1$/$ARG2$' $ARG3$
}
define service {
    service_description     elasticsearch-resident-memory
    check_command           check_http_json!9280!_cluster/nodes/_local/stats!-E resident_in_bytes -w 1024000000 -c 1536000000
}

How are you implementing it?

I encourage you to add your implementation to the wiki - that way everybody can benefit!

Fin

The script is licensed using the Apache License, Version 2.0.

Finally, I invite you to peruse the list of contributors; thank you, all!

GitHub pull requests welcome.

check_http_json's People

Contributors

candux avatar dbenesj avatar jonasverhofste avatar kohenkatz avatar kwolf avatar phrawzty avatar project0 avatar slawekp avatar stepanstipl avatar xorpaul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

check_http_json's Issues

If response is array of ints, only the last int is evaluated

$ cat ~/blah.txt
[
   {
      "datapoints" : [
            0,
            1457976120
      ]
   }
]

$ ruby work/nagios_config/plugins/check_http_json.rb -f ~/blah.txt -c 4 -w 10 -v -e datapoints
+ The value of datapoints is: 1457976120
CRIT: datapoints is above threshold value 4 (1457976120)

$ ruby work/nagios_config/plugins/check_http_json.rb -f ~/blah.txt -c 4 -w 10 -v -e datapoints.0
WARN: datapoints.0 not found in response.  
$ ruby work/nagios_config/plugins/check_http_json.rb -f ~/blah.txt -c 4 -w 10 -v -e datapoints.1
WARN: datapoints.1 not found in response. 

I'm trying to catch the first int in the response array (the '0').

Define Host

How would the Host config look like for Nagios?

HTTPS exception

When using HTTPS with this check I get an exception:

./check_http_json.rb:153:in `uri_target': undefined method `use_ssl=' for #<Net::HTTP box.example.com:443 open=false> (NoMethodError)
    from ./check_http_json.rb:371

I think there are 2 solutions to this problem. (and a 3rd just for fun)

  1. Add require 'net/https' to the top of the file
  2. Change the Net::HTTP instantiation to pass in use_ssl and remove the use_ssl = true line (then you get an OpenSSL constant error so you have to add require 'openssl' at the top, too):
    http = Net::HTTP.new(uri.host, uri.port, :use_ssl => uri.scheme == 'https')

    if uri.scheme == 'https' then
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  1. Change to rest_client because it's awesome ;)

Not actually an issue...

Was unsure of the best way to simply ask you a question, so I jumped to here.

Just for my own edification, since this was my first contribution to someone else's project, is there anything I can do to make my name in the "Commits" listing go back to my github account (like yours do)?

I'm not asking for you to change anything; just curious if I could do something differently to make it link back to my profile if/when I contribute to someone else's project again.

Thanks,
Jay

Return critical error upon 404 response

Hi,

It would be useful for us if the check could return a critical error if the URL responds with a 404.
At the moment it's returning a warning and I can't see a way to make it critical from the available options.

Thanks.

Issue Parsing JSON

I'm attempting to pull the Graylog status JSON for the cluster using the check script but despite the output of a -v showing the content I'm looking for, it's returning a "not found in response" reply.

Example:

root@munged:/usr/lib/nagios/plugins# ./check_http_json -u 'http://munged:12900/cluster?pretty=true' --user 'munged' --pass 'token' -e 'facility' -r 'graylog-server' -v
+ RESPONSE:
---
{
  "3d5326a9-f09d-4c8e-9d16-ea70add94bb2" : {
    "facility" : "graylog-server",
    "codename" : "Stiegl",
    "node_id" : "3d5326a9-f09d-4c8e-9d16-ea70add94bb2",
    "cluster_id" : "72649b5c-ff32-4365-909d-5edee2f56311",
    "version" : "2.2.3+7adc951",
    "started_at" : "2017-07-18T20:52:26.859Z",
    "hostname" : "munged",
    "lifecycle" : "running",
    "lb_status" : "alive",
    "timezone" : "America/Chicago",
    "operating_system" : "Linux 3.13.0-121-generic",
    "is_processing" : true
  }
}
---
WARN: facility not found in response.

I've removed and added the ?pretty=true, but used it in the example to make it easier to read. The item is there, it's spelled correctly, just not sure why it's not responding? Thanks!

Possible bug with arrays

Given the following JSON structure, I do not get the expected results:

{"tests":[{"name":"valueAddGiftCard","time":1240,"valid":true},{"name":"chargeGiftCard","time":218,"valid":true},{"name":"lookupGiftCard","time":2454,"valid":true}]}

I would expect to be able to reference the name of a given element as so:
tests.0.name (valueAddGiftCard) and
tests.1.valid (true)

The actual result is that you only get tests.<key> such as tests.name which actually equals the last element in the array - tests.name equals lookupGiftCard.

Is this expected, or is this a bug?

Performance Counters

Hello,

firstly: Thank you phrawzty for your great script!

Unfortunately I am not able to retrieve the performance values I need.

This is working:

check_http_json.rb -u 'http://x/jolokia/read/jboss.as:subsystem=datasources,data-source=*,statistics=pool/ActiveCount' -E '^ActiveCount' -w 5 -c 10 --element_regex_global -p request.type
OK: All 'ActiveCount' (regex) within treshold W:5 C:10 | request.type=read

But this do not give any performance values:

check_http_json.rb -u 'http://x/jolokia/read/jboss.as:subsystem=datasources,data-source=*,statistics=pool/ActiveCount' -E '^ActiveCount' -w 5 -c 10 --element_regex_global -p value.jboss.as:data-source=product1,statistics=pool,subsystem=datasources.ActiveCount
OK: All 'ActiveCount' (regex) within treshold W:5 C:10 |

this is my json:

{
  "timestamp": 1515660256,
  "status": 200,
  "request": {
    "mbean": "jboss.as:data-source=*,statistics=pool,subsystem=datasources",
    "attribute": "ActiveCount",
    "type": "read"
  },
  "value": {
    "jboss.as:data-source=product1,statistics=pool,subsystem=datasources": {
      "ActiveCount": 0
    },
    "jboss.as:data-source=product2,statistics=pool,subsystem=datasources": {
      "ActiveCount": 0
    },
    "jboss.as:data-source=product3,statistics=pool,subsystem=datasources": {
      "ActiveCount": 0
    }
  }
}

warning when running on windows host

Windows: Windows Server 2008 R2
ruby: 2.7.1
check_http_json from the master branch

C:\Program Files\NSClient++\scripts>check_http_json.rb -u http://localhost:1701/
status -E CpuUsage -w 40 -c 100 -p CpuUsage
C:/Program Files/NSClient++/scripts/check_http_json.rb:605: warning: deprecated
Object#=~ is called on Float; it always returns nil
OK: First 'CpuUsage' (regex) within treshold W:40 C:100 | CpuUsage=14.0619164304
68136

feature request: performance data

First of thanks for this plugin, super useful to me, there is one little thing that would improve my config of this plugin a lot as im monitoring larger arrays with a lot of subarrays.

let's say i have a simple array

{
"Main": {
"Sub1": 1,
"Sub2": 2
}
}

at the moment i would have to specify Main.Sub1,Main.Sub2 to get all "Sub" elements. this is okey for small arrays for bigger arrays it would be easier to simply specifiy -p Main ( and then get all Sub elements automatically ) or maybe even a -p Main.* to get all of them.

I don't know ruby at all but i think this could easily be achived with some small if around

# If performance metrics have been requested...
here

keep up the good work!

error if protocol is not present in Uri

When calling script with uri parameter which does not contain protocol, e.g.
./check_http_json.rb -v -u 'node.office.de:9200/_cluster/health' -e number_of_data_nodes -c 2:2 -w 2:2
there's an error displayed:
+ Exception occured: undefined method 'request_uri' for #<URI::Generic:0x7fe0b41e5770>.
UNKNOWN: HTTP connection failed.
My guess would be that request_uri is not present if protocol is absent, so we can not start timeout request.

Escape or remove pipes in plugin output

On using pipes (|) in regex the performance label will have wrong names:

# /srv/icinga/linux/check_http_json.rb -u http://heizung:4321/xyz1/all -e pe1.L_state -R "^(1|2|3|4|5|99)$" --perf "pe1.L_usb_stick" 
OK: 'pe1.L_state' (regex) does match ^(1|2|3|4|5|99)$ | system.L_usb_stick=0

image

Add delimiter argument

As noted in my comment on issue #6, it would be handy to allow the element delimiter (currently a period) to be specified via an argument. It must default to a period in order to preserve the expected behaviour.

Is there a way to check multiple values?

from @mimmus:

I have:

{
"units": [
{
"id": "service1",
"health": 0,
},
{
"id": "service2",
"health": 0,
},
....
{
"id": "serviceN",
"health": 0,
}
]
}

What's the best way to check that ALL health keys are equal to 0?

Optionally return performance data

Please consider adding the ability to specify 'performance attributes' to be returned, ex "-p attr1,attr2" would result in:

OK: xxxx|attr1=122565 attr2=97443921 as extracted from the JSON.

Dynamic Perf Counters

Hello,

Would it be possible to get all Perf Counters by one query without know the perf value exactly?

For now it is working with that query:

check_http_json.rb -u 'xx' -E 'ActiveCount' -w 5 -c 10 --element_regex_global -p value§jboss.as:data-source=productx,statistics=pool,subsystem=datasources§ActiveCount?value§jboss.as:data-source=producty,statistics=pool,subsystem=datasources§ActiveCount?value§jboss.as:data-source=db2-productz,statistics=pool,subsystem=datasources§ActiveCount -d § --perf_splitter=?

But I do not really always know the names of the products like 'productx', 'producty' etc. so I need to get the value for all products as perf counters.

Json:

{
  "timestamp": 1515998720,
  "status": 200,
  "request": {
    "mbean": "jboss.as:data-source=*,statistics=pool,subsystem=datasources",
    "attribute": "ActiveCount",
    "type": "read"
  },
  "value": {
    "jboss.as:data-source=productx,statistics=pool,subsystem=datasources": {
      "ActiveCount": 0
    },
    "jboss.as:data-source=producty,statistics=pool,subsystem=datasources": {
      "ActiveCount": 0
    },
    "jboss.as:data-source=db2-productz,statistics=pool,subsystem=datasources": {
      "ActiveCount": 0
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.