Ejemplo n.º 1
0
def metrics_series(selector):
    with open(f"{current_file_path}/config.json", "r") as f:
        config = json.load(f)
    d = DynatraceAPI(config["dynatrace_base_url"],
                     config["dynatrace_token"],
                     logger=app.logger)

    date_from = request.args.get("from", None)
    date_to = request.args.get("to", None)
    next_page_key = request.args.get("nextPageKey", None)
    page_size = request.args.get("pageSize", 100000)
    resolution = request.args.get("resolution", None)
    scope = request.args.get("scope", None)
    entitySelector = request.args.get("entitySelector", None)

    custom_time = request.args.get("customTime", None)
    if custom_time is not None:
        date_from, date_to = build_custom_time(custom_time)

    data = d.metrics_series(selector,
                            date_from=date_from,
                            date_to=date_to,
                            next_page_key=next_page_key,
                            page_size=page_size,
                            resolution=resolution,
                            scope=scope,
                            entitySelector=entitySelector)

    if "error" in data:
        return make_response(data, data["error"]["code"])

    # app.logger.debug(f"{data}")

    data_response = OrderedDict({
        "totalCount": 0,
        "nextPageKey": None,
        "metrics": {}
    })
    data_response["totalCount"] = data["totalCount"]
    data_response["nextPageKey"] = data["nextPageKey"]
    data_response["metrics"] = data["result"]

    lines = json_to_csv(data)
    return csv_download(lines)
Ejemplo n.º 2
0
def timeseries(identifier):
    with open(f"{current_file_path}/config.json", "r") as f:
        config = json.load(f)
    d = DynatraceAPI(config["dynatrace_base_url"],
                     config["dynatrace_token"],
                     logger=app.logger)

    date_from = request.args.get("startTimestamp", None)
    date_to = request.args.get("endTimestamp", None)
    predict = request.args.get("predict", False)
    aggregation = request.args.get("aggregation", None)
    query_mode = request.args.get("queryMode", "SERIES")
    entities = request.args.getlist("entity", None)
    tag = request.args.get("tag", None)
    percentile = request.args.get("percentile", None)
    include_parents_ids = request.args.get("includeParentIds", False)
    consider_maintenance = request.args.get(
        "considerMaintenanceWindowsForAvailability", False)

    custom_time = request.args.get("customTime", None)
    if custom_time is not None:
        date_from, date_to = build_custom_time(custom_time)

    data = d.timeseries(
        identifier,
        include_data=True,
        aggregation=aggregation,
        start_timestamp=date_from,
        end_timestamp=date_to,
        predict=predict,
        query_mode=query_mode,
        entities=entities,
        tag=tag,
        percentile=percentile,
        include_parents_ids=include_parents_ids,
        consider_maintenance=consider_maintenance,
    )

    if "error" in data:
        return make_response(data, data["error"]["code"])

    lines = json_to_csv(data)
    return csv_download(lines)
Ejemplo n.º 3
0
class PortExtension(RemoteBasePlugin):
    def initialize(self, **kwargs):
        # The Dynatrace API client
        self.client = DynatraceAPI(self.config.get("api_url"),
                                   self.config.get("api_token"),
                                   log=log)
        self.executions = 0

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

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

        name = self.config.get("test_name")
        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"
        frequency = int(self.config.get("frequency")) if self.config.get(
            "frequency") else 1

        if self.executions % frequency == 0:
            for port in target_ports:
                if port:
                    success, response_time = test_port(target_ip, int(port))
                    test_name = f"{name} {target_ip}:{port}"
                    log.info(
                        f"{target_ip}:{port} = {success}, {response_time}")

                    self.client.report_simple_test(test_name,
                                                   location,
                                                   success,
                                                   response_time,
                                                   test_type="Port",
                                                   interval=frequency * 60)

                    if not success:
                        self.client.report_simple_event(
                            test_name,
                            f"Port check failed for {name}, target: {target_ip}:{port}",
                            location,
                            state="open",
                            test_type="Port",
                        )
                    else:
                        self.client.report_simple_event(
                            test_name,
                            f"Port check failed for {name}, target: {target_ip}:{port}",
                            location,
                            state="resolved",
                            test_type="Port",
                        )

        self.executions += 1
Ejemplo n.º 4
0
class DNSExtension(RemoteBasePlugin):
    def initialize(self, **kwargs):
        # The Dynatrace API client
        self.client = DynatraceAPI(self.config.get("api_url"),
                                   self.config.get("api_token"),
                                   log=log)
        self.executions = 0

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

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

        name = self.config.get("test_name")
        dns_server = self.config.get("dns_server")
        host = self.config.get("host")
        location = self.config.get("test_location") if self.config.get(
            "test_location") else "ActiveGate"
        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.client.report_simple_test(
                name,
                location,
                success,
                response_time,
                test_type="DNS",
                interval=frequency * 60,
                edit_link=
                f"#settings/customextension;id={self.plugin_info.name}",
            )

            if not success:
                self.client.report_simple_event(
                    name,
                    f"DNS lookup failed for {name}, server: {dns_server}, host: {host}",
                    location)
            else:
                self.client.report_simple_event(
                    name,
                    f"DNS lookup failed for {name}, server: {dns_server}, host: {host}",
                    location,
                    state="resolved")

        self.executions += 1
Ejemplo n.º 5
0
class PingExtension(RemoteBasePlugin):
    def initialize(self, **kwargs):
        # The Dynatrace API client
        self.client = DynatraceAPI(self.config.get("api_url"),
                                   self.config.get("api_token"),
                                   log=log)
        self.executions = 0

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

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

        name = self.config.get("test_name")
        target = self.config.get("test_target")
        location = self.config.get(
            "test_location",
            "") if self.config.get("test_location") else "ActiveGate"
        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())

            self.client.report_simple_test(
                name,
                location,
                ping_result.packet_loss_rate is not None
                and ping_result.packet_loss_rate == 0,
                ping_result.rtt_avg or 0,
                interval=frequency * 60,
                edit_link=
                f"#settings/customextension;id={self.plugin_info.name}",
            )

            if ping_result.packet_loss_rate is None or ping_result.packet_loss_rate > 0:
                self.client.report_simple_event(
                    name, f"Ping failed for {name}, target: {target}",
                    location)
            else:
                self.client.report_simple_event(
                    name,
                    f"Ping failed for {name}, target: {target}",
                    location,
                    state="resolved")

        self.executions += 1
Ejemplo n.º 6
0
 def initialize(self, **kwargs):
     # The Dynatrace API client
     self.client = DynatraceAPI(self.config.get("api_url"),
                                self.config.get("api_token"),
                                log=log)
     self.executions = 0