-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzmon.go
More file actions
78 lines (66 loc) · 1.76 KB
/
Copy pathzmon.go
File metadata and controls
78 lines (66 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// zmon notifies the local server admin when there's a problem.
// Design: http://goo.gl/l1Y36T
package main
import (
"fmt"
"log"
"net"
"os"
"time"
"github.com/nictuku/zmon/updater"
)
// 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()
}
updater.SelfUpdate()
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)
}
}