monitor_host_create.py
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from .models import MonitorHost, db
from sqlalchemy import and_
from app.util.component.ApiTemplate import ApiTemplate
import uuid
class Api(ApiTemplate):
api_name = "注册主机"
def para_check(self):
if not self.para.get("host"):
raise Exception("缺乏host参数")
if not self.para.get("user"):
raise Exception("缺乏user参数")
if not self.para.get("type"):
raise Exception("缺乏type参数")
if not self.para.get("passwd"):
raise Exception("缺乏passwd参数")
def process(self):
# 返回结果
res = {}
res["data"] = {}
try:
host = self.para.get("host") # server
user = self.para.get("user")
password = self.para.get("passwd")
type = self.para.get("type")
host_name = self.para.get("host_name")
srcid = uuid.uuid1().__str__()
if(db.session.query(MonitorHost.host).filter(MonitorHost.host == host).count() > 0):
res['message'] = '禁止重复注册host'
res["result"] = False
else:
monitorHost = MonitorHost(
srcid=srcid, host=host, user=user, password=password, type=type, host_name=host_name)
db.session.add(monitorHost)
db.session.commit()
res['data'] = {'host': host, 'srcid': srcid}
res["result"] = True
except Exception as e:
db.session.rollback()
raise e
return res
api_doc = {
"tags": ["监控接口"],
"parameters": [
{"name": "host",
"in": "formData",
"type": "string",
"description": "主机地址",
"required": "true"},
{"name": "user",
"in": "formData",
"type": "string",
"description": "用户",
"required": "true"},
{"name": "passwd",
"in": "formData",
"type": "string",
"description": "密码",
"required": "true"},
{"name": "type",
"in": "formData",
"type": "string",
"description": "服务器类型",
"required": "true"},
{"name": "host_name",
"in": "formData",
"type": "string",
"description": "主机名"}
],
"responses": {
200: {
"schema": {
"properties": {
}
}
}
}
}