func newGraph(t *testing.T) *graph.Graph { b, err := graph.NewMemoryBackend() if err != nil { t.Error(err.Error()) } return graph.NewGraphFromConfig(b) }
func NewGraph(t *testing.T) *graph.Graph { var backend graph.GraphBackend var err error switch graphBackend { case "gremlin-ws": backend, err = graph.NewGremlinBackend("ws://127.0.0.1:8182") case "gremlin-rest": backend, err = graph.NewGremlinBackend("http://127.0.0.1:8182?gremlin=") case "elasticsearch": backend, err = graph.NewElasticSearchBackend("127.0.0.1", "9200", 10, 60, 1) if err == nil { backend, err = graph.NewShadowedBackend(backend) } case "orientdb": password := os.Getenv("ORIENTDB_ROOT_PASSWORD") if password == "" { password = "******" } backend, err = graph.NewOrientDBBackend("http://127.0.0.1:2480", "TestSkydive", "root", password) default: backend, err = graph.NewMemoryBackend() } if err != nil { t.Fatal(err.Error()) } t.Logf("Using %s as backend", graphBackend) g, err := graph.NewGraphFromConfig(backend) if err != nil { t.Fatal(err.Error()) } hostname, err := os.Hostname() if err != nil { t.Fatal(err.Error()) } root := g.LookupFirstNode(graph.Metadata{"Name": hostname, "Type": "host"}) if root == nil { root = g.NewNode(graph.Identifier(hostname), graph.Metadata{"Name": hostname, "Type": "host"}) if root == nil { t.Fatal("fail while adding root node") } } return g }
func NewAgent() *Agent { backend, err := graph.NewMemoryBackend() if err != nil { panic(err) } g, err := graph.NewGraphFromConfig(backend) if err != nil { panic(err) } tm := topology.NewTIDMapper(g) tm.Start() hserver, err := shttp.NewServerFromConfig("agent") if err != nil { panic(err) } _, err = api.NewApi(hserver, nil) if err != nil { panic(err) } wsServer := shttp.NewWSServerFromConfig(hserver, "/ws") root := createRootNode(g) api.RegisterTopologyApi("agent", g, hserver, nil, nil) gserver := graph.NewServer(g, wsServer) return &Agent{ Graph: g, WSServer: wsServer, GraphServer: gserver, Root: root, HTTPServer: hserver, TIDMapper: tm, } }
func NewServerFromConfig() (*Server, error) { embedEtcd := config.GetConfig().GetBool("etcd.embedded") backend, err := graph.BackendFromConfig() if err != nil { return nil, err } g, err := graph.NewGraphFromConfig(backend) if err != nil { return nil, err } httpServer, err := shttp.NewServerFromConfig("analyzer") if err != nil { return nil, err } wsServer := shttp.NewWSServerFromConfig(httpServer, "/ws") 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 } analyzerUpdate := config.GetConfig().GetInt("analyzer.flowtable_update") analyzerExpire := config.GetConfig().GetInt("analyzer.flowtable_expire") agentRatio := config.GetConfig().GetFloat64("analyzer.flowtable_agent_ratio") if agentRatio == 0.0 { agentRatio = 0.5 } agentUpdate := int64(float64(analyzerUpdate) * agentRatio) agentExpire := int64(float64(analyzerExpire) * agentRatio) if err := etcdClient.SetInt64("/agent/config/flowtable_update", agentUpdate); err != nil { return nil, err } if err := etcdClient.SetInt64("/agent/config/flowtable_expire", agentExpire); err != nil { return nil, err } apiServer, err := api.NewApi(httpServer, etcdClient.KeysApi) if err != nil { return nil, err } captureApiHandler := &api.CaptureApiHandler{ BasicApiHandler: api.BasicApiHandler{ ResourceHandler: &api.CaptureResourceHandler{}, EtcdKeyAPI: etcdClient.KeysApi, }, Graph: g, } err = apiServer.RegisterApiHandler(captureApiHandler) if err != nil { return nil, err } alertApiHandler := &api.AlertApiHandler{ BasicApiHandler: api.BasicApiHandler{ ResourceHandler: &api.AlertResourceHandler{}, EtcdKeyAPI: etcdClient.KeysApi, }, } err = apiServer.RegisterApiHandler(alertApiHandler) if err != nil { return nil, err } alertManager := alert.NewAlertManager(g, alertApiHandler) aserver := alert.NewServer(alertManager, wsServer) gserver := graph.NewServer(g, wsServer) gfe := mappings.NewGraphFlowEnhancer(g) ofe := mappings.NewOvsFlowEnhancer(g) pipeline := mappings.NewFlowMappingPipeline(gfe, ofe) tableClient := flow.NewTableClient(wsServer) server := &Server{ HTTPServer: httpServer, WSServer: wsServer, GraphServer: gserver, AlertServer: aserver, FlowMappingPipeline: pipeline, TableClient: tableClient, EmbeddedEtcd: etcdServer, EtcdClient: etcdClient, } server.SetStorageFromConfig() updateHandler := flow.NewFlowHandler(server.flowExpireUpdate, time.Second*time.Duration(analyzerUpdate)) expireHandler := flow.NewFlowHandler(server.flowExpireUpdate, time.Second*time.Duration(analyzerExpire)) flowtable := flow.NewTable(updateHandler, expireHandler) server.FlowTable = flowtable api.RegisterTopologyApi("analyzer", g, httpServer, tableClient, server.Storage) api.RegisterFlowApi("analyzer", flowtable, server.Storage, httpServer) return server, nil }