分析/var/log/message的python指令碼

來源:互聯網
上載者:User

標籤:伺服器   ip地址   message   python   import   

使用該程式分析/var/log/message中有error或者其他錯誤關鍵字。並在發現後搜集,發送郵件營運管理員。

可以再定義一下輔助的函數,比如說擷取伺服器的ip地址。放在郵件內容中。一起發郵件。


#!/usr/bin/env/python

#coding:utf8

import commands

import os,sys

import datetime

import time

#import file_seekwork

import urllib,urllib2

import json

today=datetime.date.today()

MONTH={

    ‘Jan‘:1,

    ‘Feb‘:2,

    ‘Mar‘:3,

    ‘Apr‘:4,

    ‘May‘:5,

    ‘Jun‘:6,

    ‘Jul‘:7,

    ‘Aug‘:8,

    ‘Sep‘:9,

    ‘Oct‘:10,

    ‘Nov‘:11,

    ‘Dec‘:12

}

def parse_file():

    pass



#擷取檔案修改時間

def get_state(file):

    file_mtime=os.stat(file).st_mtime

    return file_mtime

#查看/var/log/message 是否在10分鐘之內修改過,如果修改過返回file。

def getenfile(file,nowtime):

    if os.path.exists(file) and (nowtime-get_stat(file))/60 < 10:

        return file

#

def parse_date(datestr):

    year=today.year

    month,day,yearandtime=datestr.split("/")

    hour,minute,second = yearandtime.split(":")

    return datetime.datetime(int(year),MONTH[month],int(day),int(hour),int(minute))

#擷取10分鐘的日誌,並分析是否存在關鍵字nologmsg,如果有,便存入logmsg中。

def get_msg_result(time_minutes_ago,file,nologmsg):

    with open(file) as fd:

        y = fd.readlines()

        logmsg=[]

        logerrmsg=[]

        for line in y:

            splited_line=line.split()

            datestr = ‘/‘.join(splited_line[:3])

            #print ("datestr qqqqqq: %s"%datestr)

            try:

                date_str=parse_date(datestr)

                #print ("date_str: %s" %date_str)

            except:

                continue

            #date = date_str.strftime("%s")

            #print date_str,time_minutes_ago

            if date_str >= time_minutes_ago:

                logmsg.append(line)

            else:

                continue

            for ten_minutes_line in logmsg:

                for i in nologmsg:

                    if i not in line:

                        continue

                    else:

                        logerrmsg.append(ten_minutes_line)

            if logerrmsg:

                continue

        #for errline in logerrmsg:

        #    print errline

    return logerrmsg

if __name__ == ‘__main__‘:

    now = datetime.datetime.now()

    #print now

    d = datetime.timedelta(minutes=10)

    #print d

    time_minutes_ago=now - d

    #print time_minutes_ago

    keyword = [‘error‘]

    log_file= ‘/root/qh/qh_test.txt‘

    retrust= get_msg_result(time_minutes_ago,log_file,keyword)

print(112,retrust)


# 發郵件

receiver = ‘[email protected]‘

subject = ‘/var/log/messages error‘

a=‘‘.join(retrust)

print a

#print type(a)

#a="adfasdfasdfasdf"

#os.system (‘/usr/bin/perl /root/qh/send_msg.pl -t‘ + receiver  + ‘-s‘ + subject + ‘-c‘ + ‘test_qh‘)

commands.getoutput(‘/usr/bin/perl /root/qh/send_msg.pl -t %s -s "%s" -c "%s" ‘%(receiver,subject,a))


本文出自 “linux伺服器” 部落格,轉載請與作者聯絡!

分析/var/log/message的python指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.