增加云主机同步的自定义IP端口功能

This commit is contained in:
StarsL.cn 2022-06-23 08:18:57 +08:00
parent e448e4daa2
commit b06b8c6617
11 changed files with 264 additions and 28 deletions

View File

@ -3,7 +3,7 @@ help:
build:
cd flask-consul && docker build -t flask-consul:latest .
cd vue-consul && docker build -t nginx-consul:latest .
cd vue-consul && docker build -t nginx-consul:tensuns-latest -f Dockerfile.tensuns .
cd vue-consul && docker build -t nginx-consul:tensuns-latest -f Dockerfile.tensuns .
echo -e "\n\n自行编译的版本注意修改docker-compose.yml中的镜像地址为本地仓库后再启动。\nBlackbox-Manager:\nhttp://{ip}:1026\n"
push:
@ -16,8 +16,8 @@ push:
docker push registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:${ver}
docker push registry.cn-shenzhen.aliyuncs.com/starsl/flask-consul:latest
docker push registry.cn-shenzhen.aliyuncs.com/starsl/flask-consul:${ver}
docker tag nginx-consul:tensuns-latest registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:tensuns-latest
docker push registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:tensuns-latest
docker tag nginx-consul:tensuns-latest registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:tensuns-latest
docker push registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:tensuns-latest
update:
docker-compose pull && docker-compose up -d

View File

@ -1,5 +1,6 @@
#!/usr/bin/python3
import requests,json
from units import consul_kv
from config import consul_token,consul_url,vendors,regions
headers = {'X-Consul-Token': consul_token}
geturl = f'{consul_url}/agent/services'
@ -34,8 +35,13 @@ def w2consul(vendor,account,region,ecs_dict):
print({"code": 50000,"data": f'{dereg.status_code}:{dereg.text}'}, flush=True)
else:
on = on + 1
port = 9100 if v['ostype'] == 'linux' else 9182
ip = v['ip'] if isinstance(v['ip'],list) is False else v['ip'][0]
custom_ecs = consul_kv.get_value(f'ConsulManager/assets/sync_ecs_custom/{iid}')
port = custom_ecs.get('port')
ip = custom_ecs.get('ip')
if port == None:
port = 9100 if v['ostype'] == 'linux' else 9182
if ip == None:
ip = v['ip'] if isinstance(v['ip'],list) is False else v['ip'][0]
instance = f'{ip}:{port}'
data = {
'id': iid,
@ -73,4 +79,5 @@ def w2consul(vendor,account,region,ecs_dict):
#print({f"{account}:code": 20000,"data": "增加成功!"}, flush=True)
else:
print({f"{account}:code": 50000,"data": f'{reg.status_code}:{reg.text}'}, flush=True)
#return {"code": 50000,"data": f'{reg.status_code}:{reg.text}'}
return off,on

View File

@ -0,0 +1,31 @@
import requests,json
import sys
sys.path.append("..")
from config import consul_token,consul_url
headers = {'X-Consul-Token': consul_token}
def get_sid(iid):
url = f'{consul_url}/agent/service/{iid}'
response = requests.get(url, headers=headers)
if response.status_code == 200:
info = response.json()
return {'code': 20000,'instance':info}
else:
return {'code': 50000, 'data': f'{response.status_code}:{response.text}'}
def del_sid(iid):
reg = requests.put(f'{consul_url}/agent/service/deregister/{iid}', headers=headers)
if reg.status_code == 200:
return {"code": 20000, "data": f"{iid}】删除成功!"}
else:
return {"code": 50000, "data": f"{reg.status_code}{iid}{reg.text}"}
def add_sid(instance_dict):
reg = requests.put(f'{consul_url}/agent/service/register', headers=headers, data=json.dumps(instance_dict))
if reg.status_code == 200:
return {"code": 20000, "data": f"增加成功!"}
else:
print(f"{reg.status_code}:{reg.text}")
return {"code": 50000, "data": f"{reg.status_code}:{reg.text}"}

View File

@ -67,5 +67,6 @@ def del_service(vendor,account,region,group,name):
if reg.status_code == 200:
return {"code": 20000, "data": f"{sid}】删除成功!"}
else:
print(f"{reg.status_code}{sid}{reg.text}")
return {"code": 50000, "data": f"{reg.status_code}{sid}{reg.text}"}

View File

@ -1,7 +1,6 @@
#!/usr/bin/python3
import requests, json
import xlrd
import sys
import xlrd,re,sys
sys.path.append("..")
from config import consul_token,consul_url
@ -18,6 +17,7 @@ def importconsul(row,imptype):
}
elif imptype == 'selfnode':
vendor,account,region,group,name,instance,os = row
print(row)
sid = f"{vendor}/{account}/{region}/{group}@{name}"
ip = instance.split(':')[0]
port = instance.split(':')[1]
@ -52,7 +52,19 @@ def read_execl(file_contents,imptype):
row = table.row_values(rownum)
if rownum == 0:
continue
imp = importconsul(row,imptype)
nrow = []
for i in row:
try:
float(i)
if i % 1 == 0:
i = int(i)
nrow.append(str(i))
except:
j = i.strip()
if i != row[5]:
j = re.sub('[[ \]`~!\\\#$^/&*=|"{}\':;?]','_',j)
nrow.append(j)
imp = importconsul(nrow,imptype)
if imp['code'] == 50000:
return imp
return {"code": 20000, "data": f"导入成功!共导入 {rownum} 条数据。"}

