Skip to content

Commit

Permalink
Collector refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrusev committed Aug 2, 2016
1 parent ebb7261 commit f639895
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
28 changes: 9 additions & 19 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,21 @@ import (
"github.com/amonapp/amonagent/plugins"
)

// ConfiguredPlugin - XXX
type ConfiguredPlugin struct {
Name string
Plugin plugins.Plugin
}

// Agent - XXX
type Agent struct {
// Interval at which to gather information
Interval time.Duration

ConfiguredPlugins []ConfiguredPlugin
ConfiguredPlugins []plugins.ConfiguredPlugin
}

// Test - XXX
func (a *Agent) Test(config settings.Struct) error {

allMetrics := collectors.CollectAllData()
allMetrics := collectors.CollectAllData(a.ConfiguredPlugins)

ProcessesData := collectors.CollectProcessData()
SystemData := collectors.CollectSystemData()
EnabledPlugins, _ := plugins.GetAllEnabledPlugins()
HostData := collectors.CollectHostData()

fmt.Println("\n------------------")
Expand All @@ -51,12 +44,9 @@ func (a *Agent) Test(config settings.Struct) error {
fmt.Println("\033[92mPlugins: \033[0m")
fmt.Println("")

for _, p := range EnabledPlugins {

creator, _ := plugins.Plugins[p.Name]
plugin := creator()
for _, p := range a.ConfiguredPlugins {
start := time.Now()
PluginResult, err := plugin.Collect()
PluginResult, err := p.Plugin.Collect()
if err != nil {
log.Errorf("Can't get stats for plugin: %s", err)
}
Expand Down Expand Up @@ -95,8 +85,6 @@ func (a *Agent) Test(config settings.Struct) error {

fmt.Println("\n------------------")

// url := remote.SystemURL()

err := remote.SendData(allMetrics, true)
if err != nil {
return fmt.Errorf("%s\n", err.Error())
Expand All @@ -107,7 +95,9 @@ func (a *Agent) Test(config settings.Struct) error {

// GatherAndSend - XXX
func (a *Agent) GatherAndSend(debug bool) error {
allMetrics := collectors.CollectAllData()

allMetrics := collectors.CollectAllData(a.ConfiguredPlugins)

log.Infof("Metrics collected (Interval:%s)\n", a.Interval)

err := remote.SendData(allMetrics, debug)
Expand All @@ -121,14 +111,14 @@ func (a *Agent) GatherAndSend(debug bool) error {
// NewAgent - XXX
func NewAgent(config settings.Struct) (*Agent, error) {

var configuredPlugins = []ConfiguredPlugin{}
var configuredPlugins = []plugins.ConfiguredPlugin{}

EnabledPlugins, _ := plugins.GetAllEnabledPlugins()
for _, p := range EnabledPlugins {
creator, _ := plugins.Plugins[p.Name]
plugin := creator()

t := ConfiguredPlugin{Name: p.Name, Plugin: plugin}
t := plugins.ConfiguredPlugin{Name: p.Name, Plugin: plugin}
configuredPlugins = append(configuredPlugins, t)

}
Expand Down
18 changes: 7 additions & 11 deletions collectors/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,18 @@ type PluginResultStruct struct {
}

// CollectPluginsData - XXX
func CollectPluginsData() (interface{}, interface{}) {
func CollectPluginsData(configuredPlugins []plugins.ConfiguredPlugin) (interface{}, interface{}) {
PluginResults := make(map[string]interface{})
var CheckResults interface{}
var wg sync.WaitGroup
EnabledPlugins, _ := plugins.GetAllEnabledPlugins()

resultChan := make(chan PluginResultStruct, len(EnabledPlugins))
resultChan := make(chan PluginResultStruct, len(configuredPlugins))

for _, p := range EnabledPlugins {
for _, p := range configuredPlugins {
wg.Add(1)

creator, _ := plugins.Plugins[p.Name]
plugin := creator()

go func(p plugins.PluginConfig) {
PluginResult, err := plugin.Collect()
go func(p plugins.ConfiguredPlugin) {
PluginResult, err := p.Plugin.Collect()
if err != nil {
log.Errorf("Can't get stats for plugin: %s", err)
}
Expand Down Expand Up @@ -206,11 +202,11 @@ func CollectProcessData() ProcessesList {
}

// CollectAllData - XXX
func CollectAllData() AllMetricsStruct {
func CollectAllData(configuredPlugins []plugins.ConfiguredPlugin) AllMetricsStruct {

ProcessesData := CollectProcessData()
SystemData := CollectSystemData()
Plugins, Checks := CollectPluginsData()
Plugins, Checks := CollectPluginsData(configuredPlugins)
HostData := CollectHostData()

allMetrics := AllMetricsStruct{
Expand Down
6 changes: 6 additions & 0 deletions plugins/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
"github.com/amonapp/amonagent/internal/settings"
)

// ConfiguredPlugin - XXX
type ConfiguredPlugin struct {
Name string
Plugin Plugin
}

// PluginConfig - XXX
type PluginConfig struct {
Path string
Expand Down

0 comments on commit f639895

Please sign in to comment.