add switch_legacy plugin skelton

This commit is contained in:
Ulric Qin 2022-05-25 15:38:49 +08:00
parent 57de67e5e2
commit 369617f4d6
3 changed files with 136 additions and 0 deletions

View File

@ -36,6 +36,7 @@ import (
_ "flashcat.cloud/categraf/inputs/prometheus"
_ "flashcat.cloud/categraf/inputs/rabbitmq"
_ "flashcat.cloud/categraf/inputs/redis"
_ "flashcat.cloud/categraf/inputs/switch_legacy"
_ "flashcat.cloud/categraf/inputs/system"
_ "flashcat.cloud/categraf/inputs/tomcat"
)

View File

@ -0,0 +1,35 @@
# # collect interval
# interval = "300s"
switch_id_label = "ident"
[mappings]
"192.168.88.160" = "switch001.bj"
"192.168.88.161" = "switch002.bj"
[[instances]]
# # interval = global.interval * interval_times
# interval_times = 1
# use global unique string to specify instance
# labels = { region="beijing" }
ips = [
# "172.16.2.1",
# "172.16.4/24",
"192.168.56.102-192.168.56.120"
]
community = "public"
# true: use gosnmp, false: use snmpwalk
use_gosnmp = true
# whether use index tag
index_tag = false
# ping timeout, unix: ms
ping_timeout_ms = 300
# ping retry count
ping_retries = 4

View File

@ -0,0 +1,100 @@
package switch_legacy
import (
"fmt"
"sync"
"sync/atomic"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/types"
"github.com/toolkits/pkg/container/list"
)
const inputName = "switch_legacy"
type Switch struct {
config.Interval
counter uint64
waitgrp sync.WaitGroup
Instances []*Instance `toml:"instances"`
SwitchIdLabel string `toml:"switch_id_label"`
Mappings map[string]string `toml:"mappings"`
}
func init() {
inputs.Add(inputName, func() inputs.Input {
return &Switch{}
})
}
func (s *Switch) Prefix() string {
return inputName
}
func (s *Switch) Init() error {
if len(s.Instances) == 0 {
return types.ErrInstancesEmpty
}
for i := 0; i < len(s.Instances); i++ {
if err := s.Instances[i].Init(); err != nil {
return err
} else {
s.Instances[i].parent = s
}
}
for k, v := range s.Mappings {
fmt.Println(k, v)
}
return nil
}
func (s *Switch) Drop() {}
func (s *Switch) Gather(slist *list.SafeList) {
atomic.AddUint64(&s.counter, 1)
for i := range s.Instances {
ins := s.Instances[i]
s.waitgrp.Add(1)
go func(slist *list.SafeList, ins *Instance) {
defer s.waitgrp.Done()
if ins.IntervalTimes > 0 {
counter := atomic.LoadUint64(&s.counter)
if counter%uint64(ins.IntervalTimes) != 0 {
return
}
}
ins.gatherOnce(slist)
}(slist, ins)
}
s.waitgrp.Wait()
}
type Instance struct {
Labels map[string]string `toml:"labels"`
IntervalTimes int64 `toml:"interval_times"`
IPs []string `toml:"ips"`
Community string `toml:"community"`
UseGosnmp bool `toml:"use_gosnmp"`
IndexTag bool `toml:"index_tag"`
PingTimeoutMs int64 `toml:"ping_timeout_ms"`
PingRetries int `toml:"ping_retries"`
parent *Switch
}
func (ins *Instance) Init() error {
return nil
}
func (ins *Instance) gatherOnce(slist *list.SafeList) error {
return nil
}