Commit 0e9a9625 authored by ibuler's avatar ibuler

具体体现在 日志监控页,定期回收过期的在线log

需要运行python manage.py crontab add来添加

运行 python manage.py crontab remove 来去掉

crontab -l
parent b7905629
...@@ -6,7 +6,10 @@ from contextlib import closing ...@@ -6,7 +6,10 @@ from contextlib import closing
from io import open as copen from io import open as copen
from json import dumps from json import dumps
from math import ceil from math import ceil
import datetime
import time
import re import re
import os
from os.path import basename, dirname, exists, join from os.path import basename, dirname, exists, join
from struct import unpack from struct import unpack
from subprocess import Popen from subprocess import Popen
...@@ -17,6 +20,7 @@ from jinja2 import FileSystemLoader, Template ...@@ -17,6 +20,7 @@ from jinja2 import FileSystemLoader, Template
from jinja2.environment import Environment from jinja2.environment import Environment
from jumpserver.api import BASE_DIR from jumpserver.api import BASE_DIR
from jlog.models import Log
DEFAULT_TEMPLATE = join(BASE_DIR, 'templates', 'jlog', 'static.jinja2') DEFAULT_TEMPLATE = join(BASE_DIR, 'templates', 'jlog', 'static.jinja2')
...@@ -75,3 +79,28 @@ def renderTemplate(script_path, time_file_path, dimensions=(24, 80), templatenam ...@@ -75,3 +79,28 @@ def renderTemplate(script_path, time_file_path, dimensions=(24, 80), templatenam
return rendered return rendered
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:
try:
log_file_mtime = int(os.stat(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
log.is_finished = True
log.end_time = now
log.save()
...@@ -152,5 +152,6 @@ STATIC_URL = '/static/' ...@@ -152,5 +152,6 @@ STATIC_URL = '/static/'
BOOTSTRAP_COLUMN_COUNT = 10 BOOTSTRAP_COLUMN_COUNT = 10
CRONJOBS = [ CRONJOBS = [
('0 1 * * *', 'jasset.asset_api.asset_ansible_update_all') ('0 1 * * *', 'jasset.asset_api.asset_ansible_update_all'),
('1 * * * *', 'jlog.log_api.kill_invalid_connection'),
] ]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment