python写的日志监控程序,关键字统计,日志大小监控,关键字出现报警并截取日志

#!/bin/python
#coding:UTF-8
'''
    @author:   verlink
    @desc:     log monitor 
    @date:     2015-6-16
'''
import sys
import re
import time
import os
import random
import datetime
import pycurl
import StringIO
import urllib
import ConfigParser

class logMonitor():

    def __init__(self):

        self.conf = ConfigParser.ConfigParser()
        self.conf.read("./log_monitor.ini")
	self.email_list = []
	self.log_name = ''


    def task_portal(self):
        
        section_list = self.conf.sections()
	monitor_list = []
	email_list = []
        result = 0

        for item in section_list:
	    if item == 'basic':
		    if self.conf.get(item,'enable') == 'false':
			    return
		    else:
		            self.log_name = self.conf.get(item,'log_name')
			    self.log_name_everyday()
			    print self.log_name
		            self.email_list = self.conf.get(item,'emails').split(';')
	    else:
		    if self.conf.get(item,'enable') != 'false':
			    monitor_list.append(item)
        for monitor_item in monitor_list:
                self.worker(monitor_item)

    def worker(self,monitor_item):
        
        if monitor_item == 'error_words_monitor':
	    print 'error_words_monitor start'
            if self.conf.get(monitor_item,'monitor_words') == '':
                return
            monitor_words_list = self.conf.get(monitor_item,'monitor_words').split(';')
	    threshold = self.conf.get(monitor_item,'threshold')
	    self.error_words_monitor(monitor_words_list,threshold)
	elif monitor_item == 'log_file_monitor':
	    print 'log_file_monitor start'
	    file_max_threshold = self.conf.get(monitor_item,'file_max_threshold')
	    self.log_file_monitor(file_max_threshold)
	elif monitor_item == 'target_words_monitor':
	    print 'target_words_monitor start'
	    monitor_words_list = self.conf.get(monitor_item,'target_words').split(';')
	    self.target_words_monitor(monitor_words_list)

        else:
            return 

    def log_name_everyday(self):

    	today = datetime.datetime.today()
	try:
		log_prefix = self.log_name.split('-')[0]
		date = today.strftime("%Y-%m-%d")
		self.log_name = log_prefix + '-' + date
	except Exception,e:
		print str(e)
		return

    def target_words_monitor(self,monitor_words_list):

	file_list = self.get_file_list()
	for file_name in file_list:
		f = open(file_name,'r')
		file_content = f.read()
		for word in monitor_words_list:
			if file_content.find(word) != -1:
				print 'find it!'
				log_content = file_content[file_content.find(word):file_content.find(word) + 1000]
				email_subject = self.conf.get('target_words_monitor','email_subject')
				email_content = self.conf.get('target_words_monitor','email_content') + '         ' +log_content
				print email_content
				self.alert_emails(email_subject,email_content)

    def get_file_list(self):

    	cmd = 'ls ' + self.log_name + '*'
	file_str = os.popen(cmd).read()
	file_list = file_str.split('\n')
	return file_list[0:len(file_list) - 1]

    def error_words_monitor(self, monitor_words_list, threshold):

    	email_subject = self.conf.get('error_words_monitor','email_subject')
	email_content = self.conf.get('error_words_monitor','email_content')
	file_list = self.get_file_list()
    	for word in monitor_words_list:
		pattern = re.compile(word)
		for file_name in file_list:
			f = open(file_name,'r')
			file_content = f.read()
			result_list = pattern.findall(file_content)
			if len(result_list) >= int(threshold):
				self.alert_emails(email_subject,email_content);

    def log_file_monitor(self,file_max_threshold):

    	email_subject = self.conf.get('log_file_monitor','email_subject')
	email_content = self.conf.get('log_file_monitor','email_content')
    	file_list = self.get_file_list()
	for file_name in file_list:
		cmd = "ls -l " + file_name + " | awk '{print $5}'"
		file_size = os.popen(cmd).read()
		if int(file_size.strip()) >= int(file_max_threshold):
			self.alert_emails(email_subject,email_content)

    def send_curl_command(self,url):

        c = pycurl.Curl()
        c.setopt(c.URL, url)
        b = StringIO.StringIO()
        c.setopt(pycurl.WRITEFUNCTION,b.write)
        c.perform()
        c.close

    def alert_emails(self,email_subject,email_content):

        monitor_str = ''
    	for monitor in self.email_list:
		monitor_str = monitor_str + ',' + monitor
	monitor_str = monitor_str[1:]
	email_content = urllib.quote(email_content)
	email_subject = urllib.quote(email_subject)
    	cmd_email = 'http://sdf1.letv.cn/ews/mailer/send/?receivers='+monitor_str+'&subject='+email_subject+'&content=' + email_content 
	self.send_curl_command(cmd_email)
	 

if __name__ == '__main__':

    lm = logMonitor()
    lm.task_portal()

配置文件信息如下:

[basic]
log_name = wallpaper-2015-6-16.log
emails = [email protected]

enable = true 



[error_words_monitor]

monitor_words = error
threshold= 1
email_subject = 壁纸的日志错误词数量监控
email_content = 壁纸的error日志数量过多,已经超过报警阈值,请登陆服务器进行处理

enable = false

[target_words_monitor]

target_words = StringToJsonValue 
email_subject = 目标词监控报警
email_content  = 壁纸的fatal日志出现 部分日志内容已经截取,如下所示,请进行处理

enable = true

[log_file_monitor]

file_max_threshold = 10
email_subject = 日志文件大小监控
email_content = 壁纸的日志文件过大,已经超过报警阈值,请进行处理

enable = false


主要用到了python的configparser和urllib等模块,里面的核心部分主要是实现的细节,比如对与中文的url输入,等等。

你可能感兴趣的:(python,运维,url,监控,日志截取)