예제 #1
0
func newSnmp(params map[string]string) (sources.ConnectorList, error) {
	community := params["community"]
	if community == "" {
		return nil, errors.New("parameter 'community' required for SNMP")
	}
	return sources.ConnectorList{snmpsource.NewConnector(community)}, nil
}
예제 #2
0
func TestMarkHostsActiveExclusively(t *testing.T) {
	alBuilder := NewApplicationListBuilder()
	alBuilder.Add(
		35, "AnApp", sources.ConnectorList{trisource.GetConnector()})
	alBuilder.Add(
		92,
		"AnotherApp",
		sources.ConnectorList{snmpsource.NewConnector("community")})
	appList := alBuilder.Build()
	appStatus := NewApplicationStatuses(
		appList,
		newStore(t, "TestMarkHostsActiveExclusively", 1, 100, 1.0, 10))
	appStatus.MarkHostsActiveExclusively(
		92.5,
		[]string{"host1", "host2", "host3"})
	activateEndpoints(appStatus.ActiveEndpointIds())
	astore := appStatus.Store()
	visitor := newActiveInactiveLists()
	astore.VisitAllEndpoints(visitor)
	assertDeepEqual(
		t,
		map[string]bool{
			"host1:35": true,
			"host1:92": true,
			"host2:35": true,
			"host2:92": true,
			"host3:35": true,
			"host3:92": true,
		},
		visitor.Active)
	assertDeepEqual(
		t,
		make(map[string]bool),
		visitor.Inactive)
	appStatus.MarkHostsActiveExclusively(61.7, []string{"host2", "host4"})
	activeEndpointIds, astore := appStatus.ActiveEndpointIds()
	assertValueEquals(t, 4, len(activeEndpointIds))
	activateEndpoints(activeEndpointIds, astore)
	visitor = newActiveInactiveLists()
	astore.VisitAllEndpoints(visitor)
	assertDeepEqual(
		t,
		map[string]bool{
			"host2:35": true,
			"host2:92": true,
			"host4:35": true,
			"host4:92": true,
		},
		visitor.Active)
	assertDeepEqual(
		t,
		map[string]bool{
			"host1:35": true,
			"host1:92": true,
			"host3:35": true,
			"host3:92": true,
		},
		visitor.Inactive)

	endpointId, aStore := appStatus.EndpointIdByHostAndName(
		"host3", "AnApp")
	assertValueEquals(t, "host3", endpointId.HostName())
	assertValueEquals(t, uint(35), endpointId.Port())

	// Trying to add to inactive endpoint should fail
	if _, err := aStore.AddBatch(endpointId, 9999.0, metrics.SimpleList(nil)); err != store.ErrInactive {
		t.Error("Adding to inactive endpoint should fail.")
	}

	stats := appStatus.All()
	ByHostAndName(stats)
	assertValueEquals(t, 8, len(stats))
	assertValueEquals(t, "host1", stats[0].EndpointId.HostName())
	assertValueEquals(
		t, "tricorder", stats[0].EndpointId.ConnectorName())
	assertValueEquals(t, "AnApp", stats[0].Name)
	assertValueEquals(t, false, stats[0].Active)

	assertValueEquals(t, "host1", stats[1].EndpointId.HostName())
	assertValueEquals(
		t, "snmp", stats[1].EndpointId.ConnectorName())
	assertValueEquals(t, "AnotherApp", stats[1].Name)
	assertValueEquals(t, false, stats[1].Active)

	assertValueEquals(t, "host2", stats[2].EndpointId.HostName())
	assertValueEquals(
		t, "tricorder", stats[2].EndpointId.ConnectorName())
	assertValueEquals(t, "AnApp", stats[2].Name)
	assertValueEquals(t, true, stats[2].Active)

	assertValueEquals(t, "host2", stats[3].EndpointId.HostName())
	assertValueEquals(
		t, "snmp", stats[3].EndpointId.ConnectorName())
	assertValueEquals(t, "AnotherApp", stats[3].Name)
	assertValueEquals(t, true, stats[3].Active)

	assertValueEquals(t, "host3", stats[4].EndpointId.HostName())
	assertValueEquals(
		t, "tricorder", stats[4].EndpointId.ConnectorName())
	assertValueEquals(t, "AnApp", stats[4].Name)
	assertValueEquals(t, false, stats[4].Active)

	assertValueEquals(t, "host3", stats[5].EndpointId.HostName())
	assertValueEquals(
		t, "snmp", stats[5].EndpointId.ConnectorName())
	assertValueEquals(t, "AnotherApp", stats[5].Name)
	assertValueEquals(t, false, stats[5].Active)

	assertValueEquals(t, "host4", stats[6].EndpointId.HostName())
	assertValueEquals(
		t, "tricorder", stats[6].EndpointId.ConnectorName())
	assertValueEquals(t, "AnApp", stats[6].Name)
	assertValueEquals(t, true, stats[6].Active)

	assertValueEquals(t, "host4", stats[7].EndpointId.HostName())
	assertValueEquals(
		t, "snmp", stats[7].EndpointId.ConnectorName())
	assertValueEquals(t, "AnotherApp", stats[7].Name)
	assertValueEquals(t, true, stats[7].Active)

	appStatus.MarkHostsActiveExclusively(61.7, []string{"host2", "host3", "host4"})

	endpointId, aStore = appStatus.EndpointIdByHostAndName(
		"host3", "AnApp")

	var noMetrics metrics.SimpleList

	// Trying to add to active endpoint should succeed
	if _, err := aStore.AddBatch(endpointId, 9999.0, noMetrics); err != nil {
		t.Error("Adding to active endpoint should succeed.")
	}

	stats = appStatus.All()
	ByHostAndName(stats)
	assertValueEquals(t, 8, len(stats))
	assertValueEquals(t, "host1", stats[0].EndpointId.HostName())
	assertValueEquals(
		t, "tricorder", stats[0].EndpointId.ConnectorName())
	assertValueEquals(t, "AnApp", stats[0].Name)
	assertValueEquals(t, false, stats[0].Active)

	assertValueEquals(t, "host1", stats[1].EndpointId.HostName())
	assertValueEquals(
		t, "snmp", stats[1].EndpointId.ConnectorName())
	assertValueEquals(t, "AnotherApp", stats[1].Name)
	assertValueEquals(t, false, stats[1].Active)

	assertValueEquals(t, "host2", stats[2].EndpointId.HostName())
	assertValueEquals(
		t, "tricorder", stats[2].EndpointId.ConnectorName())
	assertValueEquals(t, "AnApp", stats[2].Name)
	assertValueEquals(t, true, stats[2].Active)

	assertValueEquals(t, "host2", stats[3].EndpointId.HostName())
	assertValueEquals(
		t, "snmp", stats[3].EndpointId.ConnectorName())
	assertValueEquals(t, "AnotherApp", stats[3].Name)
	assertValueEquals(t, true, stats[3].Active)

	assertValueEquals(t, "host3", stats[4].EndpointId.HostName())
	assertValueEquals(
		t, "tricorder", stats[4].EndpointId.ConnectorName())
	assertValueEquals(t, "AnApp", stats[4].Name)
	assertValueEquals(t, true, stats[4].Active)

	assertValueEquals(t, "host3", stats[5].EndpointId.HostName())
	assertValueEquals(
		t, "snmp", stats[5].EndpointId.ConnectorName())
	assertValueEquals(t, "AnotherApp", stats[5].Name)
	assertValueEquals(t, true, stats[5].Active)

	assertValueEquals(t, "host4", stats[6].EndpointId.HostName())
	assertValueEquals(
		t, "tricorder", stats[6].EndpointId.ConnectorName())
	assertValueEquals(t, "AnApp", stats[6].Name)
	assertValueEquals(t, true, stats[6].Active)

	assertValueEquals(t, "host4", stats[7].EndpointId.HostName())
	assertValueEquals(
		t, "snmp", stats[7].EndpointId.ConnectorName())
	assertValueEquals(t, "AnotherApp", stats[7].Name)
	assertValueEquals(t, true, stats[7].Active)

}