forked from fujiwara/fluent-agent-hydra
/
monitor.go
173 lines (151 loc) · 3.57 KB
/
monitor.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
package hydra
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"sync"
"time"
stats_api "github.com/fukata/golang-stats-api-handler"
)
const (
DefaultMonitorPort = 24223
DefaultMonitorHost = "localhost"
)
type Stats struct {
Sent map[string]*SentStat `json:"sent"`
Files map[string]*FileStat `json:"files"`
Servers []*ServerStat `json:"servers"`
Receiver *ReceiverStat `json:"receiver"`
mu sync.Mutex
}
type Stat interface {
ApplyTo(*Stats)
}
type ServerStat struct {
Index int `json:"-"`
Address string `json:"address"`
Alive bool `json:"alive"`
Error string `json:"error"`
}
type SentStat struct {
Tag string `json:"-"`
Messages int64 `json:"messages"`
Bytes int64 `json:"bytes"`
Sents int64 `json:"sents"`
}
type FileStat struct {
Tag string `json:"tag"`
File string `json:"-"`
Position int64 `json:"position"`
Error string `json:"error"`
}
type ReceiverStat struct {
Address string `json:"address"`
Connections int `json:"-"`
TotalConnections int `json:"total_connections"`
CurrentConnections int `json:"current_connections"`
Messages int64 `json:"messages"`
Disposed int64 `json:"disposed"`
Buffered int64 `json:"buffered"`
MaxBufferMessages int64 `json:"max_buffer_messages"`
}
func (s *FileStat) ApplyTo(ss *Stats) {
ss.mu.Lock()
defer ss.mu.Unlock()
ss.Files[s.File] = s
}
func (s *ServerStat) ApplyTo(ss *Stats) {
ss.mu.Lock()
defer ss.mu.Unlock()
ss.Servers[s.Index] = s
}
func (s *SentStat) ApplyTo(ss *Stats) {
ss.mu.Lock()
defer ss.mu.Unlock()
if _s, ok := ss.Sent[s.Tag]; ok {
_s.Messages += s.Messages
_s.Bytes += s.Bytes
_s.Sents += s.Sents
} else {
ss.Sent[s.Tag] = s
}
}
func (s *ReceiverStat) ApplyTo(ss *Stats) {
if ss.Receiver == nil {
ss.Receiver = s
return
}
rs := ss.Receiver
if s.Address != "" {
rs.Address = s.Address
}
if s.Connections > 0 {
rs.TotalConnections += s.Connections
}
rs.CurrentConnections += s.Connections
rs.Messages += s.Messages
rs.Disposed += s.Disposed
rs.Buffered = s.Buffered
}
func (ss *Stats) WriteJSON(w http.ResponseWriter) {
ss.mu.Lock()
defer ss.mu.Unlock()
encoder := json.NewEncoder(w)
encoder.Encode(ss)
}
func (ss *Stats) Run(ch chan Stat) {
for {
s := <-ch
s.ApplyTo(ss)
}
}
type Monitor struct {
stats *Stats
address string
Addr net.Addr
listener net.Listener
monitorCh chan Stat
}
func NewMonitor(config *Config) (*Monitor, error) {
stats := &Stats{
Sent: make(map[string]*SentStat),
Files: make(map[string]*FileStat),
Servers: make([]*ServerStat, len(config.Servers)),
}
monitor := &Monitor{
stats: stats,
}
if config.Monitor == nil {
return monitor, nil
}
monitorAddress := fmt.Sprintf("%s:%d", config.Monitor.Host, config.Monitor.Port)
listener, err := net.Listen("tcp", monitorAddress)
if err != nil {
log.Println("[error]", err)
return nil, err
}
monitor.listener = listener
monitor.Addr = listener.Addr()
return monitor, nil
}
func (m *Monitor) Run(c *Context) {
c.OutputProcess.Add(1)
defer c.OutputProcess.Done()
go m.stats.Run(c.MonitorCh)
c.StartProcess.Done()
if m.listener == nil {
return
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
m.stats.WriteJSON(w)
})
http.HandleFunc("/system", stats_api.Handler)
go http.Serve(m.listener, nil)
log.Printf("[info] Monitor server listening http://%s/\n", m.listener.Addr())
}
func monitorError(err error) string {
return fmt.Sprintf("[%s] %s", time.Now(), err)
}