forked from mackerelio/mackerel-agent-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
201 lines (178 loc) · 5.28 KB
/
redis.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
192
193
194
195
196
197
198
199
200
201
package main
import (
"crypto/md5"
"flag"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/fzzy/radix/redis"
mp "github.com/mackerelio/go-mackerel-plugin"
"github.com/mackerelio/mackerel-agent/logging"
)
var logger = logging.GetLogger("metrics.plugin.redis")
// RedisPlugin mackerel plugin for Redis
type RedisPlugin struct {
Host string
Port string
Socket string
Prefix string
Timeout int
Tempfile string
}
// FetchMetrics interface for mackerelplugin
func (m RedisPlugin) FetchMetrics() (map[string]float64, error) {
network := "tcp"
target := fmt.Sprintf("%s:%s", m.Host, m.Port)
if m.Socket != "" {
target = m.Socket
network = "unix"
}
c, err := redis.DialTimeout(network, target, time.Duration(m.Timeout)*time.Second)
defer c.Close()
r := c.Cmd("info")
if r.Err != nil {
logger.Errorf("Failed to run info command. %s", r.Err)
return nil, r.Err
}
str, err := r.Str()
if err != nil {
logger.Errorf("Failed to fetch information. %s", err)
return nil, err
}
stat := make(map[string]float64)
for _, line := range strings.Split(str, "\r\n") {
if line == "" {
continue
}
if re, _ := regexp.MatchString("^#", line); re {
continue
}
record := strings.SplitN(line, ":", 2)
if len(record) < 2 {
continue
}
key, value := record[0], record[1]
if re, _ := regexp.MatchString("^db", key); re {
kv := strings.SplitN(value, ",", 3)
keys, expired := kv[0], kv[1]
keysKv := strings.SplitN(keys, "=", 2)
keysFv, err := strconv.ParseFloat(keysKv[1], 64)
if err != nil {
logger.Warningf("Failed to parse db keys. %s", err)
}
stat["keys"] += keysFv
expiredKv := strings.SplitN(expired, "=", 2)
expiredFv, err := strconv.ParseFloat(expiredKv[1], 64)
if err != nil {
logger.Warningf("Failed to parse db expired. %s", err)
}
stat["expired"] += expiredFv
continue
}
stat[key], err = strconv.ParseFloat(value, 64)
if err != nil {
continue
}
}
if _, ok := stat["keys"]; !ok {
stat["keys"] = 0
}
if _, ok := stat["expired"]; !ok {
stat["expired"] = 0
}
return stat, nil
}
// GraphDefinition interface for mackerelplugin
func (m RedisPlugin) GraphDefinition() map[string](mp.Graphs) {
labelPrefix := strings.Title(m.Prefix)
var graphdef = map[string](mp.Graphs){
(m.Prefix + ".queries"): mp.Graphs{
Label: (labelPrefix + " Queries"),
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "instantaneous_ops_per_sec", Label: "Queries", Diff: false},
},
},
(m.Prefix + ".connections"): mp.Graphs{
Label: (labelPrefix + " Connections"),
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "total_connections_received", Label: "Connections", Diff: true, Stacked: true},
mp.Metrics{Name: "rejected_connections", Label: "Rejected Connections", Diff: true, Stacked: true},
},
},
(m.Prefix + ".clients"): mp.Graphs{
Label: (labelPrefix + " Clients"),
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "connected_clients", Label: "Connected Clients", Diff: false, Stacked: true},
mp.Metrics{Name: "blocked_clients", Label: "Blocked Clients", Diff: false, Stacked: true},
mp.Metrics{Name: "connected_slaves", Label: "Blocked Clients", Diff: false, Stacked: true},
},
},
(m.Prefix + ".keys"): mp.Graphs{
Label: (labelPrefix + " Keys"),
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "keys", Label: "Keys", Diff: false},
mp.Metrics{Name: "expired", Label: "Expired Keys", Diff: false},
},
},
(m.Prefix + ".keyspace"): mp.Graphs{
Label: (labelPrefix + " Keyspace"),
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "keyspace_hits", Label: "Keyspace Hits", Diff: true},
mp.Metrics{Name: "keyspace_misses", Label: "Keyspace Missed", Diff: true},
},
},
(m.Prefix + ".memory"): mp.Graphs{
Label: (labelPrefix + " Memory"),
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "used_memory", Label: "Used Memory", Diff: false},
mp.Metrics{Name: "used_memory_rss", Label: "Used Memory RSS", Diff: false},
mp.Metrics{Name: "used_memory_peak", Label: "Used Memory Peak", Diff: false},
mp.Metrics{Name: "used_memory_lua", Label: "Used Memory Lua engine", Diff: false},
},
},
}
return graphdef
}
func main() {
optHost := flag.String("host", "localhost", "Hostname")
optPort := flag.String("port", "6379", "Port")
optSocket := flag.String("socket", "", "Server socket (overrides host and port)")
optPrefix := flag.String("metric-key-prefix", "redis", "Metric key prefix")
optTimeout := flag.Int("timeout", 5, "Timeout")
optTempfile := flag.String("tempfile", "", "Temp file name")
flag.Parse()
redis := RedisPlugin{
Timeout: *optTimeout,
Prefix: *optPrefix,
}
if *optSocket != "" {
redis.Socket = *optSocket
} else {
redis.Host = *optHost
redis.Port = *optPort
}
helper := mp.NewMackerelPlugin(redis)
if *optTempfile != "" {
helper.Tempfile = *optTempfile
} else {
if redis.Socket != "" {
helper.Tempfile = fmt.Sprintf("/tmp/mackerel-plugin-redis-%s", fmt.Sprintf("%x", md5.Sum([]byte(redis.Socket))))
} else {
helper.Tempfile = fmt.Sprintf("/tmp/mackerel-plugin-redis-%s-%s", redis.Host, redis.Port)
}
}
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
helper.OutputDefinitions()
} else {
helper.OutputValues()
}
}