OSDN Git Service

Add validate
authorkeisuke fukawa <keisuke@karesansui-project.info>
Mon, 12 Jul 2010 06:50:40 +0000 (15:50 +0900)
committerkeisuke fukawa <keisuke@karesansui-project.info>
Mon, 12 Jul 2010 06:50:40 +0000 (15:50 +0900)
karesansui/gadget/hostby1watch.py
karesansui/gadget/hostby1watchby1.py
karesansui/gadget/hostby1watchtemplate.py
karesansui/lib/const.py
karesansui/templates/default/hostby1watch/hostby1watch.input

index ad7c226..e43995f 100644 (file)
@@ -43,7 +43,7 @@ from karesansui.db.access.machine import findbyhost1 as m_findbyhost1, \
 from karesansui.lib.collectd.utils import  create_plugin_selector, \
      get_collectd_version, create_threshold_value
 
-from karesansui.lib.const import WATCH_LIST_RANGE, \
+from karesansui.lib.const import WATCH_LIST_RANGE, WATCH_PLUGINS, \
     COLLECTD_PLUGIN_CPU,           COLLECTD_PLUGIN_DF, \
     COLLECTD_PLUGIN_INTERFACE,     COLLECTD_PLUGIN_LIBVIRT, \
     COLLECTD_PLUGIN_MEMORY,        COLLECTD_PLUGIN_LOAD, \
@@ -77,6 +77,21 @@ def validates_watch(obj):
         check = False
         checker.add_error(_('"%s" is required.') %_('Name'))
 
