def make_api_call(data, url): collectd.debug("GETTING THIS URL %s" % url) try: key_file, cert_file, ca_certs = get_ssl_params(data) opener = urllib2.build_opener( urllib_ssl_handler.HTTPSHandler(key_file=key_file, cert_file=cert_file, ca_certs=ca_certs)) response = opener.open(url) return response except (urllib2.HTTPError, urllib2.URLError), e: collectd.error("ERROR: API call failed: (%s) %s" % (e, url))
def _send_request(self, url): ''' Performs a GET against the given url. ''' LOGGER.debug('Making an api call to {0}'.format(url)) data = None try: opener = urllib2.build_opener(urllib_ssl_handler.HTTPSHandler( ca_certs=self.ssl_certs['ca_cert'], cert_file=self.ssl_certs['client_cert'], key_file=self.ssl_certs['client_key'])) opener.addheaders = [] for k, v in self.headers.items(): opener.addheaders.append((k, v)) response = opener.open(url) data = response.read() data = json.loads(data) except urllib2.HTTPError, e: LOGGER.error('HTTPError - status code: {0}, ' 'received from {1}'.format(e.code, url))
def config(conf): """ The config callback will get configuration details from 'conf' object. 'conf' object contains information like host, port, username, password etc. The read method is registered as read callback along with module_config. """ plugin_config = {} username = None password = None custom_dimensions = {} required_keys = ('Host', 'Port', 'Node') log_level = logging.WARN interval = None ssl_keys = {} enhanced_metrics = False optional_included_metrics = [] excluded_metrics = [] testing = False basic_metrics = couchdb_metrics.get_basic_metrics() for kv in conf.children: collectd.debug(str(kv)) if kv.key == 'LogLevel': log_level = sfx.get_log_level_from_config(kv.values[0]) elif kv.key == 'Interval': interval = float(kv.values[0]) elif kv.key == 'Dimension': if len(kv.values) >= 2: key = str(kv.values[0]) val = str(kv.values[1]) custom_dimensions[key] = val else: collectd.warning("Unable to parse dimensions {}".format(kv.values)) elif kv.key in required_keys: plugin_config[kv.key] = kv.values[0] elif kv.key == 'Username' and kv.values[0]: username = kv.values[0] elif kv.key == 'Password' and kv.values[0]: password = kv.values[0] elif kv.key == 'Cluster' and kv.values[0]: custom_dimensions['Cluster'] = kv.values[0] elif kv.key == 'IncludeMetric' and kv.values[0]: include_metric = kv.values[0] optional_included_metrics.append(('.'.join(include_metric.split('.')[2:]), include_metric.split('.')[0])) elif kv.key == 'ExcludeMetric' and kv.values[0]: exclude_metric = kv.values[0] excluded_metrics.append(('.'.join(exclude_metric.split('.')[2:]), exclude_metric.split('.')[0])) elif kv.key == 'ssl_keyfile' and kv.values[0]: ssl_keys['ssl_keyfile'] = kv.values[0] elif kv.key == 'ssl_certificate' and kv.values[0]: ssl_keys['ssl_certificate'] = kv.values[0] elif kv.key == 'ssl_ca_certs' and kv.values[0]: ssl_keys['ssl_ca_certs'] = kv.values[0] elif kv.key == 'EnhancedMetrics' and kv.values[0]: enhanced_metrics = sfx.str_to_bool(kv.values[0]) elif kv.key == 'Testing' and kv.values[0]: testing = sfx.str_to_bool(kv.values[0]) for key in required_keys: val = plugin_config.get(key) if val is None: raise ValueError("Missing required config setting: {0}".format(key)) base_url = "http://{0}:{1}".format(plugin_config['Host'], plugin_config['Port']) https_handler = None if 'ssl_certificate' in ssl_keys and 'ssl_keyfile' in ssl_keys: base_url = ('https' + base_url[4:]) https_handler = urllib_ssl_handler.HTTPSHandler(key_file=ssl_keys['ssl_keyfile'], cert_file=ssl_keys['ssl_certificate'], ca_certs=ssl_keys['ssl_ca_certs']) # Auth handler to handle basic http authentication. auth = urllib2.HTTPPasswordMgrWithDefaultRealm() auth_header = None if username is None and password is None: username = password = '' else: # Some CouchDB databases does not send 'WWW-authenticate' header # with the 401 response as it would hinder the futon features. # The absence of the above header will break urllib2 library. # auth_header is sent as an extra measure to prevent # urllib2 from failing. auth_header = "Basic {0}".format(base64.b64encode("{0}:{1}".format(username, password))) auth.add_password(None, uri=base_url, user=username, passwd=password) auth_handler = urllib2.HTTPBasicAuthHandler(auth) if https_handler: opener = urllib2.build_opener(auth_handler, https_handler) else: opener = urllib2.build_opener(auth_handler) node_metrics = [] node_metrics.extend(basic_metrics['node_metrics']) if enhanced_metrics: node_metrics.extend((couchdb_metrics.get_enhanced_metrics())['node_metrics']) if len(optional_included_metrics) > 0: node_metrics.extend(optional_included_metrics) if len(excluded_metrics) > 0: for t in excluded_metrics: try: node_metrics.remove(t) except ValueError: pass db_metrics = basic_metrics['db_metrics'] metrics = { 'node_metrics': node_metrics, 'db_metrics': db_metrics } instance_id = "{0}-{1}".format(PLUGIN_NAME, str(plugin_config['Node'])) plugin_instance = '{0}:{1}'.format(plugin_config['Host'], plugin_config['Port']) # If there are any custom_dimensions, those will be added to plugin_instance if any(custom_dimensions): formatted_dim = [] for k, v in six.iteritems(custom_dimensions): formatted_dim.extend(["{0}={1}".format(k, v)]) dim_str = '({0})'.format(str(formatted_dim).replace('\'', ''). replace(' ', '').replace('\"', '').replace('[', ''). replace(']', '')) plugin_instance += dim_str logger = logging.getLogger(make_logger_name(instance_id)) logger.setLevel(log_level) module_config = { 'instance_id': instance_id, 'interval': interval, 'plugin_instance': plugin_instance, 'dimensions': custom_dimensions, 'base_url': base_url, 'opener': opener, 'auth_header': auth_header, 'username': username, 'password': password, 'plugin_config': plugin_config, 'metrics': metrics, } if testing: return module_config couchdb_collector = CouchDBCollector(module_config) if interval: collectd.register_read(couchdb_collector.read, interval, name=base_url) else: collectd.register_read(couchdb_collector.read, name=base_url)
def configure_callback(conf): """Receive configuration block""" plugin_conf = {} cluster = "default" interval = DEFAULT_INTERVAL username = None password = None custom_dimensions = {} enhanced_metrics = False exclude_optional_metrics = set() include_optional_metrics = set() ssl_keys = {} http_timeout = DEFAULT_API_TIMEOUT testing = False required_keys = frozenset(("Host", "Port")) for val in conf.children: if val.key in required_keys: plugin_conf[val.key] = val.values[0] elif val.key == "Username" and val.values[0]: username = val.values[0] elif val.key == "Password" and val.values[0]: password = val.values[0] elif val.key == "Interval" and val.values[0]: interval = val.values[0] elif val.key == "Cluster" and val.values[0]: cluster = val.values[0] custom_dimensions["cluster"] = val.values[0] elif val.key == "Dimension": if len(val.values) == 2: custom_dimensions.update({val.values[0]: val.values[1]}) else: collectd.warning( "WARNING: Check configuration setting for %s" % val.key) elif val.key == "EnhancedMetrics" and val.values[0]: enhanced_metrics = str_to_bool(val.values[0]) elif val.key == "IncludeMetric" and val.values[0]: include_optional_metrics.add(val.values[0]) elif val.key == "ExcludeMetric" and val.values[0]: exclude_optional_metrics.add(val.values[0]) elif val.key == "ssl_keyfile" and val.values[0]: ssl_keys["ssl_keyfile"] = val.values[0] elif val.key == "ssl_certificate" and val.values[0]: ssl_keys["ssl_certificate"] = val.values[0] elif val.key == "ssl_ca_certs" and val.values[0]: ssl_keys["ssl_ca_certs"] = val.values[0] elif val.key == "Testing" and val.values[0]: testing = str_to_bool(val.values[0]) for key in required_keys: try: val = plugin_conf[key] except KeyError: raise KeyError("Missing required config setting: %s" % key) base_url = "http://{0}:{1}/solr".format(plugin_conf["Host"], plugin_conf["Port"]) https_handler = None if "ssl_certificate" in ssl_keys and "ssl_keyfile" in ssl_keys: base_url = "https" + base_url[4:] https_handler = urllib_ssl_handler.HTTPSHandler( key_file=ssl_keys["ssl_keyfile"], cert_file=ssl_keys["ssl_certificate"], ca_certs=ssl_keys["ssl_ca_certs"]) # Auth handler to handle basic http authentication. auth = urllib.request.HTTPPasswordMgrWithDefaultRealm() if username is None and password is None: username = password = "" auth.add_password(None, uri=base_url, user=username, passwd=password) auth_handler = urllib.request.HTTPBasicAuthHandler(auth) if https_handler: opener = urllib.request.build_opener(auth_handler, https_handler) else: opener = urllib.request.build_opener(auth_handler) module_config = { "state": None, "member_id": ("{0}:{1}".format(plugin_conf["Host"], plugin_conf["Port"])), "plugin_conf": plugin_conf, "cluster": cluster, "interval": interval, "ssl_keys": ssl_keys, "base_url": base_url, "opener": opener, "username": username, "password": password, "http_timeout": http_timeout, "custom_dimensions": custom_dimensions, "enhanced_metrics": enhanced_metrics, "include_optional_metrics": include_optional_metrics, "exclude_optional_metrics": exclude_optional_metrics, } if testing: return module_config collectd.register_read(read_metrics, data=module_config, name=module_config["member_id"])
def configure_callback(conf): """Receive configuration block""" plugin_conf = {} cluster = 'default' interval = DEFAULT_INTERVAL username = None password = None custom_dimensions = {} enhanced_metrics = False exclude_optional_metrics = set() include_optional_metrics = set() ssl_keys = {} http_timeout = DEFAULT_API_TIMEOUT testing = False required_keys = frozenset(('Host', 'Port')) for val in conf.children: if val.key in required_keys: plugin_conf[val.key] = val.values[0] elif val.key == 'Username' and val.values[0]: username = val.values[0] elif val.key == 'Password' and val.values[0]: password = val.values[0] elif val.key == 'Interval' and val.values[0]: interval = val.values[0] elif val.key == 'Cluster' and val.values[0]: cluster = val.values[0] custom_dimensions['cluster'] = val.values[0] elif val.key == 'Dimension': if len(val.values) == 2: custom_dimensions.update({val.values[0]: val.values[1]}) else: collectd.warning( "WARNING: Check configuration setting for %s" % val.key) elif val.key == 'EnhancedMetrics' and val.values[0]: enhanced_metrics = str_to_bool(val.values[0]) elif val.key == 'IncludeMetric' and val.values[0]: include_optional_metrics.add(val.values[0]) elif val.key == 'ExcludeMetric' and val.values[0]: exclude_optional_metrics.add(val.values[0]) elif val.key == 'ssl_keyfile' and val.values[0]: ssl_keys['ssl_keyfile'] = val.values[0] elif val.key == 'ssl_certificate' and val.values[0]: ssl_keys['ssl_certificate'] = val.values[0] elif val.key == 'ssl_ca_certs' and val.values[0]: ssl_keys['ssl_ca_certs'] = val.values[0] elif val.key == 'Testing' and val.values[0]: testing = str_to_bool(val.values[0]) for key in required_keys: try: val = plugin_conf[key] except KeyError: raise KeyError("Missing required config setting: %s" % key) base_url = ("http://{0}:{1}/solr".format(plugin_conf['Host'], plugin_conf['Port'])) https_handler = None if 'ssl_certificate' in ssl_keys and 'ssl_keyfile' in ssl_keys: base_url = ('https' + base_url[4:]) https_handler = urllib_ssl_handler.HTTPSHandler( key_file=ssl_keys['ssl_keyfile'], cert_file=ssl_keys['ssl_certificate'], ca_certs=ssl_keys['ssl_ca_certs']) # Auth handler to handle basic http authentication. auth = urllib2.HTTPPasswordMgrWithDefaultRealm() if username is None and password is None: username = password = '' auth.add_password(None, uri=base_url, user=username, passwd=password) auth_handler = urllib2.HTTPBasicAuthHandler(auth) if https_handler: opener = urllib2.build_opener(auth_handler, https_handler) else: opener = urllib2.build_opener(auth_handler) module_config = { 'state': None, 'member_id': ("{0}:{1}".format(plugin_conf['Host'], plugin_conf['Port'])), 'plugin_conf': plugin_conf, 'cluster': cluster, 'interval': interval, 'ssl_keys': ssl_keys, 'base_url': base_url, 'opener': opener, 'username': username, 'password': password, 'http_timeout': http_timeout, 'custom_dimensions': custom_dimensions, 'enhanced_metrics': enhanced_metrics, 'include_optional_metrics': include_optional_metrics, 'exclude_optional_metrics': exclude_optional_metrics, } if testing: return module_config collectd.register_read(read_metrics, data=module_config, name=module_config['member_id'])