alerta / log4client / src / log4tailer / notifications.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# Log4Tailer: A multicolored python tailer for log4J logs
# Copyright (C) 2010 Jordi Carrillo Bosch

# This file is part of Log4Tailer Project.
#
# Log4Tailer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Log4Tailer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import time
from log4tailer import LogColors
from log4tailer.Properties import evalvalue
from log4tailer.Timer import Timer
from smtplib import SMTP, SMTPServerDisconnected
from log4tailer.TermColorCodes import TermColorCodes
import subprocess
from subprocess import PIPE
import threading
import httplib

try:
    import json
except:
    try:
        import simplejson as json
    except:
        print ("no json library could be imported, "
                "you need simplejson for Poster notification")

try:
    import queue
except ImportError:
    import Queue as queue

try:
    # this needs more thought... Should be activated only if required 
    # from command line.
    from smtplib import SMTP_SSL
except ImportError:
    pass

class Print(object):
    '''PrintAction: prints to stdout the
    colorized log traces'''

    def __init__(self, properties=None):
        # we can append extra information to lines regarding
        # hostname we are in
        self.hostname = False
        if properties:
            printhostname = properties.get_value('print_hostname')
            if evalvalue(printhostname):
                from socket import gethostname
                self.hostname = gethostname()

    def notify(self, message, log):
        '''msg should be colorized already
        there is a module in pypy colorize, check it out'''
        (pause, colormsg) = message.getColorizedMessage()
        if colormsg:
            if self.hostname:
                print self.hostname + ': ' + colormsg
            else:
                print colormsg
            time.sleep(pause)

    def printInit(self, message):
        (pause,colormsg) = message.getColorizedMessage()
        pause = 0
        if colormsg:
            print colormsg


class PrintShot(Print):
    """Prints log trace and takes
    an screenshot whenever we find an alertable
    log trace."""

    Pullers = ['ERROR', 'FATAL', 'CRITICAL']

    def __init__(self, properties, shot_process="import"):
        super(PrintShot, self).__init__(properties)
        self.screenshot = properties.get_value('screenshot')
        self.winid = self.get_windowsid()
        self.screenproc = [shot_process, '-window', self.winid,
                self.screenshot]

    def notify(self, message, log):
        Print.notify(self, message, log)
        msg_level = message.messageLevel.upper()
        if not message.isATarget() and msg_level not in self.Pullers:
            return
        try:
            subprocess.call(self.screenproc)
        except Exception, err:
            print err

    def get_windowsid(self):
        getId = "xprop -root | grep '_NET_ACTIVE_WINDOW(WINDOW)'"
        try:
            proc = subprocess.Popen(getId, shell = True,
                    stdout = PIPE, stderr = PIPE)
        except Exception, err:
            print err
            return
        res, err = proc.communicate()
        if err:
            return
        winid = (res.split('#'))[1].strip()
        return winid

class Inactivity(object):
    '''sends an email or print
    alert in case too much inactivity
    in the log.
    This action must be triggered everytime
    we scan in the log.'''

    InactivityActionNotification = 'inactivitynotification'

    def __init__(self, inactivityTime, properties=None):
        self.inactivityTime = inactivityTime
        self.logColors = LogColors.LogColors()
        self.acumulativeTime = 0
        self.mailAction = None
        notification = None
        if properties:
            notification = properties.get_value(
                    Inactivity.InactivityActionNotification)
            self.logColors.parse_config(properties)
        self.notification = notification or 'print'
        self.alerted = False
        self.alerting_msg = 'Inactivity action detected'

    def notify(self, message, log):
        plainmessage, logpath = message.getPlainMessage()
        timer = log.inactivityTimer
        if not plainmessage:
            ellapsedTime = timer.inactivityEllapsed()
            if self.alerted:
                # already alerted
                self.alerted = False
            if ellapsedTime > float(self.inactivityTime):
                self.alerted = True
                log.setInactivityAccTime(ellapsedTime)
                messageAlert = ("Inactivity in the log " + logpath + " for " +
                        str(log.inactivityAccTime) + " seconds")
                if self.notification == 'print':
                    print (self.logColors.backgroundemph+messageAlert+
                            self.logColors.reset)
                elif self.notification == 'mail':
                    self.mailAction.setBodyMailAction(messageAlert)
                    self.mailAction.triggerAction(message,log)
                    self.mailAction.setBodyMailAction(None)
                timer.reset()
        # else if we got sth in message then, means we got
        # some kind of activity, so do nothing
        else:
            #start the timer again
            timer.reset()
            log.setInactivityAccTime(0)
            ellapsedTime = 0
            self.alerted = False

    def getNotificationType(self):
        return self.notification

    def setMailNotification(self,mailAction):
        '''sets a mailAction for inactivityAction notification'''
        self.notification = 'mail'
        self.mailAction = mailAction

    def getMailAction(self):
        return self.mailAction

class Mail(object):
    """Common actions to be taken
    by the Tailer"""

    mailLevels = ['CRITICAL', 'ERROR', 'FATAL']
    weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    monthname = [None,
                 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

    def __init__(self, fro = None, to = None, hostname = None, user = None,
            passwd = None, port = 25, ssl = "False"):
        self.fro = fro
        self.to = to
        self.hostname = hostname
        self.user = user
        self.passwd = passwd
        self.conn = None
        self.bodyMailAction = None
        self.port = port
        self.ssl = ssl
        self.connection_method = "smtp"
        if self.ssl == "True":
            self.connection_method = "smtp_ssl"

    def date_time(self):
        """
        Taken from logging.handlers SMTP python distribution

        Return the current date and time formatted for a MIME header.
        Needed for Python 1.5.2 (no email package available)
        """
        year, month, day, hh, mm, ss, wd, _, _ = time.gmtime(time.time())
        s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
                self.weekdayname[wd],
                day, self.monthname[month], year,
                hh, mm, ss)
        return s

    def getNow(self):
        try:
            from email.utils import formatdate
        except ImportError:
            formatdate = self.date_time
        return formatdate()

    def notify(self,message,log):
        '''msg to print, send by email, whatever...'''

        body = self.bodyMailAction
        if not body:
            if (message.messageLevel not in Mail.mailLevels
                    and not message.isATarget()):
                return
            message, logpath = message.getPlainMessage()
            title = "Alert found for log "+logpath
            fancyheader = len(title)*'='
            body = fancyheader+"\n"
            body += title+"\n"
            body += fancyheader+"\n"
            body += message+"\n"

        now = self.getNow()

        msg = ("Subject: Log4Tailer alert\r\nFrom: %s\r\nTo: "
                "%s\r\nDate: %s\r\n\r\n" % (self.fro,self.to,now)+ body)
        timer = log.mailTimer
        try:
            if timer.awaitSend(log.triggeredNotSent):
                log.triggeredNotSent = True
                return
            self.conn.sendmail(self.fro,self.to,msg)
        except SMTPServerDisconnected:
            # server could have disconnected
            # after a long inactivity. Connect
            # again and send the corresponding
            # alert
            self.connectSMTP()
            self.conn.sendmail(self.fro,self.to,msg)

        self.bodyMailAction = None
        log.triggeredNotSent = False
        return

    def setBodyMailAction(self,body):
        self.bodyMailAction = body

    def sendNotificationMail(self,body):
        '''Sends a notification mail'''
        now = self.getNow()
        msg = ("Subject: Log4Tailer Notification Message\r\nFrom: %s\r\nTo: "
                "%s\r\nDate: %s\r\n\r\n" % (self.fro,self.to,now)+ body)
        try:
            self.conn.sendmail(self.fro,self.to,msg)
        except SMTPServerDisconnected:
            self.connectSMTP()
            self.conn.sendmail(self.fro,self.to,msg)

    def _connect_smtp(self):
        conn = SMTP(self.hostname, self.port)
        return conn

    def _connect_smtp_ssl(self):
        print "using ssl connection"
        conn = SMTP_SSL(self.hostname, self.port)
        return conn

    def connectSMTP(self):
        connection_method = "_connect_" + self.connection_method
        conn = getattr(self, connection_method)()
        try:
            conn.connect(self.hostname, self.port)
            conn.login(self.user, self.passwd)
            print "connected"
        except Exception, ex:
            print ex
            print "Failed to connect to " + self.hostname
            sys.exit()
        self.conn = conn

    def quitSMTP(self):
        try:
            self.conn.quit()
        except:
            print "failed to quit SMTP connection"
            sys.exit()

class Filter(Print):
    """ When a pattern is found, it will notify
    the user, otherwise nothing will be notified. It
    would be a mix of tail and grep
    """
    def __init__(self, pattern):
        super(Filter, self).__init__()
        self.pattern = pattern

    def _get_plainmsg(self, message):
        plainMessage = message.plainMessage
        if not plainMessage:
            return ""
        return plainMessage

    def notify(self, message, log):
        plainMessage = self._get_plainmsg(message)
        if self.pattern.search(plainMessage):
            Print.notify(self, message, log)

class IgnoreAction(Filter):

    def notify(self, message, log):
        plainMessage = self._get_plainmsg(message)
        if not self.pattern.search(plainMessage):
            Print.notify(self, message, log)


class CornerMark(object):
    """Displays a 5 char colored empty string
    at the bottom right corner of terminal in case an error, fatal or warning
    is found."""

    MARK = 5 * " "
    markable = {'FATAL' : 'backgroundemph',
            'ERROR' : 'backgroundemph',
            'WARN' : 'onyellowemph',
            'WARNING' : 'onyellowemph',
            'TARGET' : 'oncyanemph'}

    def __init__(self, gaptime):
        self.corner_time = float(gaptime)
        self.termcolors = TermColorCodes()
        self.len_mark = len(self.MARK)
        self.timer = Timer(self.corner_time)
        self.count = 0
        self.flagged = False
        self.emphcolor = 'backgroundemph'

    def corner_mark_time(self):
        return self.corner_time

    def __term_num_cols(self):
        """Returns the number columns in the current terminal using the Linux
        tputs command line tool.

        :return: The number of columns currently in the terminal.
        """
        termcols = os.popen("tput cols")
        ttcols = termcols.readline()
        termcols.close()
        ttcols = int(ttcols)
        return ttcols

    def notify(self, message, log):
        """Displays a 5 char colored empty string in case a message comes in
        with the level specified in the class attribute markable. First time we
        get a markable level, a timer is started with the number of seconds
        specified in self.corner_time and a colored string will be displayed
        for that number of seconds. The timer will not be restarted during that
        time.

        :param message: the message object wrapping the current log trace
        :param log: the log associated with the current message
        """
        level = message.messageLevel.upper()
        isTarget = message.isATarget()
        # target has priority over markable levels
        if isTarget:
            self.flagged = True
            self.emphcolor = self.markable.get("TARGET")
        elif level in self.markable:
            self.flagged = True
            self.emphcolor = self.markable.get(level)
        if self.flagged:
            if self.count == 0:
                self.timer.startTimer()
            self.count += 1
            if self.timer.corner_mark_ellapsed() < self.corner_time:
                padding = self.__term_num_cols() - self.len_mark
                trace = (padding * " " + getattr(self.termcolors,
                    self.emphcolor)+ self.MARK + self.termcolors.reset)
                print trace
            else:
                self.timer.stopTimer()
                self.count = 0
                self.flagged = False


class TriggerExecutor(threading.Thread):
    """Triggers the trigger command, one
    trigger_command at a time, in its own thread.
    """

    def __init__(self):
        threading.Thread.__init__(self)
        self.queue = queue.Queue()
        self.go = True

    def landing(self, trigger_command):
        """One command to be triggered. Enqueued
        to be executed when ready.

        :param trigger_command: command to be triggered.
        """
        self.queue.put(trigger_command)

    def run(self):
        while self.go:
            trigger_command = self.queue.get()
            if trigger_command == 'stop':
                continue
            try:
                subprocess.call(' '.join(trigger_command), shell = True)
            except Exception, err:
                print err

    def stop(self):
        self.go = False

