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()
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'])
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()
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()
def _get_stats(self, url): """Hit a given URL and return the parsed json. """ self.log.debug('Fetching Couchdb stats at url: %s' % url) req = urllib.request.Request(url, None, headers(self.agent_config)) # Do the request, log any errors request = urllib.request.urlopen(req) response = request.read() return json.loads(response)
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)
def _get_stats(self, url): """Hit a given URL and return the parsed json. """ self.log.debug('Fetching Couchdb stats at url: %s' % url) req = urllib2.Request(url, None, headers(self.agent_config)) # Do the request, log any errors request = urllib2.urlopen(req) response = request.read() return json.loads(response)
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)
def _fetch_data(self, url, username, password): """Hit a given URL and return the parsed json. """ # Try to fetch data from the stats URL passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, username, password) authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) url = "%s%s" % (url, STATS_URL) self.log.debug("HAProxy Fetching haproxy search data from: %s" % url) req = urllib2.Request(url, None, headers(self.agent_config)) request = urllib2.urlopen(req) response = request.read() # Split the data by line return response.split('\n')
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()
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))
def _get_data(self, url): return requests.get( url=url, headers=util.headers(self.agent_config) ).json()
def _get_json(self, url, ssl_params, timeout): try: 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 r = requests.get(url, verify=verify, cert=certificate, timeout=timeout, headers=headers(self.agent_config)) except requests.exceptions.Timeout: # If there's a timeout return services_checks.Status.CRITICAL, "Timeout when hitting %s" % url if r.status_code != 200: return services_checks.Status.CRITICAL, "Got %s when hitting %s" % (r.status_code, url) return r.json()
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))
def _check(self, instance): endpoint, username, password, timeout, headers, whitelist, metricdef, \ collect_response_time, disable_ssl_validation = self._load_conf(instance) timer = util.Timer() merged_dimensions = self._set_dimensions({'component': 'influxdb', 'url': endpoint}, instance) instance_name = instance.get('name', "unnamed") try: merged_headers = headers.copy() merged_headers.update(util.headers(self.agent_config)) if username is not None and password is not None: auth = (username, password) else: auth = None self.log.debug('Query InfluxDB using GET to %s', endpoint) resp = requests.get(endpoint, params=PARAMS, headers=merged_headers, auth=auth, timeout=self.timeout, verify=not disable_ssl_validation) content = resp.json() # report response time first, even when there is HTTP errors if collect_response_time: # Stop the timer as early as possible running_time = timer.total() self.gauge('monasca.agent.collect_time', running_time, dimensions={'agent_check': 'influxdb', 'instance': instance_name}) # check HTTP errors if int(resp.status_code) >= 500: error_string = '{0} is DOWN, error code: {1}'.format(endpoint, resp.status_code) self._push_error(error_string, instance_name) return services_checks.Status.DOWN, error_string elif int(resp.status_code) >= 400: error_string = "InfluxDB check {0} causes HTTP errors when accessing {1}, error code: {2}".format( instance.get('name'), endpoint, resp.status_code) self.warning(error_string) self.log.error(error_string) return services_checks.Status.DOWN, error_string self._rate_or_gauge_statuses(content, merged_dimensions, whitelist, metricdef) success_string = '{0} is UP'.format(endpoint) self.log.debug(success_string) self.gauge(HTTP_STATUS_MNAME, 0, dimensions=merged_dimensions) return services_checks.Status.UP, success_string except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as e: error_string = '{0} is not reachable via network, error: {1}'.format(endpoint, repr(e)) self._push_error(error_string, instance_name) return services_checks.Status.DOWN, error_string except requests.exceptions.Timeout as e: length = timer.total() * 1000.0 error_string = '{0} did not respond within {2} ms, error: {1}.'.format(endpoint, repr(e), length) self._push_error(error_string, instance_name) return services_checks.Status.DOWN, error_string except requests.exceptions.RequestException as e: error_string = 'Unhandled exception {0}'.format(repr(e)) self.log.exception(error_string) self.warning(error_string) return services_checks.Status.DOWN, error_string except (KeyError, TypeError) as e: error_string = "Unsupported schema returned by query endpoint of instance {0}: {1}".format( instance.get('url'), repr(e)) self.log.exception(error_string) return services_checks.Status.UP, error_string
def _get_data(self, url): return requests.get(url=url, headers=util.headers(self.agent_config)).json()