1 # -*- coding: utf-8 -*-
3 # This file is part of Karesansui.
5 # Copyright (C) 2010 HDE, Inc.
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
15 from karesansui.lib.rest import Rest, auth
16 from karesansui.lib.utils import read_file, json_dumps
17 from karesansui.lib.const import TEMPLATE_DIR, \
18 MAIL_TEMPLATE_COLLECTD_WARNING, \
19 MAIL_TEMPLATE_COLLECTD_FAILURE, \
20 MAIL_TEMPLATE_COLLECTD_OKAY, \
21 WATCH_PLUGINS, DEFAULT_LANGS
23 from karesansui.lib.checker import Checker
25 def validates_watch(obj, target, lang):
31 if target not in WATCH_PLUGINS.values():
35 checker.add_error(_('"%s" is not watch target.') %_(target))
37 if lang not in DEFAULT_LANGS:
41 checker.add_error(_('"%s" is not supported langs.') %_(lang))
43 obj.view.alert = checker.errors
46 class HostBy1WatchTemplate(Rest):
48 def _GET(self, *param, **params):
49 host_id = self.chk_hostby1(param)
50 if host_id is None: return web.notfound()
53 if target is None: return web.notfound()
56 if lang is None: return web.notfound()
58 if not validates_watch(self, target, lang):
59 self.logger.debug("Get watch mail template failed. Did not validate.")
60 return web.badrequest(self.view.alert)
62 template_dir = "%s/%s" % (TEMPLATE_DIR,lang[0:2],)
64 mail_template_warning = read_file("%s/%s" % (template_dir,MAIL_TEMPLATE_COLLECTD_WARNING[target]))
65 mail_template_failure = read_file("%s/%s" % (template_dir,MAIL_TEMPLATE_COLLECTD_FAILURE[target]))
66 mail_template_okay = read_file("%s/%s" % (template_dir,MAIL_TEMPLATE_COLLECTD_OKAY[target]))
69 "mail_template_warning" : mail_template_warning,
70 "mail_template_failure" : mail_template_failure,
71 "mail_template_okay" : mail_template_okay,
74 self.view.data = json_dumps(data)
78 '/host/(\d+)/watch/([a-zA-Z0-9]+)/([a-z]{2}|[a-z]{2}_[A-Z]{2})?(\.json)$', HostBy1WatchTemplate,