code refactor: extract Interval struct so every plugin no need to implement function GetInterval

This commit is contained in:
Ulric Qin 2022-04-29 00:02:20 +08:00
parent dcb909f971
commit 6e982af96c
23 changed files with 61 additions and 141 deletions

9
config/interval.go Normal file
View File

@ -0,0 +1,9 @@
package config
type Interval struct {
Interval Duration `toml:"interval"`
}
func (i Interval) GetInterval() Duration {
return i.Interval
}

View File

@ -17,8 +17,8 @@ type CPUStats struct {
ps system.PS
lastStats map[string]cpuUtil.TimesStat
Interval config.Duration `toml:"interval"`
CollectPerCPU bool `toml:"collect_per_cpu"`
config.Interval
CollectPerCPU bool `toml:"collect_per_cpu"`
}
func init() {
@ -34,10 +34,6 @@ func (s *CPUStats) Prefix() string {
return inputName
}
func (s *CPUStats) GetInterval() config.Duration {
return s.Interval
}
// overwrite func
func (c *CPUStats) Init() error {
return nil

View File

@ -16,10 +16,10 @@ const inputName = "disk"
type DiskStats struct {
ps system.PS
Interval config.Duration `toml:"interval"`
MountPoints []string `toml:"mount_points"`
IgnoreFS []string `toml:"ignore_fs"`
IgnoreMountPoints []string `toml:"ignore_mount_points"`
config.Interval
MountPoints []string `toml:"mount_points"`
IgnoreFS []string `toml:"ignore_fs"`
IgnoreMountPoints []string `toml:"ignore_mount_points"`
}
func init() {
@ -35,10 +35,6 @@ func (s *DiskStats) Prefix() string {
return inputName
}
func (s *DiskStats) GetInterval() config.Duration {
return s.Interval
}
func (s *DiskStats) Init() error {
return nil
}

View File

@ -16,8 +16,8 @@ const inputName = "diskio"
type DiskIO struct {
ps system.PS
Interval config.Duration `toml:"interval"`
Devices []string `toml:"devices"`
config.Interval
Devices []string `toml:"devices"`
deviceFilter filter.Filter
}
@ -34,10 +34,6 @@ func (d *DiskIO) Prefix() string {
return inputName
}
func (d *DiskIO) GetInterval() config.Duration {
return d.Interval
}
func (d *DiskIO) Drop() {}
func (d *DiskIO) Init() error {

View File

@ -36,8 +36,8 @@ type ExecInstance struct {
}
type Exec struct {
Interval config.Duration `toml:"interval"`
Instances []ExecInstance `toml:"instances"`
config.Interval
Instances []ExecInstance `toml:"instances"`
Counter uint64
}
@ -53,10 +53,6 @@ func (e *Exec) Prefix() string {
func (e *Exec) Drop() {}
func (e *Exec) GetInterval() config.Duration {
return e.Interval
}
func (e *Exec) Init() error {
if len(e.Instances) == 0 {
return types.ErrInstancesEmpty

View File

@ -128,8 +128,8 @@ func (ins *Instance) createHTTPClient() (*http.Client, error) {
}
type HTTPResponse struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
config.Interval
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
}
@ -144,10 +144,6 @@ func (h *HTTPResponse) Prefix() string {
return inputName
}
func (h *HTTPResponse) GetInterval() config.Duration {
return h.Interval
}
func (h *HTTPResponse) Init() error {
if len(h.Instances) == 0 {
return types.ErrInstancesEmpty

View File

@ -28,10 +28,10 @@ var (
)
type KernelStats struct {
config.Interval
statFile string
entropyStatFile string
Interval config.Duration `toml:"interval"`
}
func init() {
@ -47,10 +47,6 @@ func (s *KernelStats) Prefix() string {
return inputName
}
func (s *KernelStats) GetInterval() config.Duration {
return s.Interval
}
func (s *KernelStats) Init() error {
return nil
}

View File

@ -18,8 +18,8 @@ import (
const inputName = "kernelvmstat"
type KernelVmstat struct {
Interval config.Duration `toml:"interval"`
WhiteList map[string]int `toml:"white_list"`
config.Interval
WhiteList map[string]int `toml:"white_list"`
statFile string
}
@ -36,10 +36,6 @@ func (s *KernelVmstat) Prefix() string {
return inputName
}
func (s *KernelVmstat) GetInterval() config.Duration {
return s.Interval
}
func (s *KernelVmstat) Init() error {
return nil
}

View File

@ -20,7 +20,7 @@ import (
const inputName = "linuxsysctlfs"
type SysctlFS struct {
Interval config.Duration `toml:"interval"`
config.Interval
path string
}
@ -37,10 +37,6 @@ func (s *SysctlFS) Prefix() string {
return inputName
}
func (s *SysctlFS) GetInterval() config.Duration {
return s.Interval
}
func (s *SysctlFS) Init() error {
return nil
}

View File

@ -16,8 +16,8 @@ type MemStats struct {
ps system.PS
platform string
Interval config.Duration `toml:"interval"`
CollectPlatformFields bool `toml:"collect_platform_fields"`
config.Interval
CollectPlatformFields bool `toml:"collect_platform_fields"`
}
func init() {
@ -33,10 +33,6 @@ func (s *MemStats) Prefix() string {
return inputName
}
func (s *MemStats) GetInterval() config.Duration {
return s.Interval
}
func (s *MemStats) Drop() {}
func (s *MemStats) Init() error {

View File

@ -152,8 +152,8 @@ func (ins *Instance) InitValidMetrics() {
}
type MySQL struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
config.Interval
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
@ -169,10 +169,6 @@ func (m *MySQL) Prefix() string {
return inputName
}
func (m *MySQL) GetInterval() config.Duration {
return m.Interval
}
func (m *MySQL) Init() error {
if len(m.Instances) == 0 {
return types.ErrInstancesEmpty

View File

@ -17,9 +17,9 @@ const inputName = "net"
type NetIOStats struct {
ps system.PS
Interval config.Duration `toml:"interval"`
CollectProtocolStats bool `toml:"collect_protocol_stats"`
Interfaces []string `toml:"interfaces"`
config.Interval
CollectProtocolStats bool `toml:"collect_protocol_stats"`
Interfaces []string `toml:"interfaces"`
interfaceFilters filter.Filter
}
@ -37,10 +37,6 @@ func (s *NetIOStats) Prefix() string {
return inputName
}
func (s *NetIOStats) GetInterval() config.Duration {
return s.Interval
}
func (s *NetIOStats) Drop() {}
func (s *NetIOStats) Init() error {

View File

@ -85,8 +85,8 @@ func (ins *Instance) Init() error {
}
type NetResponse struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
config.Interval
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
}
@ -101,10 +101,6 @@ func (n *NetResponse) Prefix() string {
return inputName
}
func (n *NetResponse) GetInterval() config.Duration {
return n.Interval
}
func (n *NetResponse) Init() error {
if len(n.Instances) == 0 {
return types.ErrInstancesEmpty

View File

@ -14,8 +14,7 @@ const inputName = "netstat"
type NetStats struct {
ps system.PS
Interval config.Duration `toml:"interval"`
config.Interval
}
func init() {
@ -31,10 +30,6 @@ func (s *NetStats) Prefix() string {
return inputName
}
func (s *NetStats) GetInterval() config.Duration {
return s.Interval
}
func (s *NetStats) Drop() {}
func (s *NetStats) Init() error {

View File

@ -14,8 +14,8 @@ import (
const inputName = "ntp"
type NTPStat struct {
Interval config.Duration `toml:"interval"`
NTPServers []string `toml:"ntp_servers"`
config.Interval
NTPServers []string `toml:"ntp_servers"`
server string
}
@ -29,10 +29,6 @@ func (n *NTPStat) Prefix() string {
return inputName
}
func (n *NTPStat) GetInterval() config.Duration {
return n.Interval
}
func (n *NTPStat) Drop() {}
func (n *NTPStat) Init() error {

View File

@ -44,9 +44,9 @@ type MetricConfig struct {
}
type Oracle struct {
Interval config.Duration `toml:"interval"`
Instances []OrclInstance `toml:"instances"`
Metrics []MetricConfig `toml:"metrics"`
config.Interval
Instances []OrclInstance `toml:"instances"`
Metrics []MetricConfig `toml:"metrics"`
dbconnpool map[string]*sqlx.DB // key: instance
Counter uint64
@ -63,10 +63,6 @@ func (o *Oracle) Prefix() string {
return inputName
}
func (o *Oracle) GetInterval() config.Duration {
return o.Interval
}
func (o *Oracle) Init() error {
if len(o.Instances) == 0 {
return types.ErrInstancesEmpty

View File

@ -22,7 +22,7 @@ const (
defaultPingDataBytesSize = 56
)
type PingInstance struct {
type Instance struct {
Targets []string `toml:"targets"`
Labels map[string]string `toml:"labels"`
IntervalTimes int64 `toml:"interval_times"`
@ -38,7 +38,7 @@ type PingInstance struct {
sourceAddress string
}
func (ins *PingInstance) Init() error {
func (ins *Instance) Init() error {
if ins.Count < 1 {
ins.Count = 1
}
@ -77,8 +77,8 @@ func (ins *PingInstance) Init() error {
}
type Ping struct {
Interval config.Duration `toml:"interval"`
Instances []*PingInstance `toml:"instances"`
config.Interval
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
}
@ -93,10 +93,6 @@ func (p *Ping) Prefix() string {
return inputName
}
func (p *Ping) GetInterval() config.Duration {
return p.Interval
}
func (p *Ping) Init() error {
if len(p.Instances) == 0 {
return types.ErrInstancesEmpty
@ -123,7 +119,7 @@ func (p *Ping) Gather(slist *list.SafeList) {
p.wg.Wait()
}
func (p *Ping) gatherOnce(slist *list.SafeList, ins *PingInstance) {
func (p *Ping) gatherOnce(slist *list.SafeList, ins *Instance) {
defer p.wg.Done()
if ins.IntervalTimes > 0 {
@ -150,7 +146,7 @@ func (p *Ping) gatherOnce(slist *list.SafeList, ins *PingInstance) {
wg.Wait()
}
func (ins *PingInstance) gather(slist *list.SafeList, target string) {
func (ins *Instance) gather(slist *list.SafeList, target string) {
if config.Config.DebugMode {
log.Println("D! ping...", target)
}
@ -206,7 +202,7 @@ type pingStats struct {
ttl int
}
func (ins *PingInstance) ping(destination string) (*pingStats, error) {
func (ins *Instance) ping(destination string) (*pingStats, error) {
ps := &pingStats{}
pinger, err := ping.NewPinger(destination)

View File

@ -23,9 +23,9 @@ import (
const inputName = "processes"
type Processes struct {
Interval config.Duration `toml:"interval"`
ForcePS bool `toml:"force_ps"`
ForceProc bool `toml:"force_proc"`
config.Interval
ForcePS bool `toml:"force_ps"`
ForceProc bool `toml:"force_proc"`
}
func init() {
@ -38,10 +38,6 @@ func (p *Processes) Prefix() string {
return inputName
}
func (p *Processes) GetInterval() config.Duration {
return p.Interval
}
func (p *Processes) Drop() {}
func (p *Processes) Init() error {

View File

@ -60,8 +60,8 @@ func (ins *Instance) Init() error {
}
type Procstat struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
config.Interval
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
}
@ -76,10 +76,6 @@ func (s *Procstat) Prefix() string {
return inputName
}
func (s *Procstat) GetInterval() config.Duration {
return s.Interval
}
func (s *Procstat) Init() error {
if len(s.Instances) == 0 {
return types.ErrInstancesEmpty

View File

@ -84,8 +84,8 @@ func (ins *Instance) createHTTPClient() (*http.Client, error) {
}
type Prometheus struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
config.Interval
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
@ -101,10 +101,6 @@ func (p *Prometheus) Prefix() string {
return ""
}
func (p *Prometheus) GetInterval() config.Duration {
return p.Interval
}
func (p *Prometheus) Init() error {
if len(p.Instances) == 0 {
return types.ErrInstancesEmpty

View File

@ -64,8 +64,8 @@ func (ins *Instance) Init() error {
}
type Redis struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
config.Interval
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
@ -81,10 +81,6 @@ func (r *Redis) Prefix() string {
return inputName
}
func (r *Redis) GetInterval() config.Duration {
return r.Interval
}
func (r *Redis) Init() error {
if len(r.Instances) == 0 {
return types.ErrInstancesEmpty

View File

@ -16,8 +16,8 @@ import (
const inputName = "system"
type SystemStats struct {
Interval config.Duration `toml:"interval"`
CollectUserNumber bool `toml:"collect_user_number"`
config.Interval
CollectUserNumber bool `toml:"collect_user_number"`
}
func init() {
@ -30,10 +30,6 @@ func (s *SystemStats) Prefix() string {
return inputName
}
func (s *SystemStats) GetInterval() config.Duration {
return s.Interval
}
func (s *SystemStats) Init() error {
return nil
}

View File

@ -133,8 +133,8 @@ func (ins *Instance) createHTTPClient() (*http.Client, error) {
}
type Tomcat struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
config.Interval
Instances []*Instance `toml:"instances"`
Counter uint64
wg sync.WaitGroup
@ -150,10 +150,6 @@ func (t *Tomcat) Prefix() string {
return inputName
}
func (t *Tomcat) GetInterval() config.Duration {
return t.Interval
}
func (t *Tomcat) Init() error {
if len(t.Instances) == 0 {
return types.ErrInstancesEmpty