Beispiel #1
0
def do_restart(core: MonitoringCore,
               action: CoreAction = CoreAction.RESTART) -> None:
    try:
        with activation_lock(mode=config.restart_locking):
            core_config.do_create_config(core)
            do_core_action(action)

    except Exception as e:
        if cmk.utils.debug.enabled():
            raise
        raise MKBailOut("An error occurred: %s" % e)
Beispiel #2
0
def test_do_create_config_nagios(core_scenario):
    core_config.do_create_config(create_core("nagios"))

    assert Path(cmk.utils.paths.nagios_objects_file).exists()
    assert config.PackedConfigStore(LATEST_SERIAL).path.exists()
Beispiel #3
0
def test_do_create_config_nagios(core_scenario, config_path):
    core_config.do_create_config(create_core("nagios"))

    assert Path(cmk.utils.paths.nagios_objects_file).exists()
    assert config.PackedConfigStore.from_serial(LATEST_CONFIG).path.exists()
Beispiel #4
0
def do_restart(core, only_reload=False):
    # type: (MonitoringCore, bool) -> None
    try:
        backup_path = None

        if try_get_activation_lock():
            # TODO: Replace by MKBailOut()/MKTerminate()?
            console.error("Other restart currently in progress. Aborting.\n")
            sys.exit(1)

        # Save current configuration
        if os.path.exists(cmk.utils.paths.nagios_objects_file):
            backup_path = cmk.utils.paths.nagios_objects_file + ".save"
            console.verbose("Renaming %s to %s\n",
                            cmk.utils.paths.nagios_objects_file,
                            backup_path,
                            stream=sys.stderr)
            os.rename(cmk.utils.paths.nagios_objects_file, backup_path)
        else:
            backup_path = None

        try:
            core_config.do_create_config(core, with_agents=True)
        except Exception as e:
            # TODO: Replace by MKBailOut()/MKTerminate()?
            console.error("Error creating configuration: %s\n" % e)
            if backup_path:
                os.rename(backup_path, cmk.utils.paths.nagios_objects_file)
            if cmk.utils.debug.enabled():
                raise
            sys.exit(1)

        if config.monitoring_core == "cmc" or cmk.base.nagios_utils.do_check_nagiosconfig(
        ):
            if backup_path:
                os.remove(backup_path)

            core.precompile()

            do_core_action(only_reload and "reload" or "restart")
        else:
            # TODO: Replace by MKBailOut()/MKTerminate()?
            console.error(
                "Configuration for monitoring core is invalid. Rolling back.\n"
            )

            broken_config_path = "%s/check_mk_objects.cfg.broken" % cmk.utils.paths.tmp_dir
            open(broken_config_path,
                 "w").write(open(cmk.utils.paths.nagios_objects_file).read())
            console.error(
                "The broken file has been copied to \"%s\" for analysis.\n" %
                broken_config_path)

            if backup_path:
                os.rename(backup_path, cmk.utils.paths.nagios_objects_file)
            else:
                os.remove(cmk.utils.paths.nagios_objects_file)
            sys.exit(1)

    except Exception as e:
        if backup_path:
            try:
                os.remove(backup_path)
            except OSError as oe:
                if oe.errno != errno.ENOENT:
                    raise
        if cmk.utils.debug.enabled():
            raise
        # TODO: Replace by MKBailOut()/MKTerminate()?
        console.error("An error occurred: %s\n" % e)
        sys.exit(1)