From b4d74bc5890748cec5652479c75bb8ff79b9781c Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 29 Feb 2016 17:53:15 +0800 Subject: [PATCH] =?UTF-8?q?hot=5Ffix(kill=20invalid=20connection)=20?= =?UTF-8?q?=E7=B4=A7=E6=80=A5=E4=BF=AE=E5=A4=8D=E8=B6=85=E6=97=B6=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修改日志参数 2. 修改处理间隔,每10分钟处理一次 3. 修改处理策略 ssh: 1小时不操作,就kill掉 web: 超过1天,就设置完成 --- jlog/log_api.py | 22 +++++++++++----------- jumpserver/settings.py | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/jlog/log_api.py b/jlog/log_api.py index 6f2d6edbe..7089346bc 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -15,6 +15,7 @@ from struct import unpack from subprocess import Popen from sys import platform, prefix, stderr from tempfile import NamedTemporaryFile +from jumpserver.api import logger from jinja2 import FileSystemLoader, Template from jinja2.environment import Environment @@ -80,27 +81,26 @@ def renderTemplate(script_path, time_file_path, dimensions=(24, 80), templatenam def kill_invalid_connection(): - long_time_logs = [] unfinished_logs = Log.objects.filter(is_finished=False) now = datetime.datetime.now() now_timestamp = int(time.mktime(now.timetuple())) - for log in unfinished_logs: - if (now - log.start_time).days > 1: - long_time_logs.append(log) - for log in long_time_logs: + for log in unfinished_logs: try: - log_file_mtime = int(os.stat(log.log_path).st_mtime) + log_file_mtime = int(os.stat('%s.log' % log.log_path).st_mtime) except OSError: log_file_mtime = 0 if (now_timestamp - log_file_mtime) > 3600: - try: - os.kill(int(log.pid), 9) - except OSError: - pass + if log.login_type == 'ssh': + try: + os.kill(int(log.pid), 9) + except OSError: + pass + elif (now - log.start_time).days < 1: + continue log.is_finished = True log.end_time = now log.save() - + logger.warn('kill log %s' % log.log_path) diff --git a/jumpserver/settings.py b/jumpserver/settings.py index d1c8a8f40..333f1447f 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -153,5 +153,5 @@ BOOTSTRAP_COLUMN_COUNT = 10 CRONJOBS = [ ('0 1 * * *', 'jasset.asset_api.asset_ansible_update_all'), - ('1 * * * *', 'jlog.log_api.kill_invalid_connection'), + ('*/10 * * * *', 'jlog.log_api.kill_invalid_connection'), ]