84 lines
1.4 KiB
Go
84 lines
1.4 KiB
Go
package tpl
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"flashcat.cloud/categraf/config"
|
|
"flashcat.cloud/categraf/inputs"
|
|
"flashcat.cloud/categraf/types"
|
|
"github.com/toolkits/pkg/container/list"
|
|
)
|
|
|
|
const inputName = "plugin_tpl"
|
|
|
|
type PluginTpl struct {
|
|
config.Interval
|
|
counter uint64
|
|
waitgrp sync.WaitGroup
|
|
Instances []*Instance `toml:"instances"`
|
|
}
|
|
|
|
func init() {
|
|
inputs.Add(inputName, func() inputs.Input {
|
|
return &PluginTpl{}
|
|
})
|
|
}
|
|
|
|
func (r *PluginTpl) Prefix() string {
|
|
return inputName
|
|
}
|
|
|
|
func (r *PluginTpl) Init() error {
|
|
if len(r.Instances) == 0 {
|
|
return types.ErrInstancesEmpty
|
|
}
|
|
|
|
for i := 0; i < len(r.Instances); i++ {
|
|
if err := r.Instances[i].Init(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *PluginTpl) Drop() {}
|
|
|
|
func (r *PluginTpl) Gather(slist *list.SafeList) {
|
|
atomic.AddUint64(&r.counter, 1)
|
|
|
|
for i := range r.Instances {
|
|
ins := r.Instances[i]
|
|
|
|
r.waitgrp.Add(1)
|
|
go func(slist *list.SafeList, ins *Instance) {
|
|
defer r.waitgrp.Done()
|
|
|
|
if ins.IntervalTimes > 0 {
|
|
counter := atomic.LoadUint64(&r.counter)
|
|
if counter%uint64(ins.IntervalTimes) != 0 {
|
|
return
|
|
}
|
|
}
|
|
|
|
ins.gatherOnce(slist)
|
|
}(slist, ins)
|
|
}
|
|
|
|
r.waitgrp.Wait()
|
|
}
|
|
|
|
type Instance struct {
|
|
Labels map[string]string `toml:"labels"`
|
|
IntervalTimes int64 `toml:"interval_times"`
|
|
}
|
|
|
|
func (ins *Instance) Init() error {
|
|
return nil
|
|
}
|
|
|
|
func (ins *Instance) gatherOnce(slist *list.SafeList) {
|
|
|
|
}
|