实时日志查看
参考: http://www.ttlsa.com/python/monitor-log-realtime-python-websocket/
http://www.cnblogs.com/xiaoyou2018/archive/2018/07/18/9328950.html
https://blog.csdn.net/devilcry13/article/details/81702147
https://blog.csdn.net/s740556472/article/details/78991704
https://www.cnblogs.com/shihaiming/p/6201678.html
1. websocket server 端代码: websocket_server.py
python websocket_server.py 启动服务器端.
#!/usr/bin/env python #-*- encoding: utf-8 -*- #websocket服务端 from bottle import request, Bottle, abort from geventwebsocket import WebSocketError from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler app = Bottle() users = set() @app.get('/websocket/') def handle_websocket(): wsock = request.environ.get('wsgi.websocket') print wsock users.add(wsock) if not wsock: abort(400, 'Expected WebSocket request.') while True: try: message = wsock.receive() except WebSocketError: break print u"现有连接用户:%s" % (len(users)) if message: for user in users: try: user.send(message) except WebSocketError: print u'某用户已断开连接' # 如果有客户端断开,则删除这个断开的websocket users.remove(wsock) if __name__ == "__main__": server = WSGIServer(("0.0.0.0", 8100), app,handler_class=WebSocketHandler) server.serve_forever()
2. 查看日志,就需要信息的实时采集,采集过后发给 websocket server.,代码: websocke_pipe.py
再服务端起动后再起》 python websocke_pipe
这里采集的是本地的日志.
#!/usr/bin/python # encoding=utf-8 #websocket客户端 import subprocess import time from websocket import create_connection # 配置远程服务器的IP,帐号,密码,端口等,因我做了双机密钥信任,所以不需要密码 # r_user = "root" # r_passwd='jason_zhang' # r_ip = "192.168.2.224" # r_port = 22 r_log = "/tmp/ansible.log" # 远程服务器要被采集的日志路径 # websocket服务端地址 ws_server = "ws://localhost:8100/websocket/" # 执行的shell命令(使用ssh远程执行) cmd = "/usr/bin/tailf {log_path}".format(log_path=r_log) def tailfLog(): """获取远程服务器实时日志,并发送到websocket服务端""" popen = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) print('连接成功') ws = create_connection(ws_server) # 创建websocket连接 while True: line = popen.stdout.readline().strip() #获取内容 print line if line: ws.send(line) #把内容发送到websocket服务端 print (time.time()) if __name__ == '__main__': tailfLog()
3. 打开HTML5 页面(也就是客户端)来连接就可以看到实时日志了.
<!DOCTYPE html> <html> <head> </head> <body> <p>实时日志</p> <div> <textarea name="content" id="msg" rows="20" cols="100"> </textarea> </div> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <script> $(document).ready(function() { /* !window.WebSocket、window.MozWebSocket检测浏览器对websocket的支持*/ if (!window.WebSocket) { if (window.MozWebSocket) { window.WebSocket = window.MozWebSocket; } else { $('#msg').prepend("<p>你的浏览器不支持websocket</p>"); } } /* ws = new WebSocket 创建WebSocket的实例 注意设置对以下的websocket的地址哦*/ ws = new WebSocket('ws://127.0.0.1:8100/websocket/'); /* ws.onopen 握手完成并创建TCP/IP通道,当浏览器和WebSocketServer连接成功后,会触发onopen消息 ws.onmessage 接收到WebSocketServer发送过来的数据时,就会触发onmessage消息,参数evt中包含server传输过来的数据; */ ws.onopen = function(evt) { $('#msg').append('<li>websocket连接成功</li>'); } ws.onmessage = function(evt) { $('#msg').prepend( evt.data); } }); </script> </body> </html>
一搭搭
二搭搭
三搭搭