Module: check_mk
Branch: master
Commit: c5bac9eae68f43cdefd1961d0d0da323ef220496
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c5bac9eae68f43…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Jan 10 23:13:06 2019 +0100
Moved notification rule related code to dedicated module
Change-Id: I3ce256588dd45afc9843c31f0468a86293a8c09f
---
cmk/gui/wato/pages/host_rename.py | 8 ++++--
cmk/gui/wato/pages/notifications.py | 14 ++++++----
cmk/gui/wato/pages/timeperiods.py | 3 +-
cmk/gui/watolib/__init__.py | 38 +------------------------
cmk/gui/watolib/notifications.py | 55 +++++++++++++++++++++++++++++++++++++
5 files changed, 73 insertions(+), 45 deletions(-)
diff --git a/cmk/gui/wato/pages/host_rename.py b/cmk/gui/wato/pages/host_rename.py
index e2caf8a..ca9b347 100644
--- a/cmk/gui/wato/pages/host_rename.py
+++ b/cmk/gui/wato/pages/host_rename.py
@@ -43,6 +43,10 @@ from cmk.gui.exceptions import HTTPRedirect, MKUserError,
MKGeneralException, MK
from cmk.gui.i18n import _
from cmk.gui.globals import html
from cmk.gui.watolib.hosts_and_folders import validate_host_uniqueness
+from cmk.gui.watolib.notifications import (
+ load_notification_rules,
+ save_notification_rules,
+)
from cmk.gui.valuespec import (
Hostname,
@@ -538,11 +542,11 @@ def rename_host_in_event_rules(oldname, newname):
actions += ["notify_user"] * num_changed
some_user_changed = True
- rules = watolib.load_notification_rules()
+ rules = load_notification_rules()
num_changed = rename_in_event_rules(rules)
if num_changed:
actions += ["notify_global"] * num_changed
- watolib.save_notification_rules(rules)
+ save_notification_rules(rules)
try:
import cmk.gui.cee.plugins.wato.alert_handling as alert_handling
diff --git a/cmk/gui/wato/pages/notifications.py b/cmk/gui/wato/pages/notifications.py
index 3107da9..3aefcec 100644
--- a/cmk/gui/wato/pages/notifications.py
+++ b/cmk/gui/wato/pages/notifications.py
@@ -72,6 +72,11 @@ from cmk.gui.plugins.wato import (
rule_option_elements,
)
+from cmk.gui.watolib.notifications import (
+ save_notification_rules,
+ load_notification_rules,
+)
+
class NotificationsMode(EventsMode):
# TODO: Clean this up. Use inheritance
@@ -460,11 +465,10 @@ class ModeNotifications(NotificationsMode):
else:
return self._generic_rule_list_actions(self._get_notification_rules(),
"notification",
- _("notification rule"),
- watolib.save_notification_rules)
+ _("notification rule"),
save_notification_rules)
def _get_notification_rules(self):
- return watolib.load_notification_rules()
+ return load_notification_rules()
def _save_notification_display_options(self):
config.user.save_file(
@@ -870,7 +874,7 @@ class EditNotificationRuleMode(NotificationsMode):
user = self._users[self._user_id()]
self._rules = user.setdefault("notification_rules", [])
else:
- self._rules = watolib.load_notification_rules(lock=html.is_transaction())
+ self._rules = load_notification_rules(lock=html.is_transaction())
if self._new:
if self._clone_nr >= 0 and not html.request.var("_clear"):
@@ -1231,7 +1235,7 @@ class EditNotificationRuleMode(NotificationsMode):
if self._user_id():
userdb.save_users(self._users)
else:
- watolib.save_notification_rules(self._rules)
+ save_notification_rules(self._rules)
if self._new:
log_what = "new-notification-rule"
diff --git a/cmk/gui/wato/pages/timeperiods.py b/cmk/gui/wato/pages/timeperiods.py
index 0bc13b9..08670d2 100644
--- a/cmk/gui/wato/pages/timeperiods.py
+++ b/cmk/gui/wato/pages/timeperiods.py
@@ -36,6 +36,7 @@ from cmk.gui.table import table_element
import cmk.gui.forms as forms
import cmk.gui.plugins.wato.utils
import cmk.gui.wato.mkeventd
+from cmk.gui.watolib.notifications import load_notification_rules
from cmk.gui.exceptions import MKUserError
from cmk.gui.i18n import _
from cmk.gui.globals import html
@@ -181,7 +182,7 @@ class ModeTimeperiods(WatoMode):
def _find_usages_in_notification_rules(self, tpname):
used_in = []
- for index, rule in enumerate(watolib.load_notification_rules()):
+ for index, rule in enumerate(load_notification_rules()):
used_in += self._find_usages_in_notification_rule(tpname, index, rule)
return used_in
diff --git a/cmk/gui/watolib/__init__.py b/cmk/gui/watolib/__init__.py
index 7b45916..84ec27c 100644
--- a/cmk/gui/watolib/__init__.py
+++ b/cmk/gui/watolib/__init__.py
@@ -133,6 +133,7 @@ import cmk.gui.watolib.git
import cmk.gui.watolib.changes
# TODO: Cleanup all except declare_host_attribute which is still neded for pre 1.6
plugin
# compatibility. For the others: Find the call sites and change to full module import
+from cmk.gui.watolib.notifications import save_notification_rules
from cmk.gui.watolib.timeperiods import TimeperiodSelection
from cmk.gui.watolib.host_attributes import (
all_host_attributes,
@@ -4339,43 +4340,6 @@ def find_usages_of_service_group(name):
#.
-# .--Notifications-(Rule Based)------------------------------------------.
-# | _ _ _ _ __ _ _ _ |
-# | | \ | | ___ | |_(_)/ _(_) ___ __ _| |_(_) ___ _ __ ___ |
-# | | \| |/ _ \| __| | |_| |/ __/ _` | __| |/ _ \| '_ \/ __| |
-# | | |\ | (_) | |_| | _| | (_| (_| | |_| | (_) | | | \__ \ |
-# | |_| \_|\___/ \__|_|_| |_|\___\__,_|\__|_|\___/|_| |_|___/ |
-# | |
-# +----------------------------------------------------------------------+
-# | Module for managing the new rule based notifications. |
-# '----------------------------------------------------------------------'
-
-
-def load_notification_rules(lock=False):
- filename = wato_root_dir + "notifications.mk"
- notification_rules = store.load_from_mk_file(filename,
"notification_rules", [], lock=lock)
-
- # Convert to new plugin configuration format
- for rule in notification_rules:
- if "notify_method" in rule:
- method = rule["notify_method"]
- plugin = rule["notify_plugin"]
- del rule["notify_method"]
- rule["notify_plugin"] = (plugin, method)
-
- return notification_rules
-
-
-def save_notification_rules(rules):
- store.mkdir(wato_root_dir)
- store.save_to_mk_file(
- wato_root_dir + "notifications.mk",
- "notification_rules",
- rules,
- pprint_value=config.wato_pprint_config)
-
-
-#.
# .--Users---------------------------------------------------------------.
# | _ _ |
# | | | | |___ ___ _ __ ___ |
diff --git a/cmk/gui/watolib/notifications.py b/cmk/gui/watolib/notifications.py
new file mode 100644
index 0000000..b0e840c
--- /dev/null
+++ b/cmk/gui/watolib/notifications.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+"""Module for managing the new rule based notifications"""
+
+import cmk.utils.store as store
+
+import cmk.gui.config as config
+from cmk.gui.watolib.utils import wato_root_dir
+
+
+def load_notification_rules(lock=False):
+ filename = wato_root_dir + "notifications.mk"
+ notification_rules = store.load_from_mk_file(filename,
"notification_rules", [], lock=lock)
+
+ # Convert to new plugin configuration format
+ for rule in notification_rules:
+ if "notify_method" in rule:
+ method = rule["notify_method"]
+ plugin = rule["notify_plugin"]
+ del rule["notify_method"]
+ rule["notify_plugin"] = (plugin, method)
+
+ return notification_rules
+
+
+def save_notification_rules(rules):
+ store.mkdir(wato_root_dir)
+ store.save_to_mk_file(
+ wato_root_dir + "notifications.mk",
+ "notification_rules",
+ rules,
+ pprint_value=config.wato_pprint_config)