View File

@ -3,7 +3,7 @@ from flask_restful import reqparse, Resource, Api
from flask_apscheduler import APScheduler
#import sys
#sys.path.append("..")
from units import token_auth,consul_kv,gen_config
from units import token_auth,consul_kv,gen_config,consul_svc
blueprint = Blueprint('nodes',__name__)
api = Api(blueprint)
@ -11,6 +11,10 @@ api = Api(blueprint)
parser = reqparse.RequestParser()
parser.add_argument('job_id',type=str)
parser.add_argument('services_dict',type=dict)
parser.add_argument('cst_ecs_dict',type=dict)
parser.add_argument('iid',type=str)
parser.add_argument('jobecs_name',type=str)
parser.add_argument('checked',type=str)
class Nodes(Resource):
decorators = [token_auth.auth.login_required]
@ -40,9 +44,61 @@ class Nodes(Resource):
return {'code': 20000,'services_list': sorted(set(services_list))}
elif stype == 'rules':
return gen_config.get_rules()
elif stype == 'cstecsconf':
args = parser.parse_args()
iid = args['iid']
cst_ecs_config = consul_kv.get_value(f'ConsulManager/assets/sync_ecs_custom/{iid}')
cst_ecs_config.update({'iid': iid,'ipswitch': False,'portswitch': False})
if 'ip' in cst_ecs_config and cst_ecs_config['ip'] != '':
cst_ecs_config['ipswitch'] = True
if 'port' in cst_ecs_config and cst_ecs_config['port'] != '':
cst_ecs_config['portswitch'] = True
return {'code': 20000, 'cst_ecs': cst_ecs_config}
elif stype == 'cstecslist':
args = parser.parse_args()
jobecs_name = args['jobecs_name']
checked = args['checked']
cst_ecs_dict = consul_kv.get_kv_dict('ConsulManager/assets/sync_ecs_custom/')
cst_ecs_keylist = [k.split('/')[-1] for k,v in cst_ecs_dict.items() if v != {}]
ecs_info = consul_kv.get_ecs_services(jobecs_name)
if checked == 'false':
return ecs_info
else:
cst_ecs_list = [i for i in ecs_info['ecs_list'] if i['iid'] in cst_ecs_keylist]
return {'code': 20000, 'ecs_list': cst_ecs_list}
def post(self, stype):
if stype == 'config':
args = parser.parse_args()
services_dict = args['services_dict']
return gen_config.ecs_config(services_dict['services_list'],services_dict['ostype_list'])
elif stype == 'cstecs':
args = parser.parse_args()
cst_ecs_dict = args['cst_ecs_dict']
consul_ecs_cst = {}
iid = cst_ecs_dict['iid']
try:
sid_dict = consul_svc.get_sid(iid)['instance']
if cst_ecs_dict['portswitch'] and cst_ecs_dict['port'] != '':
consul_ecs_cst['port'] = int(cst_ecs_dict['port'])
sid_dict['Port'] = consul_ecs_cst['port']
if cst_ecs_dict['ipswitch'] and cst_ecs_dict['ip'] != '':
consul_ecs_cst['ip'] = cst_ecs_dict['ip']
sid_dict['Address'] = consul_ecs_cst['ip']
consul_kv.put_kv(f'ConsulManager/assets/sync_ecs_custom/{iid}',consul_ecs_cst)
del sid_dict['TaggedAddresses']
del sid_dict['Weights']
del sid_dict['ContentHash']
del sid_dict['Datacenter']
sid_dict['name'] = sid_dict.pop('Service')
sid_dict['Meta']['instance'] = f"{sid_dict['Address']}:{sid_dict['Port']}"
sid_dict["check"] = { "tcp": sid_dict['Meta']['instance'],"interval": "60s" }
consul_svc.del_sid(iid)
consul_svc.add_sid(sid_dict)
return {'code': 20000, 'data': '自定义实例信息修改成功!'}
except Exception as e:
print(e,flush=True)
return {'code': 50000, "data": '提交自定义实例信息格式错误!'}
api.add_resource(Nodes, '/api/nodes/<stype>')

