mirror of https://github.com/openspug/spug
A api add monitor module
parent
68d9327284
commit
2054c35e23
|
@ -0,0 +1,38 @@
|
||||||
|
from django.db import models
|
||||||
|
from libs import ModelMixin, human_time
|
||||||
|
from apps.account.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class Detection(models.Model, ModelMixin):
|
||||||
|
TYPES = (
|
||||||
|
('1', '站点检测'),
|
||||||
|
('2', '端口检测'),
|
||||||
|
('3', '进程检测'),
|
||||||
|
('4', '自定义脚本'),
|
||||||
|
)
|
||||||
|
name = models.CharField(max_length=50)
|
||||||
|
type = models.CharField(max_length=2, choices=TYPES)
|
||||||
|
addr = models.CharField(max_length=255)
|
||||||
|
extra = models.TextField(null=True)
|
||||||
|
desc = models.CharField(max_length=255, null=True)
|
||||||
|
is_active = models.BooleanField(default=True)
|
||||||
|
rate = models.IntegerField(default=5)
|
||||||
|
threshold = models.IntegerField(default=3)
|
||||||
|
quiet = models.IntegerField(default=24 * 60)
|
||||||
|
|
||||||
|
created_at = models.CharField(max_length=20, default=human_time)
|
||||||
|
created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
|
||||||
|
updated_at = models.CharField(max_length=20, null=True)
|
||||||
|
updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)
|
||||||
|
|
||||||
|
def to_dict(self, *args, **kwargs):
|
||||||
|
tmp = super().to_dict(*args, **kwargs)
|
||||||
|
tmp['type_alias'] = self.get_type_display()
|
||||||
|
return tmp
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<Detection %r>' % self.name
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
db_table = 'detections'
|
||||||
|
ordering = ('-id',)
|
|
@ -0,0 +1,7 @@
|
||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from .views import *
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('', DetectionView.as_view()),
|
||||||
|
]
|
|
@ -0,0 +1,39 @@
|
||||||
|
from django.views.generic import View
|
||||||
|
from libs import json_response, JsonParser, Argument, human_time
|
||||||
|
from apps.monitor.models import Detection
|
||||||
|
|
||||||
|
|
||||||
|
class DetectionView(View):
|
||||||
|
def get(self, request):
|
||||||
|
detections = Detection.objects.all()
|
||||||
|
return json_response(detections)
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
form, error = JsonParser(
|
||||||
|
Argument('id', type=int, required=False),
|
||||||
|
Argument('name', help='请输入任务名称'),
|
||||||
|
Argument('addr', help='请输入监控地址'),
|
||||||
|
Argument('type', filter=lambda x: x in dict(Detection.TYPES), help='请选择监控类型'),
|
||||||
|
Argument('extra', required=False),
|
||||||
|
Argument('desc', required=False),
|
||||||
|
Argument('rate', type=int, default=5),
|
||||||
|
Argument('threshold', type=int, default=3),
|
||||||
|
Argument('quiet', type=int, default=24 * 60)
|
||||||
|
).parse(request.body)
|
||||||
|
if error is None:
|
||||||
|
if form.id:
|
||||||
|
form.updated_at = human_time()
|
||||||
|
form.updated_by = request.user
|
||||||
|
Detection.objects.filter(pk=form.pop('id')).update(**form)
|
||||||
|
else:
|
||||||
|
form.created_by = request.user
|
||||||
|
Detection.objects.create(**form)
|
||||||
|
return json_response(error=error)
|
||||||
|
|
||||||
|
def delete(self, request):
|
||||||
|
form, error = JsonParser(
|
||||||
|
Argument('id', type=int, help='请指定操作对象')
|
||||||
|
).parse(request.GET)
|
||||||
|
if error is None:
|
||||||
|
Detection.objects.filter(pk=form.id).delete()
|
||||||
|
return json_response(error=error)
|
|
@ -35,6 +35,7 @@ INSTALLED_APPS = [
|
||||||
'apps.setting',
|
'apps.setting',
|
||||||
'apps.exec',
|
'apps.exec',
|
||||||
'apps.schedule',
|
'apps.schedule',
|
||||||
|
'apps.monitor',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|
|
@ -20,4 +20,5 @@ urlpatterns = [
|
||||||
path('host/', include('apps.host.urls')),
|
path('host/', include('apps.host.urls')),
|
||||||
path('exec/', include('apps.exec.urls')),
|
path('exec/', include('apps.exec.urls')),
|
||||||
path('schedule/', include('apps.schedule.urls')),
|
path('schedule/', include('apps.schedule.urls')),
|
||||||
|
path('monitor/', include('apps.monitor.urls')),
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue