Check JSON responses with Nagios
- Confirm the content-type of the response header was “application/json”
- Decode the response to verify it is parsable JSON
- Optionally, verify the JSON response against a data file
Verify content of JSON response
For the most part, Perl’s LWP::UserAgent class makes short work of the first requirement. Using $response->header(“content-type”) the plugin is able to check the content-type easily. Next up, we use the JSON module’s decode function to see if we can successfully decode $response->content.
Optionally, we can give the plugin an absolute path to a file which contains a Perl hash which can be iterated through in attempt to find corresponding key/value pairs in the decoded JSON response. For each key/value in the hash it doesn’t find in the JSON response, it will append the expected and actual results to the output string, exiting with a critical status. Currently there’s no way to check a key/value does not appear in the response, but feel free to make a pull request on check_json on my GitHub page.