示例#1
0
 def _get_data(self, instance):
     url = instance.get('nginx_status_url')
     req = urllib2.Request(url, None, headers(self.agent_config))
     if 'user' in instance and 'password' in instance:
         add_basic_auth(req, instance['user'], instance['password'])
     request = urllib2.urlopen(req)
     return request.read()
示例#2
0
 def _get_data(self, instance):
     url = instance.get('nginx_status_url')
     req = urllib.request.Request(url, None, headers(self.agent_config))
     if 'user' in instance and 'password' in instance:
         add_basic_auth(req, instance['user'], instance['password'])
     request = urllib.request.urlopen(req)
     return request.read()
示例#3
0
    def check(self, instance):
        if 'lighttpd_status_url' not in instance:
            raise Exception("Missing 'lighttpd_status_url' variable in Lighttpd config")

        url = self.assumed_url.get(instance['lighttpd_status_url'], instance['lighttpd_status_url'])

        dimensions = self._set_dimensions(None, instance)
        self.log.debug("Connecting to %s" % url)
        req = urllib2.Request(url, None, headers(self.agent_config))
        if 'user' in instance and 'password' in instance:
            add_basic_auth(req, instance['user'], instance['password'])
        request = urllib2.urlopen(req)
        headers_resp = request.info().headers
        server_version = self._get_server_version(headers_resp)
        response = request.read()

        metric_count = 0
        # Loop through and extract the numerical values
        for line in response.split('\n'):
            values = line.split(': ')
            if len(values) == 2:  # match
                metric, value = values
                try:
                    value = float(value)
                except ValueError:
                    continue

                # Special case: kBytes => bytes
                if metric == 'Total kBytes':
                    value *= 1024

                # Send metric as a gauge, if applicable
                if metric in self.GAUGES:
                    metric_count += 1
                    metric_name = self.GAUGES[metric]
                    self.gauge(metric_name, value, dimensions=dimensions)

                # Send metric as a rate, if applicable
                if metric in self.RATES:
                    metric_count += 1
                    metric_name = self.RATES[metric]
                    self.rate(metric_name, value, dimensions=dimensions)

                # Send metric as a counter, if applicable
                if metric in self.COUNTERS:
                    metric_count += 1
                    metric_name = self.COUNTERS[metric]
                    self.increment(metric_name, value, dimensions=dimensions)

        if metric_count == 0:
            url_suffix = self.URL_SUFFIX_PER_VERSION[server_version]
            if self.assumed_url.get(instance['lighttpd_status_url'],
                                    None) is None and url[-len(url_suffix):] != url_suffix:
                self.assumed_url[instance['lighttpd_status_url']] = '%s%s' % (url, url_suffix)
                self.log.warn("Assuming url was not correct. Trying to add %s suffix to the url" % url_suffix)
                self.check(instance)
            else:
                raise Exception(
                    "No metrics were fetched for this instance. Make sure that %s is the proper url." %
                    instance['lighttpd_status_url'])
示例#4
0
 def _get_data(self, instance):
     url = instance.get("nginx_status_url")
     req = urllib2.Request(url, None, headers(self.agent_config))
     if "user" in instance and "password" in instance:
         add_basic_auth(req, instance["user"], instance["password"])
     request = urllib2.urlopen(req)
     return request.read()
示例#5
0
    def _get_data(self, url, auth=None):
        """Hit a given URL and return the parsed json

        `auth` is a tuple of (username, password) or None
        """
        req = urllib2.Request(url, None, headers(self.agent_config))
        if auth:
            add_basic_auth(req, *auth)
        request = urllib2.urlopen(req)
        response = request.read()
        return json.loads(response)
示例#6
0
    def _get_data(self, url, auth=None):
        """Hit a given URL and return the parsed json

        `auth` is a tuple of (username, password) or None
        """
        req = urllib2.Request(url, None, headers(self.agent_config))
        if auth:
            add_basic_auth(req, *auth)
        request = urllib2.urlopen(req)
        response = request.read()
        return json.loads(response)
