cmd

package
v2.5.2+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2019 License: BSD-3-Clause Imports: 16 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "noderig",
	Short: "Noderig expose node stats as Sensision metrics",
	Run: func(cmd *cobra.Command, args []string) {
		log.Info("Noderig starting")

		cs = getCollectors()

		log.Infof("Noderig started - %v", len(cs))

		http.Handle("/metrics", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			csMutex.Lock()
			defer csMutex.Unlock()
			for _, c := range cs {
				_, err := w.Write(c.Metrics().Bytes())
				if err != nil {
					log.WithError(err).Error("cannot write metric into file")
				}
			}
		}))
		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			_, err := w.Write([]byte(`<html>
	             <head><title>Noderig</title></head>
	             <body>
	             <h1>Noderig</h1>
	             <p><a href="/metrics">Metrics</a></p>
	             <p><a href="https://github.com/ovh/noderig">Github</a></p>
	             </body>
	             </html>`))
			if err != nil {
				log.WithError(err).Error("cannot send body to client")
			}
		})
		log.Info("Http started")

		if viper.IsSet("flushPath") {
			flushPath := viper.GetString("flushPath")
			ticker := time.NewTicker(time.Duration(viper.GetInt("flushPeriod")) * time.Millisecond)
			go func() {
				for range ticker.C {
					path := fmt.Sprintf("%v%v", flushPath, time.Now().Unix())
					log.Debugf("Flush to file: %v%v", path, ".tmp")
					file, err := os.Create(path + ".tmp")
					if err != nil {
						log.Errorf("Flush failed: %v", err)
					}

					csMutex.Lock()
					for _, c := range cs {
						_, err := file.Write(c.Metrics().Bytes())
						if err != nil {
							log.WithError(err).Error("Cannot write metric into file")
						}
					}
					csMutex.Unlock()

					if err := file.Close(); err != nil {
						log.WithError(err).Error("Cannot close flush file")
					}

					log.Debugf("Move to file: %v%v", path, ".metrics")
					err = os.Rename(path+".tmp", path+".metrics")
					if err != nil {
						log.WithError(err).Error("Cannot rotate metrics file")
					}
				}
			}()
			log.Info("Flush routine started")
		}

		log.Info("Started")

		if viper.GetString("listen") != "none" {
			log.Infof("Listen %s", viper.GetString("listen"))
			log.Fatal(http.ListenAndServe(viper.GetString("listen"), nil))
		} else {

			quit := make(chan os.Signal, 2)

			signal.Notify(quit, syscall.SIGTERM)
			signal.Notify(quit, syscall.SIGINT)

			<-quit

		}
	},
}

RootCmd launch the aggregator agent.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL