-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
191 lines (172 loc) · 4.41 KB
/
config.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"container/ring"
"encoding/json"
"fmt"
"log"
"os"
"path"
"strings"
"time"
"github.com/gregdel/pushover"
"github.com/nictuku/zmon/probes/disk"
"github.com/nictuku/zmon/probes/http"
"github.com/nictuku/zmon/probes/tcp"
)
var pushoverKey = os.Getenv("PUSHOVER_KEY")
var pushoverRecipient = os.Getenv("PUSHOVER_RECIPIENT")
type Prober struct {
// Type of the probe: "disk", tcp", "http", etc.
Type string
// Target is the resource to be probed. For disk, this is the mount point being checked. For
// TCP, it's "host:port". For HTTP, it's a URL like "http://localhost:4040/debug/vars".
Target string
// How often should the probe run, in seconds.
IntervalSeconds int
}
func (p *Prober) Probe() Probe {
switch p.Type {
case "disk":
return disk.New(p.Target)
case "tcp":
return tcp.New(p.Target)
case "http":
return http.New(p.Target)
default:
log.Printf("Ignoring unknown probe type %q", p.Type)
return nil
}
}
// Notificator represents a method to send notifications about problems.
// It's used when encoding and decoding the JSON configuration.
type Notificator struct {
// smtp, pushover, etc.
Type string
// Where to send the notifications to.
// For SMTP, the email address. For PushOver, it's the destination user's token.
Destination string
// Authentication details. For PushOver, this is the pushover application key. For SMTP, it's
// the server and login details, in the format "user[:password][@server][:port]". The "user"
// string is used as the From address. Only 'user' is required. Password is currenly unused even
// if specified.
From string `json:",omitempty"`
}
func parseSMTPAuth(auth string) (user, serverport string) {
authp := strings.SplitN(auth, "@", 2)
userpass := authp[0]
if len(authp) == 2 {
serverport = authp[1]
}
user = strings.SplitN(userpass, ":", 2)[0] // Password is ignored for now.
return user, serverport
}
func (n *Notificator) notifier() notifier {
switch n.Type {
case "smtp":
user, server := parseSMTPAuth(n.From)
return &smtpNotification{
addr: server,
from: fmt.Sprintf("%v@%v", user, server),
to: n.Destination,
}
case "pushover":
return &pushoverNotification{
app: pushover.New(pushoverKey),
identity: pushover.NewRecipient(pushoverRecipient),
}
default:
log.Printf("Ignoring unknown notifier type %q", n.Type)
return nil
}
}
// Config contains the zmon agent configuration.
type Config struct {
Probes []Prober
Notification []Notificator
}
const maxNotificationLines = 20
func (c *Config) newEscalator() *escalator {
notifiers := make([]notifier, len(c.Notification))
for i, n := range c.Notification {
notifiers[i] = n.notifier()
}
return &escalator{
escalationInterval: 30 * time.Minute,
queued: ring.New(maxNotificationLines),
Notifiers: notifiers,
}
}
func makeConfDir() string {
dir := "/var/run/zmon"
env := os.Environ()
for _, e := range env {
if strings.HasPrefix(e, "HOME=") {
dir = strings.SplitN(e, "=", 2)[1]
dir = path.Join(dir, ".zmon")
}
}
// Ignore errors.
os.MkdirAll(dir, 0750)
if s, err := os.Stat(dir); err != nil {
log.Fatal("stat config dir", err)
} else if !s.IsDir() {
log.Fatalf("Dir %v expected directory, got %v", dir, s)
}
return dir
}
func confPath() string {
dir := makeConfDir()
return path.Join(dir, "zmon.json")
}
var defaultCfg = Config{
Probes: []Prober{
{
Type: "disk",
Target: "/",
IntervalSeconds: 5,
},
{
Type: "tcp",
Target: "localhost:22",
IntervalSeconds: 5,
},
{
Type: "http",
Target: "http://localhost:4040",
IntervalSeconds: 5,
},
},
Notification: []Notificator{
{
Type: "pushover",
Destination: "userdestination",
},
{
Type: "smtp",
Destination: "[email protected]",
From: "[email protected]",
},
},
}
// ReadConfig reads the mothership configuration from $HOME/.zmon/zmon.json and returns
// the parsed Config.
func ReadConf() (cfg Config, err error) {
file, err := os.Open(confPath())
if err != nil {
return defaultCfg, nil
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&cfg)
if err != nil {
return defaultCfg, nil
}
if len(cfg.Notification) == 0 {
log.Fatal("No notification settings found. Exiting")
}
for _, p := range cfg.Probes {
if p.IntervalSeconds == 0 {
log.Fatalf("Probe of type %q missing IntervalSeconds", p.Type)
}
}
return cfg, nil
}