diff --git a/.gitignore b/.gitignore index 88877ea5ba..3c207e3c76 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ artifacts/* node_modules/* .DS_Store venv/* +vendor/ *.swp *.log !nagios.log diff --git a/checks.d/http_check.py b/checks.d/http_check.py index b5e8ad708a..1d4b50187c 100644 --- a/checks.d/http_check.py +++ b/checks.d/http_check.py @@ -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 = [] @@ -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: diff --git a/conf.d/http_check.yaml.example b/conf.d/http_check.yaml.example index 076a55314f..f80ff8f915 100644 --- a/conf.d/http_check.yaml.example +++ b/conf.d/http_check.yaml.example @@ -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 diff --git a/tests/checks/integration/test_http_check.py b/tests/checks/integration/test_http_check.py index 8e80febe0e..bf87348965 100644 --- a/tests/checks/integration/test_http_check.py +++ b/tests/checks/integration/test_http_check.py @@ -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'} @@ -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)