Exemplo n.º 1
0
func (s *Service) Open() error {
	switch s.c.File {
	case "STDERR":
		s.f = &nopCloser{f: s.stderr}
	case "STDOUT":
		s.f = &nopCloser{f: s.stdout}
	default:
		dir := path.Dir(s.c.File)
		if _, err := os.Stat(dir); os.IsNotExist(err) {
			err := os.MkdirAll(dir, 0755)
			if err != nil {
				return err
			}
		}

		f, err := os.OpenFile(s.c.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0640)
		if err != nil {
			return err
		}
		s.f = f
	}

	// Configure default logger
	log.SetPrefix("[log] ")
	log.SetFlags(log.LstdFlags)
	log.SetOutput(wlog.NewWriter(s.f))

	wlog.SetLevelFromName(s.c.Level)
	return nil
}
Exemplo n.º 2
0
// serveLogLevel sets the log level of the server
func (h *Handler) serveLogLevel(w http.ResponseWriter, r *http.Request) {
	var opt client.LogLevelOptions
	dec := json.NewDecoder(r.Body)
	err := dec.Decode(&opt)
	if err != nil {
		HttpError(w, "invalid json: "+err.Error(), true, http.StatusBadRequest)
		return
	}
	err = wlog.SetLevelFromName(opt.Level)
	if err != nil {
		HttpError(w, err.Error(), true, http.StatusBadRequest)
		return
	}
	w.WriteHeader(http.StatusNoContent)
}
Exemplo n.º 3
0
func main() {
	// Parse flags
	flag.Parse()

	// Setup logging
	log.SetOutput(wlog.NewWriter(os.Stderr))
	if err := wlog.SetLevelFromName(*logLevel); err != nil {
		log.Fatal("E! ", err)
	}

	if *socket == "" {
		a := agent.New(os.Stdin, os.Stdout)
		h := newHandler(a)
		a.Handler = h

		log.Println("I! Starting agent using STDIN/STDOUT")
		a.Start()
		err := a.Wait()
		if err != nil {
			log.Fatal("E! ", err)
		}
		log.Println("I! Agent finished")
	} else {
		// Create unix socket
		addr, err := net.ResolveUnixAddr("unix", *socket)
		if err != nil {
			log.Fatal("E! ", err)
		}
		l, err := net.ListenUnix("unix", addr)
		if err != nil {
			log.Fatal("E! ", err)
		}

		// Create server that listens on the socket
		s := agent.NewServer(l, &accepter{})

		// Setup signal handler to stop Server on various signals
		s.StopOnSignals(os.Interrupt, syscall.SIGTERM)

		log.Println("I! Socket server listening on", addr.String())
		err = s.Serve()
		if err != nil {
			log.Fatal("E! ", err)
		}
		log.Println("I! Socket server stopped")
	}
}
Exemplo n.º 4
0
// Initialize the Handler with the provided options.
func (h *Handler) Init(r *udf.InitRequest) (*udf.InitResponse, error) {
	init := &udf.InitResponse{
		Success: true,
	}
	var errors []string
	for _, opt := range r.Options {
		switch opt.Name {
		case "field":
			h.field = opt.Values[0].Value.(*udf.OptionValue_StringValue).StringValue
		case "scoreField":
			h.scoreField = opt.Values[0].Value.(*udf.OptionValue_StringValue).StringValue
		case "minSupport":
			h.minSupport = opt.Values[0].Value.(*udf.OptionValue_DoubleValue).DoubleValue
		case "errorTolerance":
			h.errorTolerance = opt.Values[0].Value.(*udf.OptionValue_DoubleValue).DoubleValue
		case "consensus":
			h.consensus = opt.Values[0].Value.(*udf.OptionValue_DoubleValue).DoubleValue
		case "logLevel":
			level := opt.Values[0].Value.(*udf.OptionValue_StringValue).StringValue
			err := wlog.SetLevelFromName(level)
			if err != nil {
				init.Success = false
				errors = append(errors, err.Error())
			}
		default:
			if info, ok := fingerprinters[opt.Name]; ok {
				createFn, err := info.init(opt.Values)
				if err != nil {
					init.Success = false
					errors = append(errors, err.Error())
				} else {
					h.fingerprinters = append(h.fingerprinters, createFn)
				}
			} else {
				return nil, fmt.Errorf("received unknown init option %q", opt.Name)
			}
		}
	}

	if h.field == "" {
		errors = append(errors, "field must not be empty")
	}
	if h.minSupport < 0 || h.minSupport > 1 {
		errors = append(errors, "minSupport must be in the range [0,1)")
	}
	if h.errorTolerance < 0 || h.errorTolerance > 1 {
		errors = append(errors, "errorTolerance must be in the range [0,1)")
	}
	if (h.consensus != -1 && h.consensus < 0) || h.consensus > 1 {
		errors = append(errors, "consensus must be in the range [0,1) or equal to -1")
	}
	if h.minSupport <= h.errorTolerance {
		errors = append(errors, "invalid minSupport or errorTolerance: minSupport must be greater than errorTolerance")
	}
	init.Success = len(errors) == 0
	init.Error = strings.Join(errors, "\n")

	log.Printf("D! %#v", h)

	return init, nil
}