功能:用websocket技術,在運維工具的瀏覽器上實時顯示遠程服務器上的日誌信息javascript
通常咱們在運維工具部署環境的時候,須要實時展示部署過程當中的信息,或者在瀏覽器中實時顯示程序日誌給開發人員看。你還在用ajax每隔段時間去獲取服務器日誌?out了,試試用websocket方式吧html
我用bottle框架,寫了個websocket服務端,瀏覽器鏈接到websocket server,再用python subprocess獲取遠程服務器的日誌信息,subprocess,就是用Popen調用shell的shell命令而已,這樣能夠獲取到實時的日誌了,而後再send到websocket server中,那鏈接到websocket server的瀏覽器,就會實時展示出來了html5
用二臺服務器來實現這個場景,A服務器是websocket服務端,B服務器是日誌端java
A服務器是我瀏覽器本機,websocket服務端也是這臺機,IP是:192.168.2.222python
B服務器是要遠程查看日誌的服務器,我這裏用:192.168.2.224jquery
如下是在A服務器的操做(Python2)linux
安裝:nginx
pip install bottleweb
pip install websocket-clientajax
pip install bottle-websocket
websocket servet的python代碼:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 from bottle import get, run 4 from bottle.ext.websocket import GeventWebSocketServer 5 from bottle.ext.websocket import websocket 6 users = set() # 鏈接進來的websocket客戶端集合 7 @get('/websocket/', apply=[websocket]) 8 def chat(ws): 9 users.add(ws) 10 while True: 11 msg = ws.receive() # 接客戶端的消息 12 if msg: 13 for u in users: 14 u.send(msg) # 發送信息給全部的客戶端 15 else: 16 break 17 # 若是有客戶端斷開鏈接,則踢出users集合 18 users.remove(ws) 19 run(host='0.0.0.0', port=8000, server=GeventWebSocketServer)
記得安裝bottle、websocket-client 、bottle-websocket 模塊,服務端容許全部的IP訪問其8000端口
websocket服務端除了用以上的方法外,還能夠用這下面的方法實現:
http://www.linuxyw.com/831.html
在電腦桌面,寫一個簡單的HTML5 javascripts頁面,隨便命名了,如test.html,這個頁面使用了websocket鏈接到websocket服務端:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 </head> 5 <style> 6 #msg{ 7 width:400px; height:400px; overflow:auto; border:2px solid #000000;background-color:#000000;color:#ffffff; 8 } 9 </style> 10 </head> 11 <body> 12 <p>實時日誌</p> 13 <div id="msg"></div> 14 <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> 15 <script> 16 $(document).ready(function() { 17 /* !window.WebSocket、window.MozWebSocket檢測瀏覽器對websocket的支持*/ 18 if (!window.WebSocket) { 19 if (window.MozWebSocket) { 20 window.WebSocket = window.MozWebSocket; 21 } else { 22 $('#msg').prepend("<p>你的瀏覽器不支持websocket</p>"); 23 } 24 } 25 /* ws = new WebSocket 建立WebSocket的實例 注意設置對如下的websocket的地址哦*/ 26 ws = new WebSocket('ws://192.168.2.222:8000/websocket/'); 27 /* 28 ws.onopen 握手完成並建立TCP/IP通道,當瀏覽器和WebSocketServer鏈接成功後,會觸發onopen消息 29 ws.onmessage 接收到WebSocketServer發送過來的數據時,就會觸發onmessage消息,參數evt中包含server傳輸過來的數據; 30 */ 31 ws.onopen = function(evt) { 32 $('#msg').append('<li>websocket鏈接成功</li>'); 33 } 34 ws.onmessage = function(evt) { 35 $('#msg').prepend('<li>' + evt.data + '</li>'); 36 } 37 }); 38 </script> 39 </body> 40 </html>
到這裏,就搞定瀏覽器鏈接到websocket服務端的場景了,如今要A服務器裏‘遠程查看日誌.py’,去採集B服務器的實時信息了,其實採集原理很簡單,就是使用shell中的tailf命令,實時顯示最新的信息而已,咱們在這段腳本中,使用subprocess.Popen()來遠程查看日誌信息:
python代碼以下:
1 #!/usr/bin/python 2 # encoding=utf-8 3 import subprocess 4 import time 5 from websocket import create_connection 6 # 配置遠程服務器的IP,賬號,密碼,端口等,因我作了雙機密鑰信任,因此不須要密碼 7 r_user = "root" 8 r_passwd='jason_zhang' 9 r_ip = "192.168.2.224" 10 r_port = 22 11 r_log = "/tmp/test.log" # 遠程服務器要被採集的日誌路徑 12 # websocket服務端地址 13 ws_server = "ws://192.168.2.222:8000/websocket/" 14 # 執行的shell命令(使用ssh遠程執行) 15 cmd = "/usr/bin/ssh -p {port} {user}@{ip} /usr/bin/tailf {log_path}".format(user=r_user,ip=r_ip,port=r_port,log_path=r_log) 16 def tailfLog(): 17 """獲取遠程服務器實時日誌,併發送到websocket服務端""" 18 popen = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) 19 print('鏈接成功') 20 ws = create_connection(ws_server) # 建立websocket鏈接 21 while True: 22 line = popen.stdout.readline().strip() #獲取內容 23 if line: 24 ws.send(line) #把內容發送到websocket服務端 25 print (time.time()) 26 if __name__ == '__main__': 27 tailfLog()
在服務器B的日誌文件隨便輸入點東西,再運行服務器A的獲取日誌腳本
獲取到的結果
tailfLog()文章最後再解析subprocess.Popen的原理和功能
執行websocket服務端腳本和上面這個websocket客戶端採集腳本,再打開用瀏覽器打開上面的html5頁面後,環境就基本部署好了,雙websocket客戶端鏈接到websocket服務端中
上面腳本指定的r_log = "/tmp/web_socket.log"日誌路徑,咱們須要生成這個日誌文件,並不停地往裏面寫入日誌,這樣才能在瀏覽器中實時顯示效果(真實場景中,能夠指定服務器某日誌,如apache,nginx日誌等)
剛纔提到subprocess.Popen的原理和功能,請看如下資料:
http://www.cnblogs.com/fengbeihong/articles/3374132.html
bottle websocket參考資料:
http://rfyiamcool.blog.51cto.com/1030776/1269232/