增加操作系统显示,以及磁盘信息显示
This commit is contained in:
@@ -210,7 +210,7 @@ func (s *Storage) WriteProcessMetric(ctx context.Context, deviceID string, proce
|
||||
}
|
||||
|
||||
// WriteDiskDetailMetric 写入磁盘详细信息
|
||||
func (s *Storage) WriteDiskDetailMetric(ctx context.Context, deviceID, diskPath, status, diskType string, sizeGB float64, model, interfaceType, description string, tags map[string]string) error {
|
||||
func (s *Storage) WriteDiskDetailMetric(ctx context.Context, deviceID, diskPath, status, diskType string, sizeGB float64, model, vendor, interfaceType, fileSystem, diskUUID, description string, tags map[string]string) error {
|
||||
// 创建标签映射,合并原有标签和新标签
|
||||
allTags := make(map[string]string)
|
||||
// 复制原有标签
|
||||
@@ -224,11 +224,14 @@ func (s *Storage) WriteDiskDetailMetric(ctx context.Context, deviceID, diskPath,
|
||||
allTags["status"] = status
|
||||
allTags["type"] = diskType
|
||||
allTags["model"] = model
|
||||
allTags["vendor"] = vendor
|
||||
allTags["interface_type"] = interfaceType
|
||||
allTags["file_system"] = fileSystem
|
||||
|
||||
// 创建字段映射
|
||||
fields := map[string]interface{}{
|
||||
"size_gb": sizeGB,
|
||||
"disk_uuid": diskUUID,
|
||||
"description": description,
|
||||
}
|
||||
|
||||
@@ -476,13 +479,24 @@ func (s *Storage) QueryHardwareMetrics(ctx context.Context, deviceID string) (ma
|
||||
|
||||
// 为每个硬件类型单独查询,避免类型冲突
|
||||
for _, hardwareType := range hardwareTypes {
|
||||
// 构建查询语句,获取设备最新的特定类型硬件信息
|
||||
query := `from(bucket: "` + s.bucket + `")
|
||||
|> range(start: -24h)
|
||||
|> filter(fn: (r) => r["_measurement"] == "hardware")
|
||||
|> filter(fn: (r) => r["device_id"] == "` + deviceID + `")
|
||||
|> filter(fn: (r) => r["type"] == "` + hardwareType + `")
|
||||
|> last()`
|
||||
// 构建查询语句
|
||||
var query string
|
||||
if hardwareType == "os" || hardwareType == "cpu" || hardwareType == "memory" {
|
||||
// 对于os、cpu、memory类型,我们只需要一个结果
|
||||
query = `from(bucket: "` + s.bucket + `")
|
||||
|> range(start: -24h)
|
||||
|> filter(fn: (r) => r["_measurement"] == "hardware")
|
||||
|> filter(fn: (r) => r["device_id"] == "` + deviceID + `")
|
||||
|> filter(fn: (r) => r["type"] == "` + hardwareType + `")
|
||||
|> last()`
|
||||
} else {
|
||||
// 对于disk和network类型,我们需要获取所有设备的所有字段记录
|
||||
query = `from(bucket: "` + s.bucket + `")
|
||||
|> range(start: -24h)
|
||||
|> filter(fn: (r) => r["_measurement"] == "hardware")
|
||||
|> filter(fn: (r) => r["device_id"] == "` + deviceID + `")
|
||||
|> filter(fn: (r) => r["type"] == "` + hardwareType + `")`
|
||||
}
|
||||
|
||||
// 执行查询
|
||||
queryResult, err := queryAPI.Query(ctx, query)
|
||||
@@ -511,10 +525,12 @@ func (s *Storage) QueryHardwareMetrics(ctx context.Context, deviceID string) (ma
|
||||
hardwareInfo[hardwareType] = fieldMap
|
||||
}
|
||||
} else {
|
||||
// 对于disk和network类型,我们可能有多个结果
|
||||
// 我们需要收集所有结果,并按index排序
|
||||
// 使用map来去重
|
||||
resultMap := make(map[string]map[string]interface{})
|
||||
// 对于disk和network类型,我们可能有多个结果,每个字段作为单独的行返回
|
||||
var allResults []map[string]interface{}
|
||||
|
||||
// 创建一个map来存储每个设备的所有字段值
|
||||
// key是设备ID或index
|
||||
deviceFields := make(map[string]map[string]interface{})
|
||||
|
||||
// 遍历所有结果行,收集所有结果
|
||||
for queryResult.Next() {
|
||||
@@ -525,35 +541,57 @@ func (s *Storage) QueryHardwareMetrics(ctx context.Context, deviceID string) (ma
|
||||
fieldName := record.Field()
|
||||
fieldValue := record.Value()
|
||||
|
||||
// 获取硬件ID(用于去重)
|
||||
hardwareID := "unknown"
|
||||
if hardwareType == "disk" {
|
||||
// 对于磁盘,使用device_id作为唯一标识
|
||||
if devID, ok := record.ValueByKey("device_id").(string); ok {
|
||||
hardwareID = devID
|
||||
}
|
||||
// 获取设备唯一标识
|
||||
deviceKey := ""
|
||||
// 优先使用id字段作为设备标识
|
||||
if id, isString := fieldValue.(string); fieldName == "id" && isString {
|
||||
deviceKey = id
|
||||
// 创建设备字段映射
|
||||
deviceFields[deviceKey] = make(map[string]interface{})
|
||||
deviceFields[deviceKey]["id"] = id
|
||||
} else {
|
||||
// 对于网卡,使用name作为唯一标识
|
||||
if name, ok := record.ValueByKey("name").(string); ok {
|
||||
hardwareID = name
|
||||
// 否则,查找已存在的设备映射
|
||||
// 遍历所有设备映射,查找当前记录对应的设备
|
||||
for key, fields := range deviceFields {
|
||||
// 如果设备映射中没有id字段,或者id字段为空,跳过
|
||||
if id, ok := fields["id"].(string); ok && id != "" {
|
||||
// 假设当前记录属于该设备
|
||||
deviceKey = key
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取或创建硬件数据映射
|
||||
if _, ok := resultMap[hardwareID]; !ok {
|
||||
resultMap[hardwareID] = make(map[string]interface{})
|
||||
// 如果没有找到设备标识,使用index作为设备标识
|
||||
if deviceKey == "" {
|
||||
index := 0
|
||||
if idx, ok := record.ValueByKey("index").(int); ok {
|
||||
index = idx
|
||||
}
|
||||
deviceKey = fmt.Sprintf("%d", index)
|
||||
// 创建设备字段映射
|
||||
deviceFields[deviceKey] = make(map[string]interface{})
|
||||
}
|
||||
|
||||
// 添加字段值
|
||||
resultMap[hardwareID][fieldName] = fieldValue
|
||||
// 添加字段值到设备字段映射中
|
||||
deviceFields[deviceKey][fieldName] = fieldValue
|
||||
}
|
||||
|
||||
// 将去重后的结果转换为切片
|
||||
var allResults []map[string]interface{}
|
||||
for _, result := range resultMap {
|
||||
allResults = append(allResults, result)
|
||||
// 将deviceFields转换为切片
|
||||
for _, fields := range deviceFields {
|
||||
allResults = append(allResults, fields)
|
||||
}
|
||||
|
||||
// 按id排序
|
||||
sort.Slice(allResults, func(i, j int) bool {
|
||||
iID, iOk := allResults[i]["id"].(string)
|
||||
jID, jOk := allResults[j]["id"].(string)
|
||||
if iOk && jOk {
|
||||
return iID < jID
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
// 如果有结果,添加到硬件信息结果中
|
||||
if len(allResults) > 0 {
|
||||
hardwareInfo[hardwareType] = allResults
|
||||
@@ -827,7 +865,9 @@ func (s *Storage) QueryDiskDetails(ctx context.Context, deviceID string, startTi
|
||||
"type": record.ValueByKey("type"),
|
||||
"size_gb": record.ValueByKey("size_gb"),
|
||||
"model": record.ValueByKey("model"),
|
||||
"vendor": record.ValueByKey("vendor"),
|
||||
"interface_type": record.ValueByKey("interface_type"),
|
||||
"file_system": record.ValueByKey("file_system"),
|
||||
"description": record.ValueByKey("description"),
|
||||
"agent_name": record.ValueByKey("agent_name"),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user