示例#7
0
    def _get_stats(self, url, instance):
        """Hit a given URL and return the parsed json.

        """
        self.log.debug('Fetching Couchbase stats at url: %s' % url)
        req = urllib2.Request(url, None, headers(self.agent_config))
        if 'user' in instance and 'password' in instance:
            add_basic_auth(req, instance['user'], instance['password'])

        if instance['is_recent_python']:
            timeout = instance.get('timeout', DEFAULT_TIMEOUT)
            request = urllib2.urlopen(req, timeout=timeout)
        else:
            request = urllib2.urlopen(req)

        response = request.read()
        return json.loads(response)
示例#8
0
    def _get_stats(self, url, instance):
        """Hit a given URL and return the parsed json.

        """
        self.log.debug('Fetching Couchbase stats at url: %s' % url)
        req = urllib2.Request(url, None, headers(self.agent_config))
        if 'user' in instance and 'password' in instance:
            add_basic_auth(req, instance['user'], instance['password'])

        if instance['is_recent_python']:
            timeout = instance.get('timeout', DEFAULT_TIMEOUT)
            request = urllib2.urlopen(req, timeout=timeout)
        else:
            request = urllib2.urlopen(req)

        response = request.read()
        return json.loads(response)
示例#9
0
    def _get_primary_addr(self, url, node_name, auth):
        """Returns a list of primary interface addresses as seen by ES.

        Used in ES < 0.19
        """
        req = urllib2.Request(url, None, headers(self.agent_config))
        # Load basic authentication configuration, if available.
        if auth:
            add_basic_auth(req, *auth)
        request = urllib2.urlopen(req)
        response = request.read()
        data = json.loads(response)

        if node_name in data['nodes']:
            node = data['nodes'][node_name]
            if 'network' in node\
                    and 'primary_interface' in node['network']\
                    and 'address' in node['network']['primary_interface']:
                return node['network']['primary_interface']['address']

        raise NodeNotFound()
示例#10
0
    def _get_primary_addr(self, url, node_name, auth):
        """Returns a list of primary interface addresses as seen by ES.

        Used in ES < 0.19
        """
        req = urllib2.Request(url, None, headers(self.agent_config))
        # Load basic authentication configuration, if available.
        if auth:
            add_basic_auth(req, *auth)
        request = urllib2.urlopen(req)
        response = request.read()
        data = json.loads(response)

        if node_name in data['nodes']:
            node = data['nodes'][node_name]
            if 'network' in node\
                    and 'primary_interface' in node['network']\
                    and 'address' in node['network']['primary_interface']:
                return node['network']['primary_interface']['address']

        raise NodeNotFound()
示例#11
0
    def check(self, instance):
        self.url = instance.get('apache_status_url', None)
        if not self.url:
            raise Exception("Missing 'apache_status_url' in Apache config")

        req = urllib2.Request(self.url, None, util.headers(self.agent_config))
        apache_user = instance.get('apache_user', None)
        apache_password = instance.get('apache_password', None)
        if apache_user and apache_password:
            utils.add_basic_auth(req, apache_user, apache_password)
        else:
            log.debug("Not using authentication for Apache Web Server")

        # Submit a service check for status page availability.
        parsed_url = urlparse.urlparse(self.url)
        apache_host = parsed_url.hostname
        apache_port = str(parsed_url.port or 80)
        service_check_name = 'apache.status'

        # Add additional dimensions
        if apache_host == 'localhost':
            # Localhost is not very useful, so get the actual hostname
            apache_host = socket.gethostname()

        dimensions = self._set_dimensions(
            {
                'apache_host': apache_host,
                'apache_port': apache_port,
                'service': 'apache',
                'component': 'apache'
            }, instance)

        try:
            request = urllib2.urlopen(req)
        except Exception as e:
            self.log.info("%s is DOWN, error: %s. Connection failed." %
                          (service_check_name, str(e)))
            self.gauge(service_check_name, 1, dimensions=dimensions)
            return services_checks.Status.DOWN, "%s is DOWN, error: %s. Connection failed." % (
                service_check_name, str(e))
        else:
            self.log.debug("%s is UP" % service_check_name)
            self.gauge(service_check_name, 0, dimensions=dimensions)

        response = request.read()
        metric_count = 0

        # Loop through and extract the numerical values
        for line in response.split('\n'):
            values = line.split(': ')
            if len(values) == 2:  # match
                metric, value = values
                try:
                    value = float(value)
                except ValueError:
                    continue

                # Send metric as a gauge, if applicable
                if metric in self.GAUGES:
                    metric_count += 1
                    metric_name = self.GAUGES[metric]
                    log.debug(
                        'Collecting gauge data for: {0}'.format(metric_name))
                    self.gauge(metric_name, value, dimensions=dimensions)

                # Send metric as a rate, if applicable
                if metric in self.RATES:
                    metric_count += 1
                    metric_name = self.RATES[metric]
                    log.debug(
                        'Collecting rate data for: {0}'.format(metric_name))
                    self.rate(metric_name, value, dimensions=dimensions)

        if metric_count == 0:
            if self.url[-5:] != '?auto':
                self.url = '%s?auto' % self.url
                self.log.warn(
                    "Assuming url was not correct. Trying to add ?auto suffix to the url"
                )
                self.check(instance)
            else:
                return services_checks.Status.DOWN,
                "%s is DOWN, error: No metrics available.".format(
                    service_check_name)
        else:
            log.debug("Collected {0} metrics for {1} Apache Web Server".format(
                apache_host, metric_count))
