This repository was archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (90 loc) · 2.67 KB
/
main.go
File metadata and controls
113 lines (90 loc) · 2.67 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
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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/cactus/go-statsd-client/statsd"
configpkg "github.com/jaxxstorm/graphping/config"
"github.com/jaxxstorm/graphping/ping"
"gopkg.in/urfave/cli.v1"
"os"
"os/signal"
"syscall"
)
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
// Output to stdout instead of the default stderr, could also be a file.
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.InfoLevel)
}
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{Name: "config-file, c", Usage: "Path to configuration file"},
cli.StringFlag{Name: "statsd, s", Usage: "Address of statsd listener"},
cli.BoolFlag{Name: "verbose", Usage: "Output metrics in logs"},
}
app.Name = "graphping"
app.Version = "0.1"
app.Usage = "Ping a list of endpoints and send the resulting metrics to statsd"
app.Authors = []cli.Author{
cli.Author{
Name: "Lee Briggs",
},
}
app.Action = func(c *cli.Context) error {
// Check we have a config file
if !c.IsSet("config-file") {
cli.ShowAppHelp(c)
return cli.NewExitError("Error: No config file specified", -1)
}
if !c.IsSet("statsd") {
cli.ShowAppHelp(c)
return cli.NewExitError("Error: No statsd client specified", -1)
}
// if debug flag is set, override loglevel:
if c.Bool("verbose") {
log.SetLevel(log.DebugLevel)
}
// if we can't parse it, error
config, err := configpkg.Parse(c.String("config-file"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error: Unable to parse config file - %s", err), -1)
} else {
// everything is fine, let's start pinging!
// create a statsdClient
statsdClient, err := statsd.NewClient(c.String("statsd"), "")
// Issues opening statsd
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error: Unable to open statsd client - %s, err"), -1)
}
// defer till later
defer statsdClient.Close()
done := make(chan bool, 1)
// loop through the groups and start a goroutine
// for each group to ping the targets
for _, groups := range config.Groups {
go func(group configpkg.TargetGroups) {
pingres := ping.RunPinger(config.Interval, config.Prefix, statsdClient, group)
log.Warn(fmt.Sprintf("Pinger exited: %s", pingres))
done <- true
}(groups)
}
// channel handling for interrupting app
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
for sig := range c {
log.Warn(fmt.Sprintf("captured %v", sig))
done <- true
}
}()
<-done
}
return nil
}
// run!
app.Run(os.Args)
}