Esempio n. 1
0
// submitCachedState periodically samples the cached state, sends it to Flapjack.
func submitCachedState(states map[string]State, config Config) {
	transport, err := flapjack.Dial(config.Server, config.Database)
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}

	for {
		log.Printf("Number of cached states: %d\n", len(states))
		for id, state := range states {
			now := time.Now().Unix()
			event := flapjack.Event{
				Entity:  state.Entity,
				Check:   state.Check,
				Type:    state.Type,
				State:   state.State,
				Summary: state.Summary,
				Time:    now,
			}

			// Stale state sends UNKNOWNs
			elapsed := now - state.Time
			if state.TTL >= 0 && elapsed > state.TTL {
				log.Printf("State for %s is stale. Sending UNKNOWN.\n", id)
				event.State = "UNKNOWN"
				event.Summary = fmt.Sprintf("httpbroker: Cached state is stale (%ds old, should be < %ds)", elapsed, state.TTL)
			}
			if config.Debug {
				log.Printf("Sending event data for %s\n", id)
			}
			transport.Send(event)
		}
		time.Sleep(config.Interval)
	}
}
Esempio n. 2
0
func main() {
	app.Version("0.0.2")
	app.Writer(os.Stdout) // direct help to stdout
	kingpin.MustParse(app.Parse(os.Args[1:]))
	app.Writer(os.Stderr) // ... but ensure errors go to stderr

	if *debug {
		fmt.Println("Flapjack version:", *flapjack_version)

		fmt.Println("Entity:", *entity)
		fmt.Println("Check:", *check)
		fmt.Println("State:", *state)
		fmt.Println("Summary:", *summary)
		fmt.Println("Debug:", *debug)
		fmt.Println("Server:", *server)
		fmt.Println("Database:", *database)
		fmt.Println("Queue:", *queue)
	}

	addr := strings.Split(*server, ":")
	if len(addr) != 2 {
		fmt.Println("Error: invalid address specified:", *server)
		fmt.Println("Should be in format `addr:port` (e.g. 127.0.0.1:6380)")
		os.Exit(1)
	}

	event := flapjack.Event{
		Entity:  *entity,
		Check:   *check,
		Type:    "service",
		State:   *state,
		Summary: *summary,
		Time:    time.Now().Unix(),
	}

	if *debug {
		data, _ := json.Marshal(event)
		fmt.Printf("Event data: %s\n", data)
	}

	transport, err := flapjack.Dial(*server, *database)
	if *debug {
		fmt.Printf("Transport: %+v\n", transport)
	}
	if err != nil {
		fmt.Println("Error: couldn't connect to Redis:", err)
		os.Exit(1)
	}

	reply, err := transport.SendVersionQueue(event, *flapjack_version, *queue)

	if *debug {
		fmt.Println("Reply from Redis:", reply)
	}
	if err != nil {
		fmt.Println("Error: couldn't send event:", err)
		os.Exit(1)
	}
}
Esempio n. 3
0
func main() {
	kingpin.Version("0.0.1")
	kingpin.Parse()

	if *debug {
		fmt.Println("Entity:", *entity)
		fmt.Println("Check:", *check)
		fmt.Println("State:", *state)
		fmt.Println("Summary:", *summary)
		fmt.Println("Debug:", *debug)
		fmt.Println("Server:", *server)
		fmt.Println("Database:", *database)
	}

	addr := strings.Split(*server, ":")
	if len(addr) != 2 {
		fmt.Println("Error: invalid address specified:", *server)
		fmt.Println("Should be in format `addr:port` (e.g. 127.0.0.1:6380)")
		os.Exit(1)
	}

	event := flapjack.Event{
		Entity:  *entity,
		Check:   *check,
		Type:    "service",
		State:   *state,
		Summary: *summary,
		Time:    time.Now().Unix(),
	}

	if *debug {
		data, _ := json.Marshal(event)
		fmt.Printf("Event data: %s\n", data)
	}

	transport, err := flapjack.Dial(*server, *database)
	if *debug {
		fmt.Printf("Transport: %+v\n", transport)
	}
	if err != nil {
		fmt.Println("Error: couldn't connect to Redis:", err)
		os.Exit(1)
	}

	reply, err := transport.Send(event)
	if *debug {
		fmt.Println("Reply from Redis:", reply)
	}
	if err != nil {
		fmt.Println("Error: couldn't send event:", err)
		os.Exit(1)
	}
}