记录日志

pull/2/head
ibuler 10 years ago
parent f766ba340f
commit 765ac5e714

@ -22,7 +22,7 @@ cur_dir = os.path.dirname(__file__)
sys.path.append('%s/webroot/AutoSa/' % cur_dir) sys.path.append('%s/webroot/AutoSa/' % cur_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'AutoSa.settings' os.environ['DJANGO_SETTINGS_MODULE'] = 'AutoSa.settings'
from UserManage.models import User from UserManage.models import User,Logs
cf = ConfigParser.ConfigParser() cf = ConfigParser.ConfigParser()
@ -40,8 +40,6 @@ assets_user_table = cf.get('jumpserver', 'assets_user_table')
key = cf.get('jumpserver', 'key') key = cf.get('jumpserver', 'key')
class PyCrypt(object): class PyCrypt(object):
"""It's used to encrypt and decrypt password.""" """It's used to encrypt and decrypt password."""
def __init__(self, key): def __init__(self, key):
@ -120,11 +118,16 @@ def connect(host, port, user, password):
log_date_dir = '%s/%s' % (log_dir, time.strftime('%Y%m%d')) log_date_dir = '%s/%s' % (log_dir, time.strftime('%Y%m%d'))
if not os.path.isdir(log_date_dir): if not os.path.isdir(log_date_dir):
os.mkdir(log_date_dir) os.mkdir(log_date_dir)
logfile = open("%s/%s_%s_%s" % (log_date_dir, host, user, time.strftime('%Y%m%d%H%M%S')), 'a') structtime_now = time.localtime()
db, cursor = connect_db(db_user, db_password, db_db, db_host, db_port) datetime_now = time.strftime('%Y%m%d%H%M%S', structtime_now)
cursor.execute('insert into logs (Fuser, Fhost, Flogfile, Fstart_time) value (%s, %s, %s, UNIX_TIMESTAMP())' logtime_now = time.strftime('%Y/%m/%d %H:%M:%S', structtime_now)
% (user, host, logfile)) timestamp_now = int(time.mktime(structtime_now))
logfile.write('\n%s\n' % time.strftime('%Y/%m/%d %H:%M:%S'))
logfile_name = "%s/%s_%s_%s" % (log_date_dir, host, user, datetime_now)
logfile = open(logfile_name, 'a')
log = Logs(user=user, host=host, logfile=logfile_name, start_time=timestamp_now)
log.save()
logfile.write('\n%s\n' % logtime_now)
try: try:
global foo global foo
foo = pxssh.pxssh() foo = pxssh.pxssh()
@ -133,13 +136,18 @@ def connect(host, port, user, password):
foo.sendline('') foo.sendline('')
signal.signal(signal.SIGWINCH, sigwinch_passthrough) signal.signal(signal.SIGWINCH, sigwinch_passthrough)
foo.interact(escape_character=chr(28)) foo.interact(escape_character=chr(28))
cursor.execute('update logs set Ffindsh=True, Fend_time=UNIX_TIMESTAMP() where Flogfile=%s' % logfile) log.finish = 1
log.end_time = int(time.time())
log.save()
except pxssh.ExceptionPxssh as e: except pxssh.ExceptionPxssh as e:
print('登录失败: %s' % e) print('登录失败: %s' % e)
cursor.execute('update logs set Ffindsh=2 where Flogfile=%s' % logfile) log.finish = 2
log.save()
except KeyboardInterrupt: except KeyboardInterrupt:
foo.logout() foo.logout()
cursor.execute('update logs set Ffindsh=True, Fend_time=UNIX_TIMESTAMP() where Flogfile=%s ' % logfile) log.finish = 1
log.end_time = int(time.time())
log.save()
def ip_all_select(username): def ip_all_select(username):

@ -26,7 +26,7 @@ cf = ConfigParser.ConfigParser()
cf.read('%s/jumpserver.conf' % CONF_DIR) cf.read('%s/jumpserver.conf' % CONF_DIR)
key = cf.get('jumpserver', 'key') key = cf.get('jumpserver', 'key')
rsa_dir = cf.get('jumpserver', 'rsa_dir') rsa_dir = os.path.join(CONF_DIR, 'keys')
ldap_host = cf.get('jumpserver', 'ldap_host') ldap_host = cf.get('jumpserver', 'ldap_host')
ldap_base_dn = cf.get('jumpserver', 'ldap_base_dn') ldap_base_dn = cf.get('jumpserver', 'ldap_base_dn')
admin_cn = cf.get('jumpserver', 'admin_cn') admin_cn = cf.get('jumpserver', 'admin_cn')

@ -27,7 +27,7 @@ class Logs(models.Model):
user = models.CharField(max_length=50) user = models.CharField(max_length=50)
host = models.CharField(max_length=20) host = models.CharField(max_length=20)
logfile = models.CharField(max_length=1000) logfile = models.CharField(max_length=1000)
finish = models.SmallIntegerField(max_length=4) finish = models.SmallIntegerField(max_length=4, default=0)
start_time = models.IntegerField() start_time = models.IntegerField()
end_time = models.IntegerField() end_time = models.IntegerField()

Loading…
Cancel
Save