Skip to content

Commit

Permalink
Merge pull request #1 from guardian/master
Browse files Browse the repository at this point in the history
commit
  • Loading branch information
msupino committed Oct 12, 2016
2 parents 1c59746 + eff95a9 commit 632ba8e
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ build
.DS_Store
dist
rpmbuild
*.log
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.8.3
4.8.5
24 changes: 16 additions & 8 deletions alerta/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from alerta.app.alert import DateEncoder

LOG_FORMAT = '%(asctime)s - %(name)s[%(process)d]: %(levelname)s - %(message)s'
LOG_FORMAT = '%(asctime)s - %(name)s[%(process)d]: %(levelname)s - %(message)s [in %(pathname)s:%(lineno)d]'

app = Flask(__name__, static_url_path='')

Expand Down Expand Up @@ -66,14 +66,22 @@
if 'PLUGINS' in os.environ:
app.config['PLUGINS'] = os.environ['PLUGINS'].split(',')

# Setup logging
from logging import getLogger
loggers = [app.logger, getLogger('werkzeug'), getLogger('requests'), getLogger('flask_cors')]

if app.debug:
app.debug_log_format = LOG_FORMAT
app.logger.setLevel(logging.DEBUG)
else:
stderr_handler = logging.StreamHandler()
stderr_handler.setFormatter(logging.Formatter(LOG_FORMAT))
app.logger.addHandler(stderr_handler)
app.logger.setLevel(logging.INFO)
for logger in loggers:
logger.setLevel(logging.DEBUG)

if app.config['LOG_FILE']:
from logging.handlers import RotatingFileHandler
del app.logger.handlers[:]
logfile_handler = RotatingFileHandler(app.config['LOG_FILE'], maxBytes=100000, backupCount=2)
logfile_handler.setLevel(logging.DEBUG)
logfile_handler.setFormatter(logging.Formatter(LOG_FORMAT))
for logger in loggers:
logger.addHandler(logfile_handler)

# Runtime config check
if app.config['CUSTOMER_VIEWS'] and not app.config['AUTH_REQUIRED']:
Expand Down
1 change: 1 addition & 0 deletions alerta/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self):
def connect(self):

mongo_uri = (os.environ.get('MONGO_URI', None) or
os.environ.get('MONGODB_URI', None) or
os.environ.get('MONGOHQ_URL', None) or
os.environ.get('MONGOLAB_URI', None))
if mongo_uri:
Expand Down
3 changes: 1 addition & 2 deletions alerta/app/management/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
from alerta import build
from alerta.version import __version__

LOG = logging.getLogger(__name__)

LOG = app.logger

switches = [
Switch('auto-refresh-allow', 'Allow consoles to auto-refresh alerts', SwitchState.to_state(app.config['AUTO_REFRESH_ALLOW'])),
Expand Down
5 changes: 3 additions & 2 deletions alerta/app/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from alerta.app import db
from alerta.version import __version__

LOG = app.logger

def main():

Expand All @@ -27,6 +28,6 @@ def main():
)
args = parser.parse_args()

app.logger.info('Starting alerta version %s ...', __version__)
app.logger.info('Using MongoDB version %s ...', db.get_version())
LOG.info('Starting alerta version %s ...', __version__)
LOG.info('Using MongoDB version %s ...', db.get_version())
app.run(host='0.0.0.0', port=args.port, debug=args.debug, threaded=True)
10 changes: 7 additions & 3 deletions alerta/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

import abc
import logging

from six import add_metaclass
from pkg_resources import iter_entry_points, load_entry_point, DistributionNotFound

from alerta.app import app

LOG = app.logger
LOG = logging.getLogger('alerta.plugins')


class RejectException(Exception):
Expand Down Expand Up @@ -44,28 +45,31 @@ def __init__(self):
self.register()

def register(self):
plugins_enabled = []
for ep in iter_entry_points('alerta.plugins'):
LOG.debug("Server plug-in '%s' found.", ep.name)
try:
if ep.name in app.config['PLUGINS']:
plugin = ep.load()
if plugin:
self.plugins[ep.name] = plugin()
plugins_enabled.append(ep.name)
LOG.info("Server plug-in '%s' enabled.", ep.name)
else:
LOG.debug("Server plug-in '%s' not enabled in 'PLUGINS'.", ep.name)
except Exception as e:
LOG.error("Server plug-in '%s' could not be loaded: %s", ep.name, e)
LOG.info("All server plug-ins enabled: %s" % ', '.join(plugins_enabled))
try:
self.rules = load_entry_point('alerta-routing', 'alerta.routing', 'rules')
except (DistributionNotFound, ImportError):
LOG.info('Failed to load any plugin routing rules. All plugins will be evaluated.')
LOG.info('No plug-in routing rules found. All plug-ins will be evaluated.')

def routing(self, alert):
try:
if self.plugins and self.rules:
return self.rules(alert, self.plugins)
except Exception as e:
LOG.warning("Plugin routing rules failed: %s" % str(e))
LOG.warning("Plug-in routing rules failed: %s" % str(e))

return self.plugins.values()
12 changes: 8 additions & 4 deletions alerta/plugins/reject.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import re
import logging

from alerta.app import app
from alerta.plugins import PluginBase, RejectException

LOG = logging.getLogger('alerta.plugins.reject')

ORIGIN_BLACKLIST_REGEX = [re.compile(x) for x in app.config['ORIGIN_BLACKLIST']]

ALLOWED_ENVIRONMENTS = app.config.get('ALLOWED_ENVIRONMENTS', [])

class RejectPolicy(PluginBase):

def pre_receive(self, alert):
if any(regex.match(alert.origin) for regex in ORIGIN_BLACKLIST_REGEX):
LOG.warning("[POLICY] Alert origin '%s' has been blacklisted" % alert.origin)
raise RejectException("[POLICY] Alert origin '%s' has been blacklisted" % alert.origin)

if alert.environment not in app.config['ALLOWED_ENVIRONMENTS']:
raise RejectException("[POLICY] Alert environment must be one of %s" %
', '.join(app.config['ALLOWED_ENVIRONMENTS']))
if alert.environment not in ALLOWED_ENVIRONMENTS:
LOG.warning("[POLICY] Alert environment must be one of %s" % ', '.join(ALLOWED_ENVIRONMENTS))
raise RejectException("[POLICY] Alert environment must be one of %s" % ', '.join(ALLOWED_ENVIRONMENTS))

if not alert.service:
LOG.warning("[POLICY] Alert must define a service")
raise RejectException("[POLICY] Alert must define a service")

return alert
Expand Down
3 changes: 3 additions & 0 deletions alerta/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

DEBUG = False

LOGGER_NAME = 'alerta'
LOG_FILE = None

SECRET_KEY = 'changeme'

QUERY_LIMIT = 10000 # maximum number of alerts returned by a single query
Expand Down
2 changes: 1 addition & 1 deletion alerta/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.8.3'
__version__ = '4.8.5'

0 comments on commit 632ba8e

Please sign in to comment.