def initialize(self, **kwargs):
     # The Dynatrace API client
     self.dt_client = Dynatrace(self.config.get("api_url"),
                                self.config.get("api_token"),
                                log=log,
                                proxies=self.build_proxy_url())
     self.executions = 0
 def initialize(self, **kwargs):
     # The Dynatrace API client
     self.dt_client = Dynatrace(self.config.get("api_url"),
                                self.config.get("api_token"),
                                log=log,
                                proxies=self.build_proxy_url())
     self.executions = 0
     self.failures: Dict[str, int] = defaultdict(int)
class PingExtension(RemoteBasePlugin):
    def initialize(self, **kwargs):
        # The Dynatrace API client
        self.dt_client = Dynatrace(self.config.get("api_url"),
                                   self.config.get("api_token"),
                                   log=log,
                                   proxies=self.build_proxy_url())
        self.executions = 0

    def build_proxy_url(self):
        proxy_address = self.config.get("proxy_address")
        proxy_username = self.config.get("proxy_username")
        proxy_password = self.config.get("proxy_password")

        if proxy_address:
            protocol, address = proxy_address.split("://")
            proxy_url = f"{protocol}://"
            if proxy_username:
                proxy_url += proxy_username
            if proxy_password:
                proxy_url += f":{proxy_password}"
            proxy_url += f"@{address}"
            return {"https": proxy_url}

        return {}

    def query(self, **kwargs) -> None:

        log.setLevel(self.config.get("log_level"))

        target = self.config.get("test_target")

        step_title = f"{target}"
        test_title = self.config.get("test_name") if self.config.get(
            "test_name") else step_title
        location = self.config.get(
            "test_location",
            "") if self.config.get("test_location") else "ActiveGate"
        location_id = location.replace(" ", "_").lower()
        frequency = int(self.config.get("frequency")) if self.config.get(
            "frequency") else 15

        if self.executions % frequency == 0:
            ping_result = ping(target)
            log.info(ping_result.as_dict())

            success = ping_result.packet_loss_rate is not None and ping_result.packet_loss_rate == 0
            response_time = ping_result.rtt_avg or 0

            self.dt_client.report_simple_thirdparty_synthetic_test(
                engine_name="Ping",
                timestamp=datetime.now(),
                location_id=location_id,
                location_name=location,
                test_id=self.activation.entity_id,
                test_title=test_title,
                step_title=step_title,
                schedule_interval=frequency * 60,
                success=success,
                response_time=response_time,
                edit_link=
                f"#settings/customextension;id={self.plugin_info.name}",
                icon_url=
                "https://raw.githubusercontent.com/Dynatrace/dynatrace-api/master/third-party-synthetic/active-gate-extensions/extension-third-party-ping/ping.png",
            )

            self.dt_client.report_simple_thirdparty_synthetic_test_event(
                test_id=self.activation.entity_id,
                name=f"Ping failed for {step_title}",
                location_id=location_id,
                timestamp=datetime.now(),
                state="open" if not success else "resolved",
                event_type=SYNTHETIC_EVENT_TYPE_OUTAGE,
                reason=
                f"Ping failed for {step_title}. Result: {str(ping_result.as_dict())}",
                engine_name="Ping",
            )
        self.executions += 1
Beispiel #4
0
def dt():
    with mock.patch.object(HttpClient, "make_request", new=local_make_request):
        dt = Dynatrace("mock_tenant", "mock_token")
        yield dt
