首页域名资讯 正文

程序Log实时监控 – python + websocket

2024-11-22 7 0条评论

需求构思: 在linux下常常需要查看程序的实时输出,我们用 tail -f logfile 即可在console下实现此需求。现在要拓宽应用: 想在web browser下查看程序(以及日志)的实时输出(也就是程序Log的Web实时监控)。

架构构思

因为考虑了“实时”这个需求,所以初步定位为socket架构; 再因为是构建在web之上,属于web app,所以socket进一步细分为:web socket。初步验证: web socket的 server部分: 可选: 1)自己实现 2) Node.js 3)其他框架(i.e. pywebsocketserver), web socket的client部分: 可选: 1) firefox 16 2) chrome 3)其他浏览器

架构实现

  • Python里的subprocess可用pipe获取数据,再开子进程获取数据行
  • Server负责把上述数据行缓存后再源源不断发送到 Client
  • Client负责显示接受到的实时数据

 

python代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 # -*- coding: utf8 -*- import sys import time import threading from pywebsocketserver . server import SocketServer from pywebsocketserver . baseio import BaseIO import subprocess def tcpdump ( ) :         global g_output_log      popen = subprocess . Popen ( [ ‘bash’ , ‘-c’ , “/usr/sbin/tcpdump -i eth0 -v” ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE )      pid = popen . pid      print ( ‘Popen.pid:’ + str ( pid ) )         while True :             line = popen . stdout . readline ( ) . strip ( )          #line =popen.communicate()          print “output:%s” % ( line )          g_output_log . append ( line )          if subprocess . Popen . poll ( popen ) is not None :                     break      print ( ‘DONE’ )         class MyIO ( BaseIO ) :      def onData ( self , uid , text ) :          self . sendData ( uid , “received the message:%s” % ( text , ) )      def onConnect ( self , uid ) :          global g_uid          g_uid = uid          while True :              #self.sendData(uid,”testing…”)              if    len ( g_output_log ) > 0 :                  log = g_output_log . pop ( )                  self . sendData ( uid , log )              else :                  time . sleep ( . 01 ) try :      g_output_log = [ ]      g_uid = None      tcpdump = threading . Thread ( name = ‘tcpdump’ , target = tcpdump )      tcpdump . start ( )               port = sys . argv [ 1 ] except :      port = 88 port = int ( port ) myIo = MyIO ( ) SocketServer ( port , myIo ) . run ( )     

JS代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 # -*- coding: utf8 -*- import sys import time import threading from pywebsocketserver . server import SocketServer from pywebsocketserver . baseio import BaseIO import subprocess def tcpdump ( ) :         global g_output_log      popen = subprocess . Popen ( [ ‘bash’ , ‘-c’ , “/usr/sbin/tcpdump -i eth0 -v” ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE )      pid = popen . pid      print ( ‘Popen.pid:’ + str ( pid ) )         while True :             line = popen . stdout . readline ( ) . strip ( )          #line =popen.communicate()          print “output:%s” % ( line )          g_output_log . append ( line )          if subprocess . Popen . poll ( popen ) is not None :                     break      print ( ‘DONE’ )         class MyIO ( BaseIO ) :      def onData ( self , uid , text ) :          self . sendData ( uid , “received the message:%s” % ( text , ) )      def onConnect ( self , uid ) :          global g_uid          g_uid = uid          while True :              #self.sendData(uid,”testing…”)              if    len ( g_output_log ) > 0 :                  log = g_output_log . pop ( )                  self . sendData ( uid , log )              else :                  time . sleep ( . 01 ) try :      g_output_log = [ ]      g_uid = None      tcpdump = threading . Thread ( name = ‘tcpdump’ , target = tcpdump )      tcpdump . start ( )               port = sys . argv [ 1 ] except :      port = 88 port = int ( port ) myIo = MyIO ( ) SocketServer ( port , myIo ) . run ( )     

html代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 < html > < head > < meta http equiv = “content-type” content = “text/html; charset=UTF-8” > <script src = “socketclient.js” > </script> < / head > < body > <script> var client = new SocketClient ( “192.168.199.3” , 88 , “chat” ) ; client . connect ( ) ; client . onData    = function ( text ) {      console . log ( text ) ;      element = document . getElementById ( “log” ) ;      if ( element ) {        if ( element . value . length > 100000 ) { element . value = “” ; }        element . value += ( text + “\n” ) ;        element . scrollTop = element . scrollHeight ;      } } client . onRegist = function ( ) {      this . sendData ( “I am coming!” ) ; } </script> < textarea name = “log” id = “log” style = “font-size:12px; width:100%; height: 600px;” > Tcpdump log : < / textarea > < / body > < / html >

演示说明

Server运行tcpdump,用户在浏览器查看运行的实时数据。

文件下载

运维生存时间下载pywebsocketserver

文章转载来自:trustauth.cn

文章版权及转载声明

本文作者:亿网 网址:https://www.edns.com/ask/post/163179.html 发布于 2024-11-22
文章转载或复制请以超链接形式并注明出处。