def get_service_replication(self, all_services, synapse_host, synapse_port, synapse_haproxy_url_format): # Get the replication data once for performance synapse_host_port = "%s:%s" % (synapse_host, synapse_port) self.log.debug( "Gathering replication information from {0}". format(synapse_host_port)) service_replication = {} try: service_replication = get_replication_for_services( synapse_host, synapse_port, synapse_haproxy_url_format, ['%s.main' % name for name in all_services] ) except requests.exceptions.ConnectionError: self.log.error( 'Failed to connect synapse haproxy on {0}'. format(synapse_host_port)) self.critical( 'Failed to connect synapse haproxy on {0}'. format(synapse_host_port)) except Exception as e: self.log.error( 'Unable to collect replication information on {0}: {1}'. format(synapse_host_port, e.message)) self.critical( 'Unable to collect replication information: {0}'. format(e.message)) self.log.debug( "Finished gathering replication information from {0}". format(synapse_host_port)) return service_replication
def get_smartstack_replication_for_attribute(attribute, service, namespace, blacklist): """Loads smartstack replication from a host with the specified attribute :param attribute: a Mesos attribute :param service: A service name, like 'example_service' :param namespace: A particular smartstack namespace to inspect, like 'main' :param constraints: A list of Marathon constraints to restrict which synapse hosts to query :param blacklist: A list of blacklisted location tuples in the form of (location, value) :returns: a dictionary of the form {'<unique_attribute_value>': <smartstack replication hash>} (the dictionary will contain keys for unique all attribute values) """ replication_info = {} unique_values = mesos_tools.get_mesos_slaves_grouped_by_attribute(attribute=attribute, blacklist=blacklist) full_name = compose_job_id(service, namespace) for value, hosts in unique_values.iteritems(): # arbitrarily choose the first host with a given attribute to query for replication stats synapse_host = hosts[0] repl_info = replication_utils.get_replication_for_services( synapse_host=synapse_host, synapse_port=smartstack_tools.DEFAULT_SYNAPSE_PORT, services=[full_name], ) replication_info[value] = repl_info return replication_info
def get_service_replication(self, all_services, synapse_host, synapse_port, synapse_haproxy_url_format): # Get the replication data once for performance synapse_host_port = "%s:%s" % (synapse_host, synapse_port) self.log.debug("Gathering replication information from {0}".format( synapse_host_port)) service_replication = {} try: service_replication = get_replication_for_services( synapse_host, synapse_port, synapse_haproxy_url_format, ['%s.main' % name for name in all_services]) except requests.exceptions.ConnectionError: self.log.error('Failed to connect synapse haproxy on {0}'.format( synapse_host_port)) self.critical('Failed to connect synapse haproxy on {0}'.format( synapse_host_port)) except Exception as e: self.log.error( 'Unable to collect replication information on {0}: {1}'.format( synapse_host_port, e.message)) self.critical( 'Unable to collect replication information: {0}'.format( e.message)) self.log.debug( "Finished gathering replication information from {0}".format( synapse_host_port)) return service_replication
def run_synapse_check(): options = parse_synapse_check_options() try: service_replications = get_replication_for_services( options.synapse_host_port, options.services) all_codes = [] for name, replication in service_replications.iteritems(): code, message = check_replication(name, replication, options.warn, options.crit) all_codes.append(code) print message sys.exit(max(all_codes)) except Exception, e: fail('UNKNOWN: {0}'.format(e), 3)
def run_synapse_check(): system_paasta_config = utils.load_system_paasta_config() options = parse_synapse_check_options(system_paasta_config) try: service_replications = get_replication_for_services( options.synapse_host, options.synapse_port, options.synapse_haproxy_url_format, options.services) all_codes = [] for name, replication in service_replications.iteritems(): code, message = check_replication(name, replication, options.warn, options.crit) all_codes.append(code) print message sys.exit(max(all_codes)) except Exception, e: fail('UNKNOWN: {0}'.format(e), 3)
def run_synapse_check(): options = parse_synapse_check_options() try: service_replications = get_replication_for_services( options.synapse_host_port, options.services ) all_codes = [] for name, replication in service_replications.iteritems(): code, message = check_replication(name, replication, options.warn, options.crit) all_codes.append(code) print message sys.exit(max(all_codes)) except Exception, e: fail('UNKNOWN: {0}'.format(e), 3)
def run_synapse_check(): system_paasta_config = utils.load_system_paasta_config() options = parse_synapse_check_options(system_paasta_config) try: service_replications = get_replication_for_services( options.synapse_host, options.synapse_port, options.synapse_haproxy_url_format, options.services ) all_codes = [] for name, replication in service_replications.iteritems(): code, message = check_replication(name, replication, options.warn, options.crit) all_codes.append(code) print message sys.exit(max(all_codes)) except Exception, e: fail('UNKNOWN: {0}'.format(e), 3)
def test_get_replication_for_service(): testdir = os.path.dirname(os.path.realpath(__file__)) testdata = os.path.join(testdir, 'haproxy_snapshot.txt') with open(testdata, 'r') as fd: mock_haproxy_data = fd.read() mock_response = mock.Mock() mock_response.text = mock_haproxy_data mock_get = mock.Mock(return_value=(mock_response)) with mock.patch.object(requests.Session, 'get', mock_get): replication_result = get_replication_for_services( 'fake_host', 6666, DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT, ['service1', 'service2', 'service3', 'service4']) expected = { 'service1': 18, 'service2': 19, 'service3': 0, 'service4': 3 } assert expected == replication_result
def test_get_replication_for_service(): testdir = os.path.dirname(os.path.realpath(__file__)) testdata = os.path.join(testdir, 'haproxy_snapshot.txt') with open(testdata, 'r') as fd: mock_haproxy_data = fd.read() mock_response = mock.Mock() mock_response.text = mock_haproxy_data mock_get = mock.Mock(return_value=(mock_response)) with mock.patch.object(requests.Session, 'get', mock_get): replication_result = get_replication_for_services( 'fake_host', 6666, ['service1', 'service2', 'service3', 'service4'] ) expected = { 'service1': 18, 'service2': 19, 'service3': 0, 'service4': 3 } assert expected == replication_result