-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
86 lines (74 loc) · 2.53 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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"time"
"github.com/Sirupsen/logrus"
)
var Logger = logrus.New()
var (
Status *RedisStatus
CmdLimit int
Frequency int
MonitorSampleLength int
SlowlogSize int
RedisHost string
RedisPort int
RedisPassword string
WebPort int
)
func init() {
Logger.Formatter = new(logrus.TextFormatter)
//Logger.Level = logrus.Info
Logger.Level = logrus.Debug
Status = &RedisStatus{Info: make(map[string]interface{}), Slowlogs: make([]Slowlog, 0), MonitorSample: make([]*MonitorCmd, 0), stats: NewStats()}
flag.StringVar(&RedisHost, "h", "127.0.0.1", "Redis host ")
flag.IntVar(&RedisPort, "p", 6379, "Redis port")
flag.StringVar(&RedisPassword, "a", "", "Redis password")
flag.IntVar(&WebPort, "webport", 7071, "Port to run the http server on")
flag.IntVar(&CmdLimit, "cmdlimit", 100, "number of commands the MONITOR will store")
flag.IntVar(&Frequency, "frequency", 60, "Number of seconds to delay between samples INFO, SLOWLOG")
flag.IntVar(&MonitorSampleLength, "monsamplen", 1000, "Length of miliseconds that the monitor is sampled (0 will be coninuous however this is very costly to performance)")
flag.IntVar(&SlowlogSize, "slogsize", 10, "SLOWLOG size")
}
type CommandStats struct {
CommandCounts map[string]int64 `json:"command_counts"`
BadCommands map[string]int64 `json:"bad_commands"`
}
type RedisStatus struct {
CommandStats CommandStats `json:"stats"`
Slowlogs []Slowlog `json:"slowlogs"`
Info map[string]interface{} `json:"info"`
MonitorSample []*MonitorCmd `json:"monitor_sample"`
stats *Stats
}
func main() {
flag.Parse()
rc := NewRedisCmds()
stopper := make(chan bool, 1)
go SampleMonitor(rc, stopper, Status)
go func() {
for {
_, err := SampleInfo(rc, Status)
if err != nil {
Logger.Errorf("SampleInfo error %s\n", err)
}
_, err = SampleSlowlog(rc, Status)
if err != nil {
Logger.Errorf("Error with slowLogger %s", err)
}
time.Sleep(time.Duration(Frequency) * time.Second)
}
}()
Logger.Debug("Starting up the http handler")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
// TODO: Fix this just added before hack session timed out
Status.CommandStats.CommandCounts = Status.stats.Counts()
Status.CommandStats.BadCommands = Status.stats.BadCmds()
enc.Encode(Status)
})
Logger.Fatalf("Failed %s", http.ListenAndServe(fmt.Sprintf(":%d", WebPort), nil))
}