python分析sa文件历史信息

有时候收到服务器报警,cpu load值彪高。但是登录到服务器上去的时候发现load值已经在下降了。

可以通过分析生成的sa文件进行获取历史的load值

写一个python脚本如下:
分析结果如下:

日期|时间  PV  load-15

2013-04-26|00:00:00 611 0.12
2013-04-26|00:10:00 420 0.09
2013-04-26|00:20:00 422 0.10
2013-04-26|00:30:00 507 0.16
2013-04-26|00:40:00 491 0.13
2013-04-26|00:50:00 504 0.09
2013-04-26|01:00:00 455 0.09

python脚本如下:

 

#!/usr/bin/env python

#import platform
#print platform.python_version()
import datetime
import re

def sar_process(LogDate, Datas):
    """Process the yesterday sar log
       CPU usage
       load average
       memory
       network I/O
       network sock
       disk I/O
    """
    import os
    import subprocess

    #import multiprocessing
    #cpus = multiprocessing.cpu_count()
    
    lastLogFile = "/var/log/sa/sa%02d" % (LogDate.day)
    ## print lastLogFile

    newEnv = os.environ
    newEnv["LANG"] = "C"
    newEnv["S_TIME_FORMAT"] = "ISO"

    #Datas = dict()
    cmds = [
               ['/usr/bin/sar', '-u', '-P', 'ALL', '-f', lastLogFile],
               ['/usr/bin/sar', '-q', '-f', lastLogFile],
               ['/usr/bin/sar', '-r', '-f', lastLogFile],
               ['/usr/bin/sar', '-n', 'DEV', '-f', lastLogFile],
               ['/usr/bin/sar', '-n', 'SOCK', '-f', lastLogFile],
               ['/usr/bin/sar', '-b', '-f', lastLogFile],
           ]
    for cmd in cmds:
        p = subprocess.Popen(cmd,
                             stdout = subprocess.PIPE,
                             stderr = subprocess.PIPE,
                             env = newEnv
                            )
    
        (sardata, err) = p.communicate()
        date = ''
        lines = re.split('[\r\n]+', sardata)
        date = lines.pop(0).rsplit('\t')[-1]
        titles = re.split('[\s]+', lines.pop(0))
        titles[0] = 'time'
        Cols = len(titles)
        for line in lines:
            if re.search('^[0-9]', line):
                ColData = re.split('[\s]+', line)
                (Hour, Minute) = ColData[0].split(':')[:2]
                newDay = datetime.datetime(2000,1,1,int(Hour),int(Minute),0) \
                             - datetime.timedelta(minutes = 10)
                ColData[0] = "%02d:%02d:00" % (newDay.hour, newDay.minute)
                masterKey = date + '|' + ColData[0]
                ColDict = dict()
                for i in xrange(Cols):
                    ColDict[titles[i]] = ColData[i]
                del ColDict[titles[0]]
                if titles[1] == 'CPU' or titles[1] == 'IFACE':
                    del ColDict[titles[1]]
                    secondKey = titles[1] + '|' + ColData[1]
                else:
                    secondKey = titles[1] + '|' + titles[1]

                if masterKey not in Datas:
                    Datas[masterKey] = dict()
                Datas[masterKey][secondKey] = ColDict
                
    
    
def ps_access_log_process(LogDate, Datas):
    import glob

    LogDateStr = "%04d-%02d-%02d" % (LogDate.year, LogDate.month, LogDate.day)
    LogDir = '/data/pt8.49/webserv/alihr/logs/his'
    Logs = glob.glob(LogDir + '/PIA_access.log*')
    Logs.sort()
    Logs = Logs[-20:]
    
    for Log in Logs:
        try:
            f = open(Log, "r")
        except IOError:
            return None
        while True:
            line = f.readline().strip()
            if re.search('^#', line):
                continue
            if re.search('^' + LogDateStr, line):
                ColData = re.split('[\s]+', line)
                AccTime = ColData[1].split(':')
                AccTime[1] = AccTime[1][0] + '0'
                ColData[1] = "%02d:%02d:%02d" % (int(AccTime[0]), int(AccTime[1]), 0)
                MasterKey = LogDateStr + '|' + ColData[1]
                SecondKey = 'access|access'
                if MasterKey not in Datas:
                    Datas[MasterKey] = dict()
                
                if SecondKey not in Datas[MasterKey]:
                    Datas[MasterKey][SecondKey] = dict()
                else:
                    if 'access' not in Datas[MasterKey][SecondKey]:
                        Datas[MasterKey][SecondKey]['access'] = 1
                    else:
                        Datas[MasterKey][SecondKey]['access'] += 1

            else:
                break


if __name__ == '__main__':
    import platform
    import sys

    currentTime = datetime.datetime.now()
    lastDay = currentTime - datetime.timedelta(days = 1)

    PyVersion = platform.python_version()
    if PyVersion < '2.4':
        print "python 2.4 or later required."
        sys.exit(1)
    #digest_map = build_digest_map("/syslog2")
    #print(len(digest_map))
    Datas = dict()
    sar_process(lastDay, Datas)
    ps_access_log_process(lastDay, Datas)
    Key1S = Datas.keys()
    Key1S.sort()
    for key1 in Key1S:
        #Key2S = Datas[key1].keys()
        #Key2S.sort()
        #for key2 in Key2S:
        #    print key1, key2, Datas[key1][key2]
        if 'access|access' in Datas[key1] and 'runq-sz|runq-sz' in Datas[key1]:
            if 'access' in  Datas[key1]['access|access']:
                print key1, Datas[key1]['access|access']['access'],
                print Datas[key1]['runq-sz|runq-sz']['ldavg-15']
#    print len(Datas)

原文我的博客:龙木-运维ing

你可能感兴趣的:(python)