Exemple #1
0
    def from_config(cls, name: str, config: dict, io: IO) -> 'ConfiguredCheck':
        quiet_periods = config.get('quiet_periods', [])

        if not isinstance(quiet_periods, list):
            raise ConfigurationException.from_quiet_periods_should_be_a_list_error(
            )

        if quiet_periods:
            for period in quiet_periods:
                period: dict
                if "starts" not in period or "duration" not in period:
                    raise ConfigurationException.from_quiet_periods_invalid_structure(
                    )

        # cache life time is disabled
        if "results_cache_time" not in config or not config.get(
                'results_cache_time'):
            io.debug('results_cache_time not configured for {}'.format(name))

        return cls(name=name,
                   check_type=config.get('type'),
                   description=config.get('description', ''),
                   input_variables=config.get('input', {}),
                   hooks=config.get('hooks', {}),
                   quiet_periods=quiet_periods,
                   results_cache_time=int(config.get('results_cache_time'))
                   if "results_cache_time" in config else None,
                   io=io)
Exemple #2
0
    def _notify_hooks(hooks: dict, exit_status: bool, io: IO) -> str:
        mapping = {True: 'on_each_up', False: 'on_each_down'}

        out = ""

        if exit_status in mapping and mapping[exit_status] in hooks:
            commands = hooks[mapping[exit_status]]

            if type(commands).__name__ != 'list':
                raise RunnerException.from_expected_list_of_hooks(
                    mapping[exit_status])

            for command in commands:
                io.debug('Triggering hook command "{}"'.format(command))

                try:
                    out += subprocess.check_output(
                        command, shell=True,
                        timeout=1800).decode('utf-8').strip()

                except subprocess.CalledProcessError as e:
                    io.error(
                        'Cannot execute hook command "{cmd}". Error: {err}'.
                        format(cmd=command, err=str(e.output) + str(e.stderr)))
                except subprocess.TimeoutExpired:
                    io.error(
                        'Cannot execute hook command "{cmd}. Timed out while executing command"'
                        .format(cmd=command))
                except Exception:
                    io.error(
                        'Cannot execute hook command "{cmd}. Unknown error"'.
                        format(cmd=command))

        return out