Beispiel #1
0
func (a *Agent) Start() {
	var err error

	go a.WSServer.ListenAndServe()

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

	if addr != "" {
		authOptions := &shttp.AuthenticationOpts{
			Username: config.GetConfig().GetString("agent.analyzer_username"),
			Password: config.GetConfig().GetString("agent.analyzer_password"),
		}
		authClient := shttp.NewAuthenticationClient(addr, port, authOptions)
		a.WSClient, err = shttp.NewWSAsyncClient(addr, port, "/ws", authClient)
		if err != nil {
			logging.GetLogger().Errorf("Unable to instantiate analyzer client %s", err.Error())
			os.Exit(1)
		}

		graph.NewForwarder(a.WSClient, a.Graph)
		a.WSClient.Connect()

		// send a first reset event to the analyzers
		a.Graph.DelSubGraph(a.Root)
	}

	a.TopologyProbeBundle = tprobes.NewTopologyProbeBundleFromConfig(a.Graph, a.Root)
	a.TopologyProbeBundle.Start()

	a.FlowProbeBundle = fprobes.NewFlowProbeBundleFromConfig(a.TopologyProbeBundle, a.Graph)
	a.FlowProbeBundle.Start()

	if addr != "" {
		a.EtcdClient, err = etcd.NewEtcdClientFromConfig()
		if err != nil {
			logging.GetLogger().Errorf("Unable to start etcd client %s", err.Error())
			os.Exit(1)
		}

		captureHandler := &api.BasicApiHandler{
			ResourceHandler: &api.CaptureHandler{},
			EtcdKeyAPI:      a.EtcdClient.KeysApi,
		}

		l, err := fprobes.NewOnDemandProbeListener(a.FlowProbeBundle, a.Graph, captureHandler)
		if err != nil {
			logging.GetLogger().Errorf("Unable to start on-demand flow probe %s", err.Error())
			os.Exit(1)
		}
		a.OnDemandProbeListener = l
		a.OnDemandProbeListener.Start()
	}

	go a.HTTPServer.ListenAndServe()
}
Beispiel #2
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
}