/
stadis.go
154 lines (146 loc) · 4.07 KB
/
stadis.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
package main
import (
"fmt"
"github.com/cactus/go-statsd-client/statsd"
"github.com/codegangsta/cli"
"github.com/fatih/color"
"gopkg.in/redis.v3"
"os"
"regexp"
"strconv"
"time"
)
func main() {
// init
app := cli.NewApp()
app.Name = "stadis"
app.Version = "0.0.2"
app.Usage = "get redis info and submit to statsd"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "redis-host, r",
Value: "localhost:6379",
Usage: "host:port of redis servier",
},
cli.StringFlag{
Name: "statsd-host, s",
Value: "localhost:8125",
Usage: "host:port of statsd servier",
},
cli.StringFlag{
Name: "prefix,p",
Usage: "host:port of redis servier",
EnvVar: "HOSTNAME",
},
cli.StringFlag{
Name: "interval,i",
Usage: "time in milliseconds to periodically check redis",
Value: "5000",
},
}
app.Action = func(c *cli.Context) {
for {
info := getStats(c.String("redis-host"))
gauges := parseGauges(info)
counters := parseCounters(info)
sendStats(c.String("statsd-host"), c.String("prefix"), gauges, counters)
color.White("-------------------")
interval, _ := strconv.ParseInt(c.String("interval"), 10, 64)
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
app.Run(os.Args)
}
func getStats(addrs string) string {
client := redis.NewClient(&redis.Options{
Addr: addrs,
Password: "", // no password set
DB: 0, // use default DB
})
info, err := client.Info().Result()
if err != nil {
color.Red("ERROR: can't connect to redis") //possibly send to statsd also
}
// DEBUG: fmt.Println(info)
return info
}
func sendStats(statsdHost string, prefix string, gauges map[string]int64, counters map[string]int64) {
client, err := statsd.NewClient(statsdHost, prefix)
if err != nil {
color.Red("ERROR: can't connect to statsd")
}
defer client.Close()
sendGauges(client, gauges)
sendCounters(client, counters)
}
func sendGauges(client statsd.Statter, gauges map[string]int64) {
for gauge, value := range gauges {
client.Gauge(gauge, value, 1.0)
}
}
func sendCounters(client statsd.Statter, counters map[string]int64) {
for counter, value := range counters {
client.Inc(counter, value, 1.0)
}
}
func parseGauges(info string) map[string]int64 {
gauges_with_values := map[string]int64{
"blocked_clients": 0,
"connected_clients": 0,
"instantaneous_ops_per_sec": 0,
"latest_fork_usec": 0,
"mem_fragmentation_ratio": 0,
"migrate_cached_sockets": 0,
"pubsub_channels": 0,
"pubsub_patterns": 0,
"uptime_in_seconds": 0,
"used_memory": 0,
"used_memory_lua": 0,
"used_memory_peak": 0,
"used_memory_rss": 0,
}
color.White("-------------------")
color.White("GAUGES:")
for gauge, _ := range gauges_with_values {
r, _ := regexp.Compile(fmt.Sprint(gauge, ":([0-9]*)"))
matches := r.FindStringSubmatch(info)
if matches == nil {
color.Yellow(fmt.Sprint("WARN: ", gauge, "is not displayed in redis info"))
} else {
value := matches[len(matches)-1]
color.Cyan(fmt.Sprint(gauge, ": ", value))
v, _ := strconv.ParseInt(value, 10, 64)
gauges_with_values[gauge] = v
}
}
return gauges_with_values
}
func parseCounters(info string) map[string]int64 {
counters := map[string]int64{
"evicted_keys": 0,
"expired_keys": 0,
"keyspace_hits": 0,
"keyspace_misses": 0,
"rejected_connections": 0,
"sync_full": 0,
"sync_partial_err": 0,
"sync_partial_ok": 0,
"total_commands_processed": 0,
"total_connections_received": 0,
}
color.White("-------------------")
color.White("COUNTERS:")
for counter, _ := range counters {
r, _ := regexp.Compile(fmt.Sprint(counter, ":([0-9]*)"))
matches := r.FindStringSubmatch(info)
if matches == nil {
color.Yellow(fmt.Sprint("ERROR: ", counter, "is not displayed in redis info"))
} else {
value := matches[len(matches)-1]
color.Cyan(fmt.Sprint(counter, ": ", value))
v, _ := strconv.ParseInt(value, 10, 64)
counters[counter] = v
}
}
return counters
}