+    if is_param(obj.input, 'watch_target'):
+        check = checker.check_string(_('Watch Target'),
+                                     obj.input.watch_target,
+                                     CHECK_EMPTY | CHECK_ONLYSPACE,
+                                     None,
+                                     ) and check
+        if obj.input.watch_target not in WATCH_PLUGINS.values():
+            check = False
+            # TRANSLATORS:
+            #  %sは監視対象ではありません。
+            checker.add_error(_('"%s" is not watch target.') %_(obj.input.watch_target))
+    else:
+        check = False
+        checker.add_error(_('"%s" is required.') %_('Watch Target'))
+
     if is_param(obj.input, 'continuation_count'):
         check = checker.check_number(_('Continuation Count'),
                                      obj.input.continuation_count,
@@ -172,16 +187,7 @@ class HostBy1Watch(Rest):
         if host_id is None: return web.notfound()
 
         if self.is_mode_input() is True:
-            plugins = {
-                "cpu"       : COLLECTD_PLUGIN_CPU,
-                "df"        : COLLECTD_PLUGIN_DF,
-                "interface" : COLLECTD_PLUGIN_INTERFACE,
-                "libvirt"   : COLLECTD_PLUGIN_LIBVIRT,
-                "load"      : COLLECTD_PLUGIN_LOAD,
-                "memory"    : COLLECTD_PLUGIN_MEMORY,
-                }
-            self.view.plugins = plugins
-
+            self.view.plugins = WATCH_PLUGINS
             self.view.cpu_type_instance = COLLECTD_CPU_TYPE_INSTANCE
             self.view.memory_type_instance = COLLECTD_MEMORY_TYPE_INSTANCE
             self.view.df_ds = COLLECTD_DF_DS
@@ -276,6 +282,7 @@ class HostBy1Watch(Rest):
         if host_id is None: return web.notfound()
 
         if not validates_watch(self):
+            self.logger.debug("Set watch failed. Did not validate.")
             return web.badrequest(self.view.alert)
 
         plugin = self.input.watch_target
@@ -340,6 +347,7 @@ class HostBy1Watch(Rest):
             plugin_ds = self.input.load_term
 
         else:
+            self.logger.debug("Set watch failed. Unknown plugin type.")
             return web.badrequest()
 
         plugin_selector = create_plugin_selector(plugin_instance, type, type_instance, plugin_ds, libvirt_host)
@@ -381,7 +389,8 @@ class HostBy1Watch(Rest):
         machine = m_findby1(self.orm, host_id)
 
         if w_is_uniq_duplication(self.orm, machine, plugin, plugin_selector) is True:
-            return web.badrequest("test duplication")
+            self.logger.debug("Set watch failed. Duplicate watch DB.")
+            return web.badrequest("Set watch failed. Duplication watch")
 
         _watch = w_new(created_user          = self.me,
                        modified_user         = self.me,
@@ -415,7 +424,6 @@ class HostBy1Watch(Rest):
                        )
         w_save(self.orm, _watch)
 
-        #import pdb;pdb.set_trace()
         modules = ["collectdplugin"]
 
         host = m_findbyhost1(self.orm, host_id)
@@ -423,9 +431,9 @@ class HostBy1Watch(Rest):
         #extra_args = {}
         dop = read_conf(modules, webobj=self, machine=host, extra_args=extra_args)
         if dop is False:
+            self.logger.debug("Set watch failed. Failed read conf.")
             return web.internalerror('Internal Server Error. (Read Conf)')
 
-        # スレッショルドの作成
         params = {}
         if threshold_type == "max":
             params['WarningMax'] = str(threshold_val1)
@@ -443,6 +451,7 @@ class HostBy1Watch(Rest):
         extra_args = {"post-command": command}
         retval = write_conf(dop,  webobj=self, machine=host, extra_args=extra_args)
         if retval is False:
+            self.logger.debug("Set watch failed. Failed write conf.")
             return web.internalerror('Internal Server Error. (Write Conf)')
 
         return web.created(None)
index 6dfaec2..faca332 100644 (file)
@@ -53,7 +53,8 @@ from karesansui.lib.const import COLLECTD_PLUGIN_CPU, \
     FQDN_MIN_LENGTH,         FQDN_MAX_LENGTH, \
     PORT_MIN_NUMBER,         PORT_MAX_NUMBER, \
     EMAIL_MIN_LENGTH,        EMAIL_MAX_LENGTH, \
-    THRESHOLD_VAL_MIN,          WATCH_INTERVAL
+    THRESHOLD_VAL_MIN,      WATCH_INTERVAL, \
+    WATCH_PLUGINS
 
 def validates_watch(obj):
     checker = Checker()
@@ -170,16 +171,7 @@ class HostBy1WatchBy1(Rest):
 
         watch = w_findby1(self.orm, watch_id)
         self.view.watch = watch
-
-        plugins = {
-            "cpu"       : COLLECTD_PLUGIN_CPU,
-            "df"        : COLLECTD_PLUGIN_DF,
-            "interface" : COLLECTD_PLUGIN_INTERFACE,
-            "libvirt"   : COLLECTD_PLUGIN_LIBVIRT,
-            "load"      : COLLECTD_PLUGIN_LOAD,
-            "memory"    : COLLECTD_PLUGIN_MEMORY,
-            }
-        self.view.plugins = plugins
+        self.view.plugins = WATCH_PLUGINS
 
         plugin_selector = plugin_selector_to_dict(watch.plugin_selector)
         self.view.plugin_selector = plugin_selector
@@ -235,6 +227,7 @@ class HostBy1WatchBy1(Rest):
         if watch_id is None: return web.notfound()
 
         if not validates_watch(self):
+            self.logger.debug("Change watch failed. Did not validate.")
             return web.badrequest(self.view.alert)
 
         watch = w_findby1(self.orm, watch_id)
@@ -288,7 +281,6 @@ class HostBy1WatchBy1(Rest):
 
         w_update(self.orm, watch)
 
-        #import pdb;pdb.set_trace()
         plugin = watch.plugin
         plugin_selector = watch.plugin_selector
         modules = ["collectdplugin"]
@@ -298,9 +290,9 @@ class HostBy1WatchBy1(Rest):
         #extra_args = {}
         dop = read_conf(modules, webobj=self, machine=host, extra_args=extra_args)
         if dop is False:
+            self.logger.debug("Change watch failed. Failed read conf.")
             return web.internalerror('Internal Server Error. (Read Conf)')
 
-        # スレッショルドの作成
         params = {}
         if threshold_type == "max":
             params['WarningMax'] = str(threshold_val1)
@@ -319,6 +311,7 @@ class HostBy1WatchBy1(Rest):
         extra_args = {"post-command": command}
         retval = write_conf(dop,  webobj=self, machine=host, extra_args=extra_args)
         if retval is False:
+            self.logger.debug("Change watch failed. Failed write conf.")
             return web.internalerror('Internal Server Error. (Write Conf)')
 
         return web.accepted()
@@ -346,6 +339,7 @@ class HostBy1WatchBy1(Rest):
         extra_args = {'include':'^threshold_'}
         dop = read_conf(modules, webobj=self, machine=host, extra_args=extra_args)
         if dop is False:
+            self.logger.debug("Delete watch failed. Failed read conf.")
             return web.internalerror('Internal Server Error. (Read Conf)')
         delete_threshold(plugin, plugin_selector, dop=dop, webobj=self, host=host)
 
@@ -354,6 +348,7 @@ class HostBy1WatchBy1(Rest):
         extra_args = {"post-command": command}
         retval = write_conf(dop, webobj=self, machine=host, extra_args=extra_args)
         if retval is False:
+            self.logger.debug("Delete watch failed. Failed write conf.")
             return web.internalerror('Internal Server Error. (Write Conf)')
 
         return web.accepted()
index fb4b2f7..783d211 100644 (file)
@@ -16,10 +16,31 @@ from karesansui.lib.rest import Rest, auth
 from karesansui.lib.utils import read_file, json_dumps
 from karesansui.lib.const import TEMPLATE_DIR, \
     MAIL_TEMPLATE_COLLECTD_WARNING, \
-    MAIL_TEMPLATE_COLLECTD_FAILURE, MAIL_TEMPLATE_COLLECTD_OKAY
+    MAIL_TEMPLATE_COLLECTD_FAILURE, \
+    MAIL_TEMPLATE_COLLECTD_OKAY, \
+    WATCH_PLUGINS, DEFAULT_LANGS
 
-def validates_watch(obj):
+from karesansui.lib.checker import Checker
+
+def validates_watch(obj, target, lang):
+    checker = Checker()
     check = True
+    _ = obj._
+    checker.errors = []
+
+    if target not in WATCH_PLUGINS.values():
+        check = False
+        # TRANSLATORS:
+        #  %sは監視対象ではありません。
+        checker.add_error(_('"%s" is not watch target.') %_(target))
+
+    if lang not in DEFAULT_LANGS:
+        check = False
+        # TRANSLATORS:
+        #  %sの言語には対応していません。
+        checker.add_error(_('"%s" is not supported langs.') %_(lang))
+
+    obj.view.alert = checker.errors
     return check
 
 class HostBy1WatchTemplate(Rest):
@@ -33,6 +54,11 @@ class HostBy1WatchTemplate(Rest):
 
         lang = param[2]
         if lang is None: return web.notfound()
+
+        if not validates_watch(self, target, lang):
+            self.logger.debug("Get watch mail template failed. Did not validate.")
+            return web.badrequest(self.view.alert)
+
         template_dir = "%s/%s" % (TEMPLATE_DIR,lang[0:2],)
 
         mail_template_warning = read_file("%s/%s" % (template_dir,MAIL_TEMPLATE_COLLECTD_WARNING[target]))
index c85ebf5..a11e9ed 100644 (file)
@@ -347,6 +347,14 @@ COLLECTD_PLUGIN_TAIL = "tail"
 COLLECTD_PLUGIN_UPTIME = "uptime"
 COLLECTD_PLUGIN_USERS = "users"
 
+WATCH_PLUGINS = {"cpu"       : COLLECTD_PLUGIN_CPU,
+                 "df"        : COLLECTD_PLUGIN_DF,
+                 "interface" : COLLECTD_PLUGIN_INTERFACE,
+                 "libvirt"   : COLLECTD_PLUGIN_LIBVIRT,
+                 "load"      : COLLECTD_PLUGIN_LOAD,
+                 "memory"    : COLLECTD_PLUGIN_MEMORY,
+                 }
+
 COLLECTD_CPU_TYPE = "cpu"
 COLLECTD_CPU_TYPE_INSTANCE = {"IDLE" : "idle",
                               "NICE" : "nice",
index 2f2e3cd..51334d1 100644 (file)
@@ -176,7 +176,6 @@ function show_libvirt_target_section(target) {
 function set_mail_template(severity,lang) {
     target = $("#watch_target").val();
     uri = "${ctx.homepath}${ctx.path}".replace(".part", "");
-    //alert(uri + "/" + target + "/" + lang + ".json")
     ajax_json(
         uri + "/" + target + "/" + lang + ".json",
         target,