Exemplo n.º 1
0
def list_rulesets(param):
    """Search rule sets"""
    user.need_permission("wato.rulesets")
    all_sets = FolderRulesets(
        param["folder"]) if param.get("folder") else AllRulesets()
    all_sets.load()

    def _get_search_options(params):
        # We remove 'folder' because that has already been handled at the start of the endpoint.
        options = dict(params)
        if "folder" in options:
            del options["folder"]
        return options

    if search_options := _get_search_options(param):
        all_sets = SearchedRulesets(all_sets, search_options)
Exemplo n.º 2
0
    def rename_host_in_folder_rules(folder):
        rulesets = FolderRulesets(folder)
        rulesets.load()

        changed_folder_rulesets = []
        for varname, ruleset in rulesets.get_rulesets().items():
            for _rule_folder, _rulenr, rule in ruleset.get_rules():
                orig_rule = rule.clone(preserve_id=True)
                if rule.replace_explicit_host_condition(oldname, newname):
                    changed_folder_rulesets.append(varname)

                    log_audit(
                        "edit-rule",
                        _('Renamed host condition from "%s" to "%s"') %
                        (oldname, newname),
                        diff_text=make_diff_text(orig_rule.to_log(),
                                                 rule.to_log()),
                        object_ref=rule.object_ref(),
                    )

        if changed_folder_rulesets:
            add_change(
                "edit-ruleset",
                _("Renamed host in %d rulesets of folder %s") %
                (len(changed_folder_rulesets), folder.title()),
                object_ref=folder.object_ref(),
                sites=folder.all_site_ids(),
            )
            rulesets.save()

        changed_rulesets.extend(changed_folder_rulesets)

        for subfolder in folder.subfolders():
            rename_host_in_folder_rules(subfolder)
Exemplo n.º 3
0
    def _rewrite_rules_for(self, conditions: RuleConditions) -> None:
        """Apply changed predefined condition to rules

        After updating a predefined condition it is necessary to rewrite the
        rules.mk the predefined condition refers to. Rules in this file may refer to
        the changed predefined condition. Since the conditions are only applied to the
        rules while saving them this step is needed.
        """
        folder = Folder.folder(conditions.host_folder)
        rulesets = FolderRulesets(folder)
        rulesets.load()

        for ruleset in rulesets.get_rulesets().values():
            for rule in ruleset.get_folder_rules(folder):
                if rule.predefined_condition_id() == self._ident:
                    rule.update_conditions(conditions)

        rulesets.save()
Exemplo n.º 4
0
def _change_host_tags_in_rules(operation, mode, folder):
    """Update tags in all rules

    The function parses all rules in all rulesets and looks for host tags that
    have been removed or renamed. If tags are removed then the depending on the
    mode affected rules are either deleted ("delete") or the vanished tags are
    removed from the rule ("remove").

    See _rename_tags_after_confirmation() doc string for additional information.
    """
    affected_rulesets = set()

    rulesets = FolderRulesets(folder)
    rulesets.load()

    for ruleset in rulesets.get_rulesets().values():
        for _folder, _rulenr, rule in ruleset.get_rules():
            affected_rulesets.update(_change_host_tags_in_rule(operation, mode, ruleset, rule))

    if affected_rulesets and mode != TagCleanupMode.CHECK:
        rulesets.save()

    return sorted(affected_rulesets, key=lambda x: x.title())
