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

来源:互联网 发布:php手册安卓版 编辑:程序博客网 时间:2024/05/01 12:07
#!/bin/python#coding:UTF-8'''    @author:   verlink    @desc:     log monitor     @date:     2015-6-16'''import sysimport reimport timeimport osimport randomimport datetimeimport pycurlimport StringIOimport urllibimport ConfigParserclass 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 + '-' + dateexcept 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_contentprint email_contentself.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 + ',' + monitormonitor_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.logemails = linlingmin@letv.comenable = true [error_words_monitor]monitor_words = errorthreshold= 1email_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 = 10email_subject = 日志文件大小监控email_content = 壁纸的日志文件过大,已经超过报警阈值,请进行处理enable = false


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

0 0
原创粉丝点击