-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (170 loc) · 4.89 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
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
//go:generate bash ./g_version.sh
package main
import (
"encoding/json"
"fmt"
"log"
"log/syslog"
"net/http"
"net/url"
"os"
"path"
"reflect"
"strings"
"time"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
appName = path.Base(os.Args[0])
app = kingpin.New(appName, "A telegraf input plugin that gatters metrics for every keyspace and table, by CrossEngage")
checkName = app.Flag("name", "Check name").Default(appName).String()
jolokiaBaseURL = app.Flag("jolokia", "The base URL of the jolokia agent running on Cassandra JVM").Default("http://localhost:1778/jolokia").URL()
debug = app.Flag("debug", "If set, enables debug logs").Default("false").Bool()
stderr = app.Flag("stderr", "If set, enables logging to stderr instead of syslog").Default("false").Bool()
skipZeros = app.Flag("skip-zeros", "If set, it will not output metrics that only has zeros").Default("false").Bool()
skipMetrics = app.Flag("skip", "CSV with metric names to skip collection").Default(
"CasCommitLatency",
"CasCommitTotalLatency",
"CasPrepareLatency",
"CasPrepareTotalLatency",
"CasProposeLatency",
"CasProposeTotalLatency",
"ColUpdateTimeDeltaHistogram",
"CompressionMetadataOffHeapMemoryUsed",
"CompressionRatio",
"RowCacheHit",
"RowCacheHitOutOfRange",
"RowCacheMiss",
"SpeculativeRetries",
).Strings()
)
func main() {
app.Version(version)
kingpin.MustParse(app.Parse(os.Args[1:]))
if *debug {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
if *stderr {
log.SetOutput(os.Stderr)
} else {
slog, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_DAEMON, appName)
if err != nil {
log.Fatal(err)
}
log.SetOutput(slog)
}
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
keys := []string{*checkName, "host=" + hostname}
loc, err := url.Parse((*jolokiaBaseURL).String() + "/read/org.apache.cassandra.metrics:type=ColumnFamily,keyspace=*,scope=*,name=*")
if err != nil {
log.Fatal(err)
}
// TODO timeouts
tr := &http.Transport{}
client := &http.Client{Transport: tr}
resp, err := client.Get(loc.String())
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("%s %s", loc, resp.Status)
}
jsonResp := &jsonResp{}
if err := json.NewDecoder(resp.Body).Decode(jsonResp); err != nil {
log.Fatal(err)
}
if jsonResp.Status != 200 || jsonResp.Error != nil {
log.Fatal(jsonResp.Error)
}
timestamp := time.Unix(jsonResp.TimeStamp, 0)
commonKey := strings.Join(keys, ",")
for keyPath, valueMap := range jsonResp.Value {
keyPath = strings.Replace(keyPath, "org.apache.cassandra.metrics:", "", 1)
if skipMetric(keyPath) {
continue
}
keyParts := strings.Split(keyPath, ",")
tags := []string{}
for _, part := range keyParts {
kv := strings.Split(part, "=")
switch kv[0] {
case "keyspace":
tags = append(tags, "keyspace="+kv[1])
case "name":
tags = append(tags, "metric="+kv[1])
case "scope":
tags = append(tags, "cf="+kv[1])
}
}
values := []string{}
zeroValuesCount := 0
numericValues := 0
for valueKey, value := range valueMap {
if value == nil {
continue
}
rt := reflect.TypeOf(value)
if rt.Kind() == reflect.Slice {
continue
}
switch v := value.(type) {
case int64, int32, int16, int8, int, uint64, uint32, uint16, uint8, uint:
values = append(values, fmt.Sprintf(`%s=%di`, valueKey, v))
numericValues++
if v == 0 {
zeroValuesCount++
}
case float32, float64, complex64, complex128:
values = append(values, fmt.Sprintf(`%s=%f`, valueKey, v))
numericValues++
if v == 0.0 {
zeroValuesCount++
}
case string:
values = append(values, fmt.Sprintf(`%s="%s"`, valueKey, v))
}
}
if *skipZeros && (zeroValuesCount == numericValues) {
if *debug {
log.Printf("Skipping `%s` because it has %d zero values of %d numeric values",
keyPath, zeroValuesCount, numericValues)
}
continue
}
if len(values) > 0 {
fmt.Print(commonKey, ",", strings.Join(tags, ","))
fmt.Print(" ")
fmt.Print(strings.Join(values, ","))
fmt.Print(" ")
fmt.Println(timestamp.UnixNano())
}
}
}
type jsonResp struct {
Request struct {
MBean string `json:"mbean"`
Type string `json:"type"`
} `json:"request"`
Status int `json:"status"`
Error error `json:"error"`
ErrorType string `json:"error_type"`
StackTrace string `json:"stacktrace"`
TimeStamp int64 `json:"timestamp"`
Value map[string]map[string]interface{} `json:"value"`
}
func skipMetric(keyPath string) bool {
for _, metricToSkip := range *skipMetrics {
part := ",name=" + metricToSkip + ","
if strings.Contains(keyPath, part) {
if *debug {
log.Printf("Skipping `%s` because it matches `%s`", keyPath, part)
}
return true
}
}
return false
}