def test_collect_worker_metrics(aggregator, instance):
    # Only ABAP instances

    instance_id = "00"
    host_agent_url = "http://localhost:50013/"
    with requests_mock.mock() as m:
        m.get(host_agent_url + "SAPHostAgent/?wsdl",
              text=_read_test_file("wsdl/HostAgent.wsdl"))
        m.post(host_agent_url,
               text=_read_test_file("samples/ABAPGetWPTable.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check._get_config(instance)
        sap_check._collect_worker_metrics(instance_id, "ABAP Instance",
                                          sap_check._get_proxy(instance_id))

        expected_tags = ["instance_id:" + instance_id]
        aggregator.assert_metric(name="DIA_workers_free",
                                 value=10,
                                 hostname="LAB-SAP-001",
                                 metric_type=aggregator.GAUGE,
                                 tags=expected_tags)
        aggregator.assert_metric(name="BTC_workers_free",
                                 value=3,
                                 hostname="LAB-SAP-001",
                                 metric_type=aggregator.GAUGE,
                                 tags=expected_tags)

        aggregator.all_metrics_asserted()
def test_collect_memory_metric(aggregator, instance):
    instance_id = "00"
    host_control_url = "http://localhost:1128/SAPHostControl"
    with requests_mock.mock() as m:
        m.get(host_control_url + "/?wsdl", text=_read_test_file("wsdl/SAPHostAgent.wsdl"))
        m.post(host_control_url + ".cgi", text=_read_test_file("samples/ParameterValue.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check._get_config(instance)
        sap_check._collect_memory_metric(instance_id, sap_check._get_proxy(instance_id))

        expected_tags = ["instance_id:" + instance_id]
        aggregator.assert_metric(
            name="phys_memsize",
            value=32767,
            hostname="LAB-SAP-001",
            metric_type=aggregator.GAUGE,
            tags=expected_tags
        )

        aggregator.all_metrics_asserted()
def test_collect_processes(aggregator, instance):
    # TODO this is needed because the topology retains data across tests
    topology.reset()

    instance_id = "00"
    host_agent_url = "http://localhost:50013/"
    with requests_mock.mock() as m:
        m.get(host_agent_url + "SAPHostAgent/?wsdl",
              text=_read_test_file("wsdl/HostAgent.wsdl"))
        m.post(host_agent_url,
               text=_read_test_file("samples/GetProcessList.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check._get_config(instance)
        sap_check._collect_processes(instance_id,
                                     sap_check._get_proxy(instance_id))

        topology.assert_snapshot(
            check_id=sap_check.check_id,
            start_snapshot=False,
            stop_snapshot=False,
            instance_key=TopologyInstance("sap", "LAB-SAP-001"),
            components=[{
                'data': {
                    'description': 'Dispatcher',
                    'elapsedtime': '119:16:01',
                    'host': 'LAB-SAP-001',
                    'labels': [],
                    'name': 'disp+work.EXE',
                    'pid': 4392,
                    'starttime': '2020 01 22 12:52:29'
                },
                'id': 'urn:process:/LAB-SAP-001:00:4392',
                'type': 'sap-process'
            }, {
                'data': {
                    'description': 'IGS Watchdog',
                    'elapsedtime': '119:16:01',
                    'host': 'LAB-SAP-001',
                    'labels': [],
                    'name': 'igswd.EXE',
                    'pid': 11088,
                    'starttime': '2020 01 22 12:52:29'
                },
                'id': 'urn:process:/LAB-SAP-001:00:11088',
                'type': 'sap-process'
            }, {
                'data': {
                    'description': 'Gateway',
                    'elapsedtime': '119:16:01',
                    'host': 'LAB-SAP-001',
                    'labels': [],
                    'name': 'gwrd',
                    'pid': 9512,
                    'starttime': '2020 01 22 12:52:29'
                },
                'id': 'urn:process:/LAB-SAP-001:00:9512',
                'type': 'sap-process'
            }, {
                'data': {
                    'description': 'ICM',
                    'elapsedtime': '119:16:01',
                    'host': 'LAB-SAP-001',
                    'labels': [],
                    'name': 'icman',
                    'pid': 6584,
                    'starttime': '2020 01 22 12:52:29'
                },
                'id': 'urn:process:/LAB-SAP-001:00:6584',
                'type': 'sap-process'
            }],
            relations=[{
                'data': {},
                'source_id': 'urn:process:/LAB-SAP-001:00:4392',
                'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'type': 'runs on'
            }, {
                'data': {},
                'source_id': 'urn:process:/LAB-SAP-001:00:11088',
                'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'type': 'runs on'
            }, {
                'data': {},
                'source_id': 'urn:process:/LAB-SAP-001:00:9512',
                'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'type': 'runs on'
            }, {
                'data': {},
                'source_id': 'urn:process:/LAB-SAP-001:00:6584',
                'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'type': 'runs on'
            }])

        aggregator.assert_event(msg_text="Running",
                                tags=[
                                    "status:SAPControl-GREEN",
                                    "pid:4392",
                                    "instance_id:" + instance_id,
                                    "starttime:2020 01 22 12:52:29",
                                ])
        aggregator.assert_event(msg_text="Running",
                                tags=[
                                    "status:SAPControl-GREEN",
                                    "pid:11088",
                                    "instance_id:" + instance_id,
                                    "starttime:2020 01 22 12:52:29",
                                ])
        aggregator.assert_event(msg_text="Running",
                                tags=[
                                    "status:SAPControl-GREEN",
                                    "pid:9512",
                                    "instance_id:" + instance_id,
                                    "starttime:2020 01 22 12:52:29",
                                ])
        aggregator.assert_event(msg_text="Running",
                                tags=[
                                    "status:SAPControl-GRAY",
                                    "pid:6584",
                                    "instance_id:" + instance_id,
                                    "starttime:2020 01 22 12:52:29",
                                ])

        aggregator.all_metrics_asserted()
def test_collect_processes(aggregator, instance):
    # TODO this is needed because the topology retains data across tests
    topology.reset()

    instance_id = "00"
    host_control_url = "http://localhost:1128/SAPHostControl"
    with requests_mock.mock() as m:
        m.get(host_control_url + "/?wsdl", text=_read_test_file("wsdl/SAPHostAgent.wsdl"))
        m.post(host_control_url + ".cgi", text=_read_test_file("samples/GetProcessList.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check._get_config(instance)
        sap_check._collect_processes(instance_id, sap_check._get_proxy(instance_id))

        topology.assert_snapshot(
            check_id=sap_check.check_id,
            start_snapshot=False,
            stop_snapshot=False,
            instance_key=TopologyInstance("sap", "LAB-SAP-001"),
            components=[
                {'data': {'description': 'Dispatcher',
                          'elapsedtime': '0:56:22',
                          'host': 'LAB-SAP-001',
                          'labels': [],
                          'tags': ['integration-type:sap', 'integration-url:LAB-SAP-001'],
                          'name': 'disp+work.EXE',
                          'pid': 5972,
                          'starttime': '2020 07 31 08:37:19',
                          "domain": "sap", "environment": "sap-prod"},
                 'id': 'urn:process:/LAB-SAP-001:00:5972',
                 'type': 'sap-process'},
                {'data': {'description': 'IGS Watchdog',
                          'elapsedtime': '0:56:22',
                          'host': 'LAB-SAP-001',
                          'labels': [],
                          'tags': ['integration-type:sap', 'integration-url:LAB-SAP-001'],
                          'name': 'igswd.EXE',
                          'pid': 15564,
                          'starttime': '2020 07 31 08:37:19',
                          "domain": "sap", "environment": "sap-prod"},
                 'id': 'urn:process:/LAB-SAP-001:00:15564',
                 'type': 'sap-process'},
                {'data': {'description': 'Gateway',
                          'elapsedtime': '0:56:21',
                          'host': 'LAB-SAP-001',
                          'labels': [],
                          'tags': ['integration-type:sap', 'integration-url:LAB-SAP-001'],
                          'name': 'gwrd',
                          'pid': 16624,
                          'starttime': '2020 07 31 08:37:20',
                          "domain": "sap", "environment": "sap-prod"},
                 'id': 'urn:process:/LAB-SAP-001:00:16624',
                 'type': 'sap-process'},
                {'data': {'description': 'ICM',
                          'elapsedtime': '0:56:21',
                          'host': 'LAB-SAP-001',
                          'labels': [],
                          'tags': ['integration-type:sap', 'integration-url:LAB-SAP-001'],
                          'name': 'icman',
                          'pid': 11508,
                          'starttime': '2020 07 31 08:37:20',
                          "domain": "sap", "environment": "sap-prod"},
                 'id': 'urn:process:/LAB-SAP-001:00:11508',
                 'type': 'sap-process'}
            ],
            relations=[
                {'data': {"domain": "sap", "environment": "sap-prod", "tags": []},
                 'source_id': 'urn:process:/LAB-SAP-001:00:5972',
                 'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                 'type': 'runs on'},
                {'data': {"domain": "sap", "environment": "sap-prod", "tags": []},
                 'source_id': 'urn:process:/LAB-SAP-001:00:15564',
                 'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                 'type': 'runs on'},
                {'data': {"domain": "sap", "environment": "sap-prod", "tags": []},
                 'source_id': 'urn:process:/LAB-SAP-001:00:16624',
                 'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                 'type': 'runs on'},
                {'data': {"domain": "sap", "environment": "sap-prod", "tags": []},
                 'source_id': 'urn:process:/LAB-SAP-001:00:11508',
                 'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                 'type': 'runs on'}
            ]
        )

        aggregator.assert_event(
            msg_text="Running",
            tags=[
                "status:SAPControl-GREEN",
                "pid:5972",
                "instance_id:" + instance_id,
                "starttime:2020 07 31 08:37:19",
            ]
        )
        aggregator.assert_event(
            msg_text="Running",
            tags=[
                "status:SAPControl-GREEN",
                "pid:15564",
                "instance_id:" + instance_id,
                "starttime:2020 07 31 08:37:19",
            ]
        )
        aggregator.assert_event(
            msg_text="Running",
            tags=[
                "status:SAPControl-GREEN",
                "pid:16624",
                "instance_id:" + instance_id,
                "starttime:2020 07 31 08:37:20",
            ]
        )
        aggregator.assert_event(
            msg_text="Running",
            tags=[
                "status:SAPControl-GREEN",
                "pid:11508",
                "instance_id:" + instance_id,
                "starttime:2020 07 31 08:37:20",
            ]
        )

        aggregator.all_metrics_asserted()