-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
207 lines (178 loc) · 4.94 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"sort"
"text/template"
"time"
"github.com/gorilla/mux"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/home.html")
if err != nil {
log.Printf("%s\n", err.Error())
return
}
w.WriteHeader(200)
w.Header().Set("Content-type", "text/html")
traces := getTraces()
if t.Execute(w, map[string]interface{}{"traces": traces}) != nil {
log.Printf("%s\n", err.Error())
}
}
func checkTraceID(v string) bool {
rep := regexp.MustCompile(`^[0-9]{8}-[0-9]{6}$`)
return rep.MatchString(v)
}
type Trace struct {
ID string `json:"id"`
CPUProf bool `json:"cpuprof"`
Accesslog bool `json:"accesslog"`
SQLLog bool `json:"sqllog"`
VMStat bool `json:"vmstat"`
AccessLogSize int64
SQLLogSize int64
PerfLogSize int64
WRLogSize int64
ExecAt time.Time
}
func getFileSize(filename string) int64 {
file, err := os.Open(filename)
if err != nil {
return 0
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return 0
}
return stat.Size()
}
func getTraces() []Trace {
files, err := ioutil.ReadDir("./logs")
if err != nil {
panic(err)
}
traceList := []Trace{}
jst := time.FixedZone("Asia/Tokyo", 9*60*60)
for _, file := range files {
if checkTraceID(file.Name()) {
traceID := file.Name()
t, _ := time.Parse("20060102-150405", traceID)
jt := t.In(jst)
trace := Trace{ID: traceID, ExecAt: jt}
trace.AccessLogSize = getFileSize("./logs/" + traceID + "/access.log")
trace.SQLLogSize = getFileSize("./logs/" + traceID + "/sql.log")
trace.PerfLogSize = getFileSize("./logs/" + traceID + "/perf.log")
trace.WRLogSize = getFileSize("./logs/" + traceID + "/webroute.log")
traceList = append(traceList, trace)
}
}
sort.Slice(traceList, func(i int, j int) bool { return traceList[i].ID > traceList[j].ID })
return traceList
}
func parseHandler(w http.ResponseWriter, r *http.Request, filename string, progname string) {
r.ParseForm()
id := r.FormValue("id")
if !checkTraceID(id) {
return
}
w.WriteHeader(200)
w.Header().Add("Content-type", "text/plain")
fp, err := os.Open("./logs/" + id + "/" + filename)
if err != nil {
panic(err)
}
cmd := exec.Command(progname)
cmd.Stdin = fp
cmd.Stdout = w
cmd.Run()
}
func kataribeHandler(w http.ResponseWriter, r *http.Request) {
parseHandler(w, r, "access.log", "./kataribe")
}
func alpHandler(w http.ResponseWriter, r *http.Request) {
parseHandler(w, r, "access.log", "./alp.sh")
}
func sqlparseHandler(w http.ResponseWriter, r *http.Request) {
parseHandler(w, r, "sql.log", "./parse_log.py")
}
func perfparseHandler(w http.ResponseWriter, r *http.Request) {
parseHandler(w, r, "perf.log", "./parse_log.py")
}
func wrparseHandler(w http.ResponseWriter, r *http.Request) {
parseHandler(w, r, "webroute.log", "./parse_log.py")
}
func ptQueryDigestHandler(w http.ResponseWriter, r *http.Request) {
parseHandler(w, r, "mysql-slow.log", "pt-query-digest")
}
func outputFileHandler(w http.ResponseWriter, r *http.Request, filename string) {
r.ParseForm()
id := r.FormValue("id")
if !checkTraceID(id) {
return
}
fp, err := os.Open("./logs/" + id + "/" + filename)
if err != nil {
panic(err)
}
buf, err := ioutil.ReadAll(fp)
if err != nil {
panic(err)
}
fp.Close()
w.WriteHeader(200)
w.Header().Add("Content-type", "text/plain")
w.Write(buf)
}
func sqllogHandler(w http.ResponseWriter, r *http.Request) {
outputFileHandler(w, r, "sql.log")
}
func accesslogHandler(w http.ResponseWriter, r *http.Request) {
outputFileHandler(w, r, "access.log")
}
func vmstatHandler(w http.ResponseWriter, r *http.Request) {
outputFileHandler(w, r, "vmstat.log")
}
func perflogHandler(w http.ResponseWriter, r *http.Request) {
outputFileHandler(w, r, "perf.log")
}
func wrlogHandler(w http.ResponseWriter, r *http.Request) {
outputFileHandler(w, r, "webroute.log")
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RequestURI)
next.ServeHTTP(w, r)
})
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
r := mux.NewRouter()
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.Handle("/favicon.ico", fs)
r.HandleFunc("/", homeHandler)
r.HandleFunc("/kataribe/", kataribeHandler)
r.HandleFunc("/alp/", alpHandler)
r.HandleFunc("/sqlparse/", sqlparseHandler)
r.HandleFunc("/perfparse/", perfparseHandler)
r.HandleFunc("/ptquery/", ptQueryDigestHandler)
r.HandleFunc("/wrparse/", wrparseHandler)
r.HandleFunc("/accesslog/", accesslogHandler)
r.HandleFunc("/sqllog/", sqllogHandler)
r.HandleFunc("/perflog/", perflogHandler)
r.HandleFunc("/wrlog/", wrlogHandler)
r.Use(loggingMiddleware)
http.Handle("/", r)
log.Printf("Start App: listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}