View File

@ -65,3 +65,27 @@ export function getRules() {
method: 'get'
})
}
export function postCstEcs(cst_ecs_dict) {
return request({
url: '/api/nodes/cstecs',
method: 'post',
data: { cst_ecs_dict }
})
}
export function getCstEcsConfig(iid) {
return request({
url: '/api/nodes/cstecsconf',
method: 'get',
params: { iid }
})
}
export function getCstEcsList(jobecs_name, checked) {
return request({
url: '/api/nodes/cstecslist',
method: 'get',
params: { jobecs_name, checked }
})
}

Binary file not shown.

View File

@ -1,6 +1,7 @@
<template>
<div class="app-container">
<el-alert v-if="services_name === 'blackbox_exporter'" title="如需管理【blackbox_exporter】的监控实例建议使用左侧菜单【Blackbox 站点监控】来维护,更加方便直观。" type="success" center close-text="知道了" />
<el-alert v-if="services_name === 'blackbox_exporter'" title="以下是站点监控的实例如需管理【blackbox_exporter】的监控实例建议使用左侧菜单【Blackbox 站点监控】来维护,更加方便直观。" type="success" center close-text="朕知道了" />
<el-alert v-if="services_name === 'selfnode_exporter'" title="以下是自建主机的实例,如需管理【自建主机】的监控实例,建议使用左侧菜单【自建主机管理】来维护,更加方便直观。" type="success" center close-text="朕知道了" />
<el-select v-model="services_name" placeholder="请选择 Services" filterable collapse-tags style="width: 250px" class="filter-item" @change="fetchData(services_name)">
<el-option v-for="item in services_name_list" :key="item" :label="item" :value="item" />
</el-select>
@ -13,8 +14,19 @@
<el-button class="filter-item" type="danger" icon="el-icon-delete" @click="handleDelAll">
批量删除
</el-button>
<el-table ref="expandstable" v-loading="listLoading" :data="instances" border fit highlight-current-row style="width: 100%;" @selection-change="handleSelectionChange">
<div style="float: right;margin-left: 10px;">
<el-input v-model="iname" prefix-icon="el-icon-search" placeholder="支持实例ID、地址、端口、Tags筛选" clearable style="width: 300px" class="filter-item" />
</div>
<el-table
ref="expandstable"
v-loading="listLoading"
:data="instances.filter(data => !iname || (data.ID.toLowerCase().includes(iname.toLowerCase()) || data.tags.toString().toLowerCase().includes(iname.toLowerCase()) || data.port.toString().toLowerCase().includes(iname.toLowerCase()) || data.address.toLowerCase().includes(iname.toLowerCase())))"
border
fit
highlight-current-row
style="width: 100%;"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" align="center" width="30" />
<el-table-column label="ID" width="50px" align="center">
<template slot-scope="scope">
@ -115,15 +127,16 @@
<el-switch v-model="newService.metaInfo.isMeta" />
</el-form-item>
<el-form-item v-if="newService.metaInfo.isMeta" prop="newmeta">
<font size="3px" color="#ff0000">键不能是中文,值不能是数字类型,键值都必须用双引号引起</font>
<span slot="label">
<span class="span-box">
<span>Meta</span>
<el-tooltip style="diaplay:inline" effect="dark" content='Meta必须是JSON字符串格式例如{ "aaa":"bbb", "ccc": "ddd" }' placement="top">
<el-tooltip style="diaplay:inline" effect="dark" content='Meta必须是JSON字符串格式例如{ "aaa":"bbb", "ccc": "123" }' placement="top">
<i class="el-icon-info" />
</el-tooltip>
</span>
</span>
<el-input v-model="newService.metaInfo.metaJson" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder='{ "aaa": "bbb", "ccc": "ddd" }' clearable class="filter-item" />
<el-input v-model="newService.metaInfo.metaJson" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder='{ "aaa": "bbb", "ccc": "123" }' clearable class="filter-item" />
</el-form-item>
<el-form-item v-if="coption !== '' && dialogStatus==='update'" label="健康检查操作" prop="coption">
@ -205,6 +218,7 @@ export default {
services_name: '',
services_name_list: [],
multipleSelection: [],
iname: '',
newService: {
ID: '',
name: '',
@ -244,10 +258,16 @@ export default {
}
},
created() {
this.fetchServicesName()
if (this.$route.query.service_name) {
this.fetchData(this.$route.query.service_name)
}
getServicesName().then(response => {
this.services_name_list = response.services_name
this.xname = this.load_name()
if (this.$route.query.service_name) {
this.fetchData(this.$route.query.service_name)
} else {
this.services_name = this.services_name_list[0]
this.fetchData(this.services_name)
}
})
},
mounted() {
if (this.$route.query.service_name) {

View File

@ -1,6 +1,6 @@
<template>
<div class="app-container">
<el-alert title="如需管理【blackbox_exporter】的监控实例建议使用左侧菜单【Blackbox 站点监控】来维护,更加方便直观。" type="success" center close-text="知道了" />
<el-alert title="如需管理【自建主机、blackbox_exporter】的监控实例建议使用左侧菜单【自建主机管理、Blackbox 站点监控】来维护,更加方便直观。" type="success" center close-text="知道了" />
<el-table
v-loading="listLoading"
:data="services"

View File

@ -3,40 +3,93 @@
<el-select v-model="jobecs_name" placeholder="请选择需要查询的ECS列表" filterable collapse-tags clearable style="width: 350px" class="filter-item" @change="fetchEcs(jobecs_name)">
<el-option v-for="item in jobecs_list" :key="item" :label="item" :value="item" />
</el-select>
<el-checkbox v-model="checked" style="margin-left: 10px;" label="仅显示修改过的" border @change="cstEcsList(jobecs_name,checked)" />
<el-tooltip class="item" effect="light" content="刷新当前ECS列表" placement="top">
<el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-refresh" circle @click="fetchEcs(jobecs_name)" />
</el-tooltip>
<div style="float: right;margin-left: 10px;">
<el-input v-model="iname" prefix-icon="el-icon-search" placeholder="请输入名称、实例或实例ID进行筛选" clearable style="width: 300px" class="filter-item" />
</div>
<el-dialog title="自定义实例信息" :visible.sync="dialogFormVisible" width="45%">
<el-form ref="dataForm" :model="cst_ecs" label-position="right" label-width="auto" style="width: 90%; margin-left: 20px;">
<el-form-item label="自定义端口">
<el-switch v-model="cst_ecs.portswitch" />
</el-form-item>
<el-form-item v-if="cst_ecs.portswitch" required label="端口:">
<el-input v-model="cst_ecs.port" />
</el-form-item>
<el-form-item label="自定义IP">
<el-switch v-model="cst_ecs.ipswitch" />
</el-form-item>
<el-form-item v-if="cst_ecs.ipswitch" required label="IP">
<el-input v-model="cst_ecs.ip" />
</el-form-item>
<font size="3px" color="#ff0000">如需恢复同步该实例的IP端口信息请关闭开启的自定义选项后再同步一次所属数据源</font>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">
取消
</el-button>
<el-button type="primary" @click="createData">
确认
</el-button>
</div>
</el-dialog>
<el-table v-loading="listLoading" :data="ecs_list" :default-sort="{ prop: 'exp', order: 'ascending' }" border fit highlight-current-row style="width: 100%;">
<el-table
v-loading="listLoading"
:data="ecs_list.filter(data => !iname || (data.name.toLowerCase().includes(iname.toLowerCase()) || data.instance.toLowerCase().includes(iname.toLowerCase()) || data.iid.toLowerCase().includes(iname.toLowerCase())))"
:default-sort="{ prop: 'exp', order: 'ascending' }"
border
fit
highlight-current-row
style="width: 100%;"
>
<el-table-column type="index" align="center" />
<el-table-column prop="group" label="分组" sortable align="center" width="180" show-overflow-tooltip />
<el-table-column prop="name" label="名称" sortable align="center" width="280" />
<el-table-column prop="group" label="分组" sortable align="center" width="150" show-overflow-tooltip />
<el-table-column prop="name" label="名称" sortable align="center" width="220" show-overflow-tooltip />
<el-table-column prop="instance" label="实例" sortable align="center" width="180" />
<el-table-column prop="os" label="系统" sortable align="center" width="100" />
<el-table-column prop="cpu" label="CPU" sortable align="center" width="80" />
<el-table-column prop="mem" label="内存" sortable align="center" width="80" />
<el-table-column prop="exp" label="到期日" sortable align="center" width="120" />
<el-table-column prop="iid" label="实例ID" sortable align="center" />
<el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
<template slot-scope="{row}">
<el-button type="primary" size="mini" @click="handleUpdate(row.iid)">
自定义实例
</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import { getEcsList, getJobEcs } from '@/api/node-exporter'
import { getEcsList, getJobEcs, postCstEcs, getCstEcsConfig, getCstEcsList } from '@/api/node-exporter'
export default {
data() {
return {
listLoading: false,
dialogFormVisible: false,
checked: false,
jobecs_name: '',
iname: '',
jobecs_list: [],
ecs_list: []
ecs_list: [],
cst_ecs: { iid: '', portswitch: false, ipswitch: false, port: '', ip: '' }
}
},
created() {
this.fetchJobEcs()
if (this.$route.query.job_id) {
this.fetchEcs(this.$route.query.job_id)
}
getJobEcs().then(response => {
this.jobecs_list = response.jobecs
if (this.$route.query.job_id) {
this.fetchEcs(this.$route.query.job_id)
} else {
this.jobecs_name = this.jobecs_list[0]
this.fetchEcs(this.jobecs_name)
}
})
},
mounted() {
if (this.$route.query.job_id) {
@ -44,12 +97,44 @@ export default {
}
},
methods: {
cstEcsList(jobecs_name, checked) {
this.listLoading = true
getCstEcsList(jobecs_name, checked).then(response => {
this.ecs_list = response.ecs_list
this.listLoading = false
})
},
handleUpdate(iid) {
this.listLoading = true
this.dialogFormVisible = true
getCstEcsConfig(iid).then(response => {
this.cst_ecs = response.cst_ecs
this.listLoading = false
this.dialogFormVisible = true
})
},
createData() {
this.$refs['dataForm'].validate((valid) => {
if (valid) {
this.dialogFormVisible = false
this.listLoading = true
postCstEcs(this.cst_ecs).then(response => {
this.fetchEcs(this.jobecs_name)
this.$message({
message: response.data,
type: 'success'
})
})
}
})
},
fetchJobEcs() {
getJobEcs().then(response => {
this.jobecs_list = response.jobecs
})
},
fetchEcs(job_id) {
this.checked = false
this.listLoading = true
getEcsList(job_id).then(response => {
this.ecs_list = response.ecs_list