Exemplo n.º 5
0
    def generate(self):
        save_global_settings(self._initial_global_settings())

        # A contact group for all hosts and services
        groups = {
            "contact": {
                'all': {
                    'alias': u'Everything'
                }
            },
        }
        save_group_information(groups)

        self._initialize_tag_config()

        # Rules that match the upper host tag definition
        ruleset_config = {
            # Make the tag 'offline' remove hosts from the monitoring
            'only_hosts': [
                {
                    'condition': {
                        'host_tags': {
                            'criticality': {
                                '$ne': 'offline'
                            }
                        }
                    },
                    'value': True,
                    'options': {
                        'description':
                        u'Do not monitor hosts with the tag "offline"'
                    },
                },
            ],

            # Rule for WAN hosts with adapted PING levels
            'ping_levels': [
                {
                    'condition': {
                        'host_tags': {
                            'networking': 'wan',
                        }
                    },
                    'value': {
                        'loss': (80.0, 100.0),
                        'packets': 6,
                        'timeout': 20,
                        'rta': (1500.0, 3000.0)
                    },
                    'options': {
                        'description':
                        u'Allow longer round trip times when pinging WAN hosts'
                    },
                },
            ],

            # All hosts should use SNMP v2c if not specially tagged
            'bulkwalk_hosts': [
                {
                    'condition': {
                        'host_tags': {
                            'snmp': 'snmp',
                            'snmp_ds': {
                                '$ne': 'snmp-v1'
                            },
                        },
                    },
                    'value': True,
                    'options': {
                        'description':
                        u'Hosts with the tag "snmp-v1" must not use bulkwalk'
                    },
                },
            ],

            # Put all hosts and the contact group 'all'
            'host_contactgroups': [
                {
                    'condition': {},
                    'value': 'all',
                    'options': {
                        'description':
                        u'Put all hosts into the contact group "all"'
                    },
                },
            ],

            # Docker container specific host check commands
            'host_check_commands': [
                {
                    'condition': {
                        'host_labels': {
                            u'cmk/docker_object': u'container'
                        }
                    },
                    'value': ('service', u'Docker container status'),
                    'options': {
                        'description':
                        u'Make all docker container host states base on the "Docker container status" service',
                    },
                },
            ],

            # Enable HW/SW inventory + status data inventory for docker containers and Check-MK servers by default to
            # simplify the setup procedure for them
            'active_checks': {
                'cmk_inv': [
                    {
                        'condition': {
                            'host_labels': {
                                u'cmk/docker_object': u'node',
                            }
                        },
                        'value': {
                            'status_data_inventory': True
                        },
                    },
                    {
                        'condition': {
                            'host_labels': {
                                u'cmk/check_mk_server': u'yes',
                            }
                        },
                        'value': {
                            'status_data_inventory': True
                        },
                    },
                ]
            },

            # Interval for HW/SW-Inventory check
            'extra_service_conf': {
                'check_interval': [
                    {
                        'condition': {
                            'service_description': [{
                                '$regex':
                                'Check_MK HW/SW Inventory$'
                            }]
                        },
                        'value': 1440,
                        'options': {
                            'description':
                            u'Restrict HW/SW-Inventory to once a day'
                        },
                    },
                ],
            },

            # Disable unreachable notifications by default
            'extra_host_conf': {
                'notification_options': [
                    {
                        'condition': {},
                        'value': 'd,r,f,s'
                    },
                ],
            },

            # Periodic service discovery
            'periodic_discovery': [
                {
                    'condition': {},
                    'value': {
                        'severity_unmonitored': 1,
                        'severity_vanished': 0,
                        'check_interval': 120.0,
                        'inventory_check_do_scan': True
                    },
                    'options': {
                        'description':
                        u'Perform every two hours a service discovery'
                    },
                },
            ],

            # Include monitoring of checkmk's tmpfs
            'inventory_df_rules': [
                {
                    'condition': {
                        'host_labels': {
                            u'cmk/check_mk_server': u'yes',
                        },
                    },
                    'value': {
                        'ignore_fs_types':
                        ['tmpfs', 'nfs', 'smbfs', 'cifs', 'iso9660'],
                        'never_ignore_mountpoints':
                        [u'~.*/omd/sites/[^/]+/tmp$']
                    }
                },
            ],
        }

        rulesets = FolderRulesets(Folder.root_folder())
        rulesets.load()
        rulesets.from_config(Folder.root_folder(), ruleset_config)
        rulesets.save()

        notification_rules = [
            {
                'allow_disable': True,
                'contact_all': False,
                'contact_all_with_email': False,
                'contact_object': True,
                'description':
                'Notify all contacts of a host/service via HTML email',
                'disabled': False,
                'notify_plugin': ('mail', {}),
            },
        ]
        save_notification_rules(notification_rules)
Exemplo n.º 6
0
    def _move_rules_for_conditions(self, conditions, old_path):
        # type (RuleConditions, str) -> None
        """Apply changed folder of predefined condition to rules"""
        old_folder = Folder.folder(old_path)
        old_rulesets = FolderRulesets(old_folder)
        old_rulesets.load()

        new_folder = Folder.folder(conditions.host_folder)
        new_rulesets = FolderRulesets(new_folder)
        new_rulesets.load()

        for old_ruleset in old_rulesets.get_rulesets().values():
            for rule in old_ruleset.get_folder_rules(old_folder):
                if rule.predefined_condition_id() == self._ident:
                    old_ruleset.delete_rule(rule)

                    new_ruleset = new_rulesets.get(old_ruleset.name)
                    new_ruleset.append_rule(new_folder, rule)

        new_rulesets.save()
        old_rulesets.save()
Exemplo n.º 7
0
    def generate(self):
        save_global_settings(self._initial_global_settings())

        # A contact group for all hosts and services
        groups = {
            "contact": {
                'all': {
                    'alias': u'Everything'
                }
            },
        }
        save_group_information(groups)

        self._initialize_tag_config()

        # Rules that match the upper host tag definition
        ruleset_config = {
            # Make the tag 'offline' remove hosts from the monitoring
            'only_hosts': [
                {
                    'id': '10843c55-11ea-4eb2-bfbc-bce65cd2ae22',
                    'condition': {
                        'host_tags': {
                            'criticality': {
                                '$ne': 'offline'
                            }
                        }
                    },
                    'value': True,
                    'options': {
                        'description':
                        u'Do not monitor hosts with the tag "offline"'
                    },
                },
            ],

            # Rule for WAN hosts with adapted PING levels
            'ping_levels': [
                {
                    'id': '0365b634-30bf-40a3-8516-08e86051508e',
                    'condition': {
                        'host_tags': {
                            'networking': 'wan',
                        }
                    },
                    'value': {
                        'loss': (80.0, 100.0),
                        'packets': 6,
                        'timeout': 20,
                        'rta': (1500.0, 3000.0)
                    },
                    'options': {
                        'description':
                        u'Allow longer round trip times when pinging WAN hosts'
                    },
                },
            ],

            # All hosts should use SNMP v2c if not specially tagged
            'bulkwalk_hosts': [
                {
                    'id': 'b92a5406-1d57-4f1d-953d-225b111239e5',
                    'condition': {
                        'host_tags': {
                            'snmp': 'snmp',
                            'snmp_ds': {
                                '$ne': 'snmp-v1'
                            },
                        },
                    },
                    'value': True,
                    'options': {
                        'description':
                        u'Hosts with the tag "snmp-v1" must not use bulkwalk'
                    },
                },
            ],

            # All SNMP managment boards should use SNMP v2c if not specially tagged
            'management_bulkwalk_hosts': [
                {
                    'id': '59d84cde-ee3a-4f8d-8bec-fce35a2b0d15',
                    'condition': {},
                    'value': True,
                    'options': {
                        'description':
                        u'All management boards use SNMP v2 (incl. bulk walks) by default'
                    },
                },
            ],

            # Put all hosts and the contact group 'all'
            'host_contactgroups': [
                {
                    'id': 'efd67dab-68f8-4d3c-a417-9f7e29ab48d5',
                    'condition': {},
                    'value': 'all',
                    'options': {
                        'description':
                        u'Put all hosts into the contact group "all"'
                    },
                },
            ],

            # Docker container specific host check commands
            'host_check_commands': [
                {
                    'id': '24da4ccd-0d1b-40e3-af87-0097df8668f2',
                    'condition': {
                        'host_labels': {
                            u'cmk/docker_object': u'container'
                        }
                    },
                    'value': ('service', u'Docker container status'),
                    'options': {
                        'description':
                        u'Make all docker container host states base on the "Docker container status" service',
                    },
                },
            ],

            # Enable HW/SW inventory + status data inventory for docker
            # containers, kubernetes objects and Check-MK servers by default to
            # simplify the setup procedure for them
            'active_checks': {
                'cmk_inv': [
                    {
                        'id': '7ba2ac2a-5a49-47ce-bc3c-1630fb191c7f',
                        'condition': {
                            'host_labels': {
                                u'cmk/docker_object': u'node',
                            }
                        },
                        'value': {
                            'status_data_inventory': True
                        },
                    },
                    {
                        'id': 'b4b151f9-c7cc-4127-87a6-9539931fcd73',
                        'condition': {
                            'host_labels': {
                                u'cmk/check_mk_server': u'yes',
                            }
                        },
                        'value': {
                            'status_data_inventory': True
                        },
                    },
                    {
                        'id': '2527cb37-e9da-4a15-a7d9-80825a7f6661',
                        'condition': {
                            'host_labels': {
                                'cmk/kubernetes': 'yes',
                            }
                        },
                        'value': {
                            'status_data_inventory': True
                        },
                    },
                ]
            },

            # Interval for HW/SW-Inventory check
            'extra_service_conf': {
                'check_interval': [
                    {
                        'id': 'b3847203-84b3-4f5b-ac67-0f06d4403905',
                        'condition': {
                            'service_description': [{
                                '$regex':
                                'Check_MK HW/SW Inventory$'
                            }]
                        },
                        'value': 1440,
                        'options': {
                            'description':
                            u'Restrict HW/SW-Inventory to once a day'
                        },
                    },
                ],
            },

            # Disable unreachable notifications by default
            'extra_host_conf': {
                'notification_options': [
                    {
                        'id': '814bf932-6341-4f96-983d-283525b5416d',
                        'condition': {},
                        'value': 'd,r,f,s'
                    },
                ],
            },

            # Periodic service discovery
            'periodic_discovery': [
                {
                    'id': '95a56ffc-f17e-44e7-a162-be656f19bedf',
                    'condition': {},
                    'value': {
                        'severity_unmonitored': 1,
                        'severity_vanished': 0,
                        'check_interval': 120.0,
                    },
                    'options': {
                        'description':
                        u'Perform every two hours a service discovery'
                    },
                },
            ],

            # Include monitoring of checkmk's tmpfs
            'inventory_df_rules': [
                {
                    'id': 'b0ee8a51-703c-47e4-aec4-76430281604d',
                    'condition': {
                        'host_labels': {
                            u'cmk/check_mk_server': u'yes',
                        },
                    },
                    'value': {
                        'ignore_fs_types':
                        ['tmpfs', 'nfs', 'smbfs', 'cifs', 'iso9660'],
                        'never_ignore_mountpoints':
                        [u'~.*/omd/sites/[^/]+/tmp$']
                    }
                },
            ],
        }

        rulesets = FolderRulesets(Folder.root_folder())
        rulesets.load()
        rulesets.from_config(Folder.root_folder(), ruleset_config)
        rulesets.save()

        notification_rules = [
            {
                'allow_disable': True,
                'contact_all': False,
                'contact_all_with_email': False,
                'contact_object': True,
                'description':
                'Notify all contacts of a host/service via HTML email',
                'disabled': False,
                'notify_plugin': ('mail', {}),
            },
        ]
        save_notification_rules(notification_rules)
Exemplo n.º 8
0
def _create_sample_config():
    """Create a very basic sample configuration

    But only if none of the
    files that we will create already exists. That is e.g. the case
    after an update from an older version where no sample config had
    been created.
    """
    if not _need_to_create_sample_config():
        return

    # Global configuration settings
    save_global_settings({
        "use_new_descriptions_for": [
            "df",
            "df_netapp",
            "df_netapp32",
            "esx_vsphere_datastores",
            "hr_fs",
            "vms_diskstat.df",
            "zfsget",
            "ps",
            "ps.perf",
            "wmic_process",
            "services",
            "logwatch",
            "logwatch.groups",
            "cmk-inventory",
            "hyperv_vms",
            "ibm_svc_mdiskgrp",
            "ibm_svc_system",
            "ibm_svc_systemstats.diskio",
            "ibm_svc_systemstats.iops",
            "ibm_svc_systemstats.disk_latency",
            "ibm_svc_systemstats.cache",
            "casa_cpu_temp",
            "cmciii.temp",
            "cmciii.psm_current",
            "cmciii_lcp_airin",
            "cmciii_lcp_airout",
            "cmciii_lcp_water",
            "etherbox.temp",
            "liebert_bat_temp",
            "nvidia.temp",
            "ups_bat_temp",
            "innovaphone_temp",
            "enterasys_temp",
            "raritan_emx",
            "raritan_pdu_inlet",
            "mknotifyd",
            "mknotifyd.connection",
            "postfix_mailq",
            "nullmailer_mailq",
            "barracuda_mailqueues",
            "qmail_stats",
            "http",
            "mssql_backup",
            "mssql_counters.cache_hits",
            "mssql_counters.transactions",
            "mssql_counters.locks",
            "mssql_counters.sqlstats",
            "mssql_counters.pageactivity",
            "mssql_counters.locks_per_batch",
            "mssql_counters.file_sizes",
            "mssql_databases",
            "mssql_datafiles",
            "mssql_tablespaces",
            "mssql_transactionlogs",
            "mssql_versions",
        ],
        "enable_rulebased_notifications": True,
        "ui_theme": "facelift",
        "lock_on_logon_failures": 10,
    })

    # A contact group for all hosts and services
    groups = {
        "contact": {
            'all': {
                'alias': u'Everything'
            }
        },
    }
    save_group_information(groups)

    _initialize_tag_config()

    # Rules that match the upper host tag definition
    ruleset_config = {
        # Make the tag 'offline' remove hosts from the monitoring
        'only_hosts': [(['!offline'], ['@all'], {
            'description': u'Do not monitor hosts with the tag "offline"'
        }),],

        # Rule for WAN hosts with adapted PING levels
        'ping_levels': [({
            'loss': (80.0, 100.0),
            'packets': 6,
            'rta': (1500.0, 3000.0),
            'timeout': 20
        }, ['wan'], ['@all'], {
            'description': u'Allow longer round trip times when pinging WAN hosts'
        }),],

        # All hosts should use SNMP v2c if not specially tagged
        'bulkwalk_hosts': [(['snmp', '!snmp-v1'], ['@all'], {
            'description': u'Hosts with the tag "snmp-v1" must not use bulkwalk'
        }),],

        # Put all hosts and the contact group 'all'
        'host_contactgroups': [('all', [], ALL_HOSTS, {
            'description': u'Put all hosts into the contact group "all"'
        }),],

        # Interval for HW/SW-Inventory check
        'extra_service_conf': {
            'check_interval': [(1440, [], ALL_HOSTS, ["Check_MK HW/SW Inventory$"], {
                'description': u'Restrict HW/SW-Inventory to once a day'
            }),],
        },

        # Disable unreachable notifications by default
        'extra_host_conf': {
            'notification_options': [('d,r,f,s', [], ALL_HOSTS, {}),],
        },

        # Periodic service discovery
        'periodic_discovery': [({
            'severity_unmonitored': 1,
            'severity_vanished': 0,
            'inventory_check_do_scan': True,
            'check_interval': 120.0
        }, [], ALL_HOSTS, {
            'description': u'Perform every two hours a service discovery'
        }),],
    }

    rulesets = FolderRulesets(Folder.root_folder())
    rulesets.from_config(Folder.root_folder(), ruleset_config)
    rulesets.save()

    notification_rules = [
        {
            'allow_disable': True,
            'contact_all': False,
            'contact_all_with_email': False,
            'contact_object': True,
            'description': 'Notify all contacts of a host/service via HTML email',
            'disabled': False,
            'notify_plugin': ('mail', {}),
        },
    ]
    save_notification_rules(notification_rules)

    try:
        import cmk.gui.cee.plugins.wato.sample_config  # pylint: disable=redefined-outer-name
        cmk.gui.cee.plugins.wato.sample_config.create_cee_sample_config()
    except ImportError:
        pass

    # Initial baking of agents (when bakery is available)
    if has_agent_bakery():
        import cmk.gui.cee.plugins.wato.agent_bakery
        bake_job = cmk.gui.cee.plugins.wato.agent_bakery.BakeAgentsBackgroundJob()
        bake_job.set_function(cmk.gui.cee.plugins.wato.agent_bakery.bake_agents_background_job)
        try:
            bake_job.start()
        except background_job.BackgroundJobAlreadyRunning:
            pass

    # This is not really the correct place for such kind of action, but the best place we could
    # find to execute it only for new created sites.
    import cmk.gui.werks as werks
    werks.acknowledge_all_werks(check_permission=False)

    cmk.gui.wato.mkeventd.save_mkeventd_sample_config()

    userdb.create_cmk_automation_user()
