import logging import os import sys import cherrypy import splunk import splunk.util import splunk.appserver.mrsparkle.controllers as controllers import splunk.appserver.mrsparkle.lib.util as util from splunk.appserver.mrsparkle.lib.decorators import expose_page from splunk.appserver.mrsparkle.lib.routes import route dir = os.path.join(util.get_apps_dir(), __file__.split('.')[-2], 'bin') if not dir in sys.path: sys.path.append(dir) from unix.models.saved_search import SavedSearch from unix.models.alerts import AlertOverlay from unix.models.macro import Macro logger = logging.getLogger('splunk') MACRO_PREFIX = '_unix_alert_threshold_' class UnixAlertsConfig(controllers.BaseController): '''Unix Alerts Config Controller''' @route('/:client_app/:action=show') @expose_page(must_login=True, methods=['GET'])
def get_template(self, uri): """ Override Mako's template lookup routine to add support for templates located by an absolute path rather than a relative one. Absolute path names must begin with an equals sign ('=') to denote that they are absolute relative to the filesystem root, rather than the root of one of the TemplateLookup directories Also allows templates to be defined in apps that contain a templates folder Priority is given to app defined templates so that they may override system defined ones. If the requested uri has an appname followed by a colon (':') then followed by a template path, this will attempt to find the given template within the specified application. e.g. uri = '/some_application:/event_renderers/_some_renderer.html" will attempt to return _some_renderer.html from /etc/apps/some_application/appserver/event_renderers """ try: if self.filesystem_checks: return self._check(uri, self._collection[uri]) else: return self._collection[uri] except KeyError: if uri[0] == '=' and os.path.exists(uri[1:]): return self._load(uri[1:], self._find_i18n_template(uri)) else: u = re.sub(r'^\/+', '', uri) # see if an app has a custom template appflag = "APP/" if u.startswith(appflag): basedir = os.path.abspath(util.get_apps_dir()) srcfile = self._normalize_template_path(os.path.join(basedir, u[len(appflag):])) if os.path.exists(srcfile) and os.path.abspath(srcfile).startswith(basedir): return self._load(srcfile, uri) else: # see if it's been defined in a specific app. we only do this if we find a ':' in the requested path if u.find(self.TEMPLATE_APPSCOPE_SEPARATOR) > -1: appScope = u.split(self.TEMPLATE_APPSCOPE_SEPARATOR)[0] if appScope in [k for k,v in local_apps.items()]: app = local_apps.apps[appScope] appPath = re.sub(r'^\/+', '', u[len(appScope)+1:] ) basedir = os.path.abspath(posixpath.join(app['full_path'], 'appserver')) srcfile = self._normalize_template_path(posixpath.join(basedir, appPath ) ) if os.path.exists(srcfile) and os.path.abspath(srcfile).startswith(basedir): return self._load(srcfile, uri) # now see if an app has defined this template for a module for appname, app in local_apps.items(): basedir = os.path.abspath(posixpath.join(app['full_path'], 'appserver', 'modules')) srcfile = self._normalize_template_path(posixpath.join(basedir, u)) if os.path.exists(srcfile) and os.path.abspath(srcfile).startswith(basedir): return self._load(srcfile, uri) # finally, look in the directories we were given at setup for dir in self.directories: srcfile = posixpath.normpath(posixpath.join(dir, u)) srcfile = self._find_i18n_template(srcfile) if os.path.exists(srcfile) and os.path.abspath(srcfile).startswith(dir): return self._load(srcfile, uri) raise exceptions.TopLevelLookupException(_("Splunk has failed to locate the template for uri '%s'." % uri))