当前位置:   article > 正文

python web 服务器实时监控 websocket_python实现websocket服务器,可以在web实时显示远程服务器日志...

python 读取web服务器运行日志

一、开始的话

使用python简单的实现websocket服务器,可以在浏览器上实时显示远程服务器的日志信息。

之前做了一个web版的发布系统,但没实现在线看日志,每次发布版本后,都需要登录到服务器上查看日志,非常麻烦,为了偷懒,能在页面点几下按钮完成工作,所以这几天查找了这方面的资料,实现了这个功能,瞬间觉的看日志什么的,太方便了,以后也可以给开发们查日志,再也不用麻烦运维了,废话少说,先看效果吧。

二、代码

在实现这功能前,看过别人的代码,发现很多都是只能在web上显示本地的日志,不能看远程主机上的日志,有些能看远程日志的是引用了其他框架(例如bottle,tornado)来实现的,而且所有这些都是重写thread的run方法来实现的,由于本人技术太菜,不知道怎么改成自己需要的样子,而且我是用django这个web框架的,不想引用太多框架,搞的太复杂,所以用python来实现websocket服务器。由于技术问题,代码有点粗糙,不过能实现功能就行,先将就着用吧。

执行下面命令启动django和websocketserver

nohup python manage.py runserver 10.1.12.110 &nohup python websocketserver.py&

启动websocket后,接收到请求,起一个线程和客户端握手,然后根据客户端发送的ip和type,去数据库查找对应的日志路径,用paramiko模块ssh登录到远程服务器上tail查看日志,再推送给浏览器,服务端完整代码如下:

1 #coding:utf-8

2 importstruct3 importbase644 importhashlib5 importsocket6 importthreading7 from public.public importget_ssh8 importos9

10

11 def recv_data(conn): #服务器解析浏览器发送的信息

12 try:13 all_data = conn.recv(1024)14 if notlen(all_data):15 returnFalse16 except:17 pass

18 else:19 code_len = ord(all_data[1]) & 127

20 if code_len == 126:21 masks = all_data[4:8]22 data = all_data[8:]23 elif code_len == 127:24 masks = all_data[10:14]25 data = all_data[14:]26 else:27 masks = all_data[2:6]28 data = all_data[6:]29 raw_str = ""

30 i =031 for d indata:32 raw_str += chr(ord(d) ^ ord(masks[i % 4]))33 i += 1

34 returnraw_str35

36

37 def send_data(conn, data): #服务器处理发送给浏览器的信息

38 ifdata:39 data =str(data)40 else:41 returnFalse42 token = "\x81"

43 length =len(data)44 if length < 126:45 token += struct.pack("B", length) #struct为Python中处理二进制数的模块,二进制流为C,或网络流的形式。

46 elif length <= 0xFFFF:47 token += struct.pack("!BH", 126, length)48 else:49 token += struct.pack("!BQ", 127, length)50 data = ‘%s%s‘ %(token, data)51 conn.send(data)52 returnTrue53

54

55 defhandshake(conn, address, thread_name):56 headers ={}57 shake = conn.recv(1024)58 if notlen(shake):59 returnFalse60

61 print (‘%s : Socket start handshaken with %s:%s‘ % (thread_name, address[0], address[1]))62 header, data = shake.split(‘\r\n\r\n‘, 1)63 for line in header.split(‘\r\n‘)[1:]:64 key, value = line.split(‘:‘, 1)65 headers[key] =value66

67 if ‘Sec-WebSocket-Key‘ not inheaders:68 print (‘%s : This socket is not websocket, client close.‘ %thread_name)69 conn.close()70 returnFalse71

72 MAGIC_STRING = ‘258EAFA5-E914-47DA-95CA-C5AB0DC85B11‘

73 HANDSHAKE_STRING = "HTTP/1.1 101 Switching Protocols\r\n"74 "Upgrade:websocket\r\n"75 "Connection: Upgrade\r\n"76 "Sec-WebSocket-Accept: {1}\r\n"77 "WebSocket-Origin: {2}\r\n"78 "WebSocket-Location: ws://{3}/\r\n\r\n"

79

80 sec_key = headers[‘Sec-WebSocket-Key‘]81 res_key = base64.b64encode(hashlib.sha1(sec_key +MAGIC_STRING).digest())82 str_handshake = HANDSHAKE_STRING.replace(‘{1}‘, res_key).replace(‘{2}‘, headers[‘Origin‘]).replace(‘{3}‘, headers[‘Host‘])83 conn.send(str_handshake)84 print (‘%s : Socket handshaken with %s:%s success‘ % (thread_name, address[0], address[1]))85 print ‘Start transmitting data...‘

86 print ‘- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -‘

87 returnTrue88

89

90 defdojob(conn, address, thread_name):91 from mode importmodels92 handshake(conn, address, thread_name) #握手

