Ejemplo n.º 1
0
def get_report_data(options):
    """ report of probes and their status """
    now = time.time()
    cfg = get_config(options=options)
    state = cfg.get_state()

    probes = cfg.get_probes()
    probe_state = state.state['probe_state']
    probes = sorted(probes, key=lambda prb: (prb['host'], prb['name']))
    for probe in probes:
        host = probe['host']
        probe_id = probe['name']
        probe_type = probe['probe']
        states = probe_state[probe_id]
        prev_state = None
        prev_info = None
        history = []
        t_probe = now + 1
        for t_probe, state, info in states[::1]:
            if state != prev_state or info != prev_info:
                age = int(now - t_probe)
                history.append((delta_sec_to_str(age), state, info))
                prev_state = state
                prev_info = info
        age = int(now - t_probe)
        yield dict(
            probe=probe,
            host=host,
            probe_type=probe_type,
            history=history,
            age=delta_sec_to_str(age) if age >= 0 else "?",
        )
Ejemplo n.º 2
0
 def __init__(self, **kwargs):
     # cls = self.__class__
     host_id = kwargs.pop('host', None)
     hostcfg = get_config().cfg['hosts'][host_id]
     super().__init__(**kwargs)
     host_str = "%s:%s" % (
         hostcfg.get("hostname"),
         hostcfg.get("port", "443")
         )
     self.cmd.append(host_str)
Ejemplo n.º 3
0
def run(options):
    # IMPLEMENT PARAMS/OPTIONS or remove doc string
    """ runs timon
        :param config: tmon config
        :param init: if True all probes will fire
    """
    print("run tmon")
    print(options)
    if options.shell_loop:
        exec_shell_loop(sys.argv[1:], options.loop_delay)

    t0 = time.time()
    #print("OPTS:", options)
    cfg = get_config(options=options)
    #print("CFG: %r" % cfg)
    state = cfg.get_state()
    #print("state", state)
    queue = cfg.get_queue()
    print("IQ", queue)
    cfg.refresh_queue()  # add any not refreshed entries to queue

    #print("IR")
    from .runner import Runner

    # get all queue entries less than a certain time stamp (dflt=now)
    probes = queue.get_probes()

    if options.probe:
        to_call = set(options.probe)
        probes = []
        print("TO CALL", to_call)
        for prb in cfg.get_probes():
            if prb['name'] not in to_call:
                continue
            prb_dict = dict(
                t_next=t0 - 1,
                interval=100,
                failinterval=100,
                done_cb=None,
            )
            prb_dict.update(prb)
            print("prb", prb)
            prb = mk_probe(prb['cls'], **prb_dict)
            print("prb", prb)
            probes.append(prb)
        queue = None
    runner = Runner(probes, queue)
    runner.run()
    runner.close()

    cfg.save_state()
    t = time.time()
    print("t_tot =%.3f" % (t - t0))
    print("IQ", queue)
Ejemplo n.º 4
0
    def __init__(self, **kwargs):
        """
        :param host: timon host that shall be probed
        :param ca_rex: regular expression that shall match the CA string
                  with format C=../ST=../L=../O=../OU=..CN=../emailAddress=..

        host config vars:
            hostname: name of ssl host
            port: port to connect to SSL host
        """
        host_id = kwargs.pop('host', None)
        hostcfg = get_config().cfg['hosts'][host_id]
        ca_rex = kwargs.pop('ca_rex', ".")
        super().__init__(**kwargs)
        host_str = "%s:%s" % (
            hostcfg.get("hostname"),
            hostcfg.get("port", "443")
            )
        self.cmd.append(host_str)
        self.cmd.append(ca_rex)
