一个简易的日志监控的脚本,功能如下:1.windows环境2.当匹配日志关键字时会发出声音,匹配的关键字不同,播放的声音不同3.能做到实时响应

注意:是在win环境下哦

直接上代码吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# encoding: utf-8
    
"""
MonitorLog.py
    
Usage: MonitorLog.py ...
Monitor the log file
    
-f  log file
-h  help info
    
python MonitorLog.py -f C:\monitor.log
    
"""
    
import sys
import os
import getopt
import subprocess
import time
import codecs
import winsound
    
ABSPATH = os.path.dirname(os.path.abspath(__file__))
MONITERCONF = 'moniter_keyword.txt' #utf8 file
    
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hf:')
    except getopt.GetoptError, err:
        print str(err)
        print __doc__
        return 1
    
    path = ''
    for k, v in opts:
        if == '-f':
            path = v
        elif == '-h':
            print __doc__
            return 0
    
    if not (path and os.path.exists(path)):
        print 'Invalid path: %s' % path 
        print __doc__
        return 2
    
    #命令行元组
    cmd = ('tail''-f', path)
    print ' '.join(cmd)
    output = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    
    keywordMap = {}
    #加载监控的关键字信息
    with codecs.open(os.path.join(ABSPATH, MONITERCONF), 'r''utf8') as f:
        lines = f.readlines()
    for line in lines:
        line = line.strip()
        if not line:
            continue
        keyword, wav = line.strip().split(':')
        keywordMap[keyword] = wav
    
    while True:
        line = output.stdout.readline()
        #process code,得到输出信息后的处理代码
        if not line:
            time.sleep(0.01)
            continue
        line = line.strip().decode('utf8')
        print line
        for keyword in keywordMap:
            if line.find(keyword) > -1:
                winsound.PlaySound(keywordMap[keyword], 
                                   winsound.SND_NODEFAULT)
        #time.sleep(0.01)
    return 0
    
if __name__ == '__main__':
    sys.exit(main())

http://www.qytang.com/
http://www.qytang.com/cn/list/29/
http://www.qytang.com/cn/list/28/358.htm
http://www.qytang.com/cn/list/41/
http://www.qytang.com/cn/list/37/
http://www.qytang.com/cn/list/46/
http://www.qytang.com/cn/page/19.htm
http://www.qytang.com/cn/list/32/
http://www.qytang.com/cn/list/28/
http://www.qytang.com/cn/list/25/
http://www.qytang.com/cn/list/28/625.htm
http://www.qytang.com/cn/list/28/612.htm
http://www.qytang.com/cn/list/28/611.htm

最新文章

  1. 我对BFC的理解
  2. Python 之旅
  3. 【Java并发编程实战】-----“J.U.C”:Condition
  4. Hadoop FS shell commands
  5. WPF:ListView 分页
  6. task mysqld:26208 blocked for more than 120 seconds
  7. .NET破解之百分百营销软件系列
  8. 这个好像、也许、或许、大概、应该、Maybe真的可以算是传说中的Spring.Net了吧
  9. UILable  /  UITextField  /   UIButton
  10. js导入的注意.txt
  11. ElasticSearch — 集群搭建
  12. flex4 list 自动适应高度
  13. [asp.net mvc 奇淫巧技] 02 - 巧用Razor引擎在Action内生成Html代码
  14. Jmeter连接DB2/ORACLE/MYSQL数据库
  15. 【64】Spring mvc详解
  16. python的IDE工具-- Pycharm
  17. poj2442 堆应用
  18. UART接口与COM口的区别
  19. js基础知识:闭包,事件处理,原型
  20. Medline Plus

热门文章

  1. Excel不同工作簿之间提取信息
  2. (转)Inno Setup入门(三)——指定压缩方式
  3. 转-SpringMVC——之 国际化
  4. Apache Ignite简介以及Ignite和Coherence、Gemfire、Redis等的比较
  5. iis 重新安装后 重新注册asp.net
  6. XML的学习
  7. START WITH...CONNECT BY PRIOR详解
  8. ubuntu下用expect实现密码自动输入
  9. JavaScript笔录
  10. CentOS 修改源为163和指定epel源和docker安装