@@ -64,7 +64,7 @@
- 内存
+ 内存
{{ record.memory_info?.value || 0 }}%
-
+
-
- {{ record.data_collection ? '已启用' : '未启用' }}
+
+ {{ record.collect_on ? '已启用' : '未启用' }}
@@ -202,215 +202,10 @@ import {
fetchServerList,
deleteServer,
} from '@/api/ops/server'
-import { fetchDatacenterAll } from '@/api/ops/datacenter'
-import { fetchRackListByDatacenter } from '@/api/ops/rack'
+import axios from 'axios'
const router = useRouter()
-// Mock 假数据
-const mockServerData = [
- {
- id: 1,
- unique_id: 'SRV-2024-0001',
- name: 'Web服务器-01',
- type: 'Web服务器',
- os: 'CentOS 7.9',
- location: '数据中心A-1楼-机柜01-U1',
- tags: 'Web,应用',
- ip: '192.168.1.101',
- remote_access: true,
- agent_config: true,
- cpu: '8核 Intel Xeon',
- memory: '32GB',
- disk: '1TB SSD',
- cpu_info: { value: 45, total: '8核', used: '3.6核' },
- memory_info: { value: 62, total: '32GB', used: '19.8GB' },
- disk_info: { value: 78, total: '1TB', used: '780GB' },
- data_collection: true,
- status: 'online',
- },
- {
- id: 2,
- unique_id: 'SRV-2024-0002',
- name: '数据库服务器-01',
- type: '数据库服务器',
- os: 'Ubuntu 22.04',
- location: '数据中心A-1楼-机柜02-U1',
- tags: '数据库,MySQL',
- ip: '192.168.1.102',
- remote_access: true,
- agent_config: true,
- cpu: '16核 AMD EPYC',
- memory: '64GB',
- disk: '2TB NVMe',
- cpu_info: { value: 78, total: '16核', used: '12.5核' },
- memory_info: { value: 85, total: '64GB', used: '54.4GB' },
- disk_info: { value: 92, total: '2TB', used: '1.84TB' },
- data_collection: true,
- status: 'online',
- },
- {
- id: 3,
- unique_id: 'SRV-2024-0003',
- name: '应用服务器-01',
- type: '应用服务器',
- os: 'Windows Server 2019',
- location: '数据中心A-2楼-机柜05-U2',
- tags: '应用,.NET',
- ip: '192.168.1.103',
- remote_access: false,
- agent_config: false,
- cpu: '4核 Intel Xeon',
- memory: '16GB',
- disk: '500GB SSD',
- cpu_info: { value: 0, total: '4核', used: '0核' },
- memory_info: { value: 0, total: '16GB', used: '0GB' },
- disk_info: { value: 0, total: '500GB', used: '0GB' },
- data_collection: false,
- status: 'offline',
- },
- {
- id: 4,
- unique_id: 'SRV-2024-0004',
- name: '缓存服务器-01',
- type: '缓存服务器',
- os: 'CentOS 8.5',
- location: '数据中心A-2楼-机柜06-U1',
- tags: '缓存,Redis',
- ip: '192.168.1.104',
- remote_access: true,
- agent_config: true,
- cpu: '8核 Intel Xeon',
- memory: '32GB',
- disk: '1TB SSD',
- cpu_info: { value: 35, total: '8核', used: '2.8核' },
- memory_info: { value: 68, total: '32GB', used: '21.8GB' },
- disk_info: { value: 42, total: '1TB', used: '420GB' },
- data_collection: true,
- status: 'online',
- },
- {
- id: 5,
- unique_id: 'SRV-2024-0005',
- name: '文件服务器-01',
- type: '文件服务器',
- os: 'Debian 11',
- location: '数据中心B-1楼-机柜03-U1',
- tags: '文件,NFS',
- ip: '192.168.2.101',
- remote_access: true,
- agent_config: true,
- cpu: '12核 Intel Xeon',
- memory: '48GB',
- disk: '10TB HDD',
- cpu_info: { value: 28, total: '12核', used: '3.4核' },
- memory_info: { value: 45, total: '48GB', used: '21.6GB' },
- disk_info: { value: 88, total: '10TB', used: '8.8TB' },
- data_collection: true,
- status: 'maintenance',
- },
- {
- id: 6,
- unique_id: 'SRV-2024-0006',
- name: '测试服务器-01',
- type: '测试服务器',
- os: 'CentOS 7.9',
- location: '数据中心B-2楼-机柜10-U1',
- tags: '测试,开发',
- ip: '192.168.2.102',
- remote_access: false,
- agent_config: false,
- cpu: '4核 Intel Xeon',
- memory: '8GB',
- disk: '256GB SSD',
- cpu_info: { value: 0, total: '4核', used: '0核' },
- memory_info: { value: 0, total: '8GB', used: '0GB' },
- disk_info: { value: 0, total: '256GB', used: '0GB' },
- data_collection: false,
- status: 'retired',
- },
- {
- id: 7,
- unique_id: 'SRV-2024-0007',
- name: '监控服务器-01',
- type: '监控服务器',
- os: 'Ubuntu 20.04',
- location: '数据中心A-1楼-机柜08-U1',
- tags: '监控,Prometheus',
- ip: '192.168.1.105',
- remote_access: true,
- agent_config: true,
- cpu: '8核 Intel Xeon',
- memory: '32GB',
- disk: '1TB SSD',
- cpu_info: { value: 55, total: '8核', used: '4.4核' },
- memory_info: { value: 72, total: '32GB', used: '23.0GB' },
- disk_info: { value: 65, total: '1TB', used: '650GB' },
- data_collection: true,
- status: 'online',
- },
- {
- id: 8,
- unique_id: 'SRV-2024-0008',
- name: '备份服务器-01',
- type: '备份服务器',
- os: 'Rocky Linux 9',
- location: '数据中心B-1楼-机柜04-U1',
- tags: '备份,存储',
- ip: '192.168.2.103',
- remote_access: true,
- agent_config: true,
- cpu: '16核 AMD EPYC',
- memory: '64GB',
- disk: '20TB HDD',
- cpu_info: { value: 42, total: '16核', used: '6.7核' },
- memory_info: { value: 38, total: '64GB', used: '24.3GB' },
- disk_info: { value: 75, total: '20TB', used: '15TB' },
- data_collection: true,
- status: 'online',
- },
- {
- id: 9,
- unique_id: 'SRV-2024-0009',
- name: 'CI/CD服务器-01',
- type: 'CI/CD服务器',
- os: 'Ubuntu 22.04',
- location: '数据中心A-2楼-机柜07-U1',
- tags: 'CI/CD,Jenkins',
- ip: '192.168.1.106',
- remote_access: true,
- agent_config: true,
- cpu: '8核 Intel Xeon',
- memory: '16GB',
- disk: '500GB SSD',
- cpu_info: { value: 68, total: '8核', used: '5.4核' },
- memory_info: { value: 75, total: '16GB', used: '12GB' },
- disk_info: { value: 55, total: '500GB', used: '275GB' },
- data_collection: true,
- status: 'online',
- },
- {
- id: 10,
- unique_id: 'SRV-2024-0010',
- name: '日志服务器-01',
- type: '日志服务器',
- os: 'CentOS Stream 9',
- location: '数据中心B-2楼-机柜12-U1',
- tags: '日志,ELK',
- ip: '192.168.2.104',
- remote_access: true,
- agent_config: true,
- cpu: '12核 Intel Xeon',
- memory: '48GB',
- disk: '2TB SSD',
- cpu_info: { value: 0, total: '12核', used: '0核' },
- memory_info: { value: 0, total: '48GB', used: '0GB' },
- disk_info: { value: 0, total: '2TB', used: '0TB' },
- data_collection: true,
- status: 'offline',
- },
-]
-
// 状态管理
const loading = ref(false)
const tableData = ref
([])
@@ -419,9 +214,7 @@ const quickConfigVisible = ref(false)
const currentRecord = ref(null)
const formModel = ref({
keyword: '',
- datacenter_id: undefined,
- rack_id: undefined,
- status: undefined,
+ collect_on: undefined as boolean | undefined,
})
const pagination = reactive({
@@ -430,69 +223,16 @@ const pagination = reactive({
total: 0,
})
-const datacenterSelectOptions = ref<{ label: string; value: number }[]>([])
-const rackSelectOptions = ref<{ label: string; value: number }[]>([])
-
-const loadDatacenterOptions = async () => {
- try {
- const res: any = await fetchDatacenterAll()
- if (res.code === 0) {
- const list = res.details?.data ?? res.data ?? res.details ?? []
- const rows = Array.isArray(list) ? list : []
- datacenterSelectOptions.value = rows.map((d: any) => ({
- label: d.name || d.code || String(d.id),
- value: d.id,
- }))
- }
- } catch (error) {
- console.error('加载数据中心列表失败:', error)
- Message.error('加载数据中心列表失败')
- }
-}
-
-const loadRackOptions = async (datacenterId: number) => {
- try {
- const res: any = await fetchRackListByDatacenter(datacenterId)
- if (res.code === 0) {
- const list = res.details?.data ?? res.data ?? res.details ?? []
- const rows = Array.isArray(list) ? list : []
- rackSelectOptions.value = rows.map((r: any) => ({
- label: r.floor?.name ? `${r.name}(${r.floor.name})` : r.name || r.code || String(r.id),
- value: r.id,
- }))
- }
- } catch (error) {
- console.error('加载机柜列表失败:', error)
- Message.error('加载机柜列表失败')
- rackSelectOptions.value = []
- }
-}
-
-watch(
- () => formModel.value.datacenter_id,
- (newId, oldId) => {
- if (newId !== oldId) {
- formModel.value.rack_id = undefined
- }
- if (newId) {
- loadRackOptions(newId)
- } else {
- rackSelectOptions.value = []
- }
- }
-)
-
-// 表单项配置(数据中心:GET /Assets/v1/datacenter/all;机柜:GET /Assets/v1/rack/datacenter/:id)
+// 表单项配置
const formItems = computed(() =>
searchFormConfig.map((item) => {
- if (item.field === 'datacenter_id') {
- return { ...item, options: datacenterSelectOptions.value }
- }
- if (item.field === 'rack_id') {
+ if (item.field === 'collect_on') {
return {
...item,
- options: rackSelectOptions.value,
- disabled: !formModel.value.datacenter_id,
+ options: [
+ { label: '是', value: true },
+ { label: '否', value: false },
+ ],
}
}
return item
@@ -507,8 +247,7 @@ const getStatusColor = (status?: string) => {
const colorMap: Record = {
online: 'green',
offline: 'red',
- maintenance: 'orange',
- retired: 'gray',
+ unknown: 'gray',
}
return colorMap[status || ''] || 'gray'
}
@@ -518,8 +257,7 @@ const getStatusText = (status?: string) => {
const textMap: Record = {
online: '在线',
offline: '离线',
- maintenance: '维护中',
- retired: '已退役',
+ unknown: '未知',
}
return textMap[status || ''] || '-'
}
@@ -532,37 +270,37 @@ const getProgressColor = (value: number) => {
return '#00B42A' // 绿色
}
-// 获取服务器列表(使用 Mock 数据)
+// 获取服务器列表
const fetchServers = async () => {
loading.value = true
try {
- // 模拟网络延迟
- await new Promise(resolve => setTimeout(resolve, 500))
+ const params: any = {
+ page: pagination.current,
+ size: pagination.pageSize,
+ }
- // 使用 Mock 数据
- tableData.value = mockServerData
- pagination.total = mockServerData.length
+ if (formModel.value.keyword) {
+ params.keyword = formModel.value.keyword
+ }
- // 如果有搜索条件,进行过滤
- if (formModel.value.keyword || formModel.value.status) {
- let filteredData = [...mockServerData]
+ if (formModel.value.collect_on !== undefined) {
+ params.collect_on = formModel.value.collect_on
+ }
+
+ const res: any = await fetchServerList(params)
+
+ if (res.code === 200 || res.code === 0) {
+ const responseData = res.data || res.details || {}
+ tableData.value = responseData.data || []
+ pagination.total = responseData.total || 0
- if (formModel.value.keyword) {
- const keyword = formModel.value.keyword.toLowerCase()
- filteredData = filteredData.filter(item =>
- item.name.toLowerCase().includes(keyword) ||
- item.unique_id.toLowerCase().includes(keyword) ||
- item.ip.toLowerCase().includes(keyword)
- )
- }
-
- if (formModel.value.status) {
- filteredData = filteredData.filter(item => item.status === formModel.value.status)
- }
-
- tableData.value = filteredData
- pagination.total = filteredData.length
+ // 列表加载成功后,获取监控指标
+ await getAllMetrics()
+ } else {
+ Message.error(res.message || '获取服务器列表失败')
+ tableData.value = []
+ pagination.total = 0
}
} catch (error) {
console.error('获取服务器列表失败:', error)
@@ -589,11 +327,8 @@ const handleFormModelUpdate = (value: any) => {
const handleReset = () => {
formModel.value = {
keyword: '',
- datacenter_id: undefined,
- rack_id: undefined,
- status: undefined,
+ collect_on: undefined,
}
- rackSelectOptions.value = []
pagination.current = 1
fetchServers()
}
@@ -673,31 +408,100 @@ const handleRemoteControl = (record: any) => {
// 删除服务器
const handleDelete = async (record: any) => {
- try {
- Modal.confirm({
- title: '确认删除',
- content: `确认删除服务器 ${record.name} 吗?`,
- onOk: async () => {
- // Mock 删除操作
- const index = mockServerData.findIndex(item => item.id === record.id)
- if (index > -1) {
- mockServerData.splice(index, 1)
+ Modal.confirm({
+ title: '确认删除',
+ content: `确认删除服务器 ${record.name} 吗?`,
+ onOk: async () => {
+ try {
+ const res: any = await deleteServer(record.id)
+ if (res.code === 200 || res.code === 0) {
Message.success('删除成功')
fetchServers()
} else {
- Message.error('删除失败')
+ Message.error(res.message || '删除失败')
}
- },
+ } catch (error) {
+ console.error('删除服务器失败:', error)
+ Message.error('删除失败')
+ }
+ },
+ })
+}
+
+// 获取所有服务器的监控指标
+const getAllMetrics = async () => {
+ try {
+ // 遍历每个服务器记录
+ const metricsPromises = tableData.value.map(async (record) => {
+ // 检查是否有 agent_config 配置
+ if (record.agent_config) {
+ try {
+ // 从 agent_config 中解析 URL
+ let metricsUrl = record.agent_config
+
+ // 验证 URL 是否合法
+ try {
+ new URL(metricsUrl)
+ } catch (urlError) {
+ console.warn(`服务器 ${record.name} 的 agent_config 不是合法的 URL:`, metricsUrl)
+ // 设置默认值 0
+ record.cpu_info = { value: 0, total: '', used: '' }
+ record.memory_info = { value: 0, total: '', used: '' }
+ record.disk_info = { value: 0, total: '', used: '' }
+ return
+ }
+
+ // 发送请求获取指标数据
+ const response = await axios.get(metricsUrl, {
+ timeout: 5000, // 5 秒超时
+ })
+
+ if (response.data) {
+ // 更新记录的监控数据
+ record.cpu_info = {
+ value: response.data.cpu_usage || 0,
+ total: response.data.cpu?.[0]?.cores ? `${response.data.cpu[0].cores}核` : '',
+ used: '',
+ }
+
+ record.memory_info = {
+ value: response.data.mem_usage?.used_percent || 0,
+ total: response.data.mem_usage?.total ? `${(response.data.mem_usage.total / 1024 / 1024 / 1024).toFixed(1)}GB` : '',
+ used: response.data.mem_usage?.used ? `${(response.data.mem_usage.used / 1024 / 1024 / 1024).toFixed(1)}GB` : '',
+ }
+
+ record.disk_info = {
+ value: response.data.disk_usage?.used_percent || 0,
+ total: response.data.disk_usage?.total ? `${(response.data.disk_usage.total / 1024 / 1024 / 1024).toFixed(0)}GB` : '',
+ used: response.data.disk_usage?.used ? `${(response.data.disk_usage.used / 1024 / 1024 / 1024).toFixed(0)}GB` : '',
+ }
+ }
+ } catch (error) {
+ console.warn(`获取服务器 ${record.name} 的监控指标失败:`, error)
+ // 初始化默认值
+ record.cpu_info = { value: 0, total: '', used: '' }
+ record.memory_info = { value: 0, total: '', used: '' }
+ record.disk_info = { value: 0, total: '', used: '' }
+ }
+ } else {
+ // 没有配置 agent,设置默认值
+ record.cpu_info = { value: 0, total: '', used: '' }
+ record.memory_info = { value: 0, total: '', used: '' }
+ record.disk_info = { value: 0, total: '', used: '' }
+ }
})
+
+ // 等待所有请求完成
+ await Promise.all(metricsPromises)
} catch (error) {
- console.error('删除服务器失败:', error)
+ console.error('获取所有服务器监控指标失败:', error)
}
}
onMounted(() => {
- loadDatacenterOptions()
fetchServers()
})
+