Exemplo n.º 9
0
    def generate(self):
        save_global_settings({
            "use_new_descriptions_for": [
                "df",
                "df_netapp",
                "df_netapp32",
                "esx_vsphere_datastores",
                "hr_fs",
                "vms_diskstat.df",
                "zfsget",
                "ps",
                "ps.perf",
                "wmic_process",
                "services",
                "logwatch",
                "logwatch.groups",
                "cmk-inventory",
                "hyperv_vms",
                "ibm_svc_mdiskgrp",
                "ibm_svc_system",
                "ibm_svc_systemstats.diskio",
                "ibm_svc_systemstats.iops",
                "ibm_svc_systemstats.disk_latency",
                "ibm_svc_systemstats.cache",
                "casa_cpu_temp",
                "cmciii.temp",
                "cmciii.psm_current",
                "cmciii_lcp_airin",
                "cmciii_lcp_airout",
                "cmciii_lcp_water",
                "etherbox.temp",
                "liebert_bat_temp",
                "nvidia.temp",
                "ups_bat_temp",
                "innovaphone_temp",
                "enterasys_temp",
                "raritan_emx",
                "raritan_pdu_inlet",
                "mknotifyd",
                "mknotifyd.connection",
                "postfix_mailq",
                "nullmailer_mailq",
                "barracuda_mailqueues",
                "qmail_stats",
                "http",
                "mssql_backup",
                "mssql_counters.cache_hits",
                "mssql_counters.transactions",
                "mssql_counters.locks",
                "mssql_counters.sqlstats",
                "mssql_counters.pageactivity",
                "mssql_counters.locks_per_batch",
                "mssql_counters.file_sizes",
                "mssql_databases",
                "mssql_datafiles",
                "mssql_tablespaces",
                "mssql_transactionlogs",
                "mssql_versions",
            ],
            "enable_rulebased_notifications":
            True,
            "ui_theme":
            "facelift",
            "lock_on_logon_failures":
            10,
        })

        content = "# Written by WATO Basic config (%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)

        # A contact group for all hosts and services
        groups = {
            "contact": {
                'all': {
                    'alias': u'Everything'
                }
            },
        }
        save_group_information(groups)

        self._initialize_tag_config()

        # Rules that match the upper host tag definition
        ruleset_config = {
            # Make the tag 'offline' remove hosts from the monitoring
            'only_hosts': [
                {
                    'condition': {
                        'host_tags': {
                            'criticality': {
                                '$ne': 'offline'
                            }
                        }
                    },
                    'value': True,
                    'options': {
                        'description':
                        u'Do not monitor hosts with the tag "offline"'
                    },
                },
            ],

            # Rule for WAN hosts with adapted PING levels
            'ping_levels': [
                {
                    'condition': {
                        'host_tags': {
                            'networking': 'wan',
                        }
                    },
                    'value': {
                        'loss': (80.0, 100.0),
                        'packets': 6,
                        'timeout': 20,
                        'rta': (1500.0, 3000.0)
                    },
                    'options': {
                        'description':
                        u'Allow longer round trip times when pinging WAN hosts'
                    },
                },
            ],

            # All hosts should use SNMP v2c if not specially tagged
            'bulkwalk_hosts': [
                {
                    'condition': {
                        'host_tags': {
                            'snmp': 'snmp',
                            'snmp_ds': {
                                '$ne': 'snmp-v1'
                            },
                        },
                    },
                    'value': True,
                    'options': {
                        'description':
                        u'Hosts with the tag "snmp-v1" must not use bulkwalk'
                    },
                },
            ],

            # Put all hosts and the contact group 'all'
            'host_contactgroups': [
                {
                    'condition': {},
                    'value': 'all',
                    'options': {
                        'description':
                        u'Put all hosts into the contact group "all"'
                    },
                },
            ],

            # Enable HW/SW inventory + status data inventory for docker containers by default to
            # simplify the setup procedure of docker monitoring
            'active_checks': {
                'cmk_inv': [
                    {
                        'condition': {
                            'host_labels': {
                                u'cmk/docker_object': u'node'
                            }
                        },
                        'value': {
                            'status_data_inventory': True
                        },
                    },
                ]
            },

            # Interval for HW/SW-Inventory check
            'extra_service_conf': {
                'check_interval': [
                    {
                        'condition': {
                            'service_description': [{
                                '$regex':
                                'Check_MK HW/SW Inventory$'
                            }]
                        },
                        'value': 1440,
                        'options': {
                            'description':
                            u'Restrict HW/SW-Inventory to once a day'
                        },
                    },
                ],
            },

            # Disable unreachable notifications by default
            'extra_host_conf': {
                'notification_options': [
                    {
                        'condition': {},
                        'value': 'd,r,f,s'
                    },
                ],
            },

            # Periodic service discovery
            'periodic_discovery': [
                {
                    'condition': {},
                    'value': {
                        'severity_unmonitored': 1,
                        'severity_vanished': 0,
                        'check_interval': 120.0,
                        'inventory_check_do_scan': True
                    },
                    'options': {
                        'description':
                        u'Perform every two hours a service discovery'
                    },
                },
            ],
        }

        rulesets = FolderRulesets(Folder.root_folder())
        rulesets.load()
        rulesets.from_config(Folder.root_folder(), ruleset_config)
        rulesets.save()

        notification_rules = [
            {
                'allow_disable': True,
                'contact_all': False,
                'contact_all_with_email': False,
                'contact_object': True,
                'description':
                'Notify all contacts of a host/service via HTML email',
                'disabled': False,
                'notify_plugin': ('mail', {}),
            },
        ]
        save_notification_rules(notification_rules)
