forked from dedalusj/cwmonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (102 loc) · 2.54 KB
/
main.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
package main
import (
"context"
"os"
"os/signal"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/dedalusj/cwmonitor/monitor"
"github.com/dedalusj/cwmonitor/util"
"github.com/urfave/cli"
log "github.com/sirupsen/logrus"
)
var version = "dev"
var buildTime = time.Now().Format("20060102T150405Z")
var buildNumber = "local"
func initLogger(c *cli.Context) {
log.SetFormatter(&util.Formatter{})
log.SetLevel(log.InfoLevel)
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
}
func getConfig(c *cli.Context) monitor.Config {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
client := cloudwatch.New(sess)
return monitor.Config{
Namespace: c.String("namespace"),
Interval: time.Duration(c.Int("interval")) * time.Second,
HostId: c.String("hostid"),
Metrics: c.String("metrics"),
Once: c.Bool("once"),
Metadata: util.Metadata{Version: c.App.Version, BuildTime: buildTime, BuildNumber: buildNumber},
Client: client,
}
}
func setupCtx() context.Context {
ctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal)
signal.Notify(sigCh, os.Interrupt)
go func() {
select {
case <-sigCh:
cancel()
case <-ctx.Done():
}
}()
return ctx
}
func main() {
app := cli.NewApp()
app.Name = "cwmonitor"
app.Usage = "Publish Custom Metrics to CloudWatch"
app.Version = version
app.Author = "Jacopo Sabbatini"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "metrics",
Usage: "Comma separated list of metrics. Available: cpu, memory, swap, disk, docker-stats, docker-health",
Value: "cpu,memory",
EnvVar: "CWMONITOR_METRICS",
},
cli.IntFlag{
Name: "interval",
Usage: "Time interval between data collection (seconds)",
Value: 60,
EnvVar: "CWMONITOR_INTERVAL",
},
cli.BoolFlag{
Name: "once",
Usage: "Run once (i.e. not on an interval)",
},
cli.StringFlag{
Name: "namespace",
Usage: "CloudWatch namespace",
Value: "CWMonitor",
EnvVar: "CWMONITOR_NAMESPACE",
},
cli.StringFlag{
Name: "hostid",
Usage: "ID of the current host used as dimension for the upload",
EnvVar: "CWMONITOR_ID",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug logging",
},
}
app.Action = func(c *cli.Context) error {
initLogger(c)
config := getConfig(c)
ctx := setupCtx()
err := monitor.Run(config, ctx)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}
app.Run(os.Args)
}