Exemplo n.º 1
0
def do_compress_history(args: List[str]) -> None:
    if not args:
        raise MKBailOut("Please specify files to compress.")

    for filename in args:
        try:
            logger.log(VERBOSE, "%s...", filename)
            compress_history_file(filename, filename + ".compressed")
        except Exception as e:
            if cmk.utils.debug.enabled():
                raise
            raise MKBailOut("%s" % e)
Exemplo n.º 2
0
    def parse_hostname_list(
            self,
            args: List[str],
            with_clusters: bool = True,
            with_foreign_hosts: bool = False) -> List[HostName]:
        config_cache = config.get_config_cache()
        if with_foreign_hosts:
            valid_hosts = config_cache.all_configured_realhosts()
        else:
            valid_hosts = config_cache.all_active_realhosts()

        if with_clusters:
            valid_hosts = valid_hosts.union(config_cache.all_active_clusters())

        hostlist = []
        for arg in args:
            if arg[0] != '@' and arg in valid_hosts:
                hostlist.append(arg)
            else:
                if arg[0] == '@':
                    arg = arg[1:]
                tagspec = arg.split(',')

                num_found = 0
                for hostname in valid_hosts:
                    if config.hosttags_match_taglist(
                            config_cache.tag_list_of_host(hostname), tagspec):
                        hostlist.append(hostname)
                        num_found += 1
                if num_found == 0:
                    raise MKBailOut("Hostname or tag specification '%s' does "
                                    "not match any host." % arg)
        return hostlist
Exemplo n.º 3
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)
Exemplo n.º 4
0
def do_snmpwalk(options, hostnames):
    if "oids" in options and "extraoids" in options:
        raise MKGeneralException("You cannot specify --oid and --extraoid at the same time.")

    if not hostnames:
        raise MKBailOut("Please specify host names to walk on.")

    if not os.path.exists(cmk.utils.paths.snmpwalks_dir):
        os.makedirs(cmk.utils.paths.snmpwalks_dir)

    for hostname in hostnames:
        #TODO: What about SNMP management boards?
        snmp_config = create_snmp_host_config(hostname)

        try:
            _do_snmpwalk_on(snmp_config, options, cmk.utils.paths.snmpwalks_dir + "/" + hostname)
        except Exception as e:
            console.error("Error walking %s: %s\n" % (hostname, e))
            if cmk.utils.debug.enabled():
                raise
        cmk_base.cleanup.cleanup_globals()
Exemplo n.º 5
0
def do_snmpget(*args):
    if not args[0]:
        raise MKBailOut("You need to specify an OID.")
    oid = args[0][0]

    config_cache = config.get_config_cache()

    hostnames = args[0][1:]
    if not hostnames:
        hostnames = []
        for host in config_cache.all_active_realhosts():
            host_config = config_cache.get_host_config(host)
            if host_config.is_snmp_host:
                hostnames.append(host)

    for hostname in hostnames:
        #TODO what about SNMP management boards?
        snmp_config = create_snmp_host_config(hostname)

        value = get_single_oid(snmp_config, oid)
        console.output("%s (%s): %r\n" % (hostname, snmp_config.ipaddress, value))
        cmk_base.cleanup.cleanup_globals()
Exemplo n.º 6
0
def activation_lock(mode: Optional[str]) -> Iterator[None]:
    """Try to acquire the activation lock and raise exception in case it was not possible"""
    if mode is None:
        # TODO: We really should purge this strange case from being configurable
        yield None  # No locking at all
        return

    lock_file = cmk.utils.paths.default_config_dir + "/main.mk"

    if mode == "abort":
        with store.try_locked(lock_file) as result:
            if result is False:
                raise MKBailOut("Other restart currently in progress. Aborting.")
            yield None
        return

    if mode == "wait":
        with store.locked(lock_file):
            yield None
        return

    raise ValueError(f"Invalid lock mode: {mode}")