-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
246 lines (204 loc) · 6.29 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"runtime/pprof"
"sort"
"strconv"
"strings"
"time"
"github.com/gosuri/uilive"
"gopkg.in/redis.v4"
)
// Config ...
type Config struct {
RedisHosts map[string][]string `json:"redis_hosts"`
}
var (
refreshRate int
valLastTotal int64
valCurrTotal int64
maxColWidth int
maxColWidthMedium int
debug bool
buildDate string
version string
commitHash string
lastRedisHostStats map[string]map[string]int64
currRedisHostStats map[string]map[string]int64
)
func main() {
v := flag.Bool("v", false, "prints current version and exits")
refreshRate := flag.Int("r", 2, "Referesh rate (seconds)")
conf := flag.String("c", "", "Config containing the redis hosts along with the lists to monitor")
debug := flag.Bool("d", false, "Enable debug mode (write cpu profile to file)")
flag.Parse()
if *v {
fmt.Printf("Version %s (commit: %s, %s)\n", version, commitHash, buildDate)
os.Exit(0)
}
if *debug {
f, err := os.Create("rltop.cpuprofile")
if err != nil {
log.Fatal("Could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("Could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
valLastTotal = 0
valCurrTotal = 0
maxColWidth = 38
maxColWidthMedium = 26
writer := uilive.New()
redisConf, err := loadConfig(*conf)
if err != nil {
exitWithMessage("ERROR", err.Error(), false)
}
redisClientConns := getRedisClientConns(redisConf)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
writer.Stop()
if *debug {
pprof.StopCPUProfile()
}
os.Exit(0)
}()
lastRedisHostStats = make(map[string]map[string]int64, len(redisConf.RedisHosts))
currRedisHostStats = make(map[string]map[string]int64, len(redisConf.RedisHosts))
writer.Start()
var consoleOutput bytes.Buffer
var diff int64
var diffSign string
firstItteration := true
// Initialize the slize of redis host connections
// ------------- Create a slice containing the redis hosts sorted -------------
sortedRedisHosts := make([]string, len(redisConf.RedisHosts))
i := 0
for k, _ := range redisConf.RedisHosts {
sortedRedisHosts[i] = k
i++
}
sort.Strings(sortedRedisHosts)
// -------------- Create another slice with the list of keys in sorted order for each redis host --------------
sortedRedisLists := map[string][]string{}
for rh, lists := range redisConf.RedisHosts {
sort.Strings(lists)
sortedRedisLists[rh] = lists
}
for {
// Get the list lengths for the current itteration
for h, c := range redisClientConns {
// If the index of the given redis host isn't set, then set it
if _, ok := currRedisHostStats[h]; !ok {
currRedisHostStats[h] = make(map[string]int64, len(redisConf.RedisHosts[h]))
}
for _, list := range redisConf.RedisHosts[h] {
// If the index of the given redis list isn't set, then set it
if _, ok := currRedisHostStats[h][list]; !ok {
currRedisHostStats[h][list] = 0
}
llen := c.LLen(list).Val()
currRedisHostStats[h][list] = llen
} // End of innner for loop
} // End of outter for loop
consoleOutput.WriteString(fmt.Sprintf("|%124s|\n", strings.Repeat("-", 123)))
consoleOutput.WriteString(fmt.Sprintf("|%-50s|%31s|%22s|%18s|\n", " Redis Host ", " List ", " Length ", " Diff "))
consoleOutput.WriteString(fmt.Sprintf("|%124s|\n", strings.Repeat("-", 123)))
if !firstItteration {
for _, rh := range sortedRedisHosts {
for _, list := range sortedRedisLists[rh] {
/*
1. If last reported list length is greater than this one, then it's a -DIFF (more consumed than put in)
2 If last reported list length is smaller than this one, then it's a +DIFF (more put in than consumed)
*/
if lastRedisHostStats[rh][list] > currRedisHostStats[rh][list] {
diff = lastRedisHostStats[rh][list] - currRedisHostStats[rh][list]
diffSign = "-"
} else if lastRedisHostStats[rh][list] < currRedisHostStats[rh][list] {
diff = currRedisHostStats[rh][list] - lastRedisHostStats[rh][list]
diffSign = "+"
} else if lastRedisHostStats[rh][list] == 0 && currRedisHostStats[rh][list] == 0 {
diff = 0
diffSign = ""
}
consoleOutput.WriteString(fmt.Sprintf("|%-50s|%31s|%22s|%18s|\n", " "+rh, list, strconv.FormatInt(currRedisHostStats[rh][list], 10), fmt.Sprintf("%s%d", diffSign, diff)))
}
consoleOutput.WriteString(fmt.Sprintf("|%124s|\n", strings.Repeat("-", 123)))
}
} else {
firstItteration = false
}
fmt.Fprintf(writer, "%s", fmt.Sprint(consoleOutput.String()))
/* Now set the last redis list length values */
for h, lists := range currRedisHostStats {
if _, ok := lastRedisHostStats[h]; !ok {
lastRedisHostStats[h] = make(map[string]int64, len(redisConf.RedisHosts[h]))
}
for list := range lists {
if _, ok := lastRedisHostStats[h][list]; !ok {
lastRedisHostStats[h][list] = 0
}
lastRedisHostStats[h][list] = currRedisHostStats[h][list]
}
}
time.Sleep(time.Second * time.Duration(*refreshRate))
consoleOutput.Reset()
}
}
// Parse is a function that unmarshals the specified yaml config file
func (c *Config) Parse(data []byte) error {
if err := json.Unmarshal(data, c); err != nil {
return err
}
if len(c.RedisHosts) == 0 {
return errors.New("Must have at least one redis host to monitor")
}
return nil
}
func getRedisClientConns(conf *Config) map[string]*redis.Client {
clientConns := map[string]*redis.Client{}
for h := range conf.RedisHosts {
conn := redis.NewClient(&redis.Options{
Addr: h,
DB: 0,
})
clientConns[h] = conn
}
return clientConns
}
func loadConfig(filname string) (*Config, error) {
content, err := ioutil.ReadFile(filname)
if err != nil {
return nil, err
}
var config Config
if err := config.Parse(content); err != nil {
return nil, err
}
return &config, nil
}
func getRedisInfo(c *redis.Client) string {
return c.Info().Val()
}
func exitWithMessage(level string, msg string, showUsage bool) {
fmt.Printf("[%s] %s\n", level, msg)
if showUsage {
flag.PrintDefaults()
}
os.Exit(1)
}
func centerFill(s string, colWidth int, fill string) string {
div := (colWidth - len(s)) / 2
return strings.Repeat(fill, div) + s + strings.Repeat(fill, div)
}