Пример #1
0
func resourcesDiskHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	collection := resources.NewCollection()
	collection.CollectDiskMetrics()
	marshalledResources, _ := json.MarshalIndent(collection, "", "  ")
	fmt.Fprintf(w, string(marshalledResources))
}
Пример #2
0
func main() {
	flagVersion := flag.Bool("version", false, "Display Procd version.")
	flagConfigFile := flag.String("config", "procd.toml", "Procd configuration file.")
	flag.Parse()

	runtime.GOMAXPROCS(runtime.NumCPU())

	// Handle no command-line paramters
	if flag.NFlag() == 0 {
		flag.PrintDefaults()
		os.Exit(0)
	}

	// Display Procd version
	if *flagVersion {
		fmt.Println("Procd v", Version)
		os.Exit(0)
	}

	// Decode configuration [https://github.com/mojombo/toml]
	var config procdConfig
	_, err := toml.DecodeFile(*flagConfigFile, &config)
	verifyErrorResponse(err, "could not decode TOML config")

	// Setup output channels
	for outputName, output := range config.Outputs {
		switch outputName {
		case "stdout":
			fmt.Printf("\nProcd: Output[stdout]\n")
		case "http":
			go HTTPServerStart(output.BindAddress)
			fmt.Printf("Procd: Output[http]: %s\n", output.BindAddress)
		case "heka":
			fmt.Printf("Procd: Output[heka]: %s [TCP / Protobufs]\n", output.Server)
		}
	}

	// Timing channels for polling interval
	collectionInterval := time.NewTicker(time.Second * time.Duration(config.TickerInterval))

	collection := resources.NewCollection()
	// Update statistics on interval until sigterm
	go func() {
		for _ = range collectionInterval.C {
			collection.CollectAllMetrics()
			for outputName, output := range config.Outputs {
				switch outputName {
				case "stdout":
					marshalledResources, _ := json.MarshalIndent(collection, "", " ")
					fmt.Println(string(marshalledResources))
				case "heka":
					hc, err := NewHekaClient(output.Server, output.Hostname)
					if err == nil {
						err := hc.SendCPUResources(collection, output.Payload)
						verifyErrorResponse(err, "generate CPU resources Heka message")
						err = hc.SendMemoryResources(collection, output.Payload)
						verifyErrorResponse(err, "generate memory resources Heka message")
						err = hc.SendDiskResources(collection, output.Payload)
						verifyErrorResponse(err, "generate disk resources Heka message")
						hc.sender.Close()
					}
				}
			}
		}
	}()

	fmt.Printf("Procd: Collection interval: %v seconds\n", config.TickerInterval)
	exitStatus := make(chan bool, 1)
	fmt.Println("Procd: Started.")
	waitForSignals(exitStatus)
}