Ejemplo n.º 1
0
    def execute(self, parameters, name, start, end):
        client = Warp10Client(url=self.configuration["url"],
                              rotoken=self.configuration["token"])

        # Generate the WarpScript and
        # change the placeholders.
        client.generate_script(start=start.timestamp,
                               end=end.timestamp,
                               script=parameters["script"])

        try:
            response = client.execute()
        except Warp10Exception as e:
            try:
                message = html.unescape(
                    re.search("<pre>(.*)<\/pre>", str(e)).groups()[0].strip())
            except Exception:
                message = str(e)
            raise BadConfigurationException(
                "Warp10 Internal Error : {0}".format(message))

        # Transform the Warp10 values
        timeseries = []
        for ts in response[0]:
            timeseries.append({
                "dps": _transform_warp10_values(ts["v"]),
                "metric": ts["c"],
                "tags": ts["l"],
            })

        if not timeseries:
            msg = "No data for {} (from {} to {})".format(name, start, end)
            logger.warning(msg)
            raise UnknownStateException(msg)

        # Parse the threshold
        try:
            top_threshold = float(parameters["top_threshold"])
            bottom_threshold = float(parameters["bottom_threshold"])
        except ValueError:
            msg = "Thresholds are not valid (must be floats): {} and {}".format(
                parameters["top_threshold"], parameters["bottom_threshold"])
            raise BadConfigurationException(msg)

        # Compute the QoS
        try:
            result = compute_interval(
                timeseries,
                start.timestamp,
                end.timestamp,
                bottom_threshold,
                top_threshold,
            )
        except DataFetchException as e:
            raise UnknownStateException(e)

        result.update({"timeseries": timeseries})

        return result
Ejemplo n.º 2
0
    def build_query(self, name, start, end, parameters):
        try:
            query = {
                "start": int(start) - 1,
                "end": int(end),
                "queries": [json.loads(parameters["query"])],
            }
        except JSONDecodeError as e:
            raise BadConfigurationException(
                "Json Error in OpenTSDB query : {0}".format(str(e)))
        except ValueError:
            msg = "OpenTSDB Query is not valid : {}".format(
                parameters["query"])
            raise BadConfigurationException(msg)

        return query
Ejemplo n.º 3
0
    async def compute(self, parameters, name, start, end, query):
        logger.debug("Computing the QoS in {0} check".format(self.name))

        timeseries = await self.make_query(query)
        if not timeseries:
            msg = "No data for {} (from {} to {})".format(name, start, end)
            logger.warning(msg)
            raise UnknownStateException(msg)

        # Parse the threshold
        try:
            top_threshold = float(parameters["top_threshold"])
            bottom_threshold = float(parameters["bottom_threshold"])
        except ValueError:
            msg = "Thresholds are not valid (must be floats): {} and {}".format(
                parameters["top_threshold"], parameters["bottom_threshold"])
            raise BadConfigurationException(msg)

        # Compute the QoS
        try:
            result = compute_interval(
                timeseries,
                start.timestamp,
                end.timestamp,
                bottom_threshold,
                top_threshold,
            )
        except DataFetchException as e:
            raise UnknownStateException(e)

        result.update({"timeseries": timeseries})

        return result
Ejemplo n.º 4
0
    async def execute(self, parameters, name, start, end):
        try:
            query = {
                "start": start.timestamp,
                "end": end.timestamp,
                "queries": [json.loads(parameters["query"])],
            }
        except JSONDecodeError as e:
            raise BadConfigurationException(
                "Json Error in OpenTSDB query : {0}".format(str(e)))
        except ValueError:
            msg = "OpenTSDB Query is not valid : {}".format(
                parameters["query"])
            raise BadConfigurationException(msg)

        return await self.compute(parameters, name, start, end, query)
Ejemplo n.º 5
0
    async def run(self, parameters, name, start, end, auto_fill):
        timeseries = await self.execute(parameters, name, start, end)
        threshold = parameters["threshold"]

        # Handle a simple threshold
        if self.is_threshold(parameters["threshold"]):
            threshold = float(threshold)
            bool_per_ts = [
                pd.Series(ts["dps"]).apply(lambda x: x <= threshold)
                for ts in timeseries
            ]

        # Handle an interval
        elif self.is_interval(parameters["threshold"]):
            bottom = float(threshold.split(":")[0])
            top = float(threshold.split(":")[1])
            bool_per_ts = [
                pd.Series(ts["dps"]).apply(lambda x: bottom <= x <= top)
                for ts in timeseries
            ]
        else:
            raise BadConfigurationException("Bad threshold format : {}".format(
                parameters["threshold"]))

        result = compute_qos_from_bools(bool_per_ts,
                                        start,
                                        end,
                                        auto_fill=auto_fill)
        result.update({"timeseries": timeseries})

        return result
Ejemplo n.º 6
0
    async def execute(self, parameters, name, start, end):
        metric = parameters["metric"]
        start = start.timestamp
        end = end.timestamp

        # Our fake database just provides 3 metrics
        random_metrics_dispatcher = {
            "depc.tutorial.ping": self._generate_fake_ping_data,
            "depc.tutorial.oco": self._generate_fake_oco_status,
            "depc.tutorial.dbconnections": self._generate_fake_db_connections,
        }

        if metric not in random_metrics_dispatcher.keys():
            raise UnknownStateException(
                "Metric is not available for the tutorial")

        random_state = super().create_random_state(start, end, name)

        # Generate datapoints
        timestamps = list(map(int, np.arange(start, end, 60, dtype=int)))
        values = random_metrics_dispatcher[metric](random_state,
                                                   len(timestamps))
        dps = dict(zip(timestamps, values))

        timeseries = [{"dps": dps, "metric": metric, "tags": {"name": name}}]

        # Parse the threshold
        try:
            threshold = float(parameters["threshold"])
        except ValueError:
            msg = "Threshold is not valid (must be float): {}".format(
                parameters["threshold"])
            raise BadConfigurationException(msg)

        try:
            result = compute_threshold(timeseries, start, end, threshold)
        except DataFetchException as e:
            raise UnknownStateException(e)

        result.update({"timeseries": timeseries})

        return result
Ejemplo n.º 7
0
    def execute(self, parameters, name, start, end):
        metric = parameters["metric"]
        start = start.timestamp
        end = end.timestamp

        # Our fake database just provides one metric for the interval check
        if metric != "depc.tutorial.httpstatus":
            raise UnknownStateException("Metric is not available for the tutorial")

        random_state = super().create_random_state(start, end, name)

        # Generate datapoints
        timestamps = list(map(int, np.arange(start, end, 60, dtype=int)))
        values = self._generate_fake_http_status(random_state, len(timestamps))

        dps = dict(zip(timestamps, values))

        timeseries = [{"dps": dps, "metric": metric, "tags": {"name": name}}]

        # Parse the thresholds
        try:
            top_threshold = float(parameters["top_threshold"])
            bottom_threshold = float(parameters["bottom_threshold"])
        except ValueError:
            msg = "Thresholds are not valid (must be floats): {} and {}".format(
                parameters["top_threshold"], parameters["bottom_threshold"]
            )
            raise BadConfigurationException(msg)

        try:
            result = compute_interval(
                timeseries, start, end, bottom_threshold, top_threshold
            )
        except DataFetchException as e:
            raise UnknownStateException(e)

        result.update({"timeseries": timeseries})

        return result