cmd

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2017 License: BSD-3-Clause Imports: 11 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")

		// Build collectors
		var cs []core.Collector

		cpu := collectors.NewCPU(uint(viper.GetInt("period")), uint8(viper.GetInt("cpu")))
		cs = append(cs, cpu)

		mem := collectors.NewMemory(uint(viper.GetInt("period")), uint8(viper.GetInt("mem")))
		cs = append(cs, mem)

		load := collectors.NewLoad(uint(viper.GetInt("period")), uint8(viper.GetInt("load")))
		cs = append(cs, load)

		net := collectors.NewNet(uint(viper.GetInt("period")), uint8(viper.GetInt("net")), viper.Get("net-opts"))
		cs = append(cs, net)

		disk := collectors.NewDisk(uint(viper.GetInt("period")), uint8(viper.GetInt("disk")))
		cs = append(cs, disk)

		cpath := viper.GetString("collectors")
		cdir, err := os.Open(cpath)
		if err == nil {
			idirs, err := cdir.Readdir(0)
			if err != nil {
				log.Error(err)
				return
			}
			for _, idir := range idirs {
				idirname := idir.Name()
				i, err := strconv.Atoi(idirname)
				if err != nil {
					if idirname != "etc" && idirname != "lib" {
						log.Warn("Bad collector folder: ", idirname)
					}
					continue
				}

				interval := i * 1000
				if i <= 0 {
					interval = viper.GetInt("period")
				}

				dir, err := os.Open(path.Join(cpath, idirname))
				if err != nil {
					log.Error(err)
					continue
				}

				files, err := dir.Readdir(0)
				if err != nil {
					log.Error(err)
					continue
				}

				for _, file := range files {
					disk := collectors.NewCollector(path.Join(dir.Name(), file.Name()), uint(interval), uint(viper.GetInt("keep-for")))
					cs = append(cs, disk)
				}
			}
		}

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

		http.Handle("/metrics", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			for _, c := range cs {
				w.Write(c.Metrics().Bytes())
			}
		}))
		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			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/runabove/noderig">Github</a></p>
	             </body>
	             </html>`))

		})
		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 {
					select {
					case <-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)
						}

						for _, c := range cs {
							file.Write(c.Metrics().Bytes())
						}

						file.Close()

						log.Debugf("Move to file: %v%v", path, ".metrics")
						os.Rename(path+".tmp", path+".metrics")
					}
				}
			}()
			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 {
			select {}
		}
	},
}

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