Код Pyt

← разместить еще код

Основной код.

Расшаренный код:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import io
import re
import logging
import logging.config
import datetime
import socket
import time




def regExp (line):
    string = '</iso-exception>'
    sendMessage = ''
    regExp1 = re.finditer('realm="\S+\"', line)
    for match_regExp1 in regExp1:
        regExp1Data = match_regExp1.group()
        sendMessage = sendMessage + regExp1Data + ' '
    regExp2 = re.finditer('at=".*"', line)
    for match_regExp2 in regExp2:
        regExp2Data = match_regExp2.group()
        sendMessage = sendMessage + regExp2Data + ' '
    RegExp3 = re.finditer('io-timeout', line)
    for match_RegExp3 in RegExp3:
        RegExp3Data = match_RegExp3.group()
        sendMessage = sendMessage + RegExp3Data + ' '
    RegExp4 = re.finditer('direction="\S+"', line)
    for match_RegExp4 in RegExp4:
        RegExp4Data = match_RegExp4.group()
        sendMessage = sendMessage + RegExp4Data + ' '
    RegExp5 = re.findall(r'<field id=\"(\S+)\"', line)
    for match_RegExp5 in RegExp5:
        RegExp5Data = match_RegExp5.__str__()
        sendMessage = sendMessage + '\"' + RegExp5Data + '\"' + '='
    value = re.findall('value="(.*)"', line)
    for match_RegExp6 in value:
        RegExp6Data = match_RegExp6.__str__()
        sendMessage = sendMessage + '\"' + RegExp6Data + '\"' + ' '
    RegExp7 = re.finditer('peer-disconnect', line)
    for match_RegExp7 in RegExp7:
        RegExp7Data = match_RegExp7.group()
        sendMessage = sendMessage + RegExp7Data + ' '
    RegExp8  = re.findall(ur'unconnected ISOChannel', line)
    for match_RegExp8 in RegExp8 :
        sendMessage = sendMessage + ' ' + 'RegExp8 ' + ' '
        while string not in line:
             tmp = re.findall('at .*', line)
             tmpData = tmp.__str__()
             sendMessage = sendMessage + tmpData + ' '
             line = fileRead.readline()
    line = fileRead.readline()
    return sendMessage


#Запись лога в файл
def writeLogFile (writeMesage):
    logger = logging.getLogger('Rotation Log')
    logger.setLevel(logging.DEBUG)
    handler = logging.handlers.RotatingFileHandler(r'C:/Users/leschenko/PycharmProjects/Py_lesch/script.log', maxBytes=3000, backupCount=31)
    logger.addHandler(handler)
    logger.debug(writeMesage)


#Отправка лога в syslog
def sendSyslog(syslogMesage):
    logging.config.fileConfig('logging.conf')
    logger = logging.getLogger('my_logger')
    logger.info(syslogMesage)


#Определение переменных
eps = 100
word = '</log>'
hostname = socket.gethostname()
now = datetime.datetime.now()
dateTime = now.strftime("%b %d %H:%M:%S")
sendMessage = dateTime + ' ' + hostname + ' '

#Чтение файла и парсинг построчно с записью в переменную
with io.open(r"C:/Users/leschenko/PycharmProjects/Py_lesch/log.txt", encoding='utf-8') as fileRead:
    for line in fileRead:
     while word not in line:
            regExp(line)
            print(sendMessage)
            writeLogFile(sendMessage)
            sendSyslog(sendMessage)
#           sendMessage = dateTime + ' ' + hostname + ' '
            sendMessage = dateTime + ' ' + hostname + ' '
            time.sleep(1/eps)