Exemplo n.º 1
0
    def _get_data(self, instance):
        url = instance.get('nginx_vts_url')
        ssl_validation = instance.get('ssl_validation', True)

        auth = None
        if 'user' in instance and 'password' in instance:
            auth = (instance['user'], instance['password'])

        # Submit a service check for status page availability.
        parsed_url = urlparse(url)
        nginx_host = parsed_url.hostname
        nginx_port = parsed_url.port or 80
        service_check_name = 'nginx_vts.can_connect'
        service_check_tags = ['host:%s' % nginx_host, 'port:%s' % nginx_port]
        try:
            self.log.debug(u"Querying URL: {0}".format(url))
            r = requests.get(url,
                             auth=auth,
                             headers=headers(self.agentConfig),
                             verify=ssl_validation)
            r.raise_for_status()
        except Exception:
            self.service_check(service_check_name,
                               AgentCheck.CRITICAL,
                               tags=service_check_tags)
            raise
        else:
            self.service_check(service_check_name,
                               AgentCheck.OK,
                               tags=service_check_tags)

        body = r.content
        resp_headers = r.headers
        return body, resp_headers.get('content-type', 'text/plain')
    def check(self, instance):
        url = instance['url']
        username = instance['username']
        password = instance['password']
        auth = (username, password)

        try:
            self.log.debug("Checking against {}".format(url))
            response = requests.get(url, auth=auth, headers=headers(self.agentConfig))
            if response.status_code != 200:
                self.service_check(
                    NextcloudCheck.STATUS_CHECK, AgentCheck.CRITICAL, message="Problem requesting {}.".format(url)
                )
                return
            json_response = response.json()
            if json_response["ocs"]["meta"]["status"] == "ok":
                self.service_check(NextcloudCheck.STATUS_CHECK, AgentCheck.OK)
                self.parse_tags(json_response["ocs"]["data"])
                self.parse_metrics(json_response["ocs"]["data"])
            else:
                self.service_check(
                    NextcloudCheck.STATUS_CHECK,
                    AgentCheck.CRITICAL,
                    message="Error parsing response from {}.".format(url),
                )
        except Exception as e:
            self.service_check(
                NextcloudCheck.STATUS_CHECK, AgentCheck.CRITICAL, message="Error hitting {}. Error: {}".format(url, e)
            )
    def check(self, _):
        url = self.instance['url']

        try:
            self.log.debug("Checking against %s", url)
            response = self.http.get(url,
                                     extra_headers=headers(self.agentConfig))
            if response.status_code != 200:
                self.service_check(
                    NextcloudCheck.STATUS_CHECK,
                    AgentCheck.CRITICAL,
                    message="Problem requesting {}.".format(url))
                return
            json_response = response.json()
            if json_response["ocs"]["meta"]["status"] == "ok":
                self.service_check(NextcloudCheck.STATUS_CHECK, AgentCheck.OK)
                self.parse_tags(json_response["ocs"]["data"])
                self.parse_metrics(json_response["ocs"]["data"])
            else:
                self.service_check(
                    NextcloudCheck.STATUS_CHECK,
                    AgentCheck.CRITICAL,
                    message="Error parsing response from {}.".format(url),
                )
        except Exception as e:
            self.service_check(NextcloudCheck.STATUS_CHECK,
                               AgentCheck.CRITICAL,
                               message="Error hitting {}. Error: {}".format(
                                   url, e))
Exemplo n.º 4
0
    def get(self, url, service_check_tags, run_check=False):
        """Hit a given URL and return the parsed json"""
        self.log.debug('Fetching CouchDB stats at url: %s', url)

        # Override Accept request header so that failures are not redirected to the Futon web-ui
        request_headers = headers(self.agentConfig)
        request_headers['Accept'] = 'text/json'

        try:
            r = self.http.get(url, headers=request_headers)
            r.raise_for_status()
            if run_check:
                self.service_check(
                    self.SERVICE_CHECK_NAME,
                    AgentCheck.OK,
                    tags=service_check_tags,
                    message='Connection to %s was successful' % url,
                )
        except requests.exceptions.Timeout as e:
            self.service_check(
                self.SERVICE_CHECK_NAME,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message="Request timeout: {0}, {1}".format(url, e),
            )
            raise
        except requests.exceptions.HTTPError as e:
            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=service_check_tags, message=str(e))
            raise
        except Exception as e:
            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=service_check_tags, message=str(e))
            raise
        return r.json()
