-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd_metrics.go
144 lines (120 loc) · 2.79 KB
/
cmd_metrics.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
//
// Submit metrics to a graphite host.
//
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/google/subcommands"
graphite "github.com/marpaia/graphite-golang"
)
//
// Get all the metrics
//
func getMetrics() map[string]string {
// A map to store the names & values which should be sent.
metrics := make(map[string]string)
// Get the node-states.
data, err := getStates("")
if err != nil {
fmt.Printf("Error getting node states: %s\n", err.Error())
os.Exit(1)
}
// Now record the metrics we would send.
for i := range data {
//
// The name + value
//
metric := fmt.Sprintf("state.%s", data[i].State)
value := fmt.Sprintf("%d", data[i].Count)
metrics[metric] = value
}
// And return them
return metrics
}
//
// SendMetrics submits the metrics discovered to the specified carbon
// server - unless `nop` is in-use, in which case they are dumped to
// STDOUT.
//
func SendMetrics(host string, port int, prefix string, nop bool) {
// Get the metrics.
metrics := getMetrics()
// Create the helper.
g, err := graphite.NewGraphite(host, port)
//
// If there was an error in the helper we're OK,
// providing we are running in `-nop`-mode.
//
if (err != nil) && (!nop) {
fmt.Printf("Error creating metrics-helper: %s\n", err.Error())
return
}
//
// For each one ..
//
for name, value := range metrics {
//
// Add the prefix.
//
name = fmt.Sprintf("%s.%s", prefix, name)
//
// Show/Send.
//
if nop {
fmt.Fprintf(out, "%s %s\n", name, value)
} else {
g.SimpleSend(name, value)
}
}
}
//
// The options set by our command-line flags.
//
type metricsCmd struct {
dbFile string
host string
port int
prefix string
nop bool
}
//
// Glue
//
func (*metricsCmd) Name() string { return "metrics" }
func (*metricsCmd) Synopsis() string { return "Submit metrics to a central carbon server." }
func (*metricsCmd) Usage() string {
return `metrics [options]:
Submit metrics to a central carbon server.
`
}
//
// Flag setup
//
func (p *metricsCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.dbFile, "db-file", "ps.db", "The SQLite database to use.")
f.StringVar(&p.host, "host", "localhost", "The carbon host to send metrics to.")
f.IntVar(&p.port, "port", 2003, "The carbon port to use, when submitting metrics.")
f.StringVar(&p.prefix, "prefix", "puppet", "The prefix to use when submitting metrics.")
f.BoolVar(&p.nop, "nop", false, "Print metrics rather than submitting them.")
}
//
// Entry-point.
//
func (p *metricsCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
//
// Setup the database, by opening a handle, and creating it if
// missing.
//
SetupDB(p.dbFile)
//
// Run metrics
//
SendMetrics(p.host, p.port, p.prefix, p.nop)
//
// All done.
//
return subcommands.ExitSuccess
}