forked from Terry-Mao/gopush-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
280 lines (252 loc) · 6.64 KB
/
stat.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright © 2014 Terry Mao, LiuDing All rights reserved.
// This file is part of gopush-cluster.
// gopush-cluster is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gopush-cluster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gopush-cluster. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"github.com/golang/glog"
"net/http"
"os"
"os/user"
"runtime"
"sync/atomic"
"time"
)
var (
// server
startTime int64 // process start unixnano
// channel
ChStat = &ChannelStat{}
// message
MsgStat = &MessageStat{}
// connection
ConnStat = &ConnectionStat{}
)
// Channel stat info
type ChannelStat struct {
Access uint64 // total access count
Create uint64 // total create count
Delete uint64 // total delete count
}
func (s *ChannelStat) IncrAccess() {
atomic.AddUint64(&s.Access, 1)
}
func (s *ChannelStat) IncrCreate() {
atomic.AddUint64(&s.Create, 1)
}
func (s *ChannelStat) IncrDelete() {
atomic.AddUint64(&s.Delete, 1)
}
// Stat get the channle stat info
func (s *ChannelStat) Stat() []byte {
res := map[string]interface{}{}
res["access"] = s.Access
res["create"] = s.Create
res["delete"] = s.Delete
res["current"] = UserChannel.Count()
return jsonRes(res)
}
// Message stat info
type MessageStat struct {
Succeed uint64 // total push message succeed count
Failed uint64 // total push message failed count
}
func (s *MessageStat) IncrSucceed(delta uint64) {
atomic.AddUint64(&s.Succeed, delta)
}
func (s *MessageStat) IncrFailed(delta uint64) {
atomic.AddUint64(&s.Failed, delta)
}
// Stat get the message stat info
func (s *MessageStat) Stat() []byte {
res := map[string]interface{}{}
res["succeed"] = s.Succeed
res["failed"] = s.Failed
res["total"] = s.Succeed + s.Failed
return jsonRes(res)
}
// Connection stat info
type ConnectionStat struct {
Add uint64 // total add connection count
Remove uint64 // total remove connection count
}
func (s *ConnectionStat) IncrAdd() {
atomic.AddUint64(&s.Add, 1)
}
func (s *ConnectionStat) IncrRemove() {
atomic.AddUint64(&s.Remove, 1)
}
// Stat get the connection stat info
func (s *ConnectionStat) Stat() []byte {
res := map[string]interface{}{}
res["add"] = s.Add
res["remove"] = s.Remove
res["current"] = s.Add - s.Remove
return jsonRes(res)
}
func statListen(bind string) {
httpServeMux := http.NewServeMux()
httpServeMux.HandleFunc("/stat", StatHandle)
if err := http.ListenAndServe(bind, httpServeMux); err != nil {
glog.Errorf("http.ListenAdServe(\"%s\") error(%v)", bind, err)
panic(err)
}
}
// start stats, called at process start
func StartStats() {
startTime = time.Now().UnixNano()
for _, bind := range Conf.StatBind {
glog.Infof("start stat listen addr:\"%s\"", bind)
go statListen(bind)
}
}
// memory stats
func MemStats() []byte {
m := &runtime.MemStats{}
runtime.ReadMemStats(m)
// general
res := map[string]interface{}{}
res["alloc"] = m.Alloc
res["total_alloc"] = m.TotalAlloc
res["sys"] = m.Sys
res["lookups"] = m.Lookups
res["mallocs"] = m.Mallocs
res["frees"] = m.Frees
// heap
res["heap_alloc"] = m.HeapAlloc
res["heap_sys"] = m.HeapSys
res["heap_idle"] = m.HeapIdle
res["heap_inuse"] = m.HeapInuse
res["heap_released"] = m.HeapReleased
res["heap_objects"] = m.HeapObjects
// low-level fixed-size struct alloctor
res["stack_inuse"] = m.StackInuse
res["stack_sys"] = m.StackSys
res["mspan_inuse"] = m.MSpanInuse
res["mspan_sys"] = m.MSpanSys
res["mcache_inuse"] = m.MCacheInuse
res["mcache_sys"] = m.MCacheSys
res["buckhash_sys"] = m.BuckHashSys
// GC
res["next_gc"] = m.NextGC
res["last_gc"] = m.LastGC
res["pause_total_ns"] = m.PauseTotalNs
res["pause_ns"] = m.PauseNs
res["num_gc"] = m.NumGC
res["enable_gc"] = m.EnableGC
res["debug_gc"] = m.DebugGC
res["by_size"] = m.BySize
return jsonRes(res)
}
// golang stats
func GoStats() []byte {
res := map[string]interface{}{}
res["compiler"] = runtime.Compiler
res["arch"] = runtime.GOARCH
res["os"] = runtime.GOOS
res["max_procs"] = runtime.GOMAXPROCS(-1)
res["root"] = runtime.GOROOT()
res["cgo_call"] = runtime.NumCgoCall()
res["goroutine_num"] = runtime.NumGoroutine()
res["version"] = runtime.Version()
return jsonRes(res)
}
// server stats
func ServerStats() []byte {
res := map[string]interface{}{}
res["uptime"] = time.Now().UnixNano() - startTime
hostname, _ := os.Hostname()
res["hostname"] = hostname
wd, _ := os.Getwd()
res["wd"] = wd
res["ppid"] = os.Getppid()
res["pid"] = os.Getpid()
res["pagesize"] = os.Getpagesize()
if usr, err := user.Current(); err != nil {
glog.Errorf("user.Current() error(%v)", err)
res["group"] = ""
res["user"] = ""
} else {
res["group"] = usr.Gid
res["user"] = usr.Uid
}
return jsonRes(res)
}
// configuration info
func ConfigInfo() []byte {
byteJson, err := json.MarshalIndent(Conf, "", " ")
if err != nil {
glog.Errorf("json.MarshalIndent(\"%v\", \"\", \" \") error(%v)", Conf, err)
return nil
}
return byteJson
}
// jsonRes format the output
func jsonRes(res map[string]interface{}) []byte {
byteJson, err := json.MarshalIndent(res, "", " ")
if err != nil {
glog.Errorf("json.MarshalIndent(\"%v\", \"\", \" \") error(%v)", res, err)
return nil
}
return byteJson
}
func ChInfoStat(key string) []byte {
res := map[string]interface{}{}
if ch, err := UserChannel.Get(key, false); err == nil {
if sch, ok := ch.(*SeqChannel); ok {
res["channel"] = map[string]interface{}{"conn": sch.conn.Len()}
} else {
return nil
}
} else {
return nil
}
return jsonRes(res)
}
// StatHandle get stat info by http
func StatHandle(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", 405)
return
}
params := r.URL.Query()
types := params.Get("type")
res := []byte{}
switch types {
case "memory":
res = MemStats()
case "server":
res = ServerStats()
case "golang":
res = GoStats()
case "config":
res = ConfigInfo()
case "channel":
key := params.Get("key")
if key == "" {
res = ChStat.Stat()
} else {
res = ChInfoStat(key)
}
case "message":
res = MsgStat.Stat()
case "connection":
res = ConnStat.Stat()
default:
http.Error(w, "Not Found", 404)
}
if res != nil {
if _, err := w.Write(res); err != nil {
glog.Errorf("w.Write(\"%s\") error(%v)", string(res), err)
}
}
}