Exemplo n.º 10
0
    def generate(self):
        save_global_settings(self._initial_global_settings())

        content = "# Written by WATO Basic config (%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)

        # A contact group for all hosts and services
        groups = {
            "contact": {
                'all': {
                    'alias': u'Everything'
                }
            },
        }
        save_group_information(groups)

        self._initialize_tag_config()

        # Rules that match the upper host tag definition
        ruleset_config = {
            # Make the tag 'offline' remove hosts from the monitoring
            'only_hosts': [
                {
                    'condition': {
                        'host_tags': {
                            'criticality': {
                                '$ne': 'offline'
                            }
                        }
                    },
                    'value': True,
                    'options': {
                        'description':
                        u'Do not monitor hosts with the tag "offline"'
                    },
                },
            ],

            # Rule for WAN hosts with adapted PING levels
            'ping_levels': [
                {
                    'condition': {
                        'host_tags': {
                            'networking': 'wan',
                        }
                    },
                    'value': {
                        'loss': (80.0, 100.0),
                        'packets': 6,
                        'timeout': 20,
                        'rta': (1500.0, 3000.0)
                    },
                    'options': {
                        'description':
                        u'Allow longer round trip times when pinging WAN hosts'
                    },
                },
            ],

            # All hosts should use SNMP v2c if not specially tagged
            'bulkwalk_hosts': [
                {
                    'condition': {
                        'host_tags': {
                            'snmp': 'snmp',
                            'snmp_ds': {
                                '$ne': 'snmp-v1'
                            },
                        },
                    },
                    'value': True,
                    'options': {
                        'description':
                        u'Hosts with the tag "snmp-v1" must not use bulkwalk'
                    },
                },
            ],

            # Put all hosts and the contact group 'all'
            'host_contactgroups': [
                {
                    'condition': {},
                    'value': 'all',
                    'options': {
                        'description':
                        u'Put all hosts into the contact group "all"'
                    },
                },
            ],

            # Docker container specific host check commands
            'host_check_commands': [
                {
                    'condition': {
                        'host_labels': {
                            u'cmk/docker_object': u'container'
                        }
                    },
                    'value': ('service', u'Docker container status'),
                    'options': {
                        'description':
                        u'Make all docker container host states base on the "Docker container status" service',
                    },
                },
            ],

            # Enable HW/SW inventory + status data inventory for docker containers by default to
            # simplify the setup procedure of docker monitoring
            'active_checks': {
                'cmk_inv': [
                    {
                        'condition': {
                            'host_labels': {
                                u'cmk/docker_object': u'node'
                            }
                        },
                        'value': {
                            'status_data_inventory': True
                        },
                    },
                ]
            },

            # Interval for HW/SW-Inventory check
            'extra_service_conf': {
                'check_interval': [
                    {
                        'condition': {
                            'service_description': [{
                                '$regex':
                                'Check_MK HW/SW Inventory$'
                            }]
                        },
                        'value': 1440,
                        'options': {
                            'description':
                            u'Restrict HW/SW-Inventory to once a day'
                        },
                    },
                ],
            },

            # Disable unreachable notifications by default
            'extra_host_conf': {
                'notification_options': [
                    {
                        'condition': {},
                        'value': 'd,r,f,s'
                    },
                ],
            },

            # Periodic service discovery
            'periodic_discovery': [
                {
                    'condition': {},
                    'value': {
                        'severity_unmonitored': 1,
                        'severity_vanished': 0,
                        'check_interval': 120.0,
                        'inventory_check_do_scan': True
                    },
                    'options': {
                        'description':
                        u'Perform every two hours a service discovery'
                    },
                },
            ],
        }

        rulesets = FolderRulesets(Folder.root_folder())
        rulesets.load()
        rulesets.from_config(Folder.root_folder(), ruleset_config)
        rulesets.save()

        notification_rules = [
            {
                'allow_disable': True,
                'contact_all': False,
                'contact_all_with_email': False,
                'contact_object': True,
                'description':
                'Notify all contacts of a host/service via HTML email',
                'disabled': False,
                'notify_plugin': ('mail', {}),
            },
        ]
        save_notification_rules(notification_rules)
