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, \
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,
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
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
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)
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,
)
w_save(self.orm, _watch)
- #import pdb;pdb.set_trace()
modules = ["collectdplugin"]
host = m_findbyhost1(self.orm, host_id)
#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)
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)
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()
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
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)
w_update(self.orm, watch)
- #import pdb;pdb.set_trace()
plugin = watch.plugin
plugin_selector = watch.plugin_selector
modules = ["collectdplugin"]
#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)
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()
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)
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()
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):
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]))
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",
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,