设为首页 收藏本站
查看: 546|回复: 0

[经验分享] 【Python真的很强大】程序Log实时监控

[复制链接]

尚未签到

发表于 2017-5-6 12:55:11 | 显示全部楼层 |阅读模式

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



  • 架构构思: 因为考虑了“实时"这个需求,所以初步定位为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)其他浏览器
  • 架构实现

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

  • 代码部分

# -*- 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()   


function SocketClient(ip,port,query) {
var _this = this;
this.socket = '';
this.uid = 0;
this.sign = '';
this.connect = function() {
this.socket = new WebSocket('ws://'+ip+':'+port+'/'+query);
this.socket.onopen = function() {
_this.onOpen()
}
this.socket.onmessage = function(event) {
data = event.data;
data = data.split("<split>")
_this.uid = data[0];
_this.sign = data[1];
text = data[2];
if(text!='SETUID') {  
_this.onData(text);
} else {
_this.onRegist()
}
}        
this.socket.onclose = function(event) {
_this.onClose();
};
}
this.onRegist = function() {
}
this.onClose = function() {
}
this.onOpen = function() {
}
this.onData = function(text) {
}
this.sendData = function (text) {
var data = this.uid+'<split>'+this.sign+'<split>'+text
this.socket.send(data);
}
this.close = function() {
this.socket.close();
}
}


<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,用户在浏览器查看运行的实时数据。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373838-1-1.html 上篇帖子: Python开发环境Wing IDE如何查看调试数据 下篇帖子: python实现数据的多维缩放(集体智慧编程)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表