def test_get_service_health_no_response(init_config, instance, aggregator): ambari = AmbariCheck('Ambari', init_config, [instance]) ambari._make_request = MagicMock(return_value=None) ambari.get_service_checks('LabCluster', 'HDFS', service_tags=['ambari_cluster:LabCluster', 'ambari_service:hdfs']) ambari._make_request.assert_called_with('localhost/api/v1/clusters/LabCluster/services/HDFS?fields=ServiceInfo') aggregator.assert_service_check( name="ambari.state", status=AgentCheck.CRITICAL, tags=['ambari_cluster:LabCluster', 'ambari_service:hdfs'] )
def test_cant_connect(init_config, instance, aggregator): ambari = AmbariCheck(init_config=init_config, instances=[instance]) ambari._make_request = MagicMock(return_value=None) try: ambari.get_clusters('localhost') except CheckException: pass aggregator.assert_service_check(name="ambari.can_connect", status=AgentCheck.CRITICAL)
def test_get_hosts(init_config, instance): ambari = AmbariCheck(init_config=init_config, instances=[instance]) ambari._make_request = MagicMock( return_value={'href': 'localhost/api/v1/clusters/myCluster/hosts?fields=metrics', 'items': responses.HOSTS_INFO} ) hosts = ambari._get_hosts_info('localhost', 'myCluster') ambari._make_request.assert_called_with('localhost/api/v1/clusters/myCluster/hosts?fields=metrics') assert len(hosts) == 2 assert hosts[0]['Hosts']['host_name'] == 'my_host_1' assert hosts[1]['Hosts']['host_name'] == 'my_host_2' assert hosts[1]['metrics'] is not None
def test_cant_connect(init_config, instance, aggregator): instance['tags'] = ['foo:bar'] ambari = AmbariCheck('Ambari', init_config, [instance]) ambari._make_request = MagicMock(return_value=None) try: ambari.get_clusters() except CheckException: pass aggregator.assert_service_check( name="ambari.can_connect", status=AgentCheck.CRITICAL, tags=['foo:bar', 'url:localhost'] )
def test_get_service_health(init_config, instance, aggregator): ambari = AmbariCheck('Ambari', init_config, [instance]) ambari._make_request = MagicMock(return_value=responses.SERVICE_HEALTH_METRICS) ambari.get_service_checks('LabCluster', 'HDFS', service_tags=['ambari_cluster:LabCluster', 'ambari_service:hdfs']) ambari._make_request.assert_called_with('localhost/api/v1/clusters/LabCluster/services/HDFS?fields=ServiceInfo') aggregator.assert_service_check( name="ambari.state", status=AgentCheck.OK, tags=['ambari_cluster:LabCluster', 'ambari_service:hdfs', 'state:INSTALLED'], )
def test_get_component_metrics(init_config, instance): ambari = AmbariCheck(init_config=init_config, instances=[instance]) ambari._make_request = MagicMock(return_value=responses.COMPONENT_METRICS) ambari._submit_gauge = MagicMock() namenode_tags = [ 'ambari_cluster:LabCluster', 'ambari_service:hdfs', 'ambari_component:namenode' ] ambari.get_component_metrics( 'localhost', 'LabCluster', 'HDFS', base_tags=['ambari_cluster:LabCluster', 'ambari_service:hdfs'], component_whitelist=['NAMENODE'], metric_whitelist=['cpu'], ) ambari._make_request.assert_called_with( 'localhost/api/v1/clusters/LabCluster/services/HDFS/components?fields=metrics' ) ambari._submit_gauge.assert_has_calls( [ call('cpu.cpu_idle', 90.3, namenode_tags), call('cpu.cpu_idle._avg', 90.3, namenode_tags), call('cpu.cpu_idle._max', 90.3, namenode_tags), call('cpu.cpu_idle._min', 90.3, namenode_tags), call('cpu.cpu_idle._sum', 90.3, namenode_tags), call('cpu.cpu_nice', 0.0, namenode_tags), call('cpu.cpu_nice._avg', 0.0, namenode_tags), call('cpu.cpu_nice._max', 0.0, namenode_tags), call('cpu.cpu_nice._min', 0.0, namenode_tags), call('cpu.cpu_nice._sum', 0.0, namenode_tags), call('cpu.cpu_system', 1.6333333333333335, namenode_tags), call('cpu.cpu_system._avg', 1.6333333333333335, namenode_tags), call('cpu.cpu_system._max', 1.6333333333333335, namenode_tags), call('cpu.cpu_system._min', 1.6333333333333335, namenode_tags), call('cpu.cpu_system._sum', 1.6333333333333335, namenode_tags), call('cpu.cpu_user', 8.033333333333333, namenode_tags), call('cpu.cpu_user._avg', 8.033333333333333, namenode_tags), call('cpu.cpu_user._max', 8.033333333333333, namenode_tags), call('cpu.cpu_user._min', 8.033333333333333, namenode_tags), call('cpu.cpu_user._sum', 8.033333333333333, namenode_tags), call('cpu.cpu_wio', 0.0, namenode_tags), call('cpu.cpu_wio._avg', 0.0, namenode_tags), call('cpu.cpu_wio._max', 0.0, namenode_tags), call('cpu.cpu_wio._min', 0.0, namenode_tags), call('cpu.cpu_wio._sum', 0.0, namenode_tags), ], any_order=True, )
def test_get_component_metrics(init_config, instance, aggregator): ambari = AmbariCheck(init_config=init_config, instances=[instance]) ambari._make_request = MagicMock(return_value=responses.COMPONENT_METRICS) namenode_tags = [ 'ambari_cluster:LabCluster', 'ambari_service:hdfs', 'ambari_component:namenode' ] ambari.get_component_metrics( 'localhost', 'LabCluster', 'HDFS', base_tags=['ambari_cluster:LabCluster', 'ambari_service:hdfs'], component_whitelist={'NAMENODE': ['cpu']}, ) ambari._make_request.assert_called_with( 'localhost/api/v1/clusters/LabCluster/services/HDFS/components?fields=metrics' ) metrics = [ ('cpu.cpu_idle', 90.3), ('cpu.cpu_idle._avg', 90.3), ('cpu.cpu_idle._max', 90.3), ('cpu.cpu_idle._min', 90.3), ('cpu.cpu_idle._sum', 90.3), ('cpu.cpu_nice', 0.0), ('cpu.cpu_nice._avg', 0.0), ('cpu.cpu_nice._max', 0.0), ('cpu.cpu_nice._min', 0.0), ('cpu.cpu_nice._sum', 0.0), ('cpu.cpu_system', 1.6333333333333335), ('cpu.cpu_system._avg', 1.6333333333333335), ('cpu.cpu_system._max', 1.6333333333333335), ('cpu.cpu_system._min', 1.6333333333333335), ('cpu.cpu_system._sum', 1.6333333333333335), ('cpu.cpu_user', 8.033333333333333), ('cpu.cpu_user._avg', 8.033333333333333), ('cpu.cpu_user._max', 8.033333333333333), ('cpu.cpu_user._min', 8.033333333333333), ('cpu.cpu_user._sum', 8.033333333333333), ('cpu.cpu_wio', 0.0), ('cpu.cpu_wio._avg', 0.0), ('cpu.cpu_wio._max', 0.0), ('cpu.cpu_wio._min', 0.0), ('cpu.cpu_wio._sum', 0.0), ] for m in metrics: aggregator.assert_metric(name='ambari.{}'.format(m[0]), value=m[1], tags=namenode_tags)
def test_get_clusters(init_config, instance, aggregator): ambari = AmbariCheck(init_config=init_config, instances=[instance]) ambari._make_request = MagicMock( return_value={ 'href': 'localhost/api/v1/clusters', 'items': [{'href': 'localhost/api/v1/clusters/LabCluster', 'Clusters': {'cluster_name': 'LabCluster'}}], } ) clusters = ambari.get_clusters('localhost', ['foo:bar']) ambari._make_request.assert_called_with('localhost/api/v1/clusters') aggregator.assert_service_check(name="ambari.can_connect", status=AgentCheck.OK, tags=['foo:bar', 'url:localhost']) assert clusters == ['LabCluster']
def test_get_clusters(init_config, instance): ambari = AmbariCheck(init_config=init_config, instances=[instance]) ambari._make_request = MagicMock( return_value={ 'href': 'localhost/api/v1/clusters', 'items': [{ 'href': 'localhost/api/v1/clusters/LabCluster', 'Clusters': { 'cluster_name': 'LabCluster' } }], }) clusters = ambari.get_clusters('localhost') ambari._make_request.assert_called_with('localhost/api/v1/clusters') assert clusters == ['LabCluster']
def test_get_service_health(init_config, instance): ambari = AmbariCheck(init_config=init_config, instances=[instance]) ambari._make_request = MagicMock( return_value=responses.SERVICE_HEALTH_METRICS) service_info = ambari._get_service_checks_info( 'localhost', 'LabCluster', 'HDFS', service_tags=['ambari_cluster:LabCluster', 'ambari_service:hdfs']) ambari._make_request.assert_called_with( 'localhost/api/v1/clusters/LabCluster/services/HDFS?fields=ServiceInfo' ) assert service_info == [{ 'state': 0, 'tags': ['ambari_cluster:LabCluster', 'ambari_service:hdfs'] }]