bin/check-http.rb in sensu-plugins-http-0.4.0 vs bin/check-http.rb in sensu-plugins-http-1.0.0

- old
+ new

@@ -1,6 +1,6 @@ -#! /usr/bin/env ruby +#!/usr/bin/env ruby # # check-http # # DESCRIPTION: # Takes either a URL or a combination of host/path/port/ssl, and checks for @@ -65,12 +65,11 @@ option :port, short: '-P PORT', long: '--port PORT', proc: proc(&:to_i), - description: 'Select another port', - default: 80 + description: 'Select another port' option :request_uri, short: '-p PATH', long: '--request-uri PATH', description: 'Specify a uri path' @@ -131,12 +130,17 @@ description: 'Warn EXPIRE days before cert expires' option :pattern, short: '-q PAT', long: '--query PAT', - description: 'Query for a specific pattern' + description: 'Query for a specific pattern that must exist' + option :negpattern, + short: '-n PAT', + long: '--negquery PAT', + description: 'Query for a specific pattern that must be absent' + option :timeout, short: '-t SECS', long: '--timeout SECS', proc: proc(&:to_i), description: 'Set the timeout', @@ -227,10 +231,12 @@ http = Net::HTTP.new(config[:host], config[:port]) end http.read_timeout = config[:timeout] http.open_timeout = config[:timeout] http.ssl_timeout = config[:timeout] + http.continue_timeout = config[:timeout] + http.keep_alive_timeout = config[:timeout] warn_cert_expire = nil if config[:ssl] http.use_ssl = true if config[:cert] @@ -259,11 +265,11 @@ Net::HTTP::Get.new(config[:request_uri], 'User-Agent' => config[:ua]) when 'POST' Net::HTTP::Post.new(config[:request_uri], 'User-Agent' => config[:ua]) end - if !config[:user].nil? && !config[:password].nil? + unless config[:user].nil? && config[:password].nil? req.basic_auth config[:user], config[:password] end if config[:header] config[:header].split(',').each do |header| h, v = header.split(':', 2) @@ -292,18 +298,28 @@ warning "Certificate will expire #{warn_cert_expire}" end size = res.body.nil? ? '0' : res.body.size + handle_response(res, size, body) + end + + def handle_response(res, size, body) case res.code when /^2/ if config[:redirectto] critical "Expected redirect to #{config[:redirectto]} but got #{res.code}" + body elsif config[:pattern] if res.body =~ /#{config[:pattern]}/ ok "#{res.code}, found /#{config[:pattern]}/ in #{size} bytes" + body else critical "#{res.code}, did not find /#{config[:pattern]}/ in #{size} bytes: #{res.body[0...200]}..." + end + elsif config[:negpattern] + if res.body =~ /#{config[:negpattern]}/ + critical "#{res.code}, found /#{config[:negpattern]}/ in #{size} bytes: #{res.body[0...200]}..." + else + ok "#{res.code}, did not find /#{config[:negpattern]}/ in #{size} bytes" + body end else ok("#{res.code}, #{size} bytes" + body) unless config[:response_code] end when /^3/