93 log_info =recv_data(conn)94 log_ip = log_info.split(":")[0]95 log_type = log_info.split(":")[1]96

97 auth_ = models.Authentication.objects.get(ip=log_ip)98 user =auth_.a_user99 pwd =auth_.a_password100 try:101 log_path = models.LogPath.objects.get(ip=log_ip, type_name__contains=log_type).log_path102 exceptException, e:103 send_data(conn, e)104 conn.close()105 print "Error:" +str(e)106 print (‘%s : Socket close with %s:%s‘ % (thread_name, address[0], address[1]))107 return

108 conn.setblocking(0) #设置socket为非阻塞

109 ssh =get_ssh(log_ip, user, pwd)110 ssh_t =ssh.get_transport()111 chan =ssh_t.open_session()112 chan.setblocking(0) #设置非阻塞

113 chan.exec_command(‘tail -f %s‘ %log_path)114 whileTrue:115 clientdata =recv_data(conn)116 if clientdata is not None and ‘quit‘ inclientdata:117 print (‘%s : Socket close with %s:%s‘ % (thread_name, address[0], address[1]))118 send_data(conn, ‘close connect‘)119 conn.close()120 break

121 whileTrue:122 whilechan.recv_ready():123 clientdata1 =recv_data(conn)124 if clientdata1 is not None and ‘quit‘ inclientdata1:125 print (‘%s : Socket close with %s:%s‘ % (thread_name, address[0], address[1]))126 send_data(conn, ‘close connect‘)127 conn.close()128 break

129 log_msg = chan.recv(10000).strip()130 printlog_msg131 send_data(conn, log_msg)132 ifchan.exit_status_ready():133 break

134 clientdata2 =recv_data(conn)135 if clientdata2 is not None and ‘quit‘ inclientdata2:136 print (‘%s : Socket close with %s:%s‘ % (thread_name, address[0], address[1]))137 send_data(conn, ‘close connect‘)138 conn.close()139 break

140 break

141

142

143 defws_service():144

145 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lbg.settings")146 index = 1

147 sock =socket.socket(socket.AF_INET, socket.SOCK_STREAM)148 sock.bind(("127.0.0.1", 12345))149 sock.listen(100)150

151 print (‘\r\n\r\nWebsocket server start, wait for connect!‘)152 print ‘- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -‘

153 whileTrue:154 connection, address =sock.accept()155 thread_name = ‘thread_%s‘ %index156 print (‘%s : Connection from %s:%s‘ % (thread_name, address[0], address[1]))157 t = threading.Thread(target=dojob, args=(connection, address, thread_name))158 t.start()159 index += 1

160

161

162 ws_service()

get_ssh的代码如下:

importparamikodefget_ssh(ip, user, pwd):try:

ssh=paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(ip,22, user, pwd, timeout=15)returnsshexceptException, e:printereturn "False"

打开页面时,自动连接websocket服务器,完成握手,并发送ip和type给服务端,所以可以看不同类型,不同机器上的日志,

页面代码如下:

varsocket;functioninit() {varhost= "ws://127.0.0.1:12345/";varips="{{ ip }}";vartypes="{{ type }}";varlog_msg=ips+":"+types;

console.log(log_msg);try{

socket= newWebSocket(host);

socket.onopen= function() {

log(‘Connected‘);

socket.send(log_msg);

};

socket.onmessage= function(msg) {

log(msg.data);varobje=document.getElementById("log");//日志过多时清屏

vartextlength=obje.scrollHeight;if(textlength> 10000) {

obje.innerHTML= ‘‘;

}

};

socket.onclose= function() {

log("Lose Connection!");

$("#start").attr(‘disabled‘,false);

$("#stop").attr(‘disabled‘,true);

};

$("#start").attr(‘disabled‘,true);

$("#stop").attr(‘disabled‘,false);

}catch(ex) {

log(ex);

}

}

window.οnbefοreunlοad= function() {try{

socket.send(‘quit‘);

socket.close();

socket= null;

}catch(ex) {

log(ex);

}

};functionlog(msg) {varobje=document.getElementById("log");

obje.innerHTML+= ‘

+msg+
‘;

obje.scrollTop=obje.scrollHeight;//滚动条显示最新数据

}functionstop() {try{

log(‘Close connection!‘);

socket.send(‘quit‘);

socket.close();

socket= null;

$("#start").attr(‘disabled‘,false);

$("#stop").attr(‘disabled‘,true);

}catch(ex) {

log(ex);

}

}functioncloselayer() {try{

log(‘Close connection!‘);

socket.send(‘quit‘);

socket.close();

socket= null;

}catch(ex) {

log(ex);

}varindex=parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引

parent.layer.close(index);//再执行关闭

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/85412
推荐阅读
相关标签
  

闽ICP备14008679号