我正在为 Telegraf 创建一个自定义插件,从 Powertop 在 CSV 中生成的文件,我解析它并将其分配给一个变量。在 main.go 中一切正常,但是当我把它全部放在我的powertop.go文件中时,将它添加到all.go并使用[[inputs.powertop]]调用它,我得到一个“无效参数”错误。
我求求你,请帮助我!我厌倦了这种阻塞:(
配置文件:
[agent]
interval = "10s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
flush_jitter = "0s"
precision = ""
debug = true
quiet = false
logfile = ""
hostname = ""
omit_hostname = false
[[outputs.influxdb_v2]]
urls = ["http://localhost:8086"]
token = "$INFLUX_TOKEN"
organization = "mycompany"
bucket = "mycompany"
[[inputs.powertop]]
转到文件:
package powertop
import (
"encoding/csv"
"io"
"log"
"os"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
type Powertop struct {
Usage string `csv:"Usage"`
Events string `csv:"Events"`
Category string `csv:"Category"`
Desc string `csv:"Description"`
PW string `csv:"PW_Estimate"`
}
func (s *Powertop) Description() string {
return "Gather Powertop infos"
}
func (s *Powertop) SampleConfig() string {
return "Sample Config"
}
func (s *Powertop) Gather(acc telegraf.Accumulator) error {
tags := map[string]string{}
fields := map[string]interface{}{}
csvFile, _ := os.Open("powertop.csv")
reader := csv.NewReader(csvFile)
defer csvFile.Close()
for {
itt, error := reader.Read()
if error == io.EOF {
break
} else if error != nil {
log.Fatal(error)
}
tags["Usage"] = itt[0]
tags["Events"] = itt[1]
tags["Category"] = itt[2]
tags["Desc"] = itt[3]
tags["PW"] = itt[4]
acc.AddFields("powertop", fields, tags)
}
return nil
}
func init() {
inputs.Add("powertop", func() telegraf.Input {
return &Powertop{}
})
}
CSV 文件:
0.5%,179.5,Timer,tick_sched_timer, 705 mW
35.5%, 11.3,Process,[PID 3791] PK-Backend, 399 mW
6.2%, 84.3,Process,[PID 1270] /usr/libexec/packagekitd , 390 mW
0.2%, 70.7,Process,[PID 0] swapper/3, 278 mW
3.6%, 45.8,Process,[PID 2049] /usr/bin/gnome-software --gapplication-service , 214 mW
0.2%, 43.3,Interrupt,[63] amdgpu, 171 mW
0.3%, 38.9,Process,[PID 0] swapper/4, 154 mW
0.2%, 31.5,Interrupt,[7] sched(softirq), 124 mW
0.0%, 28.7,Process,[PID 14] [rcu_sched], 112 mW
0.4%, 22.6,Process,[PID 614] [irq/67-rtw88_pc], 92.2 mW