Example #1
0
class SenderForm(forms.ModelForm):
    sender = forms.ChoiceField(choices=[(entry.module_name, entry.module_name)
                                        for entry in plugins.notifications()])

    class Meta:
        model = models.Sender
        exclude = ['content_object']
Example #2
0
 def driver_set(cls):
     '''Return the list of drivers for Sender model'''
     for entry in plugins.notifications():
         try:
             yield entry.module_name, entry.load()
         except ImportError:
             logger.warning('Error importing %s', entry.module_name)
Example #3
0
 def get(self, request):
     return render(
         request, 'promgen/status.html', {
             'discovery_plugins': [entry for entry in plugins.discovery()],
             'notifier_plugins':
             [entry for entry in plugins.notifications()],
         })
Example #4
0
    def post(self, request, pk):
        sender = get_object_or_404(models.Sender, id=pk)
        for entry in plugins.notifications():
            if entry.module_name == sender.sender:
                try:
                    entry.load()().test(
                        sender.value, {
                            'generatorURL': 'Promgen',
                            'status': 'Test',
                            'labels': {},
                            'annotations': {
                                'alertname': 'Test Alert',
                            },
                        })
                except:
                    logger.exception('Error sending test message with %s',
                                     entry.module_name)
                    messages.warning(
                        request,
                        'Error sending test message with ' + entry.module_name)
                else:
                    messages.info(
                        request, 'Sent test message with ' + entry.module_name)

        return HttpResponseRedirect(sender.content_object.get_absolute_url())
Example #5
0
 def get_context_data(self, **kwargs):
     context = super(Profile, self).get_context_data(**kwargs)
     context['discovery_plugins'] = [entry for entry in plugins.discovery()]
     context['notifier_plugins'] = [entry for entry in plugins.notifications()]
     context['notifiers'] = {'notifiers': models.Sender.objects.filter(obj=self.request.user)}
     context['subscriptions'] = models.Sender.objects.filter(
         sender='promgen.notification.user', value=self.request.user.username)
     return context
Example #6
0
 def driver(self):
     '''Return configured driver for Sender model instance'''
     for entry in plugins.notifications():
         if entry.module_name == self.sender:
             try:
                 return entry.load()()
             except ImportError:
                 logger.warning('Error importing %s', entry.module_name)
Example #7
0
 def test(self):
     '''Test sender plugin'''
     kwargs = {
         'commonLabels': {self.content_type.name: self.content_object.name}
     }
     for entry in plugins.notifications():
         if entry.module_name == self.sender:
             plugin = entry.load()()
             plugin.test(self.value, kwargs)
Example #8
0
    def driver(self):
        '''Return configured driver for Sender model instance'''
        if self.sender in self.__driver:
            return self.__driver[self.sender]

        for entry in plugins.notifications():
            try:
                self.__driver[entry.module_name] = entry.load()()
            except ImportError:
                logger.warning('Error importing %s', entry.module_name)
        return self.__driver[self.sender]
Example #9
0
def send_alert(sender, target, data, alert_pk=None):
    '''
    Send alert to specific target

    alert_pk is used for debugging purposes
    '''
    logger.debug('Sending %s %s', sender, target)
    for plugin in plugins.notifications():
        if sender == plugin.module_name:
            instance = plugin.load()()
            instance._send(target, data)
Example #10
0
def send_notification(sender, body):
    body = json.loads(body)
    logger.info('Attempting to send alert for %s', sender)
    for plugin in plugins.notifications():
        if sender == plugin.module_name:
            try:
                instance = plugin.load()()
                count = instance.send(body)
                logger.info('Sent %d alerts with %s', count, sender)
            except:
                logger.exception('Error sending message')
Example #11
0
    def test(self):
        '''
        Test sender plugin

        Uses the same test json from our unittests but subs in the currently
        tested object as part of the test data
        '''
        data = tests.PromgenTest.data_json('examples', 'alertmanager.json')
        data['commonLabels'][self.content_type.name] = self.content_object.name
        for alert in data.get('alerts', []):
            alert['labels'][self.content_type.name] = self.content_object.name

        for entry in plugins.notifications():
            if entry.module_name == self.sender:
                plugin = entry.load()()
                plugin.test(self.value, data)
Example #12
0
    def post(self, request, *args, **kwargs):
        body = json.loads(request.body.decode('utf-8'))
        sent = collections.defaultdict(int)
        error = collections.defaultdict(int)

        for entry in plugins.notifications():
            logger.debug('Sending notification to %s', entry.module_name)
            try:
                Sender = entry.load()
                logger.debug(Sender)
                count = Sender().send(body.copy())
            except Exception:
                logger.exception('Error sending alert')
                error[entry.module_name] += 1
            else:
                try:
                    sent[entry.module_name] += count
                except TypeError:
                    logger.error('Invalid count returned from %s',
                                 entry.module_name)
        logger.info('Notifications Sent: %s', dict(sent))
        if error:
            logger.error('Notification Errors: %s', dict(error))
        return HttpResponse('OK')
Example #13
0
def load(name):
    for driver in plugins.notifications():
        if name == driver.module_name:
            return driver.load()()
    raise ImportError("Unknown notification plugin %s" % name)
Example #14
0
 def plugins(cls):
     for entry in plugins.notifications():
         try:
             yield entry.module_name, entry.load()
         except ImportError:
             logger.warning('Error importing %s', entry.module_name)
Example #15
0
 def post(self, request, *args, **kwargs):
     body = request.body.decode('utf-8')
     for entry in plugins.notifications():
         entry.load().process(body)
     return HttpResponse('OK', status=202)
Example #16
0
# Copyright (c) 2017 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

import logging

from promgen import plugins, prometheus  # NOQA

logger = logging.getLogger(__name__)
for plugin in plugins.notifications():
    try:
        plugin.load()
    except ImportError:
        logger.exception('Error loading %s with Celery', plugin.module_name)