コード例 #1
0
func stopCPUMonitor(url string, s3config *uploader.Config) ([]byte, error) {
	startURL := fmt.Sprintf("http://%s/stop", url)
	resp, err := http.Get(startURL)
	if err != nil {
		return nil, fmt.Errorf("calling cpumonitor stop %s", err)
	}
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("received resp %d", resp.StatusCode)
	}

	rawData, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	filename := fmt.Sprintf("cpuStats-%s.csv", time.Now().UTC().Format(time.RFC3339))

	csvData, err := data.GenerateCpuCSV(rawData)
	if err != nil {
		return nil, fmt.Errorf("GeneratateCpuCSV %d", resp.StatusCode)
	}

	loc, err := uploader.Upload(s3config, bytes.NewBuffer(csvData), filename, false)
	if err != nil {
		return nil, fmt.Errorf("uploading to s3 error: %s", err)
	}
	fmt.Fprintf(os.Stdout, "csv uploaded to %s\n", loc)

	return csvData, nil
}
コード例 #2
0
{"TimeStamp":"2016-12-15T15:00:47.575579693-08:00","Percentage":[12.358514295296388]},
{"TimeStamp":"2016-12-15T15:00:47.672438722-08:00","Percentage":[20.77922077922078]}
]`

var multiplePercentageCSV = `timestamp,percentage,percentage
2016-12-15 15:00:47.575579693 -0800 PST,12.358514,13.358514
2016-12-15 15:00:47.672438722 -0800 PST,20.779221,21.779221`

var multiplePercentageJSON = `[
{"TimeStamp":"2016-12-15T15:00:47.575579693-08:00","Percentage":[12.358514295296388,13.358514295296388]},
{"TimeStamp":"2016-12-15T15:00:47.672438722-08:00","Percentage":[20.77922077922078,21.77922077922078]}
]`

var _ = Describe("GenerateCpuCSV", func() {
	It("returns an error and  empty byte slice if empty byte passed", func() {
		emptyByte, err := data.GenerateCpuCSV([]byte(""))
		Expect(err).To(HaveOccurred())
		Expect(emptyByte).To(BeEmpty())
	})

	It("returns an error and empty byte slice if nil byte passed", func() {
		emptyByte, err := data.GenerateCpuCSV([]byte(nil))
		Expect(err).To(HaveOccurred())
		Expect(emptyByte).To(BeNil())
	})

	It("returns an error if bad data passed", func() {
		badData := `timestamp, timestamp`
		emptyByte, err := data.GenerateCpuCSV([]byte(badData))
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(ContainSubstring("marshaling data"))