This repository has been archived by the owner on Mar 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
cmd.go
70 lines (56 loc) · 1.74 KB
/
cmd.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
package main
import (
"flag"
"log"
"os"
"os/signal"
"time"
"github.com/jordan-wright/email"
"github.com/pinggg/pingd"
"github.com/pinggg/pingd/io/http"
"github.com/pinggg/pingd/io/mail"
"github.com/pinggg/pingd/io/std"
"github.com/pinggg/pingd/ping"
)
// See flags
var (
emailAddr string
listenAddr string
interval time.Duration
failLimit int
)
func main() {
flag.StringVar(&emailAddr, "email", "me@example.org", "email recipient for notificiations")
flag.StringVar(&listenAddr, "listen", ":7700", "webserver listen address")
flag.IntVar(&failLimit, "failLimit", 4, "number failed ping attempts in a row to consider host down")
flag.DurationVar(&interval, "interval", 5*time.Second, "seconds between each ping")
flag.DurationVar(&ping.TimeOut, "timeOut", 5*time.Second, "seconds for single ping timeout")
flag.Parse()
// read non flag arguments as hosts to start monitoring
hosts := flag.Args()
var pool = &pingd.Pool{
Ping: ping.Ping,
Interval: interval,
FailLimit: failLimit,
Receive: http.NewReceiverFunc(listenAddr), // start/stop commands via HTTP
Notify: mail.NewNotifierFunc(emailAddr, sendMail), // notify up/down via email
Load: std.NewLoaderFunc(hosts), // load initial hosts from command line
}
pool.Start()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c // Exit on interrupt
}
// Replace this localhost version with your appropriate mail function
// https://github.com/jordan-wright/email#email
func sendMail(recipient, message string) {
e := email.NewEmail()
// Change From to something more appropriate
e.From = recipient
e.To = []string{recipient}
e.Subject = message
err := e.Send("localhost:25", nil)
if err != nil {
log.Fatal(err)
}
}