示例#12
0
    def check(self, instance):
        self.url = instance.get('apache_status_url', None)
        if not self.url:
            raise Exception("Missing 'apache_status_url' in Apache config")

        req = urllib2.Request(self.url, None, util.headers(self.agent_config))
        apache_user = instance.get('apache_user', None)
        apache_password = instance.get('apache_password', None)
        if apache_user and apache_password:
            utils.add_basic_auth(req, apache_user, apache_password)
        else:
            log.debug("Not using authentication for Apache Web Server")

        # Submit a service check for status page availability.
        parsed_url = urlparse.urlparse(self.url)
        apache_host = parsed_url.hostname
        apache_port = str(parsed_url.port or 80)
        service_check_name = 'apache.status'

        # Add additional dimensions
        if apache_host == 'localhost':
            # Localhost is not very useful, so get the actual hostname
            apache_host = socket.gethostname()

        dimensions = self._set_dimensions({'apache_host': apache_host,
                                           'apache_port': apache_port,
                                           'service': 'apache',
                                           'component': 'apache'},
                                          instance)

        try:
            request = urllib2.urlopen(req)
        except Exception as e:
            self.log.info(
                "%s is DOWN, error: %s. Connection failed." % (service_check_name, str(e)))
            self.gauge(service_check_name, 1, dimensions=dimensions)
            return services_checks.Status.DOWN, "%s is DOWN, error: %s. Connection failed." % (
                service_check_name, str(e))
        else:
            self.log.debug("%s is UP" % service_check_name)
            self.gauge(service_check_name, 0, dimensions=dimensions)

        response = request.read()
        metric_count = 0

        # Loop through and extract the numerical values
        for line in response.split('\n'):
            values = line.split(': ')
            if len(values) == 2:  # match
                metric, value = values
                try:
                    value = float(value)
                except ValueError:
                    continue

                # Send metric as a gauge, if applicable
                if metric in self.GAUGES:
                    metric_count += 1
                    metric_name = self.GAUGES[metric]
                    log.debug('Collecting gauge data for: {0}'.format(metric_name))
                    self.gauge(metric_name, value, dimensions=dimensions)

                # Send metric as a rate, if applicable
                if metric in self.RATES:
                    metric_count += 1
                    metric_name = self.RATES[metric]
                    log.debug('Collecting rate data for: {0}'.format(metric_name))
                    self.rate(metric_name, value, dimensions=dimensions)

        if metric_count == 0:
            if self.url[-5:] != '?auto':
                self.url = '%s?auto' % self.url
                self.warning("Assuming url was not correct. Trying to add ?auto suffix to the url")
                self.check(instance)
            else:
                return services_checks.Status.DOWN, "%s is DOWN, error: No metrics available.".format(service_check_name)
        else:
            log.debug("Collected {0} metrics for {1} Apache Web Server".format(apache_host, metric_count))