Exemplo n.º 11
0
    def generate(self) -> None:
        save_global_settings(self._initial_global_settings())

        # A contact group for all hosts and services
        groups: AllGroupSpecs = {
            "contact": {
                GroupName("all"): {
                    "alias": "Everything"
                }
            },
        }
        save_group_information(groups)

        self._initialize_tag_config()

        # Rules that match the upper host tag definition
        ruleset_config = {
            # Make the tag 'offline' remove hosts from the monitoring
            "only_hosts": [
                {
                    "id": "10843c55-11ea-4eb2-bfbc-bce65cd2ae22",
                    "condition": {
                        "host_tags": {
                            "criticality": {
                                "$ne": "offline"
                            }
                        }
                    },
                    "value": True,
                    "options": {
                        "description":
                        'Do not monitor hosts with the tag "offline"'
                    },
                },
            ],
            # Rule for WAN hosts with adapted PING levels
            "ping_levels": [
                {
                    "id": "0365b634-30bf-40a3-8516-08e86051508e",
                    "condition": {
                        "host_tags": {
                            "networking": "wan",
                        }
                    },
                    "value": {
                        "loss": (80.0, 100.0),
                        "packets": 6,
                        "timeout": 20,
                        "rta": (1500.0, 3000.0),
                    },
                    "options": {
                        "description":
                        "Allow longer round trip times when pinging WAN hosts"
                    },
                },
            ],
            # All hosts should use SNMP v2c if not specially tagged
            "bulkwalk_hosts": [
                {
                    "id": "b92a5406-1d57-4f1d-953d-225b111239e5",
                    "condition": {
                        "host_tags": {
                            "snmp": "snmp",
                            "snmp_ds": {
                                "$ne": "snmp-v1"
                            },
                        },
                    },
                    "value": True,
                    "options": {
                        "description":
                        'Hosts with the tag "snmp-v1" must not use bulkwalk'
                    },
                },
            ],
            # All SNMP managment boards should use SNMP v2c if not specially tagged
            "management_bulkwalk_hosts": [
                {
                    "id": "59d84cde-ee3a-4f8d-8bec-fce35a2b0d15",
                    "condition": {},
                    "value": True,
                    "options": {
                        "description":
                        "All management boards use SNMP v2 (incl. bulk walks) by default"
                    },
                },
            ],
            # Put all hosts and the contact group 'all'
            "host_contactgroups": [
                {
                    "id": "efd67dab-68f8-4d3c-a417-9f7e29ab48d5",
                    "condition": {},
                    "value": "all",
                    "options": {
                        "description":
                        'Put all hosts into the contact group "all"'
                    },
                },
            ],
            # Docker container specific host check commands
            "host_check_commands": [
                {
                    "id": "24da4ccd-0d1b-40e3-af87-0097df8668f2",
                    "condition": {
                        "host_labels": {
                            "cmk/docker_object": "container"
                        }
                    },
                    "value": ("service", "Docker container status"),
                    "options": {
                        "description":
                        'Make all docker container host states base on the "Docker container status" service',
                    },
                },
            ],
            # Enable HW/SW inventory + status data inventory for docker
            # containers, kubernetes objects and Check-MK servers by default to
            # simplify the setup procedure for them
            "active_checks": {
                "cmk_inv": [
                    {
                        "id": "7ba2ac2a-5a49-47ce-bc3c-1630fb191c7f",
                        "condition": {
                            "host_labels": {
                                "cmk/docker_object": "node",
                            }
                        },
                        "value": {
                            "status_data_inventory": True
                        },
                    },
                    {
                        "id": "b4b151f9-c7cc-4127-87a6-9539931fcd73",
                        "condition": {
                            "host_labels": {
                                "cmk/check_mk_server": "yes",
                            }
                        },
                        "value": {
                            "status_data_inventory": True
                        },
                    },
                    {
                        "id": "2527cb37-e9da-4a15-a7d9-80825a7f6661",
                        "condition": {
                            "host_labels": {
                                "cmk/kubernetes": "yes",
                            }
                        },
                        "value": {
                            "status_data_inventory": True
                        },
                    },
                ]
            },
            # Interval for HW/SW-Inventory check
            "extra_service_conf": {
                "check_interval": [
                    {
                        "id": "b3847203-84b3-4f5b-ac67-0f06d4403905",
                        "condition": {
                            "service_description": [{
                                "$regex":
                                "Check_MK HW/SW Inventory$"
                            }]
                        },
                        "value": 1440,
                        "options": {
                            "description":
                            "Restrict HW/SW-Inventory to once a day"
                        },
                    },
                ],
            },
            # Disable unreachable notifications by default
            "extra_host_conf": {
                "notification_options": [
                    {
                        "id": "814bf932-6341-4f96-983d-283525b5416d",
                        "condition": {},
                        "value": "d,r,f,s",
                    },
                ],
            },
            # Periodic service discovery
            "periodic_discovery": [
                {
                    "id": "95a56ffc-f17e-44e7-a162-be656f19bedf",
                    "condition": {},
                    "value": {
                        "severity_unmonitored": 1,
                        "severity_vanished": 0,
                        "check_interval": 120.0,
                    },
                    "options": {
                        "description":
                        "Perform every two hours a service discovery"
                    },
                },
            ],
            # Include monitoring of checkmk's tmpfs
            "inventory_df_rules": [
                {
                    "id": "b0ee8a51-703c-47e4-aec4-76430281604d",
                    "condition": {
                        "host_labels": {
                            "cmk/check_mk_server": "yes",
                        },
                    },
                    "value": {
                        "ignore_fs_types":
                        ["tmpfs", "nfs", "smbfs", "cifs", "iso9660"],
                        "never_ignore_mountpoints":
                        ["~.*/omd/sites/[^/]+/tmp$"],
                    },
                },
            ],
        }

        rulesets = FolderRulesets(Folder.root_folder())
        rulesets.load()
        rulesets.from_config(Folder.root_folder(), ruleset_config)
        rulesets.save()

        notification_rules = [
            {
                "allow_disable": True,
                "contact_all": False,
                "contact_all_with_email": False,
                "contact_object": True,
                "description":
                "Notify all contacts of a host/service via HTML email",
                "disabled": False,
                "notify_plugin": ("mail", {}),
            },
        ]
        save_notification_rules(notification_rules)
Exemplo n.º 12
0
def create_rule(param):
    """Create rule"""
    user.need_permission("wato.edit")
    user.need_permission("wato.rulesets")
    body = param["body"]
    folder: CREFolder = body["folder"]
    value = body["value_raw"]
    ruleset_name = body["ruleset"]

    folder.need_permission("write")

    rulesets = FolderRulesets(folder)
    rulesets.load()
    try:
        ruleset = rulesets.get(ruleset_name)
    except KeyError:
        return problem(
            status=400,
            detail=f"Ruleset {ruleset_name!r} could not be found.",
        )

    try:
        ruleset.valuespec().validate_value(value, "")
    except exceptions.MKUserError as exc:
        if exc.varname is None:
            title = "A field has a problem"
        else:
            field_name = strip_tags(exc.varname.replace("_p_", ""))
            title = f"Problem in (sub-)field {field_name!r}"

        return problem(
            status=400,
            detail=strip_tags(exc.message),
            title=title,
        )

    rule = Rule(
        gen_id(),
        folder,
        ruleset,
        RuleConditions(
            host_folder=folder.path(),
            host_tags=body["conditions"].get("host_tags"),
            host_labels=body["conditions"].get("host_labels"),
            host_name=body["conditions"].get("host_name"),
            service_description=body["conditions"].get("service_description"),
            service_labels=body["conditions"].get("service_labels"),
        ),
        RuleOptions.from_config(body["properties"]),
        value,
    )
    index = ruleset.append_rule(folder, rule)
    rulesets.save()
    # TODO Duplicated code is in pages/rulesets.py:2670-
    # TODO Move to
    add_change(
        "new-rule",
        _l('Created new rule #%d in ruleset "%s" in folder "%s"') %
        (index, ruleset.title(), folder.alias_path()),
        sites=folder.all_site_ids(),
        diff_text=make_diff_text({}, rule.to_log()),
        object_ref=rule.object_ref(),
    )
    rule_entry = _get_rule_by_id(rule.id)
    return serve_json(_serialize_rule(rule_entry))