<commit_before>// zmon notifies the local server admin when there's a problem.
// Design: http://goo.gl/l1Y36T
package main

import (
	"fmt"
	"log"
	"net"
	"os"
	"time"
)

// listenPort is used a simple lock mechanism for zmon. If it can't listen to
// this port, interrupt the startup.
const listenPort = "127.0.0.1:61510"

var hostname string

// TODO: Prober must not warn after the first error.
// TODO: Warn user when service restored?

type Probe interface {
	// Check must never take more than 10s.
	Check() error
	Scheme() string
}

func probeCheck(p Prober, e chan error) {
	probe := p.Probe()
	t := time.Tick(time.Duration(p.IntervalSeconds) * time.Second)
	for _ = range t {
		if err := probe.Check(); err != nil {
			e <- fmt.Errorf("%v: %v", probe.Scheme(), err)
		} else {
			// DEBUG
			// log.Println(probe.Scheme(), "went fine")
		}
	}
}

func main() {
	if fd, err := net.Listen("tcp", listenPort); err != nil {
		// Assume that this is a "address already in use" error and just exit without
		// printing anything to avoid excessive logging. If there was a nice way to test for
		// that error I'd use it.
		os.Exit(1)
	} else {
		defer fd.Close()
	}

	var err error
	hostname, err = os.Hostname()
	if err != nil {
		log.Printf("os.Hostname failed: %v. Using 'unknown' hostname", err)
		hostname = "unknown"
	}
	cfg, err := ReadConf()
	if err != nil {
		log.Fatalf("Error reading config: %v", err)
	}
	fmt.Println("Probes:", cfg.Probes)

	go contactMothership()

	e := make(chan error)
	for _, probe := range cfg.Probes {
		go probeCheck(probe, e)
	}
	escalator := cfg.newEscalator()
	for err := range e {
		escalator.escalate(err)
	}
}<commit_msg>Show error on startup if zmon already running<commit_after>// zmon notifies the local server admin when there's a problem.
// Design: http://goo.gl/l1Y36T
package main

import (
	"fmt"
	"log"
	"net"
	"os"
	"time"
)

// listenPort is used a simple lock mechanism for zmon. If it can't listen to
// this port, interrupt the startup.
const listenPort = "127.0.0.1:61510"

var hostname string

// TODO: Prober must not warn after the first error.
// TODO: Warn user when service restored?

type Probe interface {
	// Check must never take more than 10s.
	Check() error
	Scheme() string
}

func probeCheck(p Prober, e chan error) {
	probe := p.Probe()
	t := time.Tick(time.Duration(p.IntervalSeconds) * time.Second)
	for _ = range t {
		if err := probe.Check(); err != nil {
			e <- fmt.Errorf("%v: %v", probe.Scheme(), err)
		} else {
			// DEBUG
			// log.Println(probe.Scheme(), "went fine")
		}
	}
}

func main() {
	if fd, err := net.Listen("tcp", listenPort); err != nil {
		// Assume that this is a "address already in use" error and just exit without
		// printing anything to avoid excessive logging. If there was a nice way to test for
		// that error I'd use it.
		fmt.Fprintf(os.Stderr, "Could not start Zmon. Another instance may be already running (%v)\n", err)
		os.Exit(1)
	} else {
		defer fd.Close()
	}

	var err error
	hostname, err = os.Hostname()
	if err != nil {
		log.Printf("os.Hostname failed: %v. Using 'unknown' hostname", err)
		hostname = "unknown"
	}
	cfg, err := ReadConf()
	if err != nil {
		log.Fatalf("Error reading config: %v", err)
	}
	fmt.Println("Probes:", cfg.Probes)

	go contactMothership()

	e := make(chan error)
	for _, probe := range cfg.Probes {
		go probeCheck(probe, e)
	}
	escalator := cfg.newEscalator()
	for err := range e {
		escalator.escalate(err)
	}
}
{'fraction_non_alphanumeric': 0.13993075591459897, 'fraction_numerical': 0.009809578765147143, 'mean_word_length': 3.2435740514075886, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1097984', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1215, 'n_words': 422}