func TestPrometheusWritePointTag(t *testing.T) {

	p := &prometheus.Prometheus{
		Urls: []string{"http://localhost:9126/metrics"},
	}
	tags := make(map[string]string)
	tags["testtag"] = "testvalue"
	var points = []*client.Point{
		client.NewPoint(
			"test_point_3",
			tags,
			map[string]interface{}{"value": 0.0}),
		client.NewPoint(
			"test_point_4",
			tags,
			map[string]interface{}{"value": 1.0}),
	}
	require.NoError(t, pTesting.Write(points))

	expected := []struct {
		name  string
		value float64
	}{
		{"test_point_3", 0.0},
		{"test_point_4", 1.0},
	}

	var acc testutil.Accumulator

	require.NoError(t, p.Gather(&acc))
	for _, e := range expected {
		assert.True(t, acc.CheckTaggedValue(e.name, e.value, tags))
	}
}
Example #2
0
func TestZfsPoolMetrics(t *testing.T) {
	err := os.MkdirAll(testKstatPath, 0755)
	require.NoError(t, err)

	err = os.MkdirAll(testKstatPath+"/HOME", 0755)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/HOME/io", []byte(pool_ioContents), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/arcstats", []byte(arcstatsContents), 0644)
	require.NoError(t, err)

	poolMetrics := getPoolMetrics()

	var acc testutil.Accumulator

	//one pool, all metrics
	tags := map[string]string{
		"pool": "HOME",
	}

	z := &Zfs{KstatPath: testKstatPath, KstatMetrics: []string{"arcstats"}}
	err = z.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range poolMetrics {
		assert.True(t, !acc.HasIntValue(metric.name), metric.name)
		assert.True(t, !acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	z = &Zfs{KstatPath: testKstatPath, KstatMetrics: []string{"arcstats"}, PoolMetrics: true}
	err = z.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range poolMetrics {
		assert.True(t, acc.HasIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	err = os.RemoveAll(os.TempDir() + "/telegraf")
	require.NoError(t, err)
}
Example #3
0
func TestMailChimpGatherReports(t *testing.T) {
	ts := httptest.NewServer(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				w.WriteHeader(http.StatusOK)
				fmt.Fprintln(w, sampleReports)
			},
		))
	defer ts.Close()

	u, err := url.ParseRequestURI(ts.URL)
	require.NoError(t, err)

	api := &ChimpAPI{
		url:   u,
		Debug: true,
	}
	m := MailChimp{
		api: api,
	}

	var acc testutil.Accumulator
	err = m.Gather(&acc)
	require.NoError(t, err)

	tags := make(map[string]string)
	tags["id"] = "42694e9e57"
	tags["campaign_title"] = "Freddie's Jokes Vol. 1"

	testInts := []struct {
		measurement string
		value       int
	}{
		{"emails_sent", 200},
		{"abuse_reports", 0},
		{"unsubscribed", 2},
		{"hard_bounces", 0},
		{"soft_bounces", 2},
		{"syntax_errors", 0},
		{"forwards_count", 0},
		{"forwards_opens", 0},
		{"opens_total", 186},
		{"unique_opens", 100},
		{"clicks_total", 42},
		{"unique_clicks", 400},
		{"unique_subscriber_clicks", 42},
		{"facebook_recipient_likes", 5},
		{"facebook_unique_likes", 8},
		{"facebook_likes", 42},
	}
	for _, test := range testInts {
		assert.True(t, acc.CheckTaggedValue(test.measurement, test.value, tags),
			fmt.Sprintf("Measurement: %v, value: %v, tags: %v not found",
				test.measurement, test.value, tags))
	}

	testFloats := []struct {
		measurement string
		value       float64
	}{
		{"open_rate", 42},
		{"click_rate", 42},
		{"industry_open_rate", 0.17076777144396},
		{"industry_click_rate", 0.027431311866951},
		{"industry_bounce_rate", 0.0063767751251474},
		{"industry_unopen_rate", 0.82285545343089},
		{"industry_unsub_rate", 0.001436957032815},
		{"industry_abuse_rate", 0.00021111996110887},
		{"list_stats_sub_rate", 10},
		{"list_stats_unsub_rate", 20},
		{"list_stats_open_rate", 42},
		{"list_stats_click_rate", 42},
	}
	for _, test := range testFloats {
		assert.True(t, acc.CheckTaggedValue(test.measurement, test.value, tags),
			fmt.Sprintf("Measurement: %v, value: %v, tags: %v not found",
				test.measurement, test.value, tags))
	}

	testStrings := []struct {
		measurement string
		value       string
	}{
		{"industry_type", "Social Networks and Online Communities"},
	}
	for _, test := range testStrings {
		assert.True(t, acc.CheckTaggedValue(test.measurement, test.value, tags),
			fmt.Sprintf("Measurement: %v, value: %v, tags: %v not found",
				test.measurement, test.value, tags))
	}
}
Example #4
0
func TestSystemStats_GenerateStats(t *testing.T) {
	var mps MockPS

	defer mps.AssertExpectations(t)

	var acc testutil.Accumulator

	lv := &load.LoadAvgStat{
		Load1:  0.3,
		Load5:  1.5,
		Load15: 0.8,
	}

	mps.On("LoadAvg").Return(lv, nil)

	cts := cpu.CPUTimesStat{
		CPU:       "cpu0",
		User:      3.1,
		System:    8.2,
		Idle:      80.1,
		Nice:      1.3,
		Iowait:    0.2,
		Irq:       0.1,
		Softirq:   0.11,
		Steal:     0.0001,
		Guest:     8.1,
		GuestNice: 0.324,
		Stolen:    0.051,
	}

	cts2 := cpu.CPUTimesStat{
		CPU:       "cpu0",
		User:      11.4,     // increased by 8.3
		System:    10.9,     // increased by 2.7
		Idle:      158.8699, // increased by 78.7699 (for total increase of 100)
		Nice:      2.5,      // increased by 1.2
		Iowait:    0.7,      // increased by 0.5
		Irq:       1.2,      // increased by 1.1
		Softirq:   0.31,     // increased by 0.2
		Steal:     0.0002,   // increased by 0.0001
		Guest:     12.9,     // increased by 4.8
		GuestNice: 2.524,    // increased by 2.2
		Stolen:    0.281,    // increased by 0.23
	}

	mps.On("CPUTimes").Return([]cpu.CPUTimesStat{cts}, nil)

	du := &disk.DiskUsageStat{
		Path:        "/",
		Fstype:      "ext4",
		Total:       128,
		Free:        23,
		InodesTotal: 1234,
		InodesFree:  234,
	}

	mps.On("DiskUsage").Return([]*disk.DiskUsageStat{du}, nil)

	diskio := disk.DiskIOCountersStat{
		ReadCount:    888,
		WriteCount:   5341,
		ReadBytes:    100000,
		WriteBytes:   200000,
		ReadTime:     7123,
		WriteTime:    9087,
		Name:         "sda1",
		IoTime:       123552,
		SerialNumber: "ab-123-ad",
	}

	mps.On("DiskIO").Return(map[string]disk.DiskIOCountersStat{"sda1": diskio}, nil)

	netio := net.NetIOCountersStat{
		Name:        "eth0",
		BytesSent:   1123,
		BytesRecv:   8734422,
		PacketsSent: 781,
		PacketsRecv: 23456,
		Errin:       832,
		Errout:      8,
		Dropin:      7,
		Dropout:     1,
	}

	mps.On("NetIO").Return([]net.NetIOCountersStat{netio}, nil)

	vms := &mem.VirtualMemoryStat{
		Total:       12400,
		Available:   7600,
		Used:        5000,
		UsedPercent: 47.1,
		Free:        1235,
		Active:      8134,
		Inactive:    1124,
		Buffers:     771,
		Cached:      4312,
		Wired:       134,
		Shared:      2142,
	}

	mps.On("VMStat").Return(vms, nil)

	sms := &mem.SwapMemoryStat{
		Total:       8123,
		Used:        1232,
		Free:        6412,
		UsedPercent: 12.2,
		Sin:         7,
		Sout:        830,
	}

	mps.On("SwapStat").Return(sms, nil)

	ds := &DockerContainerStat{
		Name: "blah",
		CPU: &cpu.CPUTimesStat{
			CPU:       "all",
			User:      3.1,
			System:    8.2,
			Idle:      80.1,
			Nice:      1.3,
			Iowait:    0.2,
			Irq:       0.1,
			Softirq:   0.11,
			Steal:     0.0001,
			Guest:     8.1,
			GuestNice: 0.324,
			Stolen:    0.051,
		},
		Mem: &docker.CgroupMemStat{
			ContainerID:             "blah",
			Cache:                   1,
			RSS:                     2,
			RSSHuge:                 3,
			MappedFile:              4,
			Pgpgin:                  5,
			Pgpgout:                 6,
			Pgfault:                 7,
			Pgmajfault:              8,
			InactiveAnon:            9,
			ActiveAnon:              10,
			InactiveFile:            11,
			ActiveFile:              12,
			Unevictable:             13,
			HierarchicalMemoryLimit: 14,
			TotalCache:              15,
			TotalRSS:                16,
			TotalRSSHuge:            17,
			TotalMappedFile:         18,
			TotalPgpgIn:             19,
			TotalPgpgOut:            20,
			TotalPgFault:            21,
			TotalPgMajFault:         22,
			TotalInactiveAnon:       23,
			TotalActiveAnon:         24,
			TotalInactiveFile:       25,
			TotalActiveFile:         26,
			TotalUnevictable:        27,
		},
	}

	mps.On("DockerStat").Return([]*DockerContainerStat{ds}, nil)

	ss := &SystemStats{ps: &mps}

	err := ss.Gather(&acc)
	require.NoError(t, err)

	assert.True(t, acc.CheckValue("load1", 0.3))
	assert.True(t, acc.CheckValue("load5", 1.5))
	assert.True(t, acc.CheckValue("load15", 0.8))

	cs := NewCPUStats(&mps)

	cputags := map[string]string{
		"cpu": "cpu0",
	}

	preCPUPoints := len(acc.Points)
	err = cs.Gather(&acc)
	require.NoError(t, err)
	numCPUPoints := len(acc.Points) - preCPUPoints

	expectedCPUPoints := 12
	assert.Equal(t, numCPUPoints, expectedCPUPoints)

	// Computed values are checked with delta > 0 becasue of floating point arithmatic
	// imprecision
	assertContainsTaggedFloat(t, acc, "user", 3.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "system", 8.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "idle", 80.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "nice", 1.3, 0, cputags)
	assertContainsTaggedFloat(t, acc, "iowait", 0.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "irq", 0.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "softirq", 0.11, 0, cputags)
	assertContainsTaggedFloat(t, acc, "steal", 0.0001, 0, cputags)
	assertContainsTaggedFloat(t, acc, "guest", 8.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "guestNice", 0.324, 0, cputags)
	assertContainsTaggedFloat(t, acc, "stolen", 0.051, 0, cputags)
	assertContainsTaggedFloat(t, acc, "busy", 21.4851, 0.0005, cputags)

	mps2 := MockPS{}
	mps2.On("CPUTimes").Return([]cpu.CPUTimesStat{cts2}, nil)
	cs.ps = &mps2

	// Should have added cpu percentages too
	err = cs.Gather(&acc)
	require.NoError(t, err)

	numCPUPoints = len(acc.Points) - (preCPUPoints + numCPUPoints)
	expectedCPUPoints = 24
	assert.Equal(t, numCPUPoints, expectedCPUPoints)

	assertContainsTaggedFloat(t, acc, "user", 11.4, 0, cputags)
	assertContainsTaggedFloat(t, acc, "system", 10.9, 0, cputags)
	assertContainsTaggedFloat(t, acc, "idle", 158.8699, 0, cputags)
	assertContainsTaggedFloat(t, acc, "nice", 2.5, 0, cputags)
	assertContainsTaggedFloat(t, acc, "iowait", 0.7, 0, cputags)
	assertContainsTaggedFloat(t, acc, "irq", 1.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "softirq", 0.31, 0, cputags)
	assertContainsTaggedFloat(t, acc, "steal", 0.0002, 0, cputags)
	assertContainsTaggedFloat(t, acc, "guest", 12.9, 0, cputags)
	assertContainsTaggedFloat(t, acc, "guestNice", 2.524, 0, cputags)
	assertContainsTaggedFloat(t, acc, "stolen", 0.281, 0, cputags)
	assertContainsTaggedFloat(t, acc, "busy", 42.7152, 0.0005, cputags)

	assertContainsTaggedFloat(t, acc, "percentageUser", 8.3, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageSystem", 2.7, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageIdle", 78.7699, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageNice", 1.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageIowait", 0.5, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageIrq", 1.1, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageSoftirq", 0.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageSteal", 0.0001, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageGuest", 4.8, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageGuestNice", 2.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageStolen", 0.23, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageBusy", 21.2301, 0.0005, cputags)

	err = (&DiskStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	tags := map[string]string{
		"path":   "/",
		"fstype": "ext4",
	}

	assert.True(t, acc.CheckTaggedValue("total", uint64(128), tags))
	assert.True(t, acc.CheckTaggedValue("used", uint64(105), tags))
	assert.True(t, acc.CheckTaggedValue("free", uint64(23), tags))
	assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(1234), tags))
	assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(234), tags))
	assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(1000), tags))

	err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc)
	require.NoError(t, err)

	ntags := map[string]string{
		"interface": "eth0",
	}

	assert.NoError(t, acc.ValidateTaggedValue("bytes_sent", uint64(1123), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("bytes_recv", uint64(8734422), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("packets_sent", uint64(781), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("packets_recv", uint64(23456), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("err_in", uint64(832), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("err_out", uint64(8), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("drop_in", uint64(7), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("drop_out", uint64(1), ntags))

	err = (&DiskIOStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	dtags := map[string]string{
		"name":   "sda1",
		"serial": "ab-123-ad",
	}

	assert.True(t, acc.CheckTaggedValue("reads", uint64(888), dtags))
	assert.True(t, acc.CheckTaggedValue("writes", uint64(5341), dtags))
	assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(100000), dtags))
	assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(200000), dtags))
	assert.True(t, acc.CheckTaggedValue("read_time", uint64(7123), dtags))
	assert.True(t, acc.CheckTaggedValue("write_time", uint64(9087), dtags))
	assert.True(t, acc.CheckTaggedValue("io_time", uint64(123552), dtags))

	err = (&MemStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	vmtags := map[string]string(nil)

	assert.True(t, acc.CheckTaggedValue("total", uint64(12400), vmtags))
	assert.True(t, acc.CheckTaggedValue("available", uint64(7600), vmtags))
	assert.True(t, acc.CheckTaggedValue("used", uint64(5000), vmtags))
	assert.True(t, acc.CheckTaggedValue("used_prec", float64(47.1), vmtags))
	assert.True(t, acc.CheckTaggedValue("free", uint64(1235), vmtags))
	assert.True(t, acc.CheckTaggedValue("active", uint64(8134), vmtags))
	assert.True(t, acc.CheckTaggedValue("inactive", uint64(1124), vmtags))
	assert.True(t, acc.CheckTaggedValue("buffers", uint64(771), vmtags))
	assert.True(t, acc.CheckTaggedValue("cached", uint64(4312), vmtags))
	assert.True(t, acc.CheckTaggedValue("wired", uint64(134), vmtags))
	assert.True(t, acc.CheckTaggedValue("shared", uint64(2142), vmtags))

	acc.Points = nil

	err = (&SwapStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	swaptags := map[string]string(nil)

	assert.NoError(t, acc.ValidateTaggedValue("total", uint64(8123), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("used", uint64(1232), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("used_perc", float64(12.2), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("free", uint64(6412), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("in", uint64(7), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("out", uint64(830), swaptags))

	err = (&DockerStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	dockertags := map[string]string{
		"name":    "blah",
		"id":      "",
		"command": "",
	}

	assert.True(t, acc.CheckTaggedValue("user", 3.1, dockertags))
	assert.True(t, acc.CheckTaggedValue("system", 8.2, dockertags))
	assert.True(t, acc.CheckTaggedValue("idle", 80.1, dockertags))
	assert.True(t, acc.CheckTaggedValue("nice", 1.3, dockertags))
	assert.True(t, acc.CheckTaggedValue("iowait", 0.2, dockertags))
	assert.True(t, acc.CheckTaggedValue("irq", 0.1, dockertags))
	assert.True(t, acc.CheckTaggedValue("softirq", 0.11, dockertags))
	assert.True(t, acc.CheckTaggedValue("steal", 0.0001, dockertags))
	assert.True(t, acc.CheckTaggedValue("guest", 8.1, dockertags))
	assert.True(t, acc.CheckTaggedValue("guestNice", 0.324, dockertags))
	assert.True(t, acc.CheckTaggedValue("stolen", 0.051, dockertags))

	assert.True(t, acc.CheckTaggedValue("cache", uint64(1), dockertags))
	assert.True(t, acc.CheckTaggedValue("rss", uint64(2), dockertags))
	assert.True(t, acc.CheckTaggedValue("rss_huge", uint64(3), dockertags))
	assert.True(t, acc.CheckTaggedValue("mapped_file", uint64(4), dockertags))
	assert.True(t, acc.CheckTaggedValue("swap_in", uint64(5), dockertags))
	assert.True(t, acc.CheckTaggedValue("swap_out", uint64(6), dockertags))
	assert.True(t, acc.CheckTaggedValue("page_fault", uint64(7), dockertags))
	assert.True(t, acc.CheckTaggedValue("page_major_fault", uint64(8), dockertags))
	assert.True(t, acc.CheckTaggedValue("inactive_anon", uint64(9), dockertags))
	assert.True(t, acc.CheckTaggedValue("active_anon", uint64(10), dockertags))
	assert.True(t, acc.CheckTaggedValue("inactive_file", uint64(11), dockertags))
	assert.True(t, acc.CheckTaggedValue("active_file", uint64(12), dockertags))
	assert.True(t, acc.CheckTaggedValue("unevictable", uint64(13), dockertags))
	assert.True(t, acc.CheckTaggedValue("memory_limit", uint64(14), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_cache", uint64(15), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_rss", uint64(16), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_rss_huge", uint64(17), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_mapped_file", uint64(18), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_swap_in", uint64(19), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_swap_out", uint64(20), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_page_fault", uint64(21), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_page_major_fault", uint64(22), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_inactive_anon", uint64(23), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_active_anon", uint64(24), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_inactive_file", uint64(25), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_active_file", uint64(26), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_unevictable", uint64(27), dockertags))
}
Example #5
0
func TestSystemStats_GenerateStats(t *testing.T) {
	var mps MockPS

	defer mps.AssertExpectations(t)

	var acc testutil.Accumulator

	cts := cpu.CPUTimesStat{
		CPU:       "cpu0",
		User:      3.1,
		System:    8.2,
		Idle:      80.1,
		Nice:      1.3,
		Iowait:    0.2,
		Irq:       0.1,
		Softirq:   0.11,
		Steal:     0.0511,
		Guest:     8.1,
		GuestNice: 0.324,
	}

	cts2 := cpu.CPUTimesStat{
		CPU:       "cpu0",
		User:      11.4,     // increased by 8.3
		System:    10.9,     // increased by 2.7
		Idle:      158.8699, // increased by 78.7699 (for total increase of 100)
		Nice:      2.5,      // increased by 1.2
		Iowait:    0.7,      // increased by 0.5
		Irq:       1.2,      // increased by 1.1
		Softirq:   0.31,     // increased by 0.2
		Steal:     0.2812,   // increased by 0.0001
		Guest:     12.9,     // increased by 4.8
		GuestNice: 2.524,    // increased by 2.2
	}

	mps.On("CPUTimes").Return([]cpu.CPUTimesStat{cts}, nil)

	du := []*disk.DiskUsageStat{
		{
			Path:        "/",
			Fstype:      "ext4",
			Total:       128,
			Free:        23,
			InodesTotal: 1234,
			InodesFree:  234,
		},
		{
			Path:        "/home",
			Fstype:      "ext4",
			Total:       256,
			Free:        46,
			InodesTotal: 2468,
			InodesFree:  468,
		},
	}

	mps.On("DiskUsage").Return(du, nil)

	diskio := disk.DiskIOCountersStat{
		ReadCount:    888,
		WriteCount:   5341,
		ReadBytes:    100000,
		WriteBytes:   200000,
		ReadTime:     7123,
		WriteTime:    9087,
		Name:         "sda1",
		IoTime:       123552,
		SerialNumber: "ab-123-ad",
	}

	mps.On("DiskIO").Return(map[string]disk.DiskIOCountersStat{"sda1": diskio}, nil)

	netio := net.NetIOCountersStat{
		Name:        "eth0",
		BytesSent:   1123,
		BytesRecv:   8734422,
		PacketsSent: 781,
		PacketsRecv: 23456,
		Errin:       832,
		Errout:      8,
		Dropin:      7,
		Dropout:     1,
	}

	mps.On("NetIO").Return([]net.NetIOCountersStat{netio}, nil)

	vms := &mem.VirtualMemoryStat{
		Total:     12400,
		Available: 7600,
		Used:      5000,
		Free:      1235,
		// Active:      8134,
		// Inactive:    1124,
		// Buffers:     771,
		// Cached:      4312,
		// Wired:       134,
		// Shared:      2142,
	}

	mps.On("VMStat").Return(vms, nil)

	sms := &mem.SwapMemoryStat{
		Total:       8123,
		Used:        1232,
		Free:        6412,
		UsedPercent: 12.2,
		Sin:         7,
		Sout:        830,
	}

	mps.On("SwapStat").Return(sms, nil)

	netstats := []net.NetConnectionStat{
		net.NetConnectionStat{
			Type: syscall.SOCK_DGRAM,
		},
		net.NetConnectionStat{
			Status: "ESTABLISHED",
		},
		net.NetConnectionStat{
			Status: "ESTABLISHED",
		},
		net.NetConnectionStat{
			Status: "CLOSE",
		},
	}

	mps.On("NetConnections").Return(netstats, nil)

	cs := NewCPUStats(&mps)

	cputags := map[string]string{
		"cpu": "cpu0",
	}

	preCPUPoints := len(acc.Points)
	err := cs.Gather(&acc)
	require.NoError(t, err)
	numCPUPoints := len(acc.Points) - preCPUPoints

	expectedCPUPoints := 10
	assert.Equal(t, expectedCPUPoints, numCPUPoints)

	// Computed values are checked with delta > 0 becasue of floating point arithmatic
	// imprecision
	assertContainsTaggedFloat(t, acc, "time_user", 3.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_system", 8.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_idle", 80.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_nice", 1.3, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_iowait", 0.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_irq", 0.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_softirq", 0.11, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_steal", 0.0511, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_guest", 8.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_guest_nice", 0.324, 0, cputags)

	mps2 := MockPS{}
	mps2.On("CPUTimes").Return([]cpu.CPUTimesStat{cts2}, nil)
	cs.ps = &mps2

	// Should have added cpu percentages too
	err = cs.Gather(&acc)
	require.NoError(t, err)

	numCPUPoints = len(acc.Points) - (preCPUPoints + numCPUPoints)
	expectedCPUPoints = 20
	assert.Equal(t, expectedCPUPoints, numCPUPoints)

	assertContainsTaggedFloat(t, acc, "time_user", 11.4, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_system", 10.9, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_idle", 158.8699, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_nice", 2.5, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_iowait", 0.7, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_irq", 1.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_softirq", 0.31, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_steal", 0.2812, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_guest", 12.9, 0, cputags)
	assertContainsTaggedFloat(t, acc, "time_guest_nice", 2.524, 0, cputags)

	assertContainsTaggedFloat(t, acc, "usage_user", 8.3, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_system", 2.7, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_idle", 78.7699, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_nice", 1.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_iowait", 0.5, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_irq", 1.1, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_softirq", 0.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_steal", 0.2301, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_guest", 4.8, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "usage_guest_nice", 2.2, 0.0005, cputags)

	preDiskPoints := len(acc.Points)

	err = (&DiskStats{ps: &mps}).Gather(&acc)
	require.NoError(t, err)

	numDiskPoints := len(acc.Points) - preDiskPoints
	expectedAllDiskPoints := 12
	assert.Equal(t, expectedAllDiskPoints, numDiskPoints)

	tags1 := map[string]string{
		"path":   "/",
		"fstype": "ext4",
	}
	tags2 := map[string]string{
		"path":   "/home",
		"fstype": "ext4",
	}

	assert.True(t, acc.CheckTaggedValue("total", uint64(128), tags1))
	assert.True(t, acc.CheckTaggedValue("used", uint64(105), tags1))
	assert.True(t, acc.CheckTaggedValue("free", uint64(23), tags1))
	assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(1234), tags1))
	assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(234), tags1))
	assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(1000), tags1))
	assert.True(t, acc.CheckTaggedValue("total", uint64(256), tags2))
	assert.True(t, acc.CheckTaggedValue("used", uint64(210), tags2))
	assert.True(t, acc.CheckTaggedValue("free", uint64(46), tags2))
	assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(2468), tags2))
	assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(468), tags2))
	assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(2000), tags2))

	// We expect 6 more DiskPoints to show up with an explicit match on "/"
	// and /home not matching the /dev in Mountpoints
	err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/dev"}}).Gather(&acc)
	assert.Equal(t, preDiskPoints+expectedAllDiskPoints+6, len(acc.Points))

	// We should see all the diskpoints as Mountpoints includes both
	// / and /home
	err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/home"}}).Gather(&acc)
	assert.Equal(t, preDiskPoints+2*expectedAllDiskPoints+6, len(acc.Points))

	err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc)
	require.NoError(t, err)

	ntags := map[string]string{
		"interface": "eth0",
	}

	assert.NoError(t, acc.ValidateTaggedValue("bytes_sent", uint64(1123), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("bytes_recv", uint64(8734422), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("packets_sent", uint64(781), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("packets_recv", uint64(23456), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("err_in", uint64(832), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("err_out", uint64(8), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("drop_in", uint64(7), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("drop_out", uint64(1), ntags))

	err = (&DiskIOStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	dtags := map[string]string{
		"name":   "sda1",
		"serial": "ab-123-ad",
	}

	assert.True(t, acc.CheckTaggedValue("reads", uint64(888), dtags))
	assert.True(t, acc.CheckTaggedValue("writes", uint64(5341), dtags))
	assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(100000), dtags))
	assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(200000), dtags))
	assert.True(t, acc.CheckTaggedValue("read_time", uint64(7123), dtags))
	assert.True(t, acc.CheckTaggedValue("write_time", uint64(9087), dtags))
	assert.True(t, acc.CheckTaggedValue("io_time", uint64(123552), dtags))

	err = (&MemStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	vmtags := map[string]string(nil)

	assert.True(t, acc.CheckTaggedValue("total", uint64(12400), vmtags))
	assert.True(t, acc.CheckTaggedValue("available", uint64(7600), vmtags))
	assert.True(t, acc.CheckTaggedValue("used", uint64(5000), vmtags))
	assert.True(t, acc.CheckTaggedValue("available_percent",
		float64(7600)/float64(12400)*100,
		vmtags))
	assert.True(t, acc.CheckTaggedValue("used_percent",
		float64(5000)/float64(12400)*100,
		vmtags))
	assert.True(t, acc.CheckTaggedValue("free", uint64(1235), vmtags))

	acc.Points = nil

	err = (&SwapStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	swaptags := map[string]string(nil)

	assert.NoError(t, acc.ValidateTaggedValue("total", uint64(8123), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("used", uint64(1232), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("used_percent", float64(12.2), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("free", uint64(6412), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("in", uint64(7), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("out", uint64(830), swaptags))

	acc.Points = nil

	err = (&NetStats{&mps}).Gather(&acc)
	require.NoError(t, err)
	netstattags := map[string]string(nil)

	assert.NoError(t, acc.ValidateTaggedValue("tcp_established", 2, netstattags))
	assert.NoError(t, acc.ValidateTaggedValue("tcp_close", 1, netstattags))
	assert.NoError(t, acc.ValidateTaggedValue("udp_socket", 1, netstattags))

}
Example #6
0
func TestZfsGeneratesMetrics(t *testing.T) {
	err := os.MkdirAll(testKstatPath, 0755)
	require.NoError(t, err)

	err = os.MkdirAll(testKstatPath+"/HOME", 0755)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/HOME/io", []byte(""), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/arcstats", []byte(arcstatsContents), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/zfetchstats", []byte(zfetchstatsContents), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/vdev_cache_stats", []byte(vdev_cache_statsContents), 0644)
	require.NoError(t, err)

	intMetrics := getKstatMetricsAll()

	var acc testutil.Accumulator

	//one pool, all metrics
	tags := map[string]string{
		"pools": "HOME",
	}

	z := &Zfs{KstatPath: testKstatPath}
	err = z.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range intMetrics {
		assert.True(t, acc.HasIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	//two pools, all metrics
	err = os.MkdirAll(testKstatPath+"/STORAGE", 0755)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/STORAGE/io", []byte(""), 0644)
	require.NoError(t, err)

	tags = map[string]string{
		"pools": "HOME::STORAGE",
	}

	z = &Zfs{KstatPath: testKstatPath}
	acc = testutil.Accumulator{}
	err = z.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range intMetrics {
		assert.True(t, acc.HasIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	intMetrics = getKstatMetricsArcOnly()

	//two pools, one metric
	z = &Zfs{KstatPath: testKstatPath, KstatMetrics: []string{"arcstats"}}
	acc = testutil.Accumulator{}
	err = z.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range intMetrics {
		assert.True(t, acc.HasIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	err = os.RemoveAll(os.TempDir() + "/telegraf")
	require.NoError(t, err)
}
Example #7
0
func TestDockerStats_GenerateStats(t *testing.T) {
	var mps MockPS
	var acc testutil.Accumulator

	ds := &DockerContainerStat{
		Name: "blah",
		CPU: &cpu.CPUTimesStat{
			CPU:       "all",
			User:      3.1,
			System:    8.2,
			Idle:      80.1,
			Nice:      1.3,
			Iowait:    0.2,
			Irq:       0.1,
			Softirq:   0.11,
			Steal:     0.0001,
			Guest:     8.1,
			GuestNice: 0.324,
		},
		Mem: &docker.CgroupMemStat{
			ContainerID:             "blah",
			Cache:                   1,
			RSS:                     2,
			RSSHuge:                 3,
			MappedFile:              4,
			Pgpgin:                  5,
			Pgpgout:                 6,
			Pgfault:                 7,
			Pgmajfault:              8,
			InactiveAnon:            9,
			ActiveAnon:              10,
			InactiveFile:            11,
			ActiveFile:              12,
			Unevictable:             13,
			HierarchicalMemoryLimit: 14,
			TotalCache:              15,
			TotalRSS:                16,
			TotalRSSHuge:            17,
			TotalMappedFile:         18,
			TotalPgpgIn:             19,
			TotalPgpgOut:            20,
			TotalPgFault:            21,
			TotalPgMajFault:         22,
			TotalInactiveAnon:       23,
			TotalActiveAnon:         24,
			TotalInactiveFile:       25,
			TotalActiveFile:         26,
			TotalUnevictable:        27,
		},
	}

	mps.On("DockerStat").Return([]*DockerContainerStat{ds}, nil)

	err := (&DockerStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	dockertags := map[string]string{
		"name":    "blah",
		"id":      "",
		"command": "",
	}

	assert.True(t, acc.CheckTaggedValue("user", 3.1, dockertags))
	assert.True(t, acc.CheckTaggedValue("system", 8.2, dockertags))
	assert.True(t, acc.CheckTaggedValue("idle", 80.1, dockertags))
	assert.True(t, acc.CheckTaggedValue("nice", 1.3, dockertags))
	assert.True(t, acc.CheckTaggedValue("iowait", 0.2, dockertags))
	assert.True(t, acc.CheckTaggedValue("irq", 0.1, dockertags))
	assert.True(t, acc.CheckTaggedValue("softirq", 0.11, dockertags))
	assert.True(t, acc.CheckTaggedValue("steal", 0.0001, dockertags))
	assert.True(t, acc.CheckTaggedValue("guest", 8.1, dockertags))
	assert.True(t, acc.CheckTaggedValue("guest_nice", 0.324, dockertags))

	assert.True(t, acc.CheckTaggedValue("cache", uint64(1), dockertags))
	assert.True(t, acc.CheckTaggedValue("rss", uint64(2), dockertags))
	assert.True(t, acc.CheckTaggedValue("rss_huge", uint64(3), dockertags))
	assert.True(t, acc.CheckTaggedValue("mapped_file", uint64(4), dockertags))
	assert.True(t, acc.CheckTaggedValue("swap_in", uint64(5), dockertags))
	assert.True(t, acc.CheckTaggedValue("swap_out", uint64(6), dockertags))
	assert.True(t, acc.CheckTaggedValue("page_fault", uint64(7), dockertags))
	assert.True(t, acc.CheckTaggedValue("page_major_fault", uint64(8), dockertags))
	assert.True(t, acc.CheckTaggedValue("inactive_anon", uint64(9), dockertags))
	assert.True(t, acc.CheckTaggedValue("active_anon", uint64(10), dockertags))
	assert.True(t, acc.CheckTaggedValue("inactive_file", uint64(11), dockertags))
	assert.True(t, acc.CheckTaggedValue("active_file", uint64(12), dockertags))
	assert.True(t, acc.CheckTaggedValue("unevictable", uint64(13), dockertags))
	assert.True(t, acc.CheckTaggedValue("memory_limit", uint64(14), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_cache", uint64(15), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_rss", uint64(16), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_rss_huge", uint64(17), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_mapped_file", uint64(18), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_swap_in", uint64(19), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_swap_out", uint64(20), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_page_fault", uint64(21), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_page_major_fault", uint64(22), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_inactive_anon", uint64(23), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_active_anon", uint64(24), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_inactive_file", uint64(25), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_active_file", uint64(26), dockertags))
	assert.True(t, acc.CheckTaggedValue("total_unevictable", uint64(27), dockertags))
}
Example #8
0
func TestSystemStats_GenerateStats(t *testing.T) {
	var mps MockPS

	defer mps.AssertExpectations(t)

	var acc testutil.Accumulator

	cts := cpu.CPUTimesStat{
		CPU:       "cpu0",
		User:      3.1,
		System:    8.2,
		Idle:      80.1,
		Nice:      1.3,
		Iowait:    0.2,
		Irq:       0.1,
		Softirq:   0.11,
		Steal:     0.0001,
		Guest:     8.1,
		GuestNice: 0.324,
		Stolen:    0.051,
	}

	cts2 := cpu.CPUTimesStat{
		CPU:       "cpu0",
		User:      11.4,     // increased by 8.3
		System:    10.9,     // increased by 2.7
		Idle:      158.8699, // increased by 78.7699 (for total increase of 100)
		Nice:      2.5,      // increased by 1.2
		Iowait:    0.7,      // increased by 0.5
		Irq:       1.2,      // increased by 1.1
		Softirq:   0.31,     // increased by 0.2
		Steal:     0.0002,   // increased by 0.0001
		Guest:     12.9,     // increased by 4.8
		GuestNice: 2.524,    // increased by 2.2
		Stolen:    0.281,    // increased by 0.23
	}

	mps.On("CPUTimes").Return([]cpu.CPUTimesStat{cts}, nil)

	du := &disk.DiskUsageStat{
		Path:        "/",
		Fstype:      "ext4",
		Total:       128,
		Free:        23,
		InodesTotal: 1234,
		InodesFree:  234,
	}

	mps.On("DiskUsage").Return([]*disk.DiskUsageStat{du}, nil)

	diskio := disk.DiskIOCountersStat{
		ReadCount:    888,
		WriteCount:   5341,
		ReadBytes:    100000,
		WriteBytes:   200000,
		ReadTime:     7123,
		WriteTime:    9087,
		Name:         "sda1",
		IoTime:       123552,
		SerialNumber: "ab-123-ad",
	}

	mps.On("DiskIO").Return(map[string]disk.DiskIOCountersStat{"sda1": diskio}, nil)

	netio := net.NetIOCountersStat{
		Name:        "eth0",
		BytesSent:   1123,
		BytesRecv:   8734422,
		PacketsSent: 781,
		PacketsRecv: 23456,
		Errin:       832,
		Errout:      8,
		Dropin:      7,
		Dropout:     1,
	}

	mps.On("NetIO").Return([]net.NetIOCountersStat{netio}, nil)

	vms := &mem.VirtualMemoryStat{
		Total:       12400,
		Available:   7600,
		Used:        5000,
		UsedPercent: 47.1,
		Free:        1235,
		Active:      8134,
		Inactive:    1124,
		Buffers:     771,
		Cached:      4312,
		Wired:       134,
		Shared:      2142,
	}

	mps.On("VMStat").Return(vms, nil)

	sms := &mem.SwapMemoryStat{
		Total:       8123,
		Used:        1232,
		Free:        6412,
		UsedPercent: 12.2,
		Sin:         7,
		Sout:        830,
	}

	mps.On("SwapStat").Return(sms, nil)

	cs := NewCPUStats(&mps)

	cputags := map[string]string{
		"cpu": "cpu0",
	}

	preCPUPoints := len(acc.Points)
	err := cs.Gather(&acc)
	require.NoError(t, err)
	numCPUPoints := len(acc.Points) - preCPUPoints

	expectedCPUPoints := 12
	assert.Equal(t, numCPUPoints, expectedCPUPoints)

	// Computed values are checked with delta > 0 becasue of floating point arithmatic
	// imprecision
	assertContainsTaggedFloat(t, acc, "user", 3.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "system", 8.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "idle", 80.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "nice", 1.3, 0, cputags)
	assertContainsTaggedFloat(t, acc, "iowait", 0.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "irq", 0.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "softirq", 0.11, 0, cputags)
	assertContainsTaggedFloat(t, acc, "steal", 0.0001, 0, cputags)
	assertContainsTaggedFloat(t, acc, "guest", 8.1, 0, cputags)
	assertContainsTaggedFloat(t, acc, "guestNice", 0.324, 0, cputags)
	assertContainsTaggedFloat(t, acc, "stolen", 0.051, 0, cputags)
	assertContainsTaggedFloat(t, acc, "busy", 21.4851, 0.0005, cputags)

	mps2 := MockPS{}
	mps2.On("CPUTimes").Return([]cpu.CPUTimesStat{cts2}, nil)
	cs.ps = &mps2

	// Should have added cpu percentages too
	err = cs.Gather(&acc)
	require.NoError(t, err)

	numCPUPoints = len(acc.Points) - (preCPUPoints + numCPUPoints)
	expectedCPUPoints = 24
	assert.Equal(t, numCPUPoints, expectedCPUPoints)

	assertContainsTaggedFloat(t, acc, "user", 11.4, 0, cputags)
	assertContainsTaggedFloat(t, acc, "system", 10.9, 0, cputags)
	assertContainsTaggedFloat(t, acc, "idle", 158.8699, 0, cputags)
	assertContainsTaggedFloat(t, acc, "nice", 2.5, 0, cputags)
	assertContainsTaggedFloat(t, acc, "iowait", 0.7, 0, cputags)
	assertContainsTaggedFloat(t, acc, "irq", 1.2, 0, cputags)
	assertContainsTaggedFloat(t, acc, "softirq", 0.31, 0, cputags)
	assertContainsTaggedFloat(t, acc, "steal", 0.0002, 0, cputags)
	assertContainsTaggedFloat(t, acc, "guest", 12.9, 0, cputags)
	assertContainsTaggedFloat(t, acc, "guestNice", 2.524, 0, cputags)
	assertContainsTaggedFloat(t, acc, "stolen", 0.281, 0, cputags)
	assertContainsTaggedFloat(t, acc, "busy", 42.7152, 0.0005, cputags)

	assertContainsTaggedFloat(t, acc, "percentageUser", 8.3, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageSystem", 2.7, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageIdle", 78.7699, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageNice", 1.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageIowait", 0.5, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageIrq", 1.1, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageSoftirq", 0.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageSteal", 0.0001, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageGuest", 4.8, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageGuestNice", 2.2, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageStolen", 0.23, 0.0005, cputags)
	assertContainsTaggedFloat(t, acc, "percentageBusy", 21.2301, 0.0005, cputags)

	err = (&DiskStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	tags := map[string]string{
		"path":   "/",
		"fstype": "ext4",
	}

	assert.True(t, acc.CheckTaggedValue("total", uint64(128), tags))
	assert.True(t, acc.CheckTaggedValue("used", uint64(105), tags))
	assert.True(t, acc.CheckTaggedValue("free", uint64(23), tags))
	assert.True(t, acc.CheckTaggedValue("inodes_total", uint64(1234), tags))
	assert.True(t, acc.CheckTaggedValue("inodes_free", uint64(234), tags))
	assert.True(t, acc.CheckTaggedValue("inodes_used", uint64(1000), tags))

	err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc)
	require.NoError(t, err)

	ntags := map[string]string{
		"interface": "eth0",
	}

	assert.NoError(t, acc.ValidateTaggedValue("bytes_sent", uint64(1123), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("bytes_recv", uint64(8734422), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("packets_sent", uint64(781), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("packets_recv", uint64(23456), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("err_in", uint64(832), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("err_out", uint64(8), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("drop_in", uint64(7), ntags))
	assert.NoError(t, acc.ValidateTaggedValue("drop_out", uint64(1), ntags))

	err = (&DiskIOStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	dtags := map[string]string{
		"name":   "sda1",
		"serial": "ab-123-ad",
	}

	assert.True(t, acc.CheckTaggedValue("reads", uint64(888), dtags))
	assert.True(t, acc.CheckTaggedValue("writes", uint64(5341), dtags))
	assert.True(t, acc.CheckTaggedValue("read_bytes", uint64(100000), dtags))
	assert.True(t, acc.CheckTaggedValue("write_bytes", uint64(200000), dtags))
	assert.True(t, acc.CheckTaggedValue("read_time", uint64(7123), dtags))
	assert.True(t, acc.CheckTaggedValue("write_time", uint64(9087), dtags))
	assert.True(t, acc.CheckTaggedValue("io_time", uint64(123552), dtags))

	err = (&MemStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	vmtags := map[string]string(nil)

	assert.True(t, acc.CheckTaggedValue("total", uint64(12400), vmtags))
	assert.True(t, acc.CheckTaggedValue("available", uint64(7600), vmtags))
	assert.True(t, acc.CheckTaggedValue("used", uint64(5000), vmtags))
	assert.True(t, acc.CheckTaggedValue("used_perc", float64(47.1), vmtags))
	assert.True(t, acc.CheckTaggedValue("free", uint64(1235), vmtags))
	assert.True(t, acc.CheckTaggedValue("active", uint64(8134), vmtags))
	assert.True(t, acc.CheckTaggedValue("inactive", uint64(1124), vmtags))
	assert.True(t, acc.CheckTaggedValue("buffers", uint64(771), vmtags))
	assert.True(t, acc.CheckTaggedValue("cached", uint64(4312), vmtags))
	assert.True(t, acc.CheckTaggedValue("wired", uint64(134), vmtags))
	assert.True(t, acc.CheckTaggedValue("shared", uint64(2142), vmtags))

	acc.Points = nil

	err = (&SwapStats{&mps}).Gather(&acc)
	require.NoError(t, err)

	swaptags := map[string]string(nil)

	assert.NoError(t, acc.ValidateTaggedValue("total", uint64(8123), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("used", uint64(1232), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("used_perc", float64(12.2), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("free", uint64(6412), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("in", uint64(7), swaptags))
	assert.NoError(t, acc.ValidateTaggedValue("out", uint64(830), swaptags))
}
Example #9
0
func TestLustre2GeneratesMetrics(t *testing.T) {

	tempdir := os.TempDir() + "/telegraf/proc/fs/lustre/"
	ost_name := "OST0001"

	mdtdir := tempdir + "/mdt/"
	err := os.MkdirAll(mdtdir+"/"+ost_name, 0755)
	require.NoError(t, err)

	osddir := tempdir + "/osd-ldiskfs/"
	err = os.MkdirAll(osddir+"/"+ost_name, 0755)
	require.NoError(t, err)

	obddir := tempdir + "/obdfilter/"
	err = os.MkdirAll(obddir+"/"+ost_name, 0755)
	require.NoError(t, err)

	err = ioutil.WriteFile(mdtdir+"/"+ost_name+"/md_stats", []byte(mdtProcContents), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(osddir+"/"+ost_name+"/stats", []byte(osdldiskfsProcContents), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(obddir+"/"+ost_name+"/stats", []byte(obdfilterProcContents), 0644)
	require.NoError(t, err)

	m := &Lustre2{
		Ost_procfiles: []string{obddir + "/*/stats", osddir + "/*/stats"},
		Mds_procfiles: []string{mdtdir + "/*/md_stats"},
	}

	var acc testutil.Accumulator

	err = m.Gather(&acc)
	require.NoError(t, err)

	tags := map[string]string{
		"name": ost_name,
	}

	intMetrics := []*metrics{
		{
			name:  "write_bytes",
			value: 15201500833981,
		},
		{
			name:  "read_bytes",
			value: 78026117632000,
		},
		{
			name:  "write_calls",
			value: 71893382,
		},
		{
			name:  "read_calls",
			value: 203238095,
		},
		{
			name:  "cache_hit",
			value: 7393729777,
		},
		{
			name:  "cache_access",
			value: 19047063027,
		},
		{
			name:  "cache_miss",
			value: 11653333250,
		},
	}

	for _, metric := range intMetrics {
		assert.True(t, acc.HasUIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	err = os.RemoveAll(os.TempDir() + "/telegraf")
	require.NoError(t, err)
}
Example #10
0
func TestBcacheGeneratesMetrics(t *testing.T) {
	err := os.MkdirAll(testBcacheUuidPath, 0755)
	require.NoError(t, err)

	err = os.MkdirAll(testBcacheDevPath, 0755)
	require.NoError(t, err)

	err = os.MkdirAll(testBcacheBackingDevPath+"/bcache", 0755)
	require.NoError(t, err)

	err = os.Symlink(testBcacheBackingDevPath+"/bcache", testBcacheUuidPath+"/bdev0")
	require.NoError(t, err)

	err = os.Symlink(testBcacheDevPath, testBcacheUuidPath+"/bdev0/dev")
	require.NoError(t, err)

	err = os.MkdirAll(testBcacheUuidPath+"/bdev0/stats_total", 0755)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/dirty_data", []byte(dirty_data), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/bypassed", []byte(bypassed), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_bypass_hits", []byte(cache_bypass_hits), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_bypass_misses", []byte(cache_bypass_misses), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_hit_ratio", []byte(cache_hit_ratio), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_hits", []byte(cache_hits), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_miss_collisions", []byte(cache_miss_collisions), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_misses", []byte(cache_misses), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testBcacheUuidPath+"/bdev0/stats_total/cache_readaheads", []byte(cache_readaheads), 0644)
	require.NoError(t, err)

	intMetrics := []*metrics{
		{
			name:  "dirty_data",
			value: 1610612736,
		},
		{
			name:  "bypassed",
			value: 5167704440832,
		},
		{
			name:  "cache_bypass_hits",
			value: 146155333,
		},
		{
			name:  "cache_bypass_misses",
			value: 0,
		},
		{
			name:  "cache_hit_ratio",
			value: 90,
		},
		{
			name:  "cache_hits",
			value: 511469583,
		},
		{
			name:  "cache_miss_collisions",
			value: 157567,
		},
		{
			name:  "cache_misses",
			value: 50616331,
		},
		{
			name:  "cache_readaheads",
			value: 2,
		},
	}

	tags := map[string]string{
		"backing_dev": "md10",
		"bcache_dev":  "bcache0",
	}

	var acc testutil.Accumulator

	//all devs
	b := &Bcache{BcachePath: testBcachePath}

	err = b.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range intMetrics {
		assert.True(t, acc.HasUIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	//one exist dev
	b = &Bcache{BcachePath: testBcachePath, BcacheDevs: []string{"bcache0"}}

	err = b.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range intMetrics {
		assert.True(t, acc.HasUIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	err = os.RemoveAll(os.TempDir() + "/telegraf")
	require.NoError(t, err)
}
Example #11
0
func TestZfsGeneratesMetrics(t *testing.T) {
	err := os.MkdirAll(testKstatPath, 0755)
	require.NoError(t, err)

	err = os.MkdirAll(testKstatPath+"/HOME", 0755)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/HOME/io", []byte(""), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/arcstats", []byte(arcstatsContents), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/zfetchstats", []byte(zfetchstatsContents), 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/vdev_cache_stats", []byte(vdev_cache_statsContents), 0644)
	require.NoError(t, err)

	intMetrics := []*metrics{
		{
			name:  "arcstats_hits",
			value: 5968846374,
		},
		{
			name:  "arcstats_misses",
			value: 1659178751,
		},
		{
			name:  "arcstats_demand_data_hits",
			value: 4860247322,
		},
		{
			name:  "arcstats_demand_data_misses",
			value: 501499535,
		},
		{
			name:  "arcstats_demand_metadata_hits",
			value: 708608325,
		},
		{
			name:  "arcstats_demand_metadata_misses",
			value: 156591375,
		},
		{
			name:  "arcstats_prefetch_data_hits",
			value: 367047144,
		},
		{
			name:  "arcstats_prefetch_data_misses",
			value: 974529898,
		},
		{
			name:  "arcstats_prefetch_metadata_hits",
			value: 32943583,
		},
		{
			name:  "arcstats_prefetch_metadata_misses",
			value: 26557943,
		},
		{
			name:  "arcstats_mru_hits",
			value: 301176811,
		},
		{
			name:  "arcstats_mru_ghost_hits",
			value: 47066067,
		},
		{
			name:  "arcstats_mfu_hits",
			value: 5520612438,
		},
		{
			name:  "arcstats_mfu_ghost_hits",
			value: 45784009,
		},
		{
			name:  "arcstats_deleted",
			value: 1718937704,
		},
		{
			name:  "arcstats_recycle_miss",
			value: 481222994,
		},
		{
			name:  "arcstats_mutex_miss",
			value: 20575623,
		},
		{
			name:  "arcstats_evict_skip",
			value: 14655903906543,
		},
		{
			name:  "arcstats_evict_l2_cached",
			value: 145310202998272,
		},
		{
			name:  "arcstats_evict_l2_eligible",
			value: 16345402777088,
		},
		{
			name:  "arcstats_evict_l2_ineligible",
			value: 7437226893312,
		},
		{
			name:  "arcstats_hash_elements",
			value: 36617980,
		},
		{
			name:  "arcstats_hash_elements_max",
			value: 36618318,
		},
		{
			name:  "arcstats_hash_collisions",
			value: 554145157,
		},
		{
			name:  "arcstats_hash_chains",
			value: 4187651,
		},
		{
			name:  "arcstats_hash_chain_max",
			value: 26,
		},
		{
			name:  "arcstats_p",
			value: 13963222064,
		},
		{
			name:  "arcstats_c",
			value: 16381258376,
		},
		{
			name:  "arcstats_c_min",
			value: 4194304,
		},
		{
			name:  "arcstats_c_max",
			value: 16884125696,
		},
		{
			name:  "arcstats_size",
			value: 16319887096,
		},
		{
			name:  "arcstats_hdr_size",
			value: 42567864,
		},
		{
			name:  "arcstats_data_size",
			value: 60066304,
		},
		{
			name:  "arcstats_meta_size",
			value: 1701534208,
		},
		{
			name:  "arcstats_other_size",
			value: 1661543168,
		},
		{
			name:  "arcstats_anon_size",
			value: 94720,
		},
		{
			name:  "arcstats_anon_evict_data",
			value: 0,
		},
		{
			name:  "arcstats_anon_evict_metadata",
			value: 0,
		},
		{
			name:  "arcstats_mru_size",
			value: 973099008,
		},
		{
			name:  "arcstats_mru_evict_data",
			value: 9175040,
		},
		{
			name:  "arcstats_mru_evict_metadata",
			value: 32768,
		},
		{
			name:  "arcstats_mru_ghost_size",
			value: 32768,
		},
		{
			name:  "arcstats_mru_ghost_evict_data",
			value: 0,
		},
		{
			name:  "arcstats_mru_ghost_evict_metadata",
			value: 32768,
		},
		{
			name:  "arcstats_mfu_size",
			value: 788406784,
		},
		{
			name:  "arcstats_mfu_evict_data",
			value: 50881024,
		},
		{
			name:  "arcstats_mfu_evict_metadata",
			value: 81920,
		},
		{
			name:  "arcstats_mfu_ghost_size",
			value: 0,
		},
		{
			name:  "arcstats_mfu_ghost_evict_data",
			value: 0,
		},
		{
			name:  "arcstats_mfu_ghost_evict_metadata",
			value: 0,
		},
		{
			name:  "arcstats_l2_hits",
			value: 573868618,
		},
		{
			name:  "arcstats_l2_misses",
			value: 1085309718,
		},
		{
			name:  "arcstats_l2_feeds",
			value: 12182087,
		},
		{
			name:  "arcstats_l2_rw_clash",
			value: 9610,
		},
		{
			name:  "arcstats_l2_read_bytes",
			value: 32695938336768,
		},
		{
			name:  "arcstats_l2_write_bytes",
			value: 2826774778880,
		},
		{
			name:  "arcstats_l2_writes_sent",
			value: 4267687,
		},
		{
			name:  "arcstats_l2_writes_done",
			value: 4267687,
		},
		{
			name:  "arcstats_l2_writes_error",
			value: 0,
		},
		{
			name:  "arcstats_l2_writes_hdr_miss",
			value: 164,
		},
		{
			name:  "arcstats_l2_evict_lock_retry",
			value: 5,
		},
		{
			name:  "arcstats_l2_evict_reading",
			value: 0,
		},
		{
			name:  "arcstats_l2_free_on_write",
			value: 1606914,
		},
		{
			name:  "arcstats_l2_cdata_free_on_write",
			value: 1775,
		},
		{
			name:  "arcstats_l2_abort_lowmem",
			value: 83462,
		},
		{
			name:  "arcstats_l2_cksum_bad",
			value: 393860640,
		},
		{
			name:  "arcstats_l2_io_error",
			value: 53881460,
		},
		{
			name:  "arcstats_l2_size",
			value: 2471466648576,
		},
		{
			name:  "arcstats_l2_asize",
			value: 2461690072064,
		},
		{
			name:  "arcstats_l2_hdr_size",
			value: 12854175552,
		},
		{
			name:  "arcstats_l2_compress_successes",
			value: 12184849,
		},
		{
			name:  "arcstats_l2_compress_zeros",
			value: 0,
		},
		{
			name:  "arcstats_l2_compress_failures",
			value: 0,
		},
		{
			name:  "arcstats_memory_throttle_count",
			value: 0,
		},
		{
			name:  "arcstats_duplicate_buffers",
			value: 0,
		},
		{
			name:  "arcstats_duplicate_buffers_size",
			value: 0,
		},
		{
			name:  "arcstats_duplicate_reads",
			value: 0,
		},
		{
			name:  "arcstats_memory_direct_count",
			value: 5159942,
		},
		{
			name:  "arcstats_memory_indirect_count",
			value: 3034640,
		},
		{
			name:  "arcstats_arc_no_grow",
			value: 0,
		},
		{
			name:  "arcstats_arc_tempreserve",
			value: 0,
		},
		{
			name:  "arcstats_arc_loaned_bytes",
			value: 0,
		},
		{
			name:  "arcstats_arc_prune",
			value: 114554259559,
		},
		{
			name:  "arcstats_arc_meta_used",
			value: 16259820792,
		},
		{
			name:  "arcstats_arc_meta_limit",
			value: 12663094272,
		},
		{
			name:  "arcstats_arc_meta_max",
			value: 18327165696,
		},
		{
			name:  "zfetchstats_hits",
			value: 7812959060,
		},
		{
			name:  "zfetchstats_misses",
			value: 4154484207,
		},
		{
			name:  "zfetchstats_colinear_hits",
			value: 1366368,
		},
		{
			name:  "zfetchstats_colinear_misses",
			value: 4153117839,
		},
		{
			name:  "zfetchstats_stride_hits",
			value: 7309776732,
		},
		{
			name:  "zfetchstats_stride_misses",
			value: 222766182,
		},
		{
			name:  "zfetchstats_reclaim_successes",
			value: 107788388,
		},
		{
			name:  "zfetchstats_reclaim_failures",
			value: 4045329451,
		},
		{
			name:  "zfetchstats_streams_resets",
			value: 20989756,
		},
		{
			name:  "zfetchstats_streams_noresets",
			value: 503182328,
		},
		{
			name:  "zfetchstats_bogus_streams",
			value: 0,
		},
		{
			name:  "vdev_cache_stats_delegations",
			value: 0,
		},
		{
			name:  "vdev_cache_stats_hits",
			value: 0,
		},
		{
			name:  "vdev_cache_stats_misses",
			value: 0,
		},
	}

	var acc testutil.Accumulator

	//one pool, all metrics
	tags := map[string]string{
		"pools": "HOME",
	}

	z := &Zfs{KstatPath: testKstatPath}
	err = z.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range intMetrics {
		fmt.Println(metric.name)
		assert.True(t, acc.HasIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	//two pools, all metrics
	err = os.MkdirAll(testKstatPath+"/STORAGE", 0755)
	require.NoError(t, err)

	err = ioutil.WriteFile(testKstatPath+"/STORAGE/io", []byte(""), 0644)
	require.NoError(t, err)

	tags = map[string]string{
		"pools": "HOME::STORAGE",
	}

	z = &Zfs{KstatPath: testKstatPath}
	err = z.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range intMetrics {
		assert.True(t, acc.HasIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	//two pools, one metric
	z = &Zfs{KstatPath: testKstatPath, KstatMetrics: []string{"arcstats"}}
	err = z.Gather(&acc)
	require.NoError(t, err)

	for _, metric := range intMetrics {
		assert.True(t, acc.HasIntValue(metric.name), metric.name)
		assert.True(t, acc.CheckTaggedValue(metric.name, metric.value, tags))
	}

	err = os.RemoveAll(os.TempDir() + "/telegraf")
	require.NoError(t, err)
}