-
Notifications
You must be signed in to change notification settings - Fork 39
/
monitor.go
322 lines (281 loc) · 9.04 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package serv
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/stratosnet/sds/pp/p2pserver"
"github.com/stratosnet/sds/pp/setting"
"github.com/stratosnet/sds/rpc"
"github.com/stratosnet/sds/utils"
)
const (
MSG_GET_TRAFFIC_DATA_RESPONSE = "monitor_getTrafficData"
MSG_GET_DIST_USAGE_RESPONSE = "monitor_getDiskUsage"
MSG_GET_PEER_LIST = "monitor_getPeerList"
MSG_GET_ONLINE_STATE = "monitor_getOnlineState"
MSG_GET_NODE_DETAILS = "monitor_getNodeDetails"
)
type DiskUsage struct {
DataHost int64 `json:"data_host"`
}
type PeerInfo struct {
NetworkAddress string `json:"network_address"`
P2pAddress string `json:"p2p_address"`
Status int `json:"status"`
Latency int64 `json:"latency"`
Connection string `json:"connection"`
}
type PeerList struct {
Total int64 `json:"total"`
PeerList []PeerInfo `json:"peerlist"`
}
type OnlineState struct {
Online bool `json:"online"`
Since int64 `json:"since"`
}
type NodeDetails struct {
Id string `json:"id"`
Address string `json:"address"`
AdvancedDetails *[]struct {
Id string `json:"id"`
Title string `json:"title"`
Details string `json:"details"`
} `json:"advanced_details,omitempty"`
}
type MonitorResult struct {
Return string `json:"return"`
MessageType string `json:"message_type"`
TrafficInfo *[]TrafficInfo `json:"traffic_info,omitempty"`
OnlineState *OnlineState `json:"online_state,omitempty"`
PeerList *PeerList `json:"peer_list,omitempty"`
DiskUsage *DiskUsage `json:"disk_usage,omitempty"`
NodeDetails *NodeDetails `json:"node_details,omitempty"`
}
type MonitorNotificationResult struct {
TrafficInfo *TrafficInfo `json:"traffic_info"`
OnlineState *OnlineState `json:"online_state,omitempty"`
PeerList *PeerList `json:"peer_list,omitempty"`
DiskUsage *DiskUsage `json:"disk_usage,omitempty"`
}
type ParamTrafficInfo struct {
SubId string `json:"subid"`
Lines uint64 `json:"lines"`
}
type ParamMonitor struct {
SubId string `json:"subid"`
}
type monitorApi struct {
}
func MonitorApi() *monitorApi {
return &monitorApi{}
}
func monitorAPI() []rpc.API {
return []rpc.API{
{
Namespace: "monitor",
Version: "1.0",
Service: MonitorApi(),
Public: true,
},
}
}
// key: rpc.Subscription, value: chan TranfficInfo
var subscriptions = &sync.Map{}
// key: rpc.Subscription.ID (string), value: struct{} (nothing)
var subscribedIds = &sync.Map{}
// subscribeTrafficInfo subscribe the channel to receive the traffic info from the generator
func subscribeTrafficInfo(s rpc.Subscription, c chan TrafficInfo) {
var e struct{}
subscribedIds.Store(string(s.ID), e)
subscriptions.Store(s, c)
}
// unsubscribeTrafficInfo unsubscribe the channel listening to the traffic info
func unsubscribeTrafficInfo(s rpc.Subscription) {
subscribedIds.Delete(s.ID)
subscriptions.Delete(s)
}
// TrafficInfoToMonitorClient traffic info generator calls this to feed the notifiers to the subscribed clients
func TrafficInfoToMonitorClient(t TrafficInfo) {
subscriptions.Range(func(k, v interface{}) bool {
v.(chan TrafficInfo) <- t
return true
})
}
// CreateInitialToken the initial token is used to generate all following tokens
func CreateInitialToken() string {
epoch := strconv.FormatInt(time.Now().Unix(), 10)
return utils.CalcHash([]byte(epoch + setting.Config.Keys.P2PAddress))
}
// calculateToken
func calculateToken(time int64) string {
t := strconv.FormatInt(time, 10)
return utils.CalcHash([]byte(t + setting.MonitorInitialToken))
}
// GetCurrentToken
func GetCurrentToken() string {
return calculateToken(time.Now().Unix() / 3600)
}
// verifyToken verify if the input token matches the generated token
func verifyToken(token string) bool {
t := time.Now().Unix() / 3600
if calculateToken(t) == token {
return true
} else if calculateToken(t-1) == token {
return true
}
return false
}
// GetTrafficData fetch the traffic data from the file
func (api *monitorApi) GetTrafficData(ctx context.Context, param ParamTrafficInfo) (*MonitorResult, error) {
if _, found := subscribedIds.Load(param.SubId); !found {
return nil, errors.New("client hasn't subscribed to the service")
}
lines := utils.GetLastLinesFromTrafficLog(setting.TrafficLogPath, param.Lines)
var ts []TrafficInfo
var line string
for _, line = range lines {
if len(line) <= 26 {
return &MonitorResult{Return: "-1", MessageType: MSG_GET_TRAFFIC_DATA_RESPONSE}, nil
}
date := line[7:26]
content := strings.SplitN(line, "{", 2)
if len(content) < 2 {
return &MonitorResult{Return: "-1", MessageType: MSG_GET_TRAFFIC_DATA_RESPONSE}, nil
}
c := "{" + content[1]
var trafficDumpInfo TrafficDumpInfo
err := json.Unmarshal([]byte(c), &trafficDumpInfo)
if err != nil {
return &MonitorResult{Return: "-1", MessageType: MSG_GET_TRAFFIC_DATA_RESPONSE}, nil
}
trafficDumpInfo.TrafficInfo.TimeStamp = date
ts = append(ts, trafficDumpInfo.TrafficInfo)
}
if ts == nil {
return &MonitorResult{Return: "-1", MessageType: MSG_GET_TRAFFIC_DATA_RESPONSE}, nil
}
return &MonitorResult{Return: "0", MessageType: MSG_GET_TRAFFIC_DATA_RESPONSE, TrafficInfo: &ts}, nil
}
func getDiskUsage() int64 {
var size int64
_ = filepath.Walk(setting.Config.Home.StoragePath, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size
}
// GetDiskUsage the size of files in setting.Config.StorehousePath, not the disk usage of the computer
func (api *monitorApi) GetDiskUsage(ctx context.Context, param ParamMonitor) (*MonitorResult, error) {
if _, found := subscribedIds.Load(param.SubId); !found {
return nil, errors.New("client hasn't subscribed to the service")
}
return &MonitorResult{Return: "0", MessageType: MSG_GET_DIST_USAGE_RESPONSE, DiskUsage: &DiskUsage{DataHost: getDiskUsage()}}, nil
}
func getPeerList(ctx context.Context) ([]PeerInfo, int64) {
if ps := p2pserver.GetP2pServer(ctx); ps != nil {
pl, t, _ := ps.GetPPList(ctx)
var peer PeerInfo
var peers []PeerInfo
var i int64
for i = 0; i < t; i++ {
peer = PeerInfo{
NetworkAddress: pl[i].NetworkAddress,
P2pAddress: pl[i].P2pAddress,
Status: pl[i].Status,
Latency: pl[i].Latency,
Connection: "tcp4",
}
peers = append(peers, peer)
}
return peers, t
}
return nil, 0
}
// GetPeerList the peer pp list
func (api *monitorApi) GetPeerList(ctx context.Context, param ParamMonitor) (*MonitorResult, error) {
if _, found := subscribedIds.Load(param.SubId); !found {
return nil, errors.New("client hasn't subscribed to the service")
}
peers, t := getPeerList(ctx)
return &MonitorResult{
Return: "0",
MessageType: MSG_GET_PEER_LIST,
PeerList: &PeerList{Total: t, PeerList: peers},
}, nil
}
// GetOnlineState if the pp node is online
func (api *monitorApi) GetOnlineState(ctx context.Context, param ParamMonitor) (*MonitorResult, error) {
if _, found := subscribedIds.Load(param.SubId); !found {
return nil, errors.New("client hasn't subscribed to the service")
}
return &MonitorResult{
Return: "0",
MessageType: MSG_GET_ONLINE_STATE,
OnlineState: &OnlineState{Online: setting.OnlineTime != 0, Since: setting.OnlineTime},
}, nil
}
// GetNodeDetail the deatils of the node
func (api *monitorApi) GetNodeDetails(ctx context.Context, param ParamMonitor) (*MonitorResult, error) {
if _, found := subscribedIds.Load(param.SubId); !found {
return nil, errors.New("client hasn't subscribed to the service")
}
return &MonitorResult{
Return: "0",
MessageType: MSG_GET_NODE_DETAILS,
NodeDetails: &NodeDetails{Id: "1", Address: setting.Config.Keys.P2PAddress},
}, nil
}
// Subscription client calls the method monitor_subscribe with this function as the parameter
func (api *monitorApi) Subscription(ctx context.Context, token string) (*rpc.Subscription, error) {
if !verifyToken(token) {
return nil, errors.New("failed token check")
}
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
if notifier == nil {
return &rpc.Subscription{}, errors.New("invalid notifier")
}
var (
rpcSub = notifier.CreateSubscription()
trafficInfo = make(chan TrafficInfo)
)
if rpcSub == nil {
return nil, errors.New("invalid subscriber")
}
subscribeTrafficInfo(*rpcSub, trafficInfo)
go func() {
for {
select {
case ti := <-trafficInfo:
peers, t := getPeerList(ctx)
result := &MonitorNotificationResult{
TrafficInfo: &ti,
OnlineState: &OnlineState{Online: setting.OnlineTime != 0, Since: setting.OnlineTime},
PeerList: &PeerList{Total: t, PeerList: peers},
DiskUsage: &DiskUsage{DataHost: getDiskUsage()},
}
_ = notifier.Notify(rpcSub.ID, result)
case <-rpcSub.Err(): // client send an unsubscribe request
unsubscribeTrafficInfo(*rpcSub)
return
case <-notifier.Closed(): // connection dropped
unsubscribeTrafficInfo(*rpcSub)
return
}
}
}()
return rpcSub, nil
}