<commit_before>"""Copyright 2014 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from datetime import datetime
from django.utils import timezone
from django.db.models import Q
import logging
import requests
from cito_engine.models import Event, EventAction, EventActionCounter, EventActionLog, Incident, IncidentLog
from reports.actions import update_reports
from . import json_formatter
from .event_actions import update_eventaction_counters


class ProcessIncident(object):
    def __init__(self, incident, message):
        self.incident = incident
        self.incident_message = message
        self.logger = logging.getLogger('poller_logger')
        self.begin()

    def begin(self):
        for event_action in EventAction.objects.filter(event=self.incident.event, isEnabled=True):
            if self.check_incident_threshold(event_action):
                self.execute_plugin(event_action)

    def check_incident_threshold(self, event_action):
        """
        Check if an incident has met its threshold so we can dispatch an action
        """
        event_action_counter, create = EventActionCounter.objects.get_or_create(incident=self.incident,
                                                                                event_action=event_action)
        return event_action_counter.check_threshold()

    def execute_plugin(self, event_action):
        """
        Fires a relevant action based on cito_engine.models.EventAction settings
        """
        self.logger.info('Firing incident for [%s] [%s] [%s] [%s]' % (self.incident.id, self.incident.event.id,
                                                                      self.incident.element, event_action.plugin))
        # If the plugin is not enabled, lets move on.
        if not event_action.plugin.status:
            return

        incident_info = self.incident.incidentInfo
        plugin_json = json_formatter.create_json_parameters(event_action, self.incident, self.incident_message)

        try:
            response = requests.post(event_action.plugin.server.url+'/runplugin', data=plugin_json,
                                     verify=event_action.plugin.server.ssl_verify)
            EventActionLog.objects.create(eventAction=event_action, text=response.text, incident=self.incident)
        except Exception as e:
            event_action_error = "Error executing Plugin:%s: ErrMsg:%s" % (event_action.plugin.name, e)
            EventActionLog.objects.create(eventAction=event_action, text=event_action_error, incident=self.incident)
            self.logger.error(event_action_error)
        finally:
            return


def add_incident(e, timestamp):
    """
    Adds an incident sent by cito_engine.poller
    """
    incident_time_diff = 0
    create = False
    logger = logging.getLogger('poller_logger')
    try:
        event_id = int(e['eventid'])
        element = e['element']
        msg = e['message']
    except Exception as excep:
        logger.error('Bad Incident:[%s], reason:[%s] message will be ignored.' % (excep, e))
        return

    # Ignore incident if any field is empty
    if not event_id or not element or not msg:
        logger.error('Bad Incident:[%s], reason:one or more fields are empty' % e)
        return

    # Ignore incident if event does not exist
    try:
        event = Event.objects.get(pk=event_id)
    except Event.DoesNotExist:
        return
    #TODO: SQL sanity checks
    #TODO: Msg len check??

    incident_time = timezone.make_aware(datetime.fromtimestamp(float(timestamp)), timezone.get_default_timezone())
    logger.debug('Incident Time: %s' % incident_time)
    try:
        i = Incident.objects.get(~Q(status__iexact='Cleared'), event=event, element=element)
    except Incident.MultipleObjectsReturned:
        # If there are multiple open incidents for same eventID and element
        # we close them.
        print "Multiple open objects found for %s " % element
        close_duplicate_incidents(event, element)
        return
    except Incident.DoesNotExist:
        create = True
        i = Incident.objects.create(event=event, element=element)
<commit_msg>fix(actions): remove print and use logger.error()<commit_after>"""Copyright 2014 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from datetime import datetime
from django.utils import timezone
from django.db.models import Q
import logging
import requests
from cito_engine.models import Event, EventAction, EventActionCounter, EventActionLog, Incident, IncidentLog
from reports.actions import update_reports
from . import json_formatter
from .event_actions import update_eventaction_counters


class ProcessIncident(object):
    def __init__(self, incident, message):
        self.incident = incident
        self.incident_message = message
        self.logger = logging.getLogger('poller_logger')
        self.begin()

    def begin(self):
        for event_action in EventAction.objects.filter(event=self.incident.event, isEnabled=True):
            if self.check_incident_threshold(event_action):
                self.execute_plugin(event_action)

    def check_incident_threshold(self, event_action):
        """
        Check if an incident has met its threshold so we can dispatch an action
        """
        event_action_counter, create = EventActionCounter.objects.get_or_create(incident=self.incident,
                                                                                event_action=event_action)
        return event_action_counter.check_threshold()

    def execute_plugin(self, event_action):
        """
        Fires a relevant action based on cito_engine.models.EventAction settings
        """
        self.logger.info('Firing incident for [%s] [%s] [%s] [%s]' % (self.incident.id, self.incident.event.id,
                                                                      self.incident.element, event_action.plugin))
        # If the plugin is not enabled, lets move on.
        if not event_action.plugin.status:
            return

        incident_info = self.incident.incidentInfo
        plugin_json = json_formatter.create_json_parameters(event_action, self.incident, self.incident_message)

        try:
            response = requests.post(event_action.plugin.server.url+'/runplugin', data=plugin_json,
                                     verify=event_action.plugin.server.ssl_verify)
            EventActionLog.objects.create(eventAction=event_action, text=response.text, incident=self.incident)
        except Exception as e:
            event_action_error = "Error executing Plugin:%s: ErrMsg:%s" % (event_action.plugin.name, e)
            EventActionLog.objects.create(eventAction=event_action, text=event_action_error, incident=self.incident)
            self.logger.error(event_action_error)
        finally:
            return


def add_incident(e, timestamp):
    """
    Adds an incident sent by cito_engine.poller
    """
    incident_time_diff = 0
    create = False
    logger = logging.getLogger('poller_logger')
    try:
        event_id = int(e['eventid'])
        element = e['element']
        msg = e['message']
    except Exception as excep:
        logger.error('Bad Incident:[%s], reason:[%s] message will be ignored.' % (excep, e))
        return

    # Ignore incident if any field is empty
    if not event_id or not element or not msg:
        logger.error('Bad Incident:[%s], reason:one or more fields are empty' % e)
        return

    # Ignore incident if event does not exist
    try:
        event = Event.objects.get(pk=event_id)
    except Event.DoesNotExist:
        return
    #TODO: SQL sanity checks
    #TODO: Msg len check??

    incident_time = timezone.make_aware(datetime.fromtimestamp(float(timestamp)), timezone.get_default_timezone())
    logger.debug('Incident Time: %s' % incident_time)
    try:
        i = Incident.objects.get(~Q(status__iexact='Cleared'), event=event, element=element)
    except Incident.MultipleObjectsReturned:
        # If there are multiple open incidents for same eventID and element
        # we close them.
        logger.error('Multiple open objects found for %s ' % element)
        close_duplicate_incidents(event, element)
        return
    except Incident.DoesNotExist:
        create = True
        i = Incident.objects.create(event=event, element=element)

{'fraction_non_alphanumeric': 0.08277717976318621, 'fraction_numerical': 0.00193756727664155, 'mean_word_length': 2.8472049689440992, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '341479', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2460, 'n_words': 720}