<commit_before>#!/usr/bin/env python
########################################
#
# alert-logger.py - Alert Logger module
#
########################################

import os
import sys
import time
try:
    import json
except ImportError:
    import simplejson as json
import stomp
import logging
import urllib2

__version__ = '1.0'

BROKER_LIST  = [('localhost', 61613)] # list of brokers for failover
LOGGER_QUEUE = '/queue/logger' # XXX note use of queue not topic because all alerts should be logged

LOGFILE = '/var/log/alerta/alert-logger.log'
PIDFILE = '/var/run/alerta/alert-logger.pid'

ES_SERVER   = 'localhost'
ES_BASE_URL = 'http://%s:9200/logstash' % (ES_SERVER)

class MessageHandler(object):
    def on_error(self, headers, body):
        logging.error('Received an error %s', body)

    def on_message(self, headers, body):
        logging.debug("Received alert : %s", body)

        alert = dict()
        alert = json.loads(body)
        if not alert['tags']:           # Kibana GUI borks if tags are null
            alert['tags'] = 'none'

        logging.info('%s : %s', alert['lastReceiveId'], alert['summary'])

        # Index alerts in ElasticSearch using Logstash format so that logstash GUI and/or Kibana can be used as frontends
        logstash = dict() 
        logstash['@message']     = alert['summary']
        logstash['@source']      = alert['resource']
        logstash['@source_host'] = 'not_used'
        logstash['@source_path'] = alert['origin']
        logstash['@tags']        = alert['tags']
        logstash['@timestamp']   = alert['lastReceiveTime']
        logstash['@type']        = alert['type']
        logstash['@fields']      = alert

        try:
            url = "%s/%s" % (ES_BASE_URL, alert['type'])
            response = urllib2.urlopen(url, json.dumps(logstash)).read()
        except Exception, e:
            logging.error('%s : Alert indexing to %s failed - %s', alert['lastReceiveId'], url, e)
            return

        id = json.loads(response)['_id']
        logging.info('%s : Alert indexed at %s/%s/%s', alert['lastReceiveId'], ES_BASE_URL, alert['type'], id)
        
    def on_disconnected(self):
        global conn

        logging.warning('Connection lost. Attempting auto-reconnect to %s', LOGGER_QUEUE)
        conn.start()
        conn.connect(wait=True)
        conn.subscribe(destination=LOGGER_QUEUE, ack='auto')

def main():<commit_msg>Missing tags should not cause logger to crash<commit_after>#!/usr/bin/env python
########################################
#
# alert-logger.py - Alert Logger module
#
########################################

import os
import sys
import time
try:
    import json
except ImportError:
    import simplejson as json
import stomp
import logging
import urllib2

__version__ = '1.0'

BROKER_LIST  = [('localhost', 61613)] # list of brokers for failover
LOGGER_QUEUE = '/queue/logger' # XXX note use of queue not topic because all alerts should be logged

LOGFILE = '/var/log/alerta/alert-logger.log'
PIDFILE = '/var/run/alerta/alert-logger.pid'

ES_SERVER   = 'localhost'
ES_BASE_URL = 'http://%s:9200/logstash' % (ES_SERVER)

class MessageHandler(object):
    def on_error(self, headers, body):
        logging.error('Received an error %s', body)

    def on_message(self, headers, body):
        logging.debug("Received alert : %s", body)

        alert = dict()
        alert = json.loads(body)

        logging.info('%s : %s', alert['lastReceiveId'], alert['summary'])

        if 'tags' not in alert or not alert['tags']:           # Kibana GUI borks if tags are null
            alert['tags'] = 'none'

        # Index alerts in ElasticSearch using Logstash format so that logstash GUI and/or Kibana can be used as frontends
        logstash = dict() 
        logstash['@message']     = alert['summary']
        logstash['@source']      = alert['resource']
        logstash['@source_host'] = 'not_used'
        logstash['@source_path'] = alert['origin']
        logstash['@tags']        = alert['tags']
        logstash['@timestamp']   = alert['lastReceiveTime']
        logstash['@type']        = alert['type']
        logstash['@fields']      = alert

        try:
            url = "%s/%s" % (ES_BASE_URL, alert['type'])
            response = urllib2.urlopen(url, json.dumps(logstash)).read()
        except Exception, e:
            logging.error('%s : Alert indexing to %s failed - %s', alert['lastReceiveId'], url, e)
            return

        id = json.loads(response)['_id']
        logging.info('%s : Alert indexed at %s/%s/%s', alert['lastReceiveId'], ES_BASE_URL, alert['type'], id)
        
    def on_disconnected(self):
        global conn

        logging.warning('Connection lost. Attempting auto-reconnect to %s', LOGGER_QUEUE)
        conn.start()
        conn.connect(wait=True)
        conn.subscribe(destination=LOGGER_QUEUE, ack='auto')

def main():
{'fraction_non_alphanumeric': 0.1640081799591002, 'fraction_numerical': 0.005316973415132925, 'mean_word_length': 2.9253611556982344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6025742', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1478, 'n_words': 383}