Ejemplo n.º 5
0
    def __init__(self, **kwargs):
        """
        :param host: host name (as in config)
        :param verify_ssl: whether ssl server cert should be verified
        :param url_param: which probe param contains the relative url
        :param urlpath: default url path if urlparam not set

        also inherits params from SubProcBprobe except 'cmd', which
        will be overridden
        """
        cls = self.__class__
        host_id = kwargs.pop('host', None)
        hostcfg = get_config().cfg['hosts'][host_id]
        verify_ssl = kwargs.pop('verify_ssl', None)
        send_cert = kwargs.pop('send_cert', False)
        client_cert = hostcfg.get('client_cert', None)
        url_param = kwargs.pop('url_param', 'urlpath')
        if url_param != 'urlpath':
            kwargs.pop('urlpath', None)
        url_param_val = kwargs.pop(url_param, None)
        rel_url = hostcfg.get(url_param) or url_param_val or ""
        assert 'cmd' not in kwargs
        cmd = kwargs['cmd'] = [sys.executable, "-m", cls.script_module]
        super().__init__(**kwargs)

        # TODO: debug / understand param passing a little better
        # perhaps there's a more generic way of 'mixing' hostcfg / kwargs
        # print("HOSTCFG", hostcfg)

        hostname = hostcfg['hostname']
        proto = hostcfg['proto']
        port = hostcfg['port']
        self.url = url = "%s://%s:%d/%s" % (proto, hostname, port, rel_url)
        # print(url)
        cmd.append(url)
        if verify_ssl is not None:
            cmd.append('--verify_ssl=' + str(verify_ssl))
        if send_cert:
            cmd.append('--cert=' + client_cert[0])
            cmd.append('--key=' + client_cert[1])
Ejemplo n.º 6
0
    def __init__(self, **kwargs):
        """
            :param host: host name (as in config)
            also inherits params from SubProcBprobe
        """
        #print("KWARGS", kwargs)
        host_id = kwargs.get('host')
        hostcfg = get_config().cfg['hosts'][host_id]
        verify_ssl = kwargs.get('verify_ssl', None)
        client_cert = hostcfg['client_cert']
        #print("HOSTCFG", hostcfg)

        hostname = hostcfg['hostname']
        proto = hostcfg['proto']
        port = hostcfg['port']
        rel_url = hostcfg['urlpath']
        #print("MKURL", (proto, hostname, port, rel_url))
        self.url = url = "%s://%s:%d/%s" % (proto, hostname, port, rel_url)
        #print(url)
        cmd = kwargs['cmd'] = [sys.executable, "-m", "timon.scripts.isup", url]
        if verify_ssl is not None:
            cmd.append('--verify_ssl=' + str(verify_ssl))

        super().__init__(**kwargs)
Ejemplo n.º 7
0
async def run_once(options, loop=None, first=False, cfg=None):
    """ runs one probe iteration
        returns:
            (t_next, notifiers, loop)
            t_next: in how many seconds the next probe should be fired
            notifiers: list of notifiers, that were started
            loop: loop in which notofiers were started
    """

    t0 = time.time()  # now
    # logger.debug("OPTS:%s", str(options))
    cfg = cfg if cfg else get_config(options=options)
    # logger.debug("CFG: %r", str(cfg))
    # state = cfg.get_state()
    # print("state", state)
    queue = cfg.get_queue()
    logger.debug("IQ %s", str(queue))
    if first:
        cfg.refresh_queue()
        logger.debug("IR")

        # get all queue entries less than a certain time stamp (dflt=now)

    if options.probe:
        to_call = set(options.probe)
        probes = []
        logger.debug("TO CALL %s", str(to_call))
        for prb in cfg.get_probes():
            if prb['name'] not in to_call:
                continue
            prb_dict = dict(
                t_next=t0 - 1,
                interval=100,
                failinterval=100,
                done_cb=None,
            )
            prb_dict.update(prb)
            logger.debug("prb %s", str(prb))
            prb = mk_probe(prb['cls'], **prb_dict)
            logger.debug("prb %s", str(prb))
        queue = None
    else:
        logger.debug("pregetp (force=%s)", options.force)
        probes = queue.get_probes(force=options.force)
        probes = list(probes)
        logger.debug("%d probes: %s", len(probes), repr(probes))

    from timon.runner import Runner
    runner = Runner(probes, queue, loop=loop)
    t_next = await runner.run(force=options.probe)

    cfg.save_state()

    t = time.time()
    t_delta_next = max(t_next - t, 0)

    if runner.notifier_objs:
        for notifier in runner.notifier_objs:
            task = runner.loop.create_task(notifier.notify())
            runner.notifiers.append(task)
        return t_delta_next, runner.loop, runner.notifiers

    if not loop:
        runner.close()

    return max(t_next - t, 0), None, []
