日志处理系统

pull/2/head
ibuler 2014-11-04 00:10:01 +08:00
parent d728c06851
commit 2212cf24b4
4 changed files with 90 additions and 3 deletions

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 2.7.6 (C:/Python27/python.exe)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 2.7.8 (C:\Python27\python.exe)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -5,7 +5,6 @@
import os
import sys
import subprocess
import MySQLdb
import struct
import fcntl
import termios
@ -17,6 +16,7 @@ from binascii import b2a_hex, a2b_hex
import ConfigParser
import paramiko
import pxssh
import pexpect
cur_dir = os.path.dirname(__file__)
sys.path.append('%s/webroot/AutoSa/' % cur_dir)
@ -131,7 +131,7 @@ def connect(host, port, user, password):
foo.login(host, user, password, port=port, auto_prompt_reset=False)
log = Logs(user=user, host=host, logfile=logfile_name, start_time=timestamp_start, ppid=os.getpid()) # 日志信息记录到数据库
log.save()
pid = Pid(ppid=os.getpid(), cpid=foo.pid)
pid = Pid(ppid=os.getpid(), cpid=foo.pid, start_time=timestamp_start, logid=log.id)
pid.save()
logfile = open(logfile_name, 'a') # 记录日志文件
@ -145,8 +145,11 @@ def connect(host, port, user, password):
log.finish = 1
log.end_time = int(time.time())
log.save()
except pxssh.ExceptionPxssh as e:
print('登录失败: %s' % e)
except pexpect.EOF:
print('登录失败: Host refuse')
except KeyboardInterrupt:
foo.logout()
log.finish = 1

View File

@ -0,0 +1,82 @@
#!/usr/bin/python
#coding: utf-8
import os
import sys
import time
cur_dir = os.path.dirname(__file__)
sys.path.append('%s/webroot/AutoSa/' % cur_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'AutoSa.settings'
from UserManage.models import Logs, Pid
def log_hanler(logid):
log = Logs.objects.filter(id=logid)
if log:
log = log[0]
filename = log.logfile
os.system('cat %s | grep "DateTime" > %s.his' % (filename, filename))
os.system('cat %s | grep "\[.*@.*\][\$\#]" >> %s.his' % (filename, filename))
os.system('cat %s | grep "EndTime" >> %s.his' % (filename, filename))
def set_finish(id):
logs = Logs.objects.filter(id=id, finish=0)
if logs:
structtime_start = time.localtime()
timestamp_end = int(time.mktime(structtime_start))
log = logs[0]
log.finish = 1
log.end_time = timestamp_end
log.save()
def kill_pid(pid):
try:
os.kill(pid, 9)
except OSError:
pass
def pid_exist(pid):
pid_dir = "/proc/%s" % pid
if os.path.isdir(pid_dir):
return True
else:
return False
def get_pids():
pids = []
pids_obj = Pid.objects.all()
for pid_obj in pids_obj:
pids.append((pid_obj.ppid, pid_obj.cpid, pid_obj.logid, pid_obj.start_time))
return pids
def run():
for ppid, cpid, logid, start_time in get_pids():
if pid_exist(cpid):
if pid_exist(ppid):
structtime_start = time.localtime()
timestamp_end = int(time.mktime(structtime_start))
if timestamp_end - start_time > 7200:
kill_pid(ppid)
kill_pid(cpid)
set_finish(logid)
log_hanler(logid)
else:
kill_pid(cpid)
set_finish(logid)
log_hanler(logid)
else:
set_finish(logid)
log_hanler(logid)
if __name__ == '__main__':
while True:
run()
time.sleep(0.5)

View File

@ -43,4 +43,6 @@ class Pid(models.Model):
"""
ppid = models.IntegerField()
cpid = models.IntegerField()
logid = models.IntegerField()
start_time = models.IntegerField()