Ejemplo n.º 1
0
 def check_mk_alarm(self,
                    member,
                    warning_level,
                    hostname=None,
                    time_range="now() - 30s",
                    table="status",
                    reraise=True):
     Result = collections.namedtuple("Result",
                                     field_names=("status", "host",
                                                  "value"))
     filters = [
         "member = '{}'".format(member), "time >= {}".format(time_range)
     ]
     if hostname is not None:
         filters.append("hostname = '{}'".format(hostname))
     query = ("SELECT {{}} FROM {table} "
              "WHERE {filters}".format(table=table,
                                       filters=" and ".join(filters)))
     try:
         self._check_influx_query_last_value(query.format("last(value)"),
                                             warning_level)
         result = self.do_influxdb_query(
             query.format("hostname, last(value)")).json()['results'][0]
         host, value = result['series'][0]['values'][0][1:]
         return Result(True, host, value)
     except custom_exceptions.TimeoutError as e:
         if not reraise:
             return Result(False, None, None)
         raise custom_exceptions.TimeoutError(e)
Ejemplo n.º 2
0
def wait(predicate, interval=5, timeout=60, timeout_msg="Waiting timed out"):
    start_time = time.time()
    if not timeout:
        return predicate()
    while not predicate():
        if start_time + timeout < time.time():
            raise exceptions.TimeoutError(timeout_msg)

        seconds_to_sleep = max(
            0, min(interval, start_time + timeout - time.time()))
        time.sleep(seconds_to_sleep)

    return timeout + start_time - time.time()