Ejemplo n.º 8
0
def run_once(options, loop=None, first=False):
    """ runs one probe iteration
        returns:
            (t_next, notifiers, loop)
            t_next: in how many seconds the next probe should be fired
            notifiers: list of notifiers, that were started
            loop: loop in which notofiers were started
    """

    t0 = time.time()  # now
    # print("OPTS:", options)
    cfg = get_config(options=options)
    # print("CFG: %r" % cfg)
    # state = cfg.get_state()
    # print("state", state)
    queue = cfg.get_queue()
    print("IQ", queue)
    if first:
        cfg.refresh_queue()
        print("IR")

        # get all queue entries less than a certain time stamp (dflt=now)

    if options.probe:
        to_call = set(options.probe)
        probes = []
        print("TO CALL", to_call)
        for prb in cfg.get_probes():
            if prb['name'] not in to_call:
                continue
            prb_dict = dict(
                t_next=t0 - 1,
                interval=100,
                failinterval=100,
                done_cb=None,
            )
            prb_dict.update(prb)
            print("prb", prb)
            prb = mk_probe(prb['cls'], **prb_dict)
            print("prb", prb)
            probes.append(prb)
        queue = None
    else:
        probes = queue.get_probes(force=options.force)
        probes = list(probes)
        logger.debug("probes: %s", repr(probes))

    from timon.runner import Runner
    runner = Runner(probes, queue, loop=loop)
    t_next = runner.run(force=options.probe)

    cfg.save_state()

    t = time.time()
    t_delta_next = max(t_next - t, 1)

    if runner.notifiers:
        return t_delta_next, runner.loop, runner.notifiers

    if not loop:
        runner.close()

    return max(t_next - t, 1), None, []
Ejemplo n.º 9
0
    def __init__(self, **kwargs):
        """
        :param host: host name (as in config)
        :param verify_ssl: whether ssl server cert should be verified
        - 2 ways to pass url (CAUTION: Use only 1 of 2):
        - PASS COMPLETE URL
            :param url: complete_url on which request should be performed to
            :param url_params: params to pass to url via % formatters
                            (Caution: order is important)
                    EXAMPLE:
                    Next params :
                    url: 'http://titi/%s/%s/croq/'
                    url_params:
                        - 'Hello'
                        - 'World'

                    Yields final url:
                    'http://titi/Hello/World/croq/'

        -PASS URL PARAMS
            :param url_param: which probe param contains the relative url
            :param urlpath: default url path if urlparam not set

        also inherits params from SubProcBprobe except 'cmd', which
        will be overridden
        """
        cls = self.__class__
        host_id = kwargs.pop('host', None)
        hostcfg = get_config().cfg['hosts'][host_id]
        verify_ssl = kwargs.pop('verify_ssl', None)
        send_cert = kwargs.pop('send_cert', False)
        client_cert = hostcfg.get('client_cert', None)
        base_url = kwargs.pop("url", None)
        if base_url:
            url_params_name = kwargs.pop('url_params', None)
            url_params = []
            if url_params_name:
                for param in url_params_name:
                    url_params.append(
                        minibelt.get(hostcfg, *param.split(".")) or param)

            complete_url = base_url % tuple(url_params)
            self.url = url = complete_url
        else:
            url_param = kwargs.pop('url_param', 'urlpath')
            if url_param != 'urlpath':
                kwargs.pop('urlpath', None)
            url_param_val = kwargs.pop(url_param, None)
            rel_url = hostcfg.get(url_param) or url_param_val or ""
            hostname = hostcfg['hostname']
            proto = hostcfg['proto']
            port = hostcfg['port']
            self.url = url = "%s://%s:%d/%s" % (proto, hostname, port, rel_url)
        assert 'cmd' not in kwargs
        cmd = kwargs['cmd'] = [sys.executable, "-m", cls.script_module]
        super().__init__(**kwargs)

        # TODO: debug / understand param passing a little better
        # perhaps there's a more generic way of 'mixing' hostcfg / kwargs
        # print("HOSTCFG", hostcfg)

        # print(url)
        cmd.append(url)
        if verify_ssl is not None:
            cmd.append('--verify_ssl=' + str(verify_ssl))
        if send_cert:
            cmd.append('--cert=' + client_cert[0])
            cmd.append('--key=' + client_cert[1])