功能:用websocket技術,在運維工具的瀏覽器上實時顯示遠程服務器上的日志信息
一般我們在運維工具部署環境的時候,需要實時展現部署過程中的信息,或者在瀏覽器中實時顯示程序日志給開發人員看。你還在用ajax每隔段時間去獲取服務器日志?out了,試試用websocket方式吧
我用bottle框架,寫了個websocket服務端,瀏覽器連接到websocket server,再用python subprocess獲取遠程服務器的日志信息,subprocess,就是用Popen調用shell的shell命令而已,這樣可以獲取到實時的日志了,然后再send到websocket server中,那連接到websocket server的瀏覽器,就會實時展現出來了
用二台服務器來實現這個場景,A服務器是websocket服務端,B服務器是日志端
A服務器是我瀏覽器本機,websocket服務端也是這台機,IP是:192.168.1.221
B服務器是要遠程查看日志的服務器,我這里用:192.168.1.10
以下是A服務器的websocket servet的python代碼:
- #!/usr/bin/env python
- #coding=utf-8
- # __author__ = '戴儒鋒'
- # http://www.linuxyw.com
- """
- 執行代碼前需要安裝
- 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() # 連接進來的websocket客戶端集合
- @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集合
- users.remove(ws)
- run(host='0.0.0.0', port=8000, server=GeventWebSocketServer)
記得安裝bottle、websocket-client 、bottle-websocket 模塊,服務端允許所有的IP訪問其8000端口
websocket服務端除了用以上的方法外,還可以用這下面的方法實現:
在電腦桌面,寫一個簡單的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() {
- /* !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://192.168.1.221:8000/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('<li>' + evt.data + '</li>');
- }
- });
- </script>
- </body>
- </html>
注意:WebSocket('ws://192.168.1.221:8000/websocket/'); 這里的192.168.1.221一定要改成你的websocket服務端IP,切記!!!
到這里,就搞定瀏覽器連接到websocket服務端的場景了,現在要A服務器里寫一段代碼,去采集B服務器的實時信息了,其實采集原理很簡單,就是使用shell中的tailf命令,實時顯示最新的信息而已,我們在這段腳本中,使用subprocess.Popen()來遠程查看日志信息:
python代碼如下:
- #!/usr/bin/python
- # encoding=utf-8
- import subprocess
- import time
- from websocket import create_connection
- # 配置遠程服務器的IP,帳號,密碼,端口等,因我做了雙機密鑰信任,所以不需要密碼
- r_user = "root"
- r_ip = "192.168.1.10"
- r_port = 22
- r_log = "/tmp/web_socket.log" # 遠程服務器要被采集的日志路徑
- # websocket服務端地址
- ws_server = "ws://192.168.1.221:8000/websocket/"
- # 執行的shell命令(使用ssh遠程執行)
- 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) # 創建websocket連接
- while True:
- line = popen.stdout.readline().strip() #獲取內容
- if line:
- ws.send(line) #把內容發送到websocket服務端
- 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代碼如下:
- #!/usr/bin/env python
- #coding=utf-8
- 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
執行這段腳本,然后看瀏覽器效果:
這只是我臨時寫的,如果要在真實的運維工具中使用,還需要根據具體情況,修改不少內容,但原理就是這樣,大家可根據自己的情況修改,完善使用。
剛才提到subprocess.Popen的原理和功能,請看以下資料:
http://www.cnblogs.com/fengbeihong/articles/3374132.html
bottle websocket參考資料:
http://rfyiamcool.blog.51cto.com/1030776/1269232/