Exemple #1
0
def _save_auth_serials(updated_profiles: Users) -> None:
    """Write out the users serials"""
    # Write out the users serials
    serials = u""
    for user_id, user in updated_profiles.items():
        serials += u'%s:%d\n' % (user_id, user.get('serial', 0))
    store.save_file('%s/auth.serials' % os.path.dirname(cmk.utils.paths.htpasswd_file), serials)
    def _fetch_agent_output(self, job_interface):
        job_interface.send_progress_update(
            _("Fetching '%s'...") % self._request.agent_type)

        success, output, agent_data = watolib.check_mk_automation(
            self._request.host.site_id(), "get-agent-output",
            [self._request.host.name(), self._request.agent_type])

        if not success:
            job_interface.send_progress_update(_("Failed: %s") % output)

        preview_filepath = os.path.join(
            job_interface.get_work_dir(),
            AgentOutputPage.file_name(self._request))
        store.save_file(preview_filepath, agent_data)

        download_url = makeuri_contextless(
            request,
            [("host", self._request.host.name()),
             ("type", self._request.agent_type)],
            filename="download_agent_output.py",
        )

        button = html.render_icon_button(download_url, _("Download"),
                                         "agent_output")
        job_interface.send_progress_update(
            _("Finished. Click on the icon to download the data."))
        job_interface.send_result_message(_("%s Finished.") % button)
Exemple #3
0
def _create_nagvis_backends(sites_config):
    cfg = [
        '; MANAGED BY CHECK_MK WATO - Last Update: %s' % time.strftime('%Y-%m-%d %H:%M:%S'),
    ]
    for site_id, site in sites_config.items():
        if site == config.omd_site():
            continue  # skip local site, backend already added by omd

        socket = _encode_socket_for_nagvis(site_id, site)

        cfg += [
            '',
            '[backend_%s]' % site_id,
            'backendtype="mklivestatus"',
            'socket="%s"' % socket,
        ]

        if site.get("status_host"):
            cfg.append('statushost="%s"' % ':'.join(site['status_host']))

        if site["proxy"] is None and is_livestatus_encrypted(site):
            address_spec = site["socket"][1]
            tls_settings = address_spec["tls"][1]
            cfg.append('verify_tls_peer=%d' % tls_settings["verify"])
            cfg.append('verify_tls_ca_path=%s' % ConfigDomainCACertificates.trusted_cas_file)

    store.save_file('%s/etc/nagvis/conf.d/cmk_backends.ini.php' % cmk.utils.paths.omd_root,
                    '\n'.join(cfg))
Exemple #4
0
 def save_to(self, path, filename, pretty=False):
     filepath = "%s/%s" % (path, filename)
     output = self.get_raw_tree()
     store.save_object_to_file(filepath, output, pretty=pretty)
     gzip.open(filepath + ".gz", "w").write(repr(output) + "\n")
     # Inform Livestatus about the latest inventory update
     store.save_file("%s/.last" % path, "")
Exemple #5
0
def _delete_distributed_wato_file():
    p = cmk.utils.paths.check_mk_config_dir + "/distributed_wato.mk"
    # We do not delete the file but empty it. That way
    # we do not need write permissions to the conf.d
    # directory!
    if os.path.exists(p):
        store.save_file(p, "")
Exemple #6
0
    def _write_cache_file(self, raw_data):
        # type: (BoundedAbstractRawData) -> None
        if self.is_agent_cache_disabled():
            self._logger.debug(
                "Not writing data to cache file (Cache usage disabled)")
            return

        cachefile = self._cache_file_path()

        try:
            try:
                os.makedirs(os.path.dirname(cachefile))
            except OSError as e:
                if e.errno == errno.EEXIST:
                    pass
                else:
                    raise
        except Exception as e:
            raise MKGeneralException("Cannot create directory %r: %s" %
                                     (os.path.dirname(cachefile), e))

        self._logger.debug("Write data to cache file %s" % (cachefile))
        try:
            store.save_file(cachefile, self._to_cache_file(raw_data))
        except Exception as e:
            raise MKGeneralException("Cannot write cache file %s: %s" %
                                     (cachefile, e))
