From 86a6a816af132ff9684154e4c13cc2bf89e05dbe Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 15 Jan 2026 17:20:39 +0800 Subject: [PATCH] fix(telegraf-raid-plugin): working for victoria metrics (#24085) --- Makefile | 2 +- cmd/telegraf-raid-plugin/main.go | 31 ++++++++++++----- pkg/monitor/dbinit/measurements/all.go | 2 ++ pkg/monitor/dbinit/measurements/host_raid.go | 36 ++++++++++++++++++++ 4 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 pkg/monitor/dbinit/measurements/host_raid.go diff --git a/Makefile b/Makefile index d86f3ea2bc..4a9e78abd0 100644 --- a/Makefile +++ b/Makefile @@ -323,7 +323,7 @@ image: .PHONY: image image-telegraf-raid-plugin: - VERSION=release-1.6.6 GOOS=linux ARCH=all make image telegraf-raid-plugin + VERSION=v4.0-20260115.0 GOOS=linux ARCH=all make image telegraf-raid-plugin %: @: diff --git a/cmd/telegraf-raid-plugin/main.go b/cmd/telegraf-raid-plugin/main.go index 34b8beb7b1..34931830e6 100644 --- a/cmd/telegraf-raid-plugin/main.go +++ b/cmd/telegraf-raid-plugin/main.go @@ -164,19 +164,32 @@ func (c *RaidInfoCollector) toTelegrafReportData(raidDiskInfo []*baremetal.Barem tag := fmt.Sprintf("%s=%s,%s=%s", "hostname", c.Hostname, "host_ip", c.HostIp) ret := []string{} for i := 0; i < len(raidDiskInfo); i++ { - statArr := []string{} + fieldArr := []string{} + tagArr := []string{} raidDiskInfo[i].Status = strings.ToLower(raidDiskInfo[i].Status) jStat := jsonutils.Marshal(raidDiskInfo[i]) jMap, _ := jStat.GetMap() for k, v := range jMap { - statArr = append(statArr, fmt.Sprintf("%s=%s", k, v.String())) + vStr, _ := v.GetString() + if vStr == "" { + continue + } + vStr = strings.ReplaceAll(vStr, " ", "\\ ") + kv := fmt.Sprintf("%s=%s", k, vStr) + switch k { + case "adapter", "slot": + fieldArr = append(fieldArr, kv) + case "model", "driver", "status": + tagArr = append(tagArr, kv) + case "index": + continue + default: + tagArr = append(tagArr, kv) + } } - stat := strings.Join(statArr, ",") - diskTag := fmt.Sprintf( - "%s,%s=%s,%s=%d,%s=%d", tag, "driver", raidDiskInfo[i].Driver, - "adapter", raidDiskInfo[i].Adapter, "slot", raidDiskInfo[i].Slot, - ) - line := fmt.Sprintf("%s,%s %s", MEASUREMENT, diskTag, stat) + field := strings.Join(fieldArr, ",") + diskTag := tag + "," + strings.Join(tagArr, ",") + line := fmt.Sprintf("%s,%s %s", MEASUREMENT, diskTag, field) ret = append(ret, line) } return strings.Join(ret, "\n") @@ -185,7 +198,7 @@ func (c *RaidInfoCollector) toTelegrafReportData(raidDiskInfo []*baremetal.Barem func (c *RaidInfoCollector) reportRaidInfoToTelegraf(data string) { body := strings.NewReader(data) res, err := httputils.Request( - httputils.GetDefaultClient(), context.Background(), "POST", TelegrafServer, nil, body, false) + httputils.GetDefaultClient(), context.Background(), "POST", TelegrafServer, nil, body, true) if err != nil { log.Errorf("Upload guest metric failed: %s", err) return diff --git a/pkg/monitor/dbinit/measurements/all.go b/pkg/monitor/dbinit/measurements/all.go index a74373ee99..2f9ab315ab 100644 --- a/pkg/monitor/dbinit/measurements/all.go +++ b/pkg/monitor/dbinit/measurements/all.go @@ -118,4 +118,6 @@ var All = []SMeasurement{ swap, temp, + + hostRaid, } diff --git a/pkg/monitor/dbinit/measurements/host_raid.go b/pkg/monitor/dbinit/measurements/host_raid.go new file mode 100644 index 0000000000..482f5be5f2 --- /dev/null +++ b/pkg/monitor/dbinit/measurements/host_raid.go @@ -0,0 +1,36 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package measurements + +import "yunion.io/x/onecloud/pkg/apis/monitor" + +var hostRaid = SMeasurement{ + Context: []SMonitorContext{ + { + "host_raid", "Host RAID metrics", + monitor.METRIC_RES_TYPE_HOST, monitor.METRIC_DATABASE_TELE, + }, + }, + Metrics: []SMetric{ + { + "adapter", "Raid controller adapter", + monitor.METRIC_UNIT_NULL, + }, + { + "slot", "Physical disk slot", + monitor.METRIC_UNIT_NULL, + }, + }, +}