功能:用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.1.221python
B服務器是要遠程查看日誌的服務器,我這裏用:192.168.1.10jquery
如下是A服務器的websocket servet的python代碼:linux
- """
- 執行代碼前須要安裝
- pip install bottle
- pip install websocket-client
- pip install bottle-websocket
- """
- from bottle import get, run
- from bottle.ext.websocket import GeventWebSocketServer
- from bottle.ext.websocket import websocket
- users = set()
- @get('/websocket/', apply=[websocket])
- def chat(ws):
- users.add(ws)
- while True:
- msg = ws.receive()
- if msg:
- for u in users:
- u.send(msg)
- else:
- break
-
- users.remove(ws)
- run(host='0.0.0.0', port=8000, server=GeventWebSocketServer)
記得安裝bottle、websocket-client 、bottle-websocket 模塊,服務端容許全部的IP訪問其8000端口nginx
websocket服務端除了用以上的方法外,還能夠用這下面的方法實現:web
使用gevent-websocket實現websocket服務端程序ajax
在電腦桌面,寫一個簡單的HTML5 javascripts頁面,隨便命名了,如web_socket.html,這個頁面使用了websocket鏈接到websocket服務端:
- <!DOCTYPE html>
- <html>
- <head>
- </head>
- <style>
- #msg{
- width:400px; height:400px; overflow:auto; border:2px solid #000000;color:#ffffff;
- }
- </style>
- </head>
- <body>
- <p>實時日誌</p>
- <div id="msg"></div>
- <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
- <script>
- $(document).ready(function() {
-
- if (!window.WebSocket) {
- if (window.MozWebSocket) {
- window.WebSocket = window.MozWebSocket;
- } else {
- $('#msg').prepend("<p>你的瀏覽器不支持websocket</p>");
- }
- }
-
- ws = new WebSocket('ws:
-
- ws.onopen = function(evt) {
- $('#msg').append('<li>websocket鏈接成功</li>');
- }
- ws.onmessage = function(evt) {
- $('#msg').prepend('<li>' + evt.data + '</li>');
- }
- });
- </script>
- </body>
- </html>
注意:WebSocket('ws:
到這裏,就搞定瀏覽器鏈接到websocket服務端的場景了,如今要A服務器裏寫一段代碼,去採集B服務器的實時信息了,其實採集原理很簡單,就是使用shell中的tailf命令,實時顯示最新的信息而已,咱們在這段腳本中,使用subprocess.Popen()來遠程查看日誌信息:
python代碼以下:
- import subprocess
- import time
- from websocket import create_connection
- r_user = "root"
- r_ip = "192.168.1.10"
- r_port = 22
- r_log = "/tmp/web_socket.log" # 遠程服務器要被採集的日誌路徑
- ws_server = "ws://192.168.1.221:8000/websocket/"
- 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)
- def tailfLog():
- """獲取遠程服務器實時日誌,併發送到websocket服務端"""
- popen = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
- print('鏈接成功')
- ws = create_connection(ws_server)
- while True:
- line = popen.stdout.readline().strip()
- if line:
- ws.send(line)
- print time.time()
- if __name__ == '__main__':
- tailfLog()
文章最後再解析subprocess.Popen的原理和功能
執行websocket服務端腳本和上面這個websocket客戶端採集腳本,再打開用瀏覽器打開上面的html5頁面後,環境就基本部署好了,雙websocket客戶端鏈接到websocket服務端中
上面腳本指定的r_log = "/tmp/web_socket.log"日誌路徑,咱們須要生成這個日誌文件,並不停地往裏面寫入日誌,這樣才能在瀏覽器中實時顯示效果(真實場景中,能夠指定服務器某日誌,如apache,nginx日誌等)
咱們在B服務器寫一段python代碼,而後每隔一秒就往r_log = "/tmp/web_socket.log"日誌中寫入內容:
python代碼以下:
- import time
- import random
- log_path = '/tmp/web_socket.log'
- while 1:
- with open(log_path,'a') as f:
- f.write('[%s] %s \n' % (time.ctime(),random.random()))
- time.sleep(1)
腳本寫入的內容大概是:
[Tue Jul 26 18:30:41 2016] 0.527242649654
[Tue Jul 26 18:30:42 2016] 0.21080845298
[Tue Jul 26 18:30:43 2016] 0.23128691356
[Tue Jul 26 18:30:44 2016] 0.689547600796
執行這段腳本,而後看瀏覽器效果:
![websocket實時日誌效果](http://static.javashuo.com/static/loading.gif)
這只是我臨時寫的,若是要在真實的運維工具中使用,還須要根據具體狀況,修改很多內容,但原理就是這樣,你們可根據本身的狀況修改,完善使用。
剛纔提到subprocess.Popen的原理和功能,請看如下資料:
http://www.cnblogs.com/fengbeihong/articles/3374132.html
bottle websocket參考資料:
http://rfyiamcool.blog.51cto.com/1030776/1269232/