diff --git a/jumpserver/api.py b/jumpserver/api.py index 9d0ab1c23..f693832f6 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -182,6 +182,7 @@ class Jtty(object): raise ServerError('Create %s failed, Please modify %s permission.' % (today_connect_log_dir, tty_log_dir)) try: + # log_file_f = open('/opt/jumpserver/logs/tty/20151102/a_b_191034.log', 'a') log_file_f = open(log_file_path + '.log', 'a') log_time_f = open(log_file_path + '.time', 'a') except IOError: diff --git a/run_log_watch.py b/run_log_watch.py index ad8168739..3313ed65d 100644 --- a/run_log_watch.py +++ b/run_log_watch.py @@ -11,11 +11,13 @@ import tornado.options import tornado.web import tornado.websocket import tornado.httpserver +import tornado.gen from tornado.options import define, options from pyinotify import WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY define("port", default=8080, help="run on the given port", type=int) +define("host", default='0.0.0.0', help="run port on", type=str) class EventHandler(ProcessEvent): @@ -63,7 +65,7 @@ class Application(tornado.web.Application): def __init__(self): handlers = [ (r'/', MainHandler), - (r'/send', SendHandler), + (r'/monitor', MonitorHandler), ] setting = { @@ -75,31 +77,33 @@ class Application(tornado.web.Application): tornado.web.Application.__init__(self, handlers, **setting) -class SendHandler(tornado.websocket.WebSocketHandler): +class MonitorHandler(tornado.websocket.WebSocketHandler): clients = set() + def __init__(self, *args, **kwargs): + self.file_path = None + super(self.__class__, self).__init__(*args, **kwargs) + def check_origin(self, origin): return True def open(self): - SendHandler.clients.add(self) + # 获取监控的path + self.file_path = self.get_argument('file_path', '') + MonitorHandler.clients.add(self) self.stream.set_nodelay(True) def on_message(self, message): - self.write_message(message) - # while True: - # self.write_message(json.dumps(message)) - # time.sleep(1) - # # 服务器主动关闭 - # self.close() - # SendHandler.clients.remove(self) - - file_monitor('/opt/jumpserver/logs/tty/20151102/a_b_191034.log', client=self) - self.write_message('monitor /tmp/test1234') + self.write_message('Connect WebSocket Success.
') + # 监控日志,发生变动发向客户端 + file_monitor('%s.log' % self.file_path, client=self) + self.write_message('Disconnect WebSocket.
') def on_close(self): # 客户端主动关闭 - SendHandler.clients.remove(self) + self.close() + self.finish() + MonitorHandler.clients.remove(self) class MainHandler(tornado.web.RequestHandler): @@ -111,5 +115,7 @@ if __name__ == '__main__': tornado.options.parse_command_line() app = Application() server = tornado.httpserver.HTTPServer(app) - server.listen(options.port) + server.bind(options.port, options.host) + # server.listen(options.port) + server.start(num_processes=1) tornado.ioloop.IOLoop.instance().start() diff --git a/templates/jlog/log_monitor.html b/templates/jlog/log_monitor.html index 91037b1c2..8f3c1ef3d 100644 --- a/templates/jlog/log_monitor.html +++ b/templates/jlog/log_monitor.html @@ -42,7 +42,7 @@ ws.onmessage = function(evt){ console.log(evt.data); - $('#content').append(evt.data.replace('\r\n', '
').replace('[\r\n]', '
')); + $('#content').append(evt.data.replace(/\n|\r|(\r\n)|(\u0085)|(\u2028)|(\u2029)/g, '
')); }; ws.onclose = function(evt){ diff --git a/templates/jlog/log_online.html b/templates/jlog/log_online.html index 55bacb4fb..b12302331 100644 --- a/templates/jlog/log_online.html +++ b/templates/jlog/log_online.html @@ -77,7 +77,7 @@ {{ post.remote_ip }} {% ifnotequal session_role_id 0 %} 命令统计 - 监控 + 监控 {% endifnotequal %} {{ post.start_time|date:"Y-m-d H:i:s" }} @@ -99,13 +99,66 @@ {##}