Esempio n. 1
0
// TestPlugin - XXX
func (a *Agent) TestPlugin(pluginName string) error {

	_, err := plugins.GetConfigPath(pluginName)

	if err != nil {
		fmt.Printf("Can't get config file for plugin: %s", err)
	}

	for _, p := range a.ConfiguredPlugins {
		if pluginName == p.Name {
			start := time.Now()

			PluginResult, err := p.Plugin.Collect()
			if err != nil {
				fmt.Printf("Can't get stats for plugin: %s", err)
			}
			fmt.Println(PluginResult)

			elapsed := time.Since(start)
			fmt.Printf("\nExecuted in \033[92m%s\033[0m", elapsed)
		}
	}

	return nil

}
Esempio n. 2
0
func main() {

	flag.Parse()

	if *fListPlugins {
		ListPlugins()
		return
	}

	if len(*fPluginConfig) > 0 {
		pluginConfig, _ := plugins.GetConfigPath(*fPluginConfig)
		creator, ok := plugins.Plugins[pluginConfig.Name]
		if ok {
			plugin := creator()
			conf := plugin.SampleConfig()
			fmt.Println(conf)
		} else {
			fmt.Printf("Non existing plugin: %s", pluginConfig.Name)
			ListPlugins()
		}
		return
	}

	if len(*fTestPlugin) > 0 {
		pluginConfig, _ := plugins.GetConfigPath(*fTestPlugin)
		creator, ok := plugins.Plugins[pluginConfig.Name]
		if ok {
			plugin := creator()
			PluginResult, err := plugin.Collect(pluginConfig.Path)
			if err != nil {
				fmt.Printf("Can't get stats for plugin: %s", err)
			}
			fmt.Println(PluginResult)
		} else {
			fmt.Printf("Non existing plugin: %s", pluginConfig.Name)
			ListPlugins()
		}
		return
	}

	if *fVersion {
		v := fmt.Sprintf("Amon - Version %s", Version)
		fmt.Println(v)
		return
	}

	config := settings.Settings()

	// Detect Machine ID or ask for a valid Server Key in Settings
	machineID := collectors.MachineID()
	serverKey := config.ServerKey

	ag, err := amonagent.NewAgent(config)
	if err != nil {
		log.Fatal(err)
	}

	if *fTest {
		err = ag.Test(config)
		if err != nil {
			log.Fatal(err)
		}
		return
	}

	if *fMachineID {
		fmt.Print(machineID)
		return
	}

	if len(machineID) == 0 && len(serverKey) == 0 {
		log.Fatal("Can't detect Machine ID. Please define `server_key` in /etc/opt/amonagent/amonagent.conf ")
	}

	shutdown := make(chan struct{})
	signals := make(chan os.Signal)
	signal.Notify(signals, os.Interrupt)
	go func() {
		<-signals
		close(shutdown)
	}()

	log.Printf("Starting Amon Agent (version %s)\n", Version)

	if *fPidfile != "" {
		// Ensure the required directory structure exists.
		err := os.MkdirAll(filepath.Dir(*fPidfile), 0700)
		if err != nil {
			log.Fatal(3, "Failed to verify pid directory", err)
		}

		f, err := os.Create(*fPidfile)
		if err != nil {
			log.Fatalf("Unable to create pidfile: %s", err)
		}

		fmt.Fprintf(f, "%d\n", os.Getpid())

		f.Close()
	}

	ag.Run(shutdown)
}