From 6e982af96c396bd0be59a9d38ef41d83ea7bedd9 Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Fri, 29 Apr 2022 00:02:20 +0800 Subject: [PATCH] code refactor: extract Interval struct so every plugin no need to implement function GetInterval --- config/interval.go | 9 +++++++++ inputs/cpu/cpu.go | 8 ++------ inputs/disk/disk.go | 12 ++++-------- inputs/diskio/diskio.go | 8 ++------ inputs/exec/exec.go | 8 ++------ inputs/httpresponse/httpresponse.go | 8 ++------ inputs/kernel/kernel.go | 8 ++------ inputs/kernelvmstat/kernelvmstat.go | 8 ++------ inputs/linuxsysctlfs/linuxsysctlfslinux.go | 6 +----- inputs/mem/mem.go | 8 ++------ inputs/mysql/mysql.go | 8 ++------ inputs/net/net.go | 10 +++------- inputs/netresponse/netresponse.go | 8 ++------ inputs/netstat/netstat.go | 7 +------ inputs/ntp/ntp.go | 8 ++------ inputs/oracle/oracle.go | 10 +++------- inputs/ping/ping.go | 18 +++++++----------- inputs/processes/processes_notwindows.go | 10 +++------- inputs/procstat/procstat.go | 8 ++------ inputs/prometheus/prometheus.go | 8 ++------ inputs/redis/redis.go | 8 ++------ inputs/system/system.go | 8 ++------ inputs/tomcat/tomcat.go | 8 ++------ 23 files changed, 61 insertions(+), 141 deletions(-) create mode 100644 config/interval.go diff --git a/config/interval.go b/config/interval.go new file mode 100644 index 0000000..dc9f948 --- /dev/null +++ b/config/interval.go @@ -0,0 +1,9 @@ +package config + +type Interval struct { + Interval Duration `toml:"interval"` +} + +func (i Interval) GetInterval() Duration { + return i.Interval +} diff --git a/inputs/cpu/cpu.go b/inputs/cpu/cpu.go index 91eeba3..d93f978 100644 --- a/inputs/cpu/cpu.go +++ b/inputs/cpu/cpu.go @@ -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 diff --git a/inputs/disk/disk.go b/inputs/disk/disk.go index 6910ce0..c189702 100644 --- a/inputs/disk/disk.go +++ b/inputs/disk/disk.go @@ -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 } diff --git a/inputs/diskio/diskio.go b/inputs/diskio/diskio.go index 3627e7d..57b7c06 100644 --- a/inputs/diskio/diskio.go +++ b/inputs/diskio/diskio.go @@ -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 { diff --git a/inputs/exec/exec.go b/inputs/exec/exec.go index db8258f..beb61e1 100644 --- a/inputs/exec/exec.go +++ b/inputs/exec/exec.go @@ -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 diff --git a/inputs/httpresponse/httpresponse.go b/inputs/httpresponse/httpresponse.go index d92a153..294e39a 100644 --- a/inputs/httpresponse/httpresponse.go +++ b/inputs/httpresponse/httpresponse.go @@ -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 diff --git a/inputs/kernel/kernel.go b/inputs/kernel/kernel.go index ec8421d..33177db 100644 --- a/inputs/kernel/kernel.go +++ b/inputs/kernel/kernel.go @@ -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 } diff --git a/inputs/kernelvmstat/kernelvmstat.go b/inputs/kernelvmstat/kernelvmstat.go index 41d71ad..3f2eab8 100644 --- a/inputs/kernelvmstat/kernelvmstat.go +++ b/inputs/kernelvmstat/kernelvmstat.go @@ -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 } diff --git a/inputs/linuxsysctlfs/linuxsysctlfslinux.go b/inputs/linuxsysctlfs/linuxsysctlfslinux.go index 715604d..97af640 100644 --- a/inputs/linuxsysctlfs/linuxsysctlfslinux.go +++ b/inputs/linuxsysctlfs/linuxsysctlfslinux.go @@ -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 } diff --git a/inputs/mem/mem.go b/inputs/mem/mem.go index a6e2691..536d783 100644 --- a/inputs/mem/mem.go +++ b/inputs/mem/mem.go @@ -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 { diff --git a/inputs/mysql/mysql.go b/inputs/mysql/mysql.go index c041e51..09e4761 100644 --- a/inputs/mysql/mysql.go +++ b/inputs/mysql/mysql.go @@ -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 diff --git a/inputs/net/net.go b/inputs/net/net.go index 8d68d12..9d93ea1 100644 --- a/inputs/net/net.go +++ b/inputs/net/net.go @@ -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 { diff --git a/inputs/netresponse/netresponse.go b/inputs/netresponse/netresponse.go index 719f173..fed4cad 100644 --- a/inputs/netresponse/netresponse.go +++ b/inputs/netresponse/netresponse.go @@ -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 diff --git a/inputs/netstat/netstat.go b/inputs/netstat/netstat.go index 4175495..c8eeeb8 100644 --- a/inputs/netstat/netstat.go +++ b/inputs/netstat/netstat.go @@ -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 { diff --git a/inputs/ntp/ntp.go b/inputs/ntp/ntp.go index e2e31fe..99f32a6 100644 --- a/inputs/ntp/ntp.go +++ b/inputs/ntp/ntp.go @@ -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 { diff --git a/inputs/oracle/oracle.go b/inputs/oracle/oracle.go index 1517b41..08748d6 100644 --- a/inputs/oracle/oracle.go +++ b/inputs/oracle/oracle.go @@ -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 diff --git a/inputs/ping/ping.go b/inputs/ping/ping.go index 3124c93..819b23d 100644 --- a/inputs/ping/ping.go +++ b/inputs/ping/ping.go @@ -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) diff --git a/inputs/processes/processes_notwindows.go b/inputs/processes/processes_notwindows.go index c192f5f..4f85d8e 100644 --- a/inputs/processes/processes_notwindows.go +++ b/inputs/processes/processes_notwindows.go @@ -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 { diff --git a/inputs/procstat/procstat.go b/inputs/procstat/procstat.go index 3dcca5c..5404579 100644 --- a/inputs/procstat/procstat.go +++ b/inputs/procstat/procstat.go @@ -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 diff --git a/inputs/prometheus/prometheus.go b/inputs/prometheus/prometheus.go index aa959c6..15fb880 100644 --- a/inputs/prometheus/prometheus.go +++ b/inputs/prometheus/prometheus.go @@ -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 diff --git a/inputs/redis/redis.go b/inputs/redis/redis.go index c30e139..c5604e3 100644 --- a/inputs/redis/redis.go +++ b/inputs/redis/redis.go @@ -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 diff --git a/inputs/system/system.go b/inputs/system/system.go index 5021b93..1679164 100644 --- a/inputs/system/system.go +++ b/inputs/system/system.go @@ -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 } diff --git a/inputs/tomcat/tomcat.go b/inputs/tomcat/tomcat.go index 7b36782..287c37d 100644 --- a/inputs/tomcat/tomcat.go +++ b/inputs/tomcat/tomcat.go @@ -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