Пример #1
0
func NewFlowProbeBundleFromConfig(tb *probes.TopologyProbeBundle, g *graph.Graph) *FlowProbeBundle {
	list := config.GetConfig().GetStringSlice("agent.flow.probes")

	logging.GetLogger().Infof("Flow probes: %v", list)

	gfe := mappings.NewGraphFlowEnhancer(g)

	var aclient *analyzer.Client

	addr, port, err := config.GetAnalyzerClientAddr()
	if err != nil {
		logging.GetLogger().Errorf("Unable to parse analyzer client: %s", err.Error())
		return nil
	}

	if addr != "" {
		aclient, err = analyzer.NewClient(addr, port)
		if err != nil {
			logging.GetLogger().Errorf("Analyzer client error %s:%d : %s", addr, port, err.Error())
			return nil
		}
	}

	probes := make(map[string]probe.Probe)
	for _, t := range list {
		if _, ok := probes[t]; ok {
			continue
		}

		switch t {
		case "ovssflow":
			ofe := mappings.NewOvsFlowEnhancer(g)
			pipeline := mappings.NewFlowMappingPipeline(gfe, ofe)

			o := NewOvsSFlowProbesHandler(tb, g, pipeline, aclient)
			if o != nil {
				probes[t] = o
			}
		case "pcap":
			pipeline := mappings.NewFlowMappingPipeline(gfe)

			o := NewPcapProbesHandler(tb, g, pipeline, aclient)
			if o != nil {
				probes[t] = o
			}
		default:
			logging.GetLogger().Errorf("unknown probe type %s", t)
		}
	}

	p := probe.NewProbeBundle(probes)

	return &FlowProbeBundle{
		ProbeBundle: *p,
		Graph:       g,
	}
}
Пример #2
0
func NewServer(addr string, port int, router *mux.Router) (*Server, error) {
	backend, err := graph.BackendFromConfig()
	if err != nil {
		return nil, err
	}

	g, err := graph.NewGraph(backend)
	if err != nil {
		return nil, err
	}

	tserver := topology.NewServer(g, addr, port, router)
	tserver.RegisterStaticEndpoints()
	tserver.RegisterRpcEndpoints()

	alertmgr := graph.NewAlert(g, router)
	alertmgr.RegisterRpcEndpoints()

	gserver, err := graph.NewServerFromConfig(g, alertmgr, router)
	if err != nil {
		return nil, err
	}

	gfe, err := mappings.NewGraphFlowEnhancer(g)
	if err != nil {
		return nil, err
	}

	pipeline := mappings.NewFlowMappingPipeline([]mappings.FlowEnhancer{gfe})

	flowtable := flow.NewFlowTable()

	server := &Server{
		Addr:                addr,
		Port:                port,
		Router:              router,
		TopoServer:          tserver,
		GraphServer:         gserver,
		FlowMappingPipeline: pipeline,
		FlowTable:           flowtable,
	}
	server.RegisterStaticEndpoints()
	server.RegisterRpcEndpoints()
	cfgFlowtable_expire, err := config.GetConfig().Section("analyzer").Key("flowtable_expire").Int()
	if err != nil || cfgFlowtable_expire < 1 {
		logging.GetLogger().Error("Config flowTable_expire invalid value ", cfgFlowtable_expire, err.Error())
		return nil, err
	}
	go flowtable.AsyncExpire(server.flowExpire, time.Duration(cfgFlowtable_expire)*time.Minute)

	return server, nil
}
Пример #3
0
func (a *Agent) Start() {
	var err error
	// send a first reset event to the analyzers
	a.Graph.DelSubGraph(a.Root)

	a.SFlowProbe, err = fprobes.NewSFlowProbeFromConfig(a.Graph)
	if err != nil {
		panic(err)
	}

	ovsSFlowProbe := ovsdb.SFlowProbe{
		ID:         "SkydiveSFlowProbe",
		Interface:  "eth0",
		Target:     a.SFlowProbe.GetTarget(),
		HeaderSize: 256,
		Sampling:   1,
		Polling:    0,
	}
	sflowHandler := ovsdb.NewOvsSFlowProbesHandler([]ovsdb.SFlowProbe{ovsSFlowProbe})

	a.OvsMon.AddMonitorHandler(sflowHandler)

	analyzers := config.GetConfig().Section("agent").Key("analyzers").Strings(",")
	// TODO(safchain) HA Connection ???
	if len(analyzers) > 0 {
		analyzerAddr := strings.Split(analyzers[0], ":")[0]
		analyzerPort, err := strconv.Atoi(strings.Split(analyzers[0], ":")[1])
		if err != nil {
			panic(err)
		}

		analyzer, err := analyzer.NewClient(analyzerAddr, analyzerPort)
		if err != nil {
			panic(err)
		}

		a.SFlowProbe.SetAnalyzerClient(analyzer)

		a.Gclient = graph.NewAsyncClient(analyzerAddr, analyzerPort, "/ws/graph")
		graph.NewForwarder(a.Gclient, a.Graph)
		a.Gclient.Connect()
	}

	gfe, err := mappings.NewGraphFlowEnhancer(a.Graph)
	if err != nil {
		panic(err)
	}

	pipeline := mappings.NewFlowMappingPipeline([]mappings.FlowEnhancer{gfe})
	a.SFlowProbe.SetMappingPipeline(pipeline)

	// start probes that will update the graph
	a.NsProbe.Start()
	a.NlProbe.Start()
	a.OvsProbe.Start()

	go a.SFlowProbe.Start()

	if err := a.OvsMon.StartMonitoring(); err != nil {
		panic(err)
	}

	go a.TopoServer.ListenAndServe()

	go a.GraphServer.ListenAndServe()
}
Пример #4
0
func NewServer(addr string, port int, router *mux.Router, embedEtcd bool) (*Server, error) {
	backend, err := graph.BackendFromConfig()
	if err != nil {
		return nil, err
	}

	g, err := graph.NewGraph(backend)
	if err != nil {
		return nil, err
	}

	tserver := topology.NewServer("analyzer", g, addr, port, router)
	tserver.RegisterStaticEndpoints()
	tserver.RegisterRPCEndpoints()

	var etcd *storage.EmbeddedEtcd
	if embedEtcd {
		if etcd, err = storage.NewEmbeddedEtcdFromConfig(); err != nil {
			return nil, err
		}
	}

	alertmgr, err := graph.NewAlertFromConfig(g, router)
	if err != nil {
		return nil, err
	}
	alertmgr.RegisterRPCEndpoints()

	gserver, err := graph.NewServerFromConfig(g, alertmgr, router)
	if err != nil {
		return nil, err
	}

	gfe, err := mappings.NewGraphFlowEnhancer(g)
	if err != nil {
		return nil, err
	}

	pipeline := mappings.NewFlowMappingPipeline(gfe)

	flowtable := flow.NewFlowTable()

	server := &Server{
		Addr:                addr,
		Port:                port,
		Router:              router,
		TopoServer:          tserver,
		GraphServer:         gserver,
		FlowMappingPipeline: pipeline,
		FlowTable:           flowtable,
		EmbeddedEtcd:        etcd,
	}
	server.RegisterRPCEndpoints()
	cfgFlowtable_expire := config.GetConfig().GetInt("analyzer.flowtable_expire")
	if cfgFlowtable_expire < 1 {
		logging.GetLogger().Errorf("Config flowTable_expire invalid value %d : %s", cfgFlowtable_expire, err.Error())
		return nil, err
	}
	go flowtable.AsyncExpire(server.flowExpire, time.Duration(cfgFlowtable_expire)*time.Minute)

	return server, nil
}
Пример #5
0
func NewServerFromConfig() (*Server, error) {
	embedEtcd := config.GetConfig().GetBool("etcd.embedded")

	backend, err := graph.BackendFromConfig()
	if err != nil {
		return nil, err
	}

	g, err := graph.NewGraph(backend)
	if err != nil {
		return nil, err
	}

	httpServer, err := shttp.NewServerFromConfig("analyzer")
	if err != nil {
		return nil, err
	}

	wsServer := shttp.NewWSServerFromConfig(httpServer, "/ws")

	api.RegisterTopologyApi("analyzer", g, httpServer)

	var etcdServer *etcd.EmbeddedEtcd
	if embedEtcd {
		if etcdServer, err = etcd.NewEmbeddedEtcdFromConfig(); err != nil {
			return nil, err
		}
	}

	etcdClient, err := etcd.NewEtcdClientFromConfig()
	if err != nil {
		return nil, err
	}

	apiServer, err := api.NewApi(httpServer, etcdClient.KeysApi)
	if err != nil {
		return nil, err
	}

	captureHandler := &api.BasicApiHandler{
		ResourceHandler: &api.CaptureHandler{},
		EtcdKeyAPI:      etcdClient.KeysApi,
	}
	err = apiServer.RegisterApiHandler(captureHandler)
	if err != nil {
		return nil, err
	}

	alertHandler := &api.BasicApiHandler{
		ResourceHandler: &api.AlertHandler{},
		EtcdKeyAPI:      etcdClient.KeysApi,
	}
	err = apiServer.RegisterApiHandler(alertHandler)
	if err != nil {
		return nil, err
	}

	alertManager := alert.NewAlertManager(g, alertHandler)

	aserver := alert.NewServer(alertManager, wsServer)
	gserver := graph.NewServer(g, wsServer)

	gfe := mappings.NewGraphFlowEnhancer(g)
	ofe := mappings.NewOvsFlowEnhancer(g)

	pipeline := mappings.NewFlowMappingPipeline(gfe, ofe)

	flowtable := flow.NewTable()

	server := &Server{
		HTTPServer:          httpServer,
		WSServer:            wsServer,
		GraphServer:         gserver,
		AlertServer:         aserver,
		FlowMappingPipeline: pipeline,
		FlowTable:           flowtable,
		EmbeddedEtcd:        etcdServer,
		EtcdClient:          etcdClient,
	}
	server.SetStorageFromConfig()

	api.RegisterFlowApi("analyzer", flowtable, server.Storage, httpServer)

	cfgFlowtable_expire := config.GetConfig().GetInt("analyzer.flowtable_expire")
	flowtable.RegisterExpire(server.flowExpireUpdate, time.Duration(cfgFlowtable_expire)*time.Second)
	cfgFlowtable_update := config.GetConfig().GetInt("analyzer.flowtable_update")
	flowtable.RegisterUpdated(server.flowExpireUpdate, time.Duration(cfgFlowtable_update)*time.Second)

	return server, nil
}