Module: check_mk
Branch: master
Commit: fa82e4c774d42efce850ac221ff2d6a988e197b6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fa82e4c774d42e…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Wed Jan 4 16:11:33 2017 +0100
WebAPI: new functions add/edit/delete host/service/contact groups
Change-Id: I401fef078ae7001feadca375f38087cd4c50d133
---
web/htdocs/wato.py | 8 ----
web/htdocs/watolib.py | 42 +++++++++----------
web/htdocs/webapi.py | 7 ++--
web/plugins/webapi/webapi.py | 96 +++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 118 insertions(+), 35 deletions(-)
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 6366762..4b8c72d 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -8023,14 +8023,6 @@ def mode_timeperiods(phase):
-def load_timeperiods():
- return store.load_from_mk_file(wato_root_dir + "timeperiods.mk",
"timeperiods", {})
-
-
-def save_timeperiods(timeperiods):
- make_nagios_directory(wato_root_dir)
- store.save_to_mk_file(wato_root_dir + "timeperiods.mk",
"timeperiods", timeperiods)
-
class ExceptionName(TextAscii):
def __init__(self, **kwargs):
diff --git a/web/htdocs/watolib.py b/web/htdocs/watolib.py
index 4a37cef..a0f2dd3 100644
--- a/web/htdocs/watolib.py
+++ b/web/htdocs/watolib.py
@@ -6910,38 +6910,34 @@ def may_override_read_only_mode():
#.
-# .--Groups--------------------------------------------------------------.
-# | ____ |
-# | / ___|_ __ ___ _ _ _ __ ___ |
-# | | | _| '__/ _ \| | | | '_ \/ __| |
-# | | |_| | | | (_) | |_| | |_) \__ \ |
-# | \____|_| \___/ \__,_| .__/|___/ |
-# | |_| |
+# .--Timeperiods---------------------------------------------------------.
+# | _____ _ _ _ |
+# | |_ _(_)_ __ ___ ___ _ __ ___ _ __(_) ___ __| |___ |
+# | | | | | '_ ` _ \ / _ \ '_ \ / _ \ '__| |/ _ \ / _` / __| |
+# | | | | | | | | | | __/ |_) | __/ | | | (_) | (_| \__ \ |
+# | |_| |_|_| |_| |_|\___| .__/ \___|_| |_|\___/ \__,_|___/ |
+# | |_| |
# +----------------------------------------------------------------------+
-
def load_timeperiods():
- filename = wato_root_dir + "timeperiods.mk"
- if not os.path.exists(filename):
- return {}
- try:
- vars = { "timeperiods" : {} }
- execfile(filename, vars, vars)
- return vars["timeperiods"]
+ return store.load_from_mk_file(wato_root_dir + "timeperiods.mk",
"timeperiods", {})
- except Exception, e:
- if config.debug:
- raise MKGeneralException(_("Cannot read configuration file %s: %s")
%
- (filename, e))
- return {}
def save_timeperiods(timeperiods):
make_nagios_directory(wato_root_dir)
- out = create_user_file(wato_root_dir + "timeperiods.mk", "w")
- out.write(wato_fileheader())
- out.write("timeperiods.update(%s)\n" % pprint.pformat(timeperiods))
+ store.save_to_mk_file(wato_root_dir + "timeperiods.mk",
"timeperiods", timeperiods)
+#.
+# .--Groups--------------------------------------------------------------.
+# | ____ |
+# | / ___|_ __ ___ _ _ _ __ ___ |
+# | | | _| '__/ _ \| | | | '_ \/ __| |
+# | | |_| | | | (_) | |_| | |_) \__ \ |
+# | \____|_| \___/ \__,_| .__/|___/ |
+# | |_| |
+# +----------------------------------------------------------------------+
+
def is_alias_used(my_what, my_name, my_alias):
# Host / Service / Contact groups
all_groups = userdb.load_group_information()
diff --git a/web/htdocs/webapi.py b/web/htdocs/webapi.py
index 15d918b..d109c9e 100644
--- a/web/htdocs/webapi.py
+++ b/web/htdocs/webapi.py
@@ -100,16 +100,17 @@ def page_api():
action_response = api_actions[action]["handler"](request_object)
response = { "result_code": 0, "result": action_response }
+ except MKAuthException, e:
+ response = { "result_code": 1, "result":
_("Authorization Error. Insufficent permissions for '%s'") % e }
except MKException, e:
- response = { "result_code": 1, "result": "%s" % e
}
-
+ response = { "result_code": 1, "result": _("Check_MK
exception: %s") % e }
except Exception, e:
if config.debug:
raise
log_exception()
response = {
"result_code" : 1,
- "result" : "Unhandled exception: %s" %
traceback.format_exc(),
+ "result" : _("Unhandled exception: %s") %
traceback.format_exc(),
}
if html.output_format == "json":
diff --git a/web/plugins/webapi/webapi.py b/web/plugins/webapi/webapi.py
index b4884ab..f97ea1b 100644
--- a/web/plugins/webapi/webapi.py
+++ b/web/plugins/webapi/webapi.py
@@ -32,6 +32,16 @@ def validate_request_keys(request, valid_keys):
raise MKUserError(None, _("Invalid key: %s") % key)
+#.
+# .--Hosts---------------------------------------------------------------.
+# | _ _ _ |
+# | | | | | ___ ___| |_ ___ |
+# | | |_| |/ _ \/ __| __/ __| |
+# | | _ | (_) \__ \ |_\__ \ |
+# | |_| |_|\___/|___/\__|___/ |
+# | |
+# +----------------------------------------------------------------------+
+
# Check if the given attribute name exists, no type check
def validate_general_host_attributes(host_attributes):
# inventory_failed and site are no "real" host_attributes (TODO: Clean this
up!)
@@ -250,7 +260,90 @@ api_actions["delete_host"] = {
"locking" : True,
}
-###############
+#.
+# .--Groups--------------------------------------------------------------.
+# | ____ |
+# | / ___|_ __ ___ _ _ _ __ ___ |
+# | | | _| '__/ _ \| | | | '_ \/ __| |
+# | | |_| | | | (_) | |_| | |_) \__ \ |
+# | \____|_| \___/ \__,_| .__/|___/ |
+# | |_| |
+# +----------------------------------------------------------------------+
+
+
+def action_get_all_groups(request, group_type):
+ validate_request_keys(request, [])
+ return userdb.load_group_information().get(group_type, {})
+
+
+def action_delete_group(request, group_type):
+ validate_request_keys(request, ["groupname"])
+ groupname = request.get("groupname")
+ delete_group(groupname, group_type)
+
+
+def get_group_extra_info(request, group_type):
+ extra_info = {}
+ extra_info["alias"] = request.get("alias")
+ if group_type == "contact" and "nagvis_maps" in request:
+ extra_info["nagvis_maps"] = request["nagvis_maps"]
+ return extra_info
+
+
+def validate_group_request_keys(request, group_type):
+ if group_type == "contact":
+ validate_request_keys(request, ["groupname", "alias",
"nagvis_maps"])
+ else:
+ validate_request_keys(request, ["groupname", "alias"])
+
+
+def action_add_group(request, group_type):
+ validate_group_request_keys(request, group_type)
+ add_group(request.get("groupname"), group_type,
get_group_extra_info(request, group_type))
+
+
+def action_edit_group(request, group_type):
+ validate_group_request_keys(request, group_type)
+ edit_group(request.get("groupname"), group_type,
get_group_extra_info(request, group_type))
+
+
+def register_group_apis():
+ for group_type in [ "contact", "host", "service" ]:
+ api_actions["get_all_%sgroups" % group_type] = {
+ # Note: group_type=group_type bypasses pythons late binding behaviour
+ "handler" : lambda x, group_type=group_type:
action_get_all_groups(x, group_type),
+ "locking" : False,
+ }
+
+ api_actions["delete_%sgroup" % group_type] = {
+ # Note: group_type=group_type bypasses pythons late binding behaviour
+ "handler" : lambda x, group_type=group_type:
action_delete_group(x, group_type),
+ "locking" : True,
+ }
+
+ api_actions["add_%sgroup" % group_type] = {
+ # Note: group_type=group_type bypasses pythons late binding behaviour
+ "handler" : lambda x, group_type=group_type:
action_add_group(x, group_type),
+ "locking" : True,
+ }
+
+ api_actions["edit_%sgroup" % group_type] = {
+ # Note: group_type=group_type bypasses pythons late binding behaviour
+ "handler" : lambda x, group_type=group_type:
action_edit_group(x, group_type),
+ "locking" : True,
+ }
+
+register_group_apis() # Otherwise, group_type is known in the global scope..
+
+#.
+# .--Other---------------------------------------------------------------.
+# | ___ _ _ |
+# | / _ \| |_| |__ ___ _ __ |
+# | | | | | __| '_ \ / _ \ '__| |
+# | | |_| | |_| | | | __/ | |
+# | \___/ \__|_| |_|\___|_| |
+# | |
+# +----------------------------------------------------------------------+
def action_discover_services(request):
validate_request_keys(request, ["hostname", "mode"])
@@ -339,3 +432,4 @@ api_actions["activate_changes"] = {
"locking" : True,
}
+