class Executor(object):
    """Will execute a program if a certain condition is given"""
    PlaceHolders = '%s'
    Pullers = ['ERROR', 'FATAL', 'CRITICAL']

    def __init__(self, properties):
        executable = properties.get_value('executor')
        if not executable:
            raise Exception("need to provide executor option")
        self.executable = executable.split(' ')
        self.full_trigger_active = False
        if self.PlaceHolders in self.executable:
            self.full_trigger_active = True
        self.trigger_executor = TriggerExecutor()
        self.started = False

    def _build_trigger(self, logtrace, logpath):
        if self.full_trigger_active:
            params = [logtrace, logpath]
            trigger = []
            for param in self.executable:
                if param == self.PlaceHolders:
                    param = params.pop(0)
                trigger.append(param)
            return trigger
        return self.executable

    def notify(self, message, log):
        msg_level = message.messageLevel.upper()
        if not message.isATarget() and msg_level not in self.Pullers:
            return
        logtrace, logpath = message.getPlainMessage()
        trigger = self._build_trigger(logtrace, logpath)
        if not self.started:
            self.started = True
            self.trigger_executor.start()
        self.trigger_executor.landing(trigger)

    def stop(self):
        if self.started:
            self.trigger_executor.stop()
            # unblock the queue
            self.trigger_executor.landing("stop")
            self.trigger_executor.join()


class Poster(object):
    """This is basically a REST client that
    will notify a log4tailer centralized server.
    It will register first to the server and will
    notify it anytime an undesirable trace has been found.
    Fatals, criticals, errors and targets will be notified.
    """
    Pullers = ['ERROR', 'FATAL', 'CRITICAL']

    def __init__(self, properties):
        """Instantiates a REST notifier.

        :param properties: a Property instance holding the necessary parameters
            to connect to the centralized server. The base_url, port, service
            uri, register and unregister uri will need to be provided in a
            configuration file.
        """
        self.url = properties.get_value('server_url')
        self.port = properties.get_value('server_port')
        self.service_uri = properties.get_value('server_service_uri')
        self.register_uri = properties.get_value('server_service_register_uri')
        self.unregister_uri = properties.get_value('server_service_unregister_uri')
        self.headers = {'Content-type' : 'application/json'}
        self.registered_logs = {}
        from socket import gethostname
        self.hostname = gethostname()
        # TODO should we set socket timeout?
        # socket.setdefaulttimeout(timeout)

    def notify(self, message, log):
        if log not in self.registered_logs:
            self.register(log)
        msg_level = message.messageLevel.upper()
        if not message.isATarget() and msg_level not in self.Pullers:
            return
        logtrace, _ = message.getPlainMessage()
        log_info = self.registered_logs[log]
        log_id = log_info['id']
        params = json.dumps({'logtrace': logtrace, 'loglevel' : msg_level,
            'log': { 'id' : log_id, 'logpath' : log.path,
                'logserver' : self.hostname}})
        body = self.send(self.service_uri, params)
        return body

    def register(self, log):
        params = json.dumps({'logpath': log.path, 'logserver' : self.hostname})
        body = self.send(self.register_uri, params)
        if not body:
            return
        log_id = body
        self.registered_logs[log] = {'id' : log_id, 'logserver' :
                self.hostname}

    def unregister(self, log):
        if log in self.registered_logs:
            log_info = self.registered_logs[log]
            log_id = log_info['id']
            params = {'id' : log_id}
            body = self.send(self.unregister_uri, json.dumps(params))
            return body

    def send(self, uri, params):
        conn = httplib.HTTPConnection(self.url, self.port)
        try:
            conn.request('POST', uri, params, self.headers)
            response = conn.getresponse()
        except Exception, err:
            print err
            return None
        body = response.read()
        conn.close()
        return body
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.