Exemple #1
0
    def _list():
        """List all configured monitors"""
        zkclient = context.GLOBAL.zk.conn

        suspended_monitors = masterapi.get_suspended_appmonitors(zkclient)

        monitors = [
            masterapi.get_appmonitor(
                zkclient,
                app,
                suspended_monitors=suspended_monitors,
            ) for app in masterapi.appmonitors(zkclient)
        ]

        cli.out(formatter(monitors))
Exemple #2
0
        def _list(match=None):
            """List configured monitors."""
            if match is None:
                match = '*'

            zkclient = context.GLOBAL.zk.conn

            # get suspended monitors
            suspended_monitors = masterapi.get_suspended_appmonitors(zkclient)

            monitors = [
                masterapi.get_appmonitor(
                    zkclient,
                    app,
                    suspended_monitors=suspended_monitors,
                ) for app in masterapi.appmonitors(zkclient)
            ]

            filtered = [
                monitor for monitor in monitors
                if (monitor is not None
                    and fnmatch.fnmatch(monitor['_id'], match))
            ]
            return sorted(filtered, key=lambda item: item['_id'])
Exemple #3
0
def _run_sync(api_url, alerts_dir, once):
    """Sync app monitor count with instance count."""

    zkclient = context.GLOBAL.zk.conn

    alerter = make_alerter(alerts_dir, context.GLOBAL.cell)

    state = {
        'scheduled': {},
        'monitors': {},
        'suspended': {},
    }

    @zkclient.ChildrenWatch(z.path.scheduled())
    @utils.exit_on_unhandled
    def _scheduled_watch(children):
        """Watch scheduled instances."""
        scheduled = sorted(children)

        def _appname_fn(instancename):
            return instancename.rpartition('#')[0]

        grouped = collections.defaultdict(
            list,
            {k: list(v)
             for k, v in itertools.groupby(scheduled, _appname_fn)})
        state['scheduled'] = grouped
        return True

    def _watch_monitor(name):
        """Watch monitor."""

        # Establish data watch on each monitor.
        @zkwatchers.ExistingDataWatch(zkclient, z.path.appmonitor(name))
        @utils.exit_on_unhandled
        def _monitor_data_watch(data, stat, event):
            """Monitor individual monitor."""
            if (event is not None and event.type == 'DELETED') or stat is None:
                _LOGGER.info('Removing watch on deleted monitor: %s', name)
                return

            try:
                loaded = yaml.load(data)
                count = loaded['count']
                policy = loaded.get('policy')
            except Exception:  # pylint: disable=W0703
                _LOGGER.exception('Invalid monitor: %s', name)
                return

            _LOGGER.info('Reconfigure monitor: %s, count: %s', name, count)
            state['monitors'][name] = {
                'count': count,
                'available': 2.0 * count,
                'last_update': time.time(),
                'policy': policy,
                'rate': (2.0 * count / _INTERVAL)
            }

    @zkclient.ChildrenWatch(z.path.appmonitor())
    @utils.exit_on_unhandled
    def _appmonitors_watch(children):
        """Watch app monitors."""

        monitors = set(children)
        extra = six.viewkeys(state['monitors']) - monitors
        for name in extra:
            _LOGGER.info('Removing extra monitor: %r', name)
            if state['monitors'].pop(name, None) is None:
                _LOGGER.warning('Failed to remove non-existent monitor: %r',
                                name)

        missing = monitors - six.viewkeys(state['monitors'])

        for name in missing:
            _LOGGER.info('Adding missing monitor: %s', name)
            _watch_monitor(name)

    _LOGGER.info('Ready, loading waited app list')
    last_waited = masterapi.get_suspended_appmonitors(zkclient)
    while True:
        time.sleep(1)
        last_waited = reevaluate(api_url, alerter, state, zkclient,
                                 last_waited)
        if once:
            break