class PortExtension(RemoteBasePlugin):
    def initialize(self, **kwargs):
        # The Dynatrace API client
        self.dt_client = Dynatrace(
            self.config.get("api_url"), self.config.get("api_token"), log=log, proxies=self.build_proxy_url()
        )
        self.executions = 0

    def build_proxy_url(self):
        proxy_address = self.config.get("proxy_address")
        proxy_username = self.config.get("proxy_username")
        proxy_password = self.config.get("proxy_password")

        if proxy_address:
            protocol, address = proxy_address.split("://")
            proxy_url = f"{protocol}://"
            if proxy_username:
                proxy_url += proxy_username
            if proxy_password:
                proxy_url += f":{proxy_password}"
            proxy_url += f"@{address}"
            return {"https": proxy_url}

        return {}

    def query(self, **kwargs) -> None:

        log.setLevel(self.config.get("log_level"))

        target_ip = self.config.get("test_target_ip")

        target_ports = self.config.get("test_target_ports", "").split(",")
        location = self.config.get("test_location", "") if self.config.get("test_location") else "ActiveGate"
        location_id = location.replace(" ", "_").lower()
        frequency = int(self.config.get("frequency")) if self.config.get("frequency") else 15

        if self.executions % frequency == 0:
            # This test has multiple steps, these variables will be modfied as the steps are created
            test_success = True
            test_response_time = 0
            test_steps = []
            test_step_results = []
            test_title = f"{self.config.get('test_name')}" if self.config.get("test_name") else f"Port checks for {target_ip}"

            for i, port in enumerate(target_ports):
                if port:
                    step_success, step_response_time = test_port(target_ip, int(port))
                    if not step_success:
                        test_success = False
                    test_response_time += step_response_time

                    log.info(f"{target_ip}:{port} = {step_success}, {step_response_time}")
                    step_title = f"{target_ip}:{port}"
                    event_name = f"Port check failed for {test_title} ({step_title})"

                    test_steps.append(self.dt_client.create_synthetic_test_step(i + 1, step_title))
                    test_step_results.append(
                        self.dt_client.create_synthetic_test_step_result(i + 1, datetime.now(), step_response_time)
                    )

                    self.dt_client.report_simple_thirdparty_synthetic_test_event(
                        test_id=f"{self.activation.entity_id}",
                        name=event_name,
                        location_id=location_id,
                        timestamp=datetime.now(),
                        state="open" if not test_success else "resolved",
                        event_type=SYNTHETIC_EVENT_TYPE_OUTAGE,
                        reason=event_name,
                        engine_name="Port",
                    )

            self.dt_client.report_simple_thirdparty_synthetic_test(
                engine_name="Port",
                timestamp=datetime.now(),
                location_id=location_id,
                location_name=location,
                test_id=f"{self.activation.entity_id}",
                test_title=test_title,
                schedule_interval=frequency * 60,
                success=test_success,
                response_time=test_response_time,
                edit_link=f"#settings/customextension;id={self.plugin_info.name}",
                detailed_steps=test_steps,
                detailed_step_results=test_step_results,
                icon_url="https://raw.githubusercontent.com/Dynatrace/dynatrace-api/tree/master/third-party-synthetic/active-gate-extensions/extension-third-party-port/port.png",
            )

        self.executions += 1
class DNSExtension(RemoteBasePlugin):
    def initialize(self, **kwargs):
        # The Dynatrace API client
        self.dt_client = Dynatrace(self.config.get("api_url"),
                                   self.config.get("api_token"),
                                   log=log,
                                   proxies=self.build_proxy_url())
        self.executions = 0

    def build_proxy_url(self):
        proxy_address = self.config.get("proxy_address")
        proxy_username = self.config.get("proxy_username")
        proxy_password = self.config.get("proxy_password")

        if proxy_address:
            protocol, address = proxy_address.split("://")
            proxy_url = f"{protocol}://"
            if proxy_username:
                proxy_url += proxy_username
            if proxy_password:
                proxy_url += f":{proxy_password}"
            proxy_url += f"@{address}"
            return {"https": proxy_url}

        return {}

    def query(self, **kwargs) -> None:

        log.setLevel(self.config.get("log_level"))
        dns_server = self.config.get("dns_server")
        host = self.config.get("host")

        step_title = f"{host} (DNS: {dns_server})"
        test_title = self.config.get("test_name") if self.config.get(
            "test_name") else step_title
        location = self.config.get("test_location") if self.config.get(
            "test_location") else "ActiveGate"
        location_id = location.replace(" ", "_").lower()
        frequency = int(self.config.get("frequency")) if self.config.get(
            "frequency") else 15

        if self.executions % frequency == 0:
            success, response_time = test_dns(dns_server, host)
            log.info(
                f"DNS test, DNS server: {dns_server}, host: {host}, success: {success}, time: {response_time} "
            )

            self.dt_client.report_simple_thirdparty_synthetic_test(
                engine_name="DNS",
                timestamp=datetime.now(),
                location_id=location_id,
                location_name=location,
                test_id=self.activation.entity_id,
                test_title=test_title,
                step_title=step_title,
                schedule_interval=frequency * 60,
                success=success,
                response_time=response_time,
                edit_link=
                f"#settings/customextension;id={self.plugin_info.name}",
                icon_url=
                "https://raw.githubusercontent.com/Dynatrace/dynatrace-api/master/third-party-synthetic/active-gate-extensions/extension-third-party-dns/dns.png",
            )

            self.dt_client.report_simple_thirdparty_synthetic_test_event(
                test_id=self.activation.entity_id,
                name=f"DNS lookup failed for {step_title}",
                location_id=location_id,
                timestamp=datetime.now(),
                state="open" if not success else "resolved",
                event_type=SYNTHETIC_EVENT_TYPE_OUTAGE,
                reason=f"DNS lookup failed for {step_title}",
                engine_name="DNS",
            )

        self.executions += 1