diff --git a/etc/collector.yml b/etc/collector.yml index 0b4b351a..98c09ac8 100644 --- a/etc/collector.yml +++ b/etc/collector.yml @@ -2,6 +2,7 @@ logger: dir: logs/collector level: WARNING keepHours: 2 + identity: specify: "" shell: ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|awk -F ':' '{print $NF}'|head -n 1 @@ -18,12 +19,18 @@ sys: timeout: 1000 interval: 20 plugin: ./plugin + + # monitor nic which filtered by prefix ifacePrefix: - eth - em - mountPoint: [] - mountIgnorePrefix: - - /var/lib + + # ignore disk mount point + mountIgnore: + prefix: + - /var/lib + # collect anyway + exclude: [] ignoreMetrics: - cpu.core.idle diff --git a/src/modules/collector/collector.go b/src/modules/collector/collector.go index dd4d26a9..425edb46 100644 --- a/src/modules/collector/collector.go +++ b/src/modules/collector/collector.go @@ -28,7 +28,10 @@ import ( "github.com/toolkits/pkg/runner" ) -const version = 1 +// changelog: +// 1: init project +// 2: mount ignore refactor +const version = 2 var ( vers *bool diff --git a/src/modules/collector/sys/config.go b/src/modules/collector/sys/config.go index 143890ab..d6fef2cc 100644 --- a/src/modules/collector/sys/config.go +++ b/src/modules/collector/sys/config.go @@ -1,16 +1,20 @@ package sys type SysSection struct { - IfacePrefix []string `yaml:"ifacePrefix"` - MountPoint []string `yaml:"mountPoint"` - MountIgnorePrefix []string `yaml:"mountIgnorePrefix"` - IgnoreMetrics []string `yaml:"ignoreMetrics"` - IgnoreMetricsMap map[string]struct{} `yaml:"-"` - NtpServers []string `yaml:"ntpServers"` - Plugin string `yaml:"plugin"` - PluginRemote bool `yaml:"pluginRemote"` - Interval int `yaml:"interval"` - Timeout int `yaml:"timeout"` + IfacePrefix []string `yaml:"ifacePrefix"` + MountIgnore MountIgnoreSection `yaml:"mountIgnore"` + IgnoreMetrics []string `yaml:"ignoreMetrics"` + IgnoreMetricsMap map[string]struct{} `yaml:"-"` + NtpServers []string `yaml:"ntpServers"` + Plugin string `yaml:"plugin"` + PluginRemote bool `yaml:"pluginRemote"` + Interval int `yaml:"interval"` + Timeout int `yaml:"timeout"` +} + +type MountIgnoreSection struct { + Prefix []string `yaml:"prefix"` + Exclude []string `yaml:"exclude"` } var Config SysSection diff --git a/src/modules/collector/sys/funcs/dfstat.go b/src/modules/collector/sys/funcs/dfstat.go index 414ff914..b53f2ed7 100644 --- a/src/modules/collector/sys/funcs/dfstat.go +++ b/src/modules/collector/sys/funcs/dfstat.go @@ -9,6 +9,7 @@ import ( "github.com/toolkits/pkg/logger" "github.com/toolkits/pkg/nux" + "github.com/toolkits/pkg/slice" ) func DeviceMetrics() []*dataobj.MetricValue { @@ -21,26 +22,11 @@ func DeviceMetrics() []*dataobj.MetricValue { return ret } - var myMountPoints = make(map[string]bool) - if len(sys.Config.MountPoint) > 0 { - for _, mp := range sys.Config.MountPoint { - myMountPoints[mp] = true - } - } - - ignoreMountPointsPrefix := sys.Config.MountIgnorePrefix - var diskTotal uint64 = 0 var diskUsed uint64 = 0 for idx := range mountPoints { fsSpec, fsFile, fsVfstype := mountPoints[idx][0], mountPoints[idx][1], mountPoints[idx][2] - if len(myMountPoints) > 0 { - if _, ok := myMountPoints[fsFile]; !ok { - logger.Debug("mount point not matched with config", fsFile, "ignored.") - continue - } - } if _, exists := fsFileFilter[fsFile]; exists { logger.Debugf("mount point %s was collected", fsFile) @@ -49,7 +35,9 @@ func DeviceMetrics() []*dataobj.MetricValue { fsFileFilter[fsFile] = struct{}{} } - if hasIgnorePrefix(fsFile, ignoreMountPointsPrefix) { + // 注意: 虽然前缀被忽略了,但是被忽略的这部分分区里边有些仍然是需要采集的 + if hasIgnorePrefix(fsFile, sys.Config.MountIgnore.Prefix) && + !slice.ContainsString(sys.Config.MountIgnore.Exclude, fsFile) { continue } diff --git a/src/modules/collector/sys/funcs/fsstat.go b/src/modules/collector/sys/funcs/fsstat.go index 8350c933..b3c3f014 100644 --- a/src/modules/collector/sys/funcs/fsstat.go +++ b/src/modules/collector/sys/funcs/fsstat.go @@ -13,6 +13,7 @@ import ( "github.com/toolkits/pkg/logger" "github.com/toolkits/pkg/nux" + "github.com/toolkits/pkg/slice" ) func FsRWMetrics() []*dataobj.MetricValue { @@ -26,8 +27,6 @@ func FsRWMetrics() []*dataobj.MetricValue { fsFileFilter := make(map[string]struct{}) //过滤 /proc/mounts 出现重复的fsFile - ignoreMountPointsPrefix := sys.Config.MountIgnorePrefix - for idx := range mountPoints { var du *nux.DeviceUsage du, err = nux.BuildDeviceUsage(mountPoints[idx][0], mountPoints[idx][1], mountPoints[idx][2]) @@ -36,7 +35,8 @@ func FsRWMetrics() []*dataobj.MetricValue { continue } - if hasIgnorePrefix(mountPoints[idx][1], ignoreMountPointsPrefix) { + if hasIgnorePrefix(du.FsFile, sys.Config.MountIgnore.Prefix) && + !slice.ContainsString(sys.Config.MountIgnore.Exclude, du.FsFile) { continue }