Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
39f7ac2e
Commit
39f7ac2e
authored
Aug 07, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 添加sys log
parent
0fac184a
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
96 additions
and
35 deletions
+96
-35
apps.py
apps/audits/apps.py
+4
-0
serializers.py
apps/audits/serializers.py
+27
-2
signals_handler.py
apps/audits/signals_handler.py
+37
-7
common.py
apps/common/utils/common.py
+4
-0
conf.py
apps/jumpserver/conf.py
+2
-0
settings.py
apps/jumpserver/settings.py
+20
-26
tasks.py
apps/ops/tasks.py
+2
-0
No files found.
apps/audits/apps.py
View file @
39f7ac2e
from
django.apps
import
AppConfig
from
django.conf
import
settings
from
django.db.models.signals
import
post_save
class
AuditsConfig
(
AppConfig
):
...
...
@@ -6,3 +8,5 @@ class AuditsConfig(AppConfig):
def
ready
(
self
):
from
.
import
signals_handler
if
settings
.
SYSLOG_ENABLE
:
post_save
.
connect
(
signals_handler
.
on_audits_log_create
)
apps/audits/serializers.py
View file @
39f7ac2e
...
...
@@ -3,11 +3,36 @@
from
rest_framework
import
serializers
from
.models
import
FTPLog
from
terminal.models
import
Session
from
.
import
models
class
FTPLogSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
FTPLog
model
=
models
.
FTPLog
fields
=
'__all__'
class
LoginLogSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
models
.
UserLoginLog
fields
=
'__all__'
class
OperateLogSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
models
.
OperateLog
fields
=
'__all__'
class
PasswordChangeLogSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
models
.
PasswordChangeLog
fields
=
'__all__'
class
SessionAuditSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
Session
fields
=
'__all__'
apps/audits/signals_handler.py
View file @
39f7ac2e
...
...
@@ -4,13 +4,18 @@
from
django.db.models.signals
import
post_save
,
post_delete
from
django.dispatch
import
receiver
from
django.db
import
transaction
from
rest_framework.renderers
import
JSONRenderer
from
jumpserver.utils
import
current_request
from
common.utils
import
get_request_ip
,
get_logger
from
common.utils
import
get_request_ip
,
get_logger
,
get_syslogger
from
users.models
import
User
from
.models
import
OperateLog
,
PasswordChangeLog
from
terminal.models
import
Session
from
.
import
models
from
.
import
serializers
logger
=
get_logger
(
__name__
)
sys_logger
=
get_syslogger
(
"audits"
)
json_render
=
JSONRenderer
()
MODELS_NEED_RECORD
=
(
...
...
@@ -36,7 +41,7 @@ def create_operate_log(action, sender, resource):
}
with
transaction
.
atomic
():
try
:
OperateLog
.
objects
.
create
(
**
data
)
models
.
OperateLog
.
objects
.
create
(
**
data
)
except
Exception
as
e
:
logger
.
error
(
"Create operate log error: {}"
.
format
(
e
))
...
...
@@ -44,15 +49,15 @@ def create_operate_log(action, sender, resource):
@receiver
(
post_save
,
dispatch_uid
=
"my_unique_identifier"
)
def
on_object_created_or_update
(
sender
,
instance
=
None
,
created
=
False
,
**
kwargs
):
if
created
:
action
=
OperateLog
.
ACTION_CREATE
action
=
models
.
OperateLog
.
ACTION_CREATE
else
:
action
=
OperateLog
.
ACTION_UPDATE
action
=
models
.
OperateLog
.
ACTION_UPDATE
create_operate_log
(
action
,
sender
,
instance
)
@receiver
(
post_delete
,
dispatch_uid
=
"my_unique_identifier"
)
def
on_object_delete
(
sender
,
instance
=
None
,
**
kwargs
):
create_operate_log
(
OperateLog
.
ACTION_DELETE
,
sender
,
instance
)
create_operate_log
(
models
.
OperateLog
.
ACTION_DELETE
,
sender
,
instance
)
@receiver
(
post_save
,
sender
=
User
,
dispatch_uid
=
"my_unique_identifier"
)
...
...
@@ -61,7 +66,32 @@ def on_user_change_password(sender, instance=None, **kwargs):
if
not
current_request
or
not
current_request
.
user
.
is_authenticated
:
return
with
transaction
.
atomic
():
PasswordChangeLog
.
objects
.
create
(
models
.
PasswordChangeLog
.
objects
.
create
(
user
=
instance
,
change_by
=
current_request
.
user
,
remote_addr
=
get_request_ip
(
current_request
),
)
def
on_audits_log_create
(
sender
,
instance
=
None
,
**
kwargs
):
if
sender
==
models
.
UserLoginLog
:
category
=
"login_log"
serializer
=
serializers
.
LoginLogSerializer
elif
sender
==
models
.
FTPLog
:
serializer
=
serializers
.
FTPLogSerializer
category
=
"ftp_log"
elif
sender
==
models
.
OperateLog
:
category
=
"operation_log"
serializer
=
serializers
.
OperateLogSerializer
elif
sender
==
models
.
PasswordChangeLog
:
category
=
"password_change_log"
serializer
=
serializers
.
PasswordChangeLogSerializer
elif
sender
==
Session
:
category
=
"host_session_log"
serializer
=
serializers
.
SessionAuditSerializer
else
:
return
s
=
serializer
(
instance
=
instance
)
data
=
json_render
.
render
(
s
.
data
)
.
decode
(
errors
=
'ignore'
)
msg
=
"{} - {}"
.
format
(
category
,
data
)
sys_logger
.
info
(
msg
)
apps/common/utils/common.py
View file @
39f7ac2e
...
...
@@ -31,6 +31,10 @@ def get_logger(name=None):
return
logging
.
getLogger
(
'jumpserver.
%
s'
%
name
)
def
get_syslogger
(
name
=
None
):
return
logging
.
getLogger
(
'jms.
%
s'
%
name
)
def
timesince
(
dt
,
since
=
''
,
default
=
"just now"
):
"""
Returns string representing "time since" e.g.
...
...
apps/jumpserver/conf.py
View file @
39f7ac2e
...
...
@@ -379,6 +379,8 @@ defaults = {
'ASSETS_PERM_CACHE_TIME'
:
3600
*
24
,
'SECURITY_MFA_VERIFY_TTL'
:
3600
,
'ASSETS_PERM_CACHE_ENABLE'
:
False
,
'SYSLOG_ADDR'
:
''
,
# '192.168.0.1:514'
'SYSLOG_FACILITY'
:
'user'
,
}
...
...
apps/jumpserver/settings.py
View file @
39f7ac2e
...
...
@@ -214,6 +214,9 @@ LOGGING = {
'simple'
:
{
'format'
:
'
%(levelname)
s
%(message)
s'
},
'syslog'
:
{
'format'
:
'
%(name)
s:
%(message)
s'
},
'msg'
:
{
'format'
:
'
%(message)
s'
}
...
...
@@ -246,20 +249,6 @@ LOGGING = {
'backupCount'
:
7
,
'filename'
:
ANSIBLE_LOG_FILE
,
},
'gunicorn_file'
:
{
'encoding'
:
'utf8'
,
'level'
:
'DEBUG'
,
'class'
:
'logging.handlers.RotatingFileHandler'
,
'formatter'
:
'msg'
,
'maxBytes'
:
1024
*
1024
*
100
,
'backupCount'
:
2
,
'filename'
:
GUNICORN_LOG_FILE
,
},
'gunicorn_console'
:
{
'level'
:
'DEBUG'
,
'class'
:
'logging.StreamHandler'
,
'formatter'
:
'msg'
},
},
'loggers'
:
{
'django'
:
{
...
...
@@ -281,14 +270,6 @@ LOGGING = {
'handlers'
:
[
'console'
,
'file'
],
'level'
:
LOG_LEVEL
,
},
'jumpserver.users.api'
:
{
'handlers'
:
[
'console'
,
'file'
],
'level'
:
LOG_LEVEL
,
},
'jumpserver.users.view'
:
{
'handlers'
:
[
'console'
,
'file'
],
'level'
:
LOG_LEVEL
,
},
'ops.ansible_api'
:
{
'handlers'
:
[
'console'
,
'ansible_logs'
],
'level'
:
LOG_LEVEL
,
...
...
@@ -297,10 +278,6 @@ LOGGING = {
'handlers'
:
[
'console'
,
'file'
],
'level'
:
"INFO"
,
},
# 'gunicorn': {
# 'handlers': ['gunicorn_console', 'gunicorn_file'],
# 'level': 'INFO',
# },
# 'django.db': {
# 'handlers': ['console', 'file'],
# 'level': 'DEBUG'
...
...
@@ -308,6 +285,23 @@ LOGGING = {
}
}
SYSLOG_ENABLE
=
False
if
CONFIG
.
SYSLOG_ADDR
!=
''
and
len
(
CONFIG
.
SYSLOG_ADDR
.
split
(
':'
))
==
2
:
host
,
port
=
CONFIG
.
SYSLOG_ADDR
.
split
(
':'
)
SYSLOG_ENABLE
=
True
LOGGING
[
'handlers'
][
'syslog'
]
=
{
'level'
:
'INFO'
,
'class'
:
'logging.handlers.SysLogHandler'
,
'facility'
:
CONFIG
.
SYSLOG_FACILITY
,
'address'
:
(
host
,
int
(
port
)),
'formatter'
:
'syslog'
}
LOGGING
[
'loggers'
][
'jms'
]
=
{
'handlers'
:
[
'syslog'
],
'level'
:
'INFO'
}
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
# LANGUAGE_CODE = 'en'
...
...
apps/ops/tasks.py
View file @
39f7ac2e
...
...
@@ -109,7 +109,9 @@ def hello(name, callback=None):
# @after_app_shutdown_clean_periodic
# @register_as_period_task(interval=30)
def
hello123
():
p
=
subprocess
.
Popen
(
'ls /tmp'
,
shell
=
True
)
print
(
"{} Hello world"
.
format
(
datetime
.
datetime
.
now
()
.
strftime
(
"
%
H:
%
M:
%
S"
)))
return
None
@shared_task
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment