程序Log实时监控 – python + websocket

凉白开 python536,07011字数 3124阅读10分24秒阅读模式

需求构思: 在linux下常常需要查看程序的实时输出,我们用 tail -f logfile 即可在console下实现此需求。现在要拓宽应用: 想在web browser下查看程序(以及日志)的实时输出(也就是程序Log的Web实时监控)。

python文章源自运维生存时间-https://www.ttlsa.com/python/monitor-log-realtime-python-websocket/

架构构思

因为考虑了“实时"这个需求,所以初步定位为socket架构; 再因为是构建在web之上,属于web app,所以socket进一步细分为:web socket。初步验证: web socket的 server部分: 可选: 1)自己实现 2) Node.js 3)其他框架(i.e. pywebsocketserver), web socket的client部分: 可选: 1) firefox 16 2) chrome 3)其他浏览器文章源自运维生存时间-https://www.ttlsa.com/python/monitor-log-realtime-python-websocket/

架构实现

  • Python里的subprocess可用pipe获取数据,再开子进程获取数据行
  • Server负责把上述数据行缓存后再源源不断发送到 Client
  • Client负责显示接受到的实时数据

 文章源自运维生存时间-https://www.ttlsa.com/python/monitor-log-realtime-python-websocket/

python代码

# -*- coding: utf8 -*-
import sys
import time 
import threading
from pywebsocketserver.server import SocketServer
from pywebsocketserver.baseio import BaseIO
import subprocess


def tcpdump():  
    global g_output_log
    popen=subprocess.Popen(['bash','-c',"/usr/sbin/tcpdump -i eth0 -v"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    pid=popen.pid
    print('Popen.pid:'+str(pid))  
    while True:  
        line=popen.stdout.readline().strip()
        #line =popen.communicate()
        print "output:%s" %(line)
        g_output_log.append(line)
        if subprocess.Popen.poll(popen) is not None:  
                break
    print('DONE')  
    

class MyIO(BaseIO):
    def onData(self,uid,text):
        self.sendData(uid,"received the message:%s"%(text,))
    def onConnect(self,uid):
        global g_uid
        g_uid=uid
        while True:
            #self.sendData(uid,"testing...")
            if  len(g_output_log) >0:
                log = g_output_log.pop()
                self.sendData(uid,log)
            else:
                time.sleep(.01)

try:
    g_output_log=[]
    g_uid=None
    tcpdump = threading.Thread(name='tcpdump', target=tcpdump)
    tcpdump.start()        
    port = sys.argv[1]
except:
    port = 88

port = int(port)
myIo = MyIO()
SocketServer(port,myIo).run()    

JS代码

# -*- coding: utf8 -*-
import sys
import time 
import threading
from pywebsocketserver.server import SocketServer
from pywebsocketserver.baseio import BaseIO
import subprocess


def tcpdump():  
    global g_output_log
    popen=subprocess.Popen(['bash','-c',"/usr/sbin/tcpdump -i eth0 -v"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    pid=popen.pid
    print('Popen.pid:'+str(pid))  
    while True:  
        line=popen.stdout.readline().strip()
        #line =popen.communicate()
        print "output:%s" %(line)
        g_output_log.append(line)
        if subprocess.Popen.poll(popen) is not None:  
                break
    print('DONE')  
    

class MyIO(BaseIO):
    def onData(self,uid,text):
        self.sendData(uid,"received the message:%s"%(text,))
    def onConnect(self,uid):
        global g_uid
        g_uid=uid
        while True:
            #self.sendData(uid,"testing...")
            if  len(g_output_log) >0:
                log = g_output_log.pop()
                self.sendData(uid,log)
            else:
                time.sleep(.01)

try:
    g_output_log=[]
    g_uid=None
    tcpdump = threading.Thread(name='tcpdump', target=tcpdump)
    tcpdump.start()        
    port = sys.argv[1]
except:
    port = 88

port = int(port)
myIo = MyIO()
SocketServer(port,myIo).run()    

html代码

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="socketclient.js"></script>
</head>

<body>
<script>
var client = new SocketClient("192.168.199.3",88,"chat");

client.connect();

client.onData  = function(text) {
    console.log(text);
    element=document.getElementById("log");
    if(element){
       if(element.value.length >100000) {
	 element.value="";
	}
       element.value += (text + "\n");
       element.scrollTop = element.scrollHeight;
    }
}

client.onRegist = function() {
    this.sendData("I am coming!");
}

</script>

<textarea name="log" id="log" style="font-size:12px; width:100%; height: 600px;">Tcpdump log:</textarea>
</body>
</html>

演示说明

Server运行tcpdump,用户在浏览器查看运行的实时数据。文章源自运维生存时间-https://www.ttlsa.com/python/monitor-log-realtime-python-websocket/

项目地址

https://github.com/suxianbaozi/pywebsocketserver文章源自运维生存时间-https://www.ttlsa.com/python/monitor-log-realtime-python-websocket/

或者本站下载:[file:https://www.ttlsa.com/wp-content/uploads/2015/09/pywebsocketserver-master.zip]文章源自运维生存时间-https://www.ttlsa.com/python/monitor-log-realtime-python-websocket/ 文章源自运维生存时间-https://www.ttlsa.com/python/monitor-log-realtime-python-websocket/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 14/09/2015 00:01:34
  • 转载请务必保留本文链接:https://www.ttlsa.com/python/monitor-log-realtime-python-websocket/
  • python
  • pywebsocketserver
  • taillog
  • websocket
评论  5  访客  5
    • Emma
      Emma 0

      我喜欢代码简洁易读,服务稳定的推送服务,前段时间研究了一下goeasy,java后台推送只需要两行代码, js前端推送也只需要3,4行,而且文档齐全,还提供了后台查询信息收发情况,所以我觉得GoEasy推送服务是个不错的选择。官网: https://goeasy.io/

      • 帅章
        帅章 0

        非常好。。。

        • 无影风随
          无影风随 0

          这儿有个界面更好看的,原理差不多,http://logio.org/

            • jone
              jone 1

              @ 无影风随 这个logio我在github上下载下来,感觉缺文件,跑不起来啊,你弄好了吗?

            • zzzZ
              zzzZ 9

              支持白开大神,掌声 :wink:

            评论已关闭!