Skip to content

Commit

Permalink
Merge pull request #2586 from n0ts/topic-http-check-redirects
Browse files Browse the repository at this point in the history
[http_check] Add allow_redirects parameter
  • Loading branch information
truthbk committed Aug 11, 2016
2 parents f42cefe + 1bc5ac1 commit d5ec6f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ artifacts/*
node_modules/*
.DS_Store
venv/*
vendor/
*.swp
*.log
!nagios.log
Expand Down
8 changes: 5 additions & 3 deletions checks.d/http_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ def _load_conf(self, instance):
weakcipher = _is_affirmative(instance.get('weakciphers', False))
ignore_ssl_warning = _is_affirmative(instance.get('ignore_ssl_warning', False))
skip_proxy = _is_affirmative(instance.get('no_proxy', False))
allow_redirects = _is_affirmative(instance.get('allow_redirects', True))

return url, username, password, http_response_status_code, timeout, include_content,\
headers, response_time, content_match, tags, ssl, ssl_expire, instance_ca_certs,\
weakcipher, ignore_ssl_warning, skip_proxy
weakcipher, ignore_ssl_warning, skip_proxy, allow_redirects

def _check(self, instance):
addr, username, password, http_response_status_code, timeout, include_content, headers,\
response_time, content_match, tags, disable_ssl_validation,\
ssl_expire, instance_ca_certs, weakcipher, ignore_ssl_warning, skip_proxy = self._load_conf(instance)
ssl_expire, instance_ca_certs, weakcipher, ignore_ssl_warning, skip_proxy, allow_redirects = self._load_conf(instance)
start = time.time()

service_checks = []
Expand Down Expand Up @@ -225,7 +226,8 @@ def _check(self, instance):
self.log.debug("Weak Ciphers will be used for {0}. Suppoted Cipherlist: {1}".format(
base_addr, WeakCiphersHTTPSConnection.SUPPORTED_CIPHERS))

r = sess.request('GET', addr, auth=auth, timeout=timeout, headers=headers, proxies=instance_proxy,
r = sess.request('GET', addr, auth=auth, timeout=timeout, headers=headers,
proxies=instance_proxy, allow_redirects=allow_redirects,
verify=False if disable_ssl_validation else instance_ca_certs)

except (socket.timeout, requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
Expand Down
5 changes: 5 additions & 0 deletions conf.d/http_check.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ instances:
#
# no_proxy: false

# The (optional) allow_redirects parameter can enable redirection.
# Defaults to True.
#
# allow_redirects: true

# tags:
# - url:http://alternative.host.example.com
# - env:production
Expand Down
19 changes: 19 additions & 0 deletions tests/checks/integration/test_http_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@
}]
}

CONFIG_HTTP_REDIRECTS = {
'instances': [{
'name': 'redirect_service',
'url': 'http://github.com',
'timeout': 1,
'http_response_status_code': 301,
'allow_redirects': False,
}]
}

FAKE_CERT = {'notAfter': 'Apr 12 12:00:00 2006 GMT'}

Expand Down Expand Up @@ -230,6 +239,16 @@ def test_check_ssl(self):

self.coverage_report()

def test_check_allow_redirects(self):
self.run_check(CONFIG_HTTP_REDIRECTS)
# Overrides self.service_checks attribute when values are available\
self.service_checks = self.wait_for_async('get_service_checks', 'service_checks', 1)

tags = ['url:http://github.com', 'instance:redirect_service']
self.assertServiceCheckOK("http.can_connect", tags=tags)

self.coverage_report()

@mock.patch('ssl.SSLSocket.getpeercert', return_value=FAKE_CERT)
def test_mock_case(self, getpeercert_func):
self.run_check(CONFIG_EXPIRED_SSL)
Expand Down

0 comments on commit d5ec6f6

Please sign in to comment.