Exemplo n.º 1
0
func (collector *Collector) work() {
	collector.isRunning = true
	start := time.Now()
	var result chan []string
	var errorChan chan error
	var oldCache []string
	var newCache []string
	oldCache = []string{}
	for {
		select {
		case <-collector.quit:
			collector.quit <- true
			return
		default:
			result = make(chan []string, 10)
			errorChan = make(chan error)
			newCache = []string{}
			timeToHandleRequest := time.Now().Sub(start)
			//fmt.Println(time.Now().Unix(), timeToHandleRequest)
			go collector.conn.connectToLivestatus(addTimestampToLivestatusQuery(LogQuery, timeToHandleRequest), result, errorChan)
			start = time.Now()
			queryRunning := true
			for queryRunning {
				select {
				case line, alive := <-result:
					if alive {
						newCache = append(newCache, fmt.Sprint(line))
						if !Strings.Contains(oldCache, fmt.Sprint(line)) {
							fmt.Println("-->", line)
							jsonEvent := collector.convertQueryResultToJSON(line)
							collector.sendEvent(jsonEvent)
						}
					} else {
						queryRunning = false
					}
				case err, alive := <-errorChan:
					if alive {
						fmt.Println(err, alive)
						collector.logger.Error(err)
					} else {
						queryRunning = false
					}
				case <-time.After(time.Duration(30) * time.Second):
					collector.logger.Debug("Livestatus collector timed out")
				case <-collector.quit:
					collector.quit <- true
					return
				}
			}
			oldCache = newCache
		}
	}
}
Exemplo n.º 2
0
//Server start a server config depending on the config file
func Server(serverConfigPath, clientConfigPath, cpuProfile string) {
	gob.Register(map[string]interface{}{})
	if cpuProfile != "" {
		f, err := os.Create(cpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
	}
	Config.InitServerConfig(serverConfigPath)
	Config.InitClientConfig(clientConfigPath)

	stoppables := []Stoppable{}

	rpcInterfaces := []string{}
	httpInterfaces := []string{}

	if Config.GetServerConfig().LogServer.Enabled {
		logServer := LogServer.NewLogServer()
		logServer.Start()
		stoppables = append(stoppables, logServer)
		fmt.Println("Starting: LogServer")
		if Config.GetServerConfig().LogServer.RPCInterface != "" {
			fmt.Println("Starting: LogServer - RPC Interface")
			logServerRPCI := rpcIncoming.NewLogServerRPCInterface(logServer.LogQueue)
			logServerRPCI.Start()
			stoppables = append(stoppables, logServerRPCI)
			rpcInterfaces = append(rpcInterfaces, Config.GetServerConfig().LogServer.RPCInterface)
		}
		if Config.GetServerConfig().LogServer.HTTPInterface != "" {
			fmt.Println("Starting: LogServer - HTTP Interface")
			logServerRPCI := httpIncoming.NewLogServerHTTPInterface(logServer.InfluxClient)
			logServerRPCI.Start()
			stoppables = append(stoppables, logServerRPCI)
			httpInterfaces = append(httpInterfaces, Config.GetServerConfig().LogServer.HTTPInterface)
		}
		time.Sleep(time.Duration(100) * time.Millisecond)
	}

	if Config.GetServerConfig().RuleSystem.Enabled {
		ruleSystem := RuleSystem.NewRuleSystem()
		ruleSystem.Start()
		stoppables = append(stoppables, ruleSystem)
		fmt.Println("Starting: RuleSystem")
		if Config.GetServerConfig().RuleSystem.RPCInterface != "" {
			fmt.Println("Starting: RuleSystem - RPC Interface")
			ruleSystemRPCI := rpcIncoming.NewRuleSystemRPCInterface(ruleSystem)
			if !Strings.Contains(rpcInterfaces, Config.GetServerConfig().RuleSystem.RPCInterface) {
				fmt.Println("Starting: RPC")
				ruleSystemRPCI.Start()
				stoppables = append(stoppables, ruleSystemRPCI)
			}
		}
	}

	if Config.GetServerConfig().Proxy.Enabled {
		fmt.Println("Starting: Proxy - RPC Interface")
		proxyRPCI := rpcIncoming.NewProxyRPCInterface()
		if !Strings.Contains(rpcInterfaces, Config.GetServerConfig().RuleSystem.RPCInterface) {
			fmt.Println("Starting: RPC")
			proxyRPCI.Start()
			stoppables = append(stoppables, proxyRPCI)
		}
	}

	interruptChannel := make(chan os.Signal, 1)
	signal.Notify(interruptChannel, syscall.SIGINT)
	signal.Notify(interruptChannel, syscall.SIGTERM)
	quit := make(chan bool)
	go func() {
		<-interruptChannel
		cleanUp(stoppables)
		quit <- true
	}()
	fmt.Println("Everything's ready!")
	//wait for the end to come
	<-quit
	if cpuProfile != "" {
		pprof.StopCPUProfile()
	}
	fmt.Println("Bye")
}