zoukankan      html  css  js  c++  java
  • nginx 并发统计

    #!/usr/local/bin/python3
    # coding: utf-8
    
    # ====================================================
    # Author: chang - EMail:changbo@hmg100.com
    # Last modified: 2017-6-2
    # Filename: logcheck.py
    # Description: real time analysis nginx log,base sys, Counter
    # blog:http://www.cnblogs.com/changbo
    # ====================================================
    
    from collections import Counter
    import json
    import os
    import sys
    import threading
    
    logfile = sys.argv[1]
    # logfile = 'hmgaccess.log'
    logname = logfile[:-4]
    txtname = '%s.txt' % logname
    
    
    def concurrent(filepath):
        timelist = []
    
        with open(filepath) as f:
            line = f.readlines()
            for i in line:
                timetmp = i.split(" ")
                if len(timetmp) > 2:
                    timelist.append((((timetmp[3]).split("/"))[2])[5:])
        count = dict(Counter(timelist))
        # output = sys.stdout
        jsObj = json.dumps(count)
        with open('concurrent.txt', 'a+') as f:
            # sys.stdout = f
            f.write(jsObj)
    
        # sys.stdout = output
        print("success!please check file concurrent.txt!!")
    
        with open('concurrent.txt') as f2:
            line = f2.readlines()
            newline = eval(line[0])
            linetmp = dict(zip(newline.values(), newline.keys()))
            newline = sorted(linetmp.items(), key=lambda d: d[0], reverse=True)
            print(type(newline))
            for line in newline:
                  key, value = line
                  print(key, value)
                  with open(txtname, 'a+') as f3:
                      f3.write('%s %s
    ' % (value, key))
        os.remove('concurrent.txt')
    
    if __name__ == '__main__':
        t1 = threading.Thread(target=concurrent(logfile))
        t1.start()

    END!

  • 相关阅读:
    NOIP 模拟 序列操作
    LUOGU 1525 关押罪犯
    HDU2473 Junk-Mail Filter
    BZOJ 2096 Pilots
    luogu 3939 数颜色
    NOIP模拟 赌博游戏
    Unity3D
    HTML5
    Cocos2d-x——支持多触点
    Cocos2d-x——Cocos2d-x 屏幕适配总结
  • 原文地址:https://www.cnblogs.com/changbo/p/6932769.html
Copyright © 2011-2022 走看看