Exemplo n.º 5
0
    def _perform_request(self, url, path, ssl_params, timeout):
        certificate = None
        if 'ssl_certfile' in ssl_params and 'ssl_keyfile' in ssl_params:
            certificate = (ssl_params['ssl_certfile'], ssl_params['ssl_keyfile'])

        verify = ssl_params.get('ssl_ca_certs', True) if ssl_params['ssl_cert_validation'] else False

        return requests.get(
            url + path, verify=verify, cert=certificate, timeout=timeout, headers=headers(self.agentConfig)
        )
Exemplo n.º 6
0
 def _perform_request(self, instance, url, ssl_validation, auth):
     r = requests.get(
         url,
         auth=auth,
         headers=headers(self.agentConfig),
         verify=ssl_validation,
         timeout=self.default_integration_http_timeout,
         proxies=self.get_instance_proxy(instance, url),
     )
     r.raise_for_status()
     return r
Exemplo n.º 7
0
    def _get_data(self, url, config, send_sc=True):
        """
        Hit a given URL and return the parsed json
        """
        # Load basic authentication configuration, if available.
        if config.username and config.password:
            auth = (config.username, config.password)
        else:
            auth = None

        # Load SSL configuration, if available.
        # ssl_verify can be a bool or a string
        # (http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification)
        if isinstance(config.ssl_verify, bool) or isinstance(
                config.ssl_verify, str):
            verify = config.ssl_verify
        else:
            verify = None

        if config.ssl_cert:
            if config.ssl_key:
                cert = (config.ssl_cert, config.ssl_key)
            else:
                cert = config.ssl_cert
        else:
            cert = None

        resp = None
        try:
            resp = requests.get(url,
                                timeout=config.timeout,
                                headers=headers(self.agentConfig),
                                auth=auth,
                                verify=verify,
                                cert=cert)
            resp.raise_for_status()
        except Exception as e:
            # this means we've hit a particular kind of auth error that means the config is broken
            if resp and resp.status_code == 400:
                raise AuthenticationError(
                    "The ElasticSearch credentials are incorrect")

            if send_sc:
                self.service_check(
                    self.SERVICE_CHECK_CONNECT_NAME,
                    AgentCheck.CRITICAL,
                    message="Error {} when hitting {}".format(e, url),
                    tags=config.service_check_tags,
                )
            raise

        self.log.debug("request to url {} returned: {}".format(url, resp))

        return resp.json()
Exemplo n.º 8
0
    def _get_data(self, url, config, send_sc=True):
        """ Hit a given URL and return the parsed json
        """
        auth = None

        # Load SSL configuration, if available.
        # ssl_verify can be a bool or a string
        # (http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification)
        if isinstance(config.ssl_verify, (bool, str)):
            verify = config.ssl_verify
        else:
            self.log.error(
                "ssl_verify in the configuration file must be a bool or a string."
            )
            verify = None
        if config.ssl_cert and config.ssl_key:
            cert = (config.ssl_cert, config.ssl_key)
        elif config.ssl_cert:
            cert = config.ssl_cert
        else:
            cert = None

        try:
            resp = requests.get(url,
                                timeout=config.timeout,
                                headers=headers(self.agentConfig),
                                auth=auth,
                                verify=verify,
                                cert=cert)
            resp.raise_for_status()
        except Exception as e:
            if send_sc:
                self.service_check(
                    self.SERVICE_CHECK_CONNECT_NAME,
                    AgentCheck.CRITICAL,
                    message="Error {0} when hitting {1}".format(e, url),
                    tags=config.service_check_tags,
                )
            raise

        return resp.json()