Exemple #7
0
def export_rule_pack(rule_pack, pretty_print=False, dir_=None):
    # type: (Dict[str, Any], bool, Optional[Path]) -> None
    """
    Export the representation of a rule pack (i.e. a dict) to a .mk
    file accessible by the WATO module Extension Packages. In case
    of a MkpRulePackProxy the representation of the underlying rule
    pack is used.
    The name of the .mk file is determined by the ID of the rule pack,
    i.e. the rule pack 'test' will be saved as 'test.mk'
    By default the rule pack is saved to the default directory for
    mkp rule packs. If dir_ is given the default is replaced by the
    directory dir_.
    """
    if isinstance(rule_pack, MkpRulePackProxy):
        rule_pack = rule_pack.rule_pack

    repr_ = (pprint.pformat(rule_pack) if pretty_print else repr(rule_pack))
    output = ("# Written by WATO\n"
              "# encoding: utf-8\n"
              "\n"
              "mkp_rule_packs['%s'] = \\\n"
              "%s\n") % (rule_pack['id'], repr_)

    if not dir_:
        dir_ = mkp_rule_pack_dir()
    dir_.mkdir(parents=True, exist_ok=True)
    store.save_file(str(dir_ / ("%s.mk" % rule_pack['id'])), make_utf8(output))
Exemple #8
0
def _save_user_profiles(updated_profiles: Users) -> None:
    non_contact_keys = _non_contact_keys()
    multisite_keys = _multisite_keys()

    for user_id, user in updated_profiles.items():
        user_dir = cmk.utils.paths.var_dir + "/web/" + ensure_str(user_id)
        store.mkdir(user_dir)

        # authentication secret for local processes
        auth_file = user_dir + "/automation.secret"
        if "automation_secret" in user:
            store.save_file(auth_file, "%s\n" % user["automation_secret"])
        elif os.path.exists(auth_file):
            os.unlink(auth_file)

        # Write out user attributes which are written to dedicated files in the user
        # profile directory. The primary reason to have separate files, is to reduce
        # the amount of data to be loaded during regular page processing
        save_custom_attr(user_id, 'serial', str(user.get('serial', 0)))
        save_custom_attr(user_id, 'num_failed_logins', str(user.get('num_failed_logins', 0)))
        save_custom_attr(user_id, 'enforce_pw_change', str(int(user.get('enforce_pw_change',
                                                                        False))))
        save_custom_attr(user_id, 'last_pw_change', str(user.get('last_pw_change',
                                                                 int(time.time()))))

        if "idle_timeout" in user:
            save_custom_attr(user_id, "idle_timeout", user["idle_timeout"])
        else:
            remove_custom_attr(user_id, "idle_timeout")

        _save_cached_profile(user_id, user, multisite_keys, non_contact_keys)
Exemple #9
0
def save_new_config():
    content = "# Written by RRD migration script (%s)\n\n" % time.strftime("%Y-%m-%d %H:%M:%S")
    content += "df_use_fs_used_as_metric_name = True\n"

    store.save_file(os.path.join(cmk.utils.paths.omd_root, 'etc/check_mk/conf.d/fs_cap.mk'),
                    content)

    logger.info(subprocess.check_output(split('cmk -U')))
Exemple #10
0
    def save(self, settings, site_specific=False):
        filename = self.config_file(site_specific)

        output = wato_fileheader()
        for varname, value in settings.items():
            output += "%s = %s\n" % (varname, pprint.pformat(value))

        store.makedirs(os.path.dirname(filename))
        store.save_file(filename, output)
Exemple #11
0
def save_custom_attrs_to_mk_file(attrs):
    output = watolib.wato_fileheader()
    for what in ["user", "host"]:
        if what in attrs and len(attrs[what]) > 0:
            output += "if type(wato_%s_attrs) != list:\n    wato_%s_attrs = []\n" % (what, what)
            output += "wato_%s_attrs += %s\n\n" % (what, pprint.pformat(attrs[what]))

    store.mkdir(watolib.multisite_dir())
    store.save_file(watolib.multisite_dir() + "custom_attrs.mk", output)
Exemple #12
0
def create_distributed_wato_file(siteid, is_slave):
    output = wato_fileheader()
    output += ("# This file has been created by the master site\n"
               "# push the configuration to us. It makes sure that\n"
               "# we only monitor hosts that are assigned to our site.\n\n")
    output += "distributed_wato_site = '%s'\n" % siteid
    output += "is_wato_slave_site = %r\n" % is_slave

    store.save_file(cmk.utils.paths.check_mk_config_dir + "/distributed_wato.mk", output)
    def _write_config_file(self):
        config = self._get_effective_config()

        output = wato_fileheader()
        for key, val in sorted(config.get("rrdcached_tuning", {}).items()):
            output += "%s=%d\n" % (key, val)

        config_file_path = os.path.join(cmk.utils.paths.omd_root,
                                        "etc/rrdcached.d", "zzz_check_mk.conf")
        store.save_file(config_file_path, output)
Exemple #14
0
    def save(self, crash):
        # type: (ABCCrashReport) -> None
        """Save the crash report instance to it's crash report directory"""
        self._prepare_crash_dump_directory(crash)

        for key, value in crash.serialize().items():
            fname = "crash.info" if key == "crash_info" else key
            store.save_file(str(crash.crash_dir() / fname),
                            json.dumps(value, cls=RobustJSONEncoder) + "\n")

        self._cleanup_old_crashes(crash.crash_dir().parent)
Exemple #15
0
    def _update_trusted_cas(self, current_config):
        trusted_cas, errors = [], []

        if current_config["use_system_wide_cas"]:
            trusted, errors = self._get_system_wide_trusted_ca_certificates()
            trusted_cas += trusted

        trusted_cas += current_config["trusted_cas"]

        store.save_file(self.trusted_cas_file, "\n".join(trusted_cas))
        return errors
Exemple #16
0
    def save_to(self, path, filename, pretty=False):
        filepath = "%s/%s" % (path, filename)
        output = self.get_raw_tree()
        store.save_object_to_file(filepath, output, pretty=pretty)

        buf = io.BytesIO()
        with gzip.GzipFile(fileobj=buf, mode="wb") as f:
            f.write((repr(output) + "\n").encode("utf-8"))
        store.save_file(filepath + ".gz", buf.getvalue())

        # Inform Livestatus about the latest inventory update
        store.save_text_to_file("%s/.last" % path, u"")
Exemple #17
0
def save_autochecks_file(
    hostname: HostName,
    services: Sequence[Service],
) -> None:
    path = _autochecks_path_for(hostname)
    path.parent.mkdir(parents=True, exist_ok=True)
    content = []
    content.append("[")
    for service in sorted(services):
        content.append("  %s," % service.dump_autocheck())
    content.append("]\n")
    store.save_file(path, "\n".join(content))
Exemple #18
0
    def _write_config_file(self):
        config = self.get_effective_config()

        output = wato_fileheader()

        if config:
            output += "ServerLimit %d\n" % config["apache_process_tuning"]["number_of_processes"]
            output += "MaxClients %d\n" % config["apache_process_tuning"]["number_of_processes"]

        config_file_path = os.path.join(cmk.utils.paths.omd_root, "etc/apache/conf.d",
                                        "zzz_check_mk.conf")
        store.save_file(config_file_path, output)
Exemple #19
0
    def save(self, settings, site_specific=False, custom_site_path=None):
        filename = self.config_file(site_specific)
        if custom_site_path:
            filename = os.path.join(custom_site_path,
                                    os.path.relpath(filename, cmk.utils.paths.omd_root))

        output = wato_fileheader()
        for varname, value in settings.items():
            output += "%s = %s\n" % (varname, pprint.pformat(value))

        store.makedirs(os.path.dirname(filename))
        store.save_file(filename, output)
Exemple #20
0
    def save_config(self) -> None:
        store.save_file(self._bi_configuration_file,
                        repr(self.generate_config()))
        enabled_aggregations = str(
            len([
                bi_aggr for bi_aggr in self.get_all_aggregations()
                if not bi_aggr.computation_options.disabled
            ]))

        store.makedirs(self._num_enabled_aggregations_dir())
        store.save_file(self._num_enabled_aggregations_path(),
                        enabled_aggregations)
def save_autochecks_file(hostname, items):
    # type: (HostName, List[DiscoveredService]) -> None
    path = _autochecks_path_for(hostname)
    path.parent.mkdir(parents=True, exist_ok=True)
    content = []
    content.append("[")
    for discovered_service in sorted(items, key=lambda s: (s.check_plugin_name, s.item)):
        content.append(
            "  {'check_plugin_name': %r, 'item': %r, 'parameters': %s, 'service_labels': %r}," %
            (discovered_service.check_plugin_name, discovered_service.item,
             discovered_service.parameters_unresolved, discovered_service.service_labels.to_dict()))
    content.append("]\n")
    store.save_file(path, "\n".join(content))
Exemple #22
0
    def save_config(self) -> None:
        store.save_file(self._bi_configuration_file,
                        repr(self.generate_config()))
        enabled_aggregations = str(
            len([
                bi_aggr for bi_aggr in self.get_all_aggregations()
                if not bi_aggr.computation_options.disabled
            ]))

        enabled_info_path = os.path.join(cmk.utils.paths.var_dir, "wato")
        store.makedirs(enabled_info_path)
        store.save_file(
            os.path.join(enabled_info_path, "num_enabled_aggregations"),
            enabled_aggregations)
Exemple #23
0
    def write(self, raw_data: TRawData) -> None:
        if self.disabled:
            self._logger.debug("Not writing data to cache file (Cache usage disabled)")
            return

        try:
            self.path.parent.mkdir(parents=True, exist_ok=True)
        except Exception as e:
            raise MKGeneralException("Cannot create directory %r: %s" % (self.path.parent, e))

        self._logger.debug("Write data to cache file %s", self.path)
        try:
            store.save_file(self.path, self._to_cache_file(raw_data))
        except Exception as e:
            raise MKGeneralException("Cannot write cache file %s: %s" % (self.path, e))
Exemple #24
0
def save_autochecks_file(hostname, items):
    # type: (HostName, List[DiscoveredService]) -> None
    if not os.path.exists(cmk.utils.paths.autochecks_dir):
        os.makedirs(cmk.utils.paths.autochecks_dir)

    filepath = Path(cmk.utils.paths.autochecks_dir) / ("%s.mk" % hostname)
    content = []
    content.append("[")
    for discovered_service in sorted(items, key=lambda s: (s.check_plugin_name, s.item)):
        content.append(
            "  {'check_plugin_name': %r, 'item': %r, 'parameters': %s, 'service_labels': %r}," %
            (discovered_service.check_plugin_name, discovered_service.item,
             discovered_service.parameters_unresolved, discovered_service.service_labels.to_dict()))
    content.append("]\n")
    store.save_file(str(filepath), "\n".join(content))
Exemple #25
0
    def write(self, raw_data: TRawData, mode: Mode) -> None:
        if not self._do_cache(mode):
            return

        path = self.make_path(mode)
        try:
            path.parent.mkdir(parents=True, exist_ok=True)
        except Exception as e:
            raise MKGeneralException("Cannot create directory %r: %s" %
                                     (path.parent, e))

        self._logger.debug("Write data to cache file %s", path)
        try:
            _store.save_file(path, self._to_cache_file(raw_data))
        except Exception as e:
            raise MKGeneralException("Cannot write cache file %s: %s" %
                                     (path, e))
Exemple #26
0
def save_rule_packs(rule_packs, pretty_print=False, dir_=None):
    # type: (List[Dict[str, Any]], bool, Optional[Path]) -> None
    """Saves the given rule packs to rules.mk. By default they are saved to the
    default directory for rule packs. If dir_ is given it is used instead of
    the default."""
    output = "# Written by WATO\n# encoding: utf-8\n\n"

    if pretty_print:
        rule_packs_text = pprint.pformat(rule_packs)
    else:
        rule_packs_text = repr(rule_packs)

    output += "rule_packs += \\\n%s\n" % rule_packs_text

    if not dir_:
        dir_ = rule_pack_dir()
    dir_.mkdir(parents=True, exist_ok=True)
    store.save_file(str(dir_ / "rules.mk"), make_utf8(output))
Exemple #27
0
def store_piggyback_raw_data(source_host, piggybacked_raw_data):
    piggyback_file_paths = []
    for piggybacked_host, lines in piggybacked_raw_data.items():
        piggyback_file_path = str(cmk.utils.paths.piggyback_dir / piggybacked_host / source_host)
        console.verbose("Storing piggyback data for: %s\n" % piggybacked_host)
        content = "\n".join(lines) + "\n"
        store.save_file(piggyback_file_path, content)
        piggyback_file_paths.append(piggyback_file_path)

    # Store the last contact with this piggyback source to be able to filter outdated data later
    # We use the mtime of this file later for comparison.
    # Only do this for hosts that sent piggyback data this turn, cleanup the status file when no
    # piggyback data was sent this turn.
    if piggybacked_raw_data:
        status_file_path = _piggyback_source_status_path(source_host)
        _store_status_file_of(status_file_path, piggyback_file_paths)
    else:
        remove_source_status_file(source_host)
Exemple #28
0
    def _upload_csv_file(self):
        store.makedirs(self._upload_tmp_path)

        self._cleanup_old_files()

        upload_info = self._vs_upload().from_html_vars("_upload")
        self._vs_upload().validate_value(upload_info, "_upload")
        _file_name, _mime_type, content = upload_info["file"]

        file_id = "%s-%d" % (config.user.id, int(time.time()))

        store.save_file(self._file_path(), content.encode("utf-8"))

        # make selections available to next page
        html.request.set_var("file_id", file_id)

        if upload_info["do_service_detection"]:
            html.request.set_var("do_service_detection", "1")
Exemple #29
0
def save_group_information(all_groups, custom_default_config_dir=None):
    # Split groups data into Check_MK/Multisite parts
    check_mk_groups = {}
    multisite_groups = {}

    if custom_default_config_dir:
        check_mk_config_dir = "%s/conf.d/wato" % custom_default_config_dir
        multisite_config_dir = "%s/multisite.d/wato" % custom_default_config_dir
    else:
        check_mk_config_dir = "%s/conf.d/wato" % cmk.utils.paths.default_config_dir
        multisite_config_dir = "%s/multisite.d/wato" % cmk.utils.paths.default_config_dir

    for what, groups in all_groups.items():
        check_mk_groups[what] = {}
        for gid, group in groups.items():
            check_mk_groups[what][gid] = group['alias']

            for attr, value in group.items():
                if attr != 'alias':
                    multisite_groups.setdefault(what, {})
                    multisite_groups[what].setdefault(gid, {})
                    multisite_groups[what][gid][attr] = value

    # Save Check_MK world related parts
    store.makedirs(check_mk_config_dir)
    output = wato_fileheader()
    for what in ["host", "service", "contact"]:
        if check_mk_groups.get(what):
            output += "if type(define_%sgroups) != dict:\n    define_%sgroups = {}\n" % (
                what, what)
            output += "define_%sgroups.update(%s)\n\n" % (
                what, format_config_value(check_mk_groups[what]))
    store.save_file("%s/groups.mk" % check_mk_config_dir, output)

    # Users with passwords for Multisite
    store.makedirs(multisite_config_dir)
    output = wato_fileheader()
    for what in ["host", "service", "contact"]:
        if multisite_groups.get(what):
            output += "multisite_%sgroups = \\\n%s\n\n" % (
                what, format_config_value(multisite_groups[what]))
    store.save_file("%s/groups.mk" % multisite_config_dir, output)

    _clear_group_information_request_cache()
    def save(self, settings, site_specific=False):
        config = {}

        if "diskspace_cleanup" in settings:
            # Convert to old config format.
            for k, v in settings.get("diskspace_cleanup", {}).items():
                if k == "min_free_bytes":
                    config["min_free_bytes"], config["min_file_age"] = v
                else:
                    config[k] = v

            if "cleanup_abandoned_host_files" not in settings.get("diskspace_cleanup", {}):
                config["cleanup_abandoned_host_files"] = None

        output = ""
        for k, v in sorted(config.items()):
            output += '%s = %r\n' % (k, v)

        store.save_file(self.diskspace_config, output)