mao-service-discovery 1.6.0-commit-2023-07-24-04-37-15.0 → 1.6.0-commit-2023-07-24-06-22-14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MaoServerDiscovery +0 -0
- package/package.json +1 -1
- package/.github/dependabot.yml +0 -11
- package/.github/workflows/codeql-analysis.yml +0 -75
- package/.github/workflows/docker-image.yml +0 -24
- package/.github/workflows/docker-publish.yml +0 -94
- package/.github/workflows/go.yml +0 -29
- package/.github/workflows/go_static.yml +0 -29
- package/.github/workflows/npm-publish-release.yml +0 -36
- package/.github/workflows/npm-publish.yml +0 -32
- package/Dockerfile +0 -19
- package/LICENSE +0 -201
- package/MODULES.md +0 -39
- package/README.md +0 -54
- package/bak/index.html +0 -42
- package/bak/receiveProcessIcmpLoopV4V6.bak +0 -73
- package/bak/test/test-icmp-ka-module.go +0 -297
- package/bak/test/test-tech.go +0 -110
- package/bak/using_etcd/main.go +0 -165
- package/cmd/api/aux-data-module.go +0 -14
- package/cmd/api/config-module.go +0 -11
- package/cmd/api/email-module.go +0 -14
- package/cmd/api/gateway-module.go +0 -23
- package/cmd/api/grpc-ka-module.go +0 -29
- package/cmd/api/icmp-ka-module.go +0 -26
- package/cmd/api/restful-server-module.go +0 -13
- package/cmd/api/topo-module.go +0 -31
- package/cmd/api/wechat-module.go +0 -18
- package/cmd/general_client.go +0 -366
- package/cmd/lib/AuxDataProcessor/aux-data-processor-module.go +0 -64
- package/cmd/lib/AuxDataProcessor/env-temp-processor.go +0 -63
- package/cmd/lib/AuxDataProcessor/env-temp-processor_test.go +0 -30
- package/cmd/lib/Config/config-yaml-module.go +0 -303
- package/cmd/lib/Config/config-yaml-module_test.go +0 -308
- package/cmd/lib/Email/login-auth.go +0 -35
- package/cmd/lib/Email/login-auth_test.go +0 -13
- package/cmd/lib/Email/smtp-email-module.go +0 -229
- package/cmd/lib/Email/smtp-email-module_test.go +0 -12
- package/cmd/lib/GrpcKa/grpc-detect-module.go +0 -269
- package/cmd/lib/GrpcKa/grpc-detect-module_test.go +0 -26
- package/cmd/lib/IcmpKa/icmp-detect-module.go +0 -545
- package/cmd/lib/IcmpKa/icmp-detect-module_test.go +0 -23
- package/cmd/lib/InfluxDB/influxdb-util.go +0 -72
- package/cmd/lib/MaoCommon/gps.go +0 -15
- package/cmd/lib/MaoCommon/service-registry-util.go +0 -59
- package/cmd/lib/MaoCommon/service-registry.go +0 -14
- package/cmd/lib/MaoCommon/service-registry_test.go +0 -30
- package/cmd/lib/MaoEnhancedGolang/smtp.go +0 -438
- package/cmd/lib/Restful/restful-server-impl.go +0 -87
- package/cmd/lib/Soap/soap-tplink-util.go +0 -186
- package/cmd/lib/Soap/soap-tplink-util_test.go +0 -45
- package/cmd/lib/Soap/tplink-gateway-module.go +0 -200
- package/cmd/server.go +0 -327
- package/go.mod +0 -38
- package/go.sum +0 -222
- package/grpc.maojianwei.com/server/discovery/api/generate_api_from_protobuf.sh +0 -1
- package/grpc.maojianwei.com/server/discovery/api/mao-server-discovery.pb.go +0 -403
- package/grpc.maojianwei.com/server/discovery/api/mao-server-discovery.proto +0 -35
- package/grpc.maojianwei.com/server/discovery/api/mao-server-discovery_grpc.pb.go +0 -207
- package/incubator/OnosTopoShow/onos-topo-module.go +0 -347
- package/incubator/Wechat/wechat-message-module.go +0 -267
- package/incubator/Wechat/wechat-message-module_test.go +0 -108
- package/main.go +0 -383
- package/notebook/notebook.txt +0 -8
- package/resource/index-email.html +0 -67
- package/resource/index-icmp.html +0 -41
- package/resource/index-onos.html +0 -38
- package/resource/index-server.html +0 -57
- package/resource/index-wechat.html +0 -64
- package/resource/jquery-3.6.0.min.js +0 -2
- package/screenshot/2-cli-output.png +0 -0
- package/screenshot/2-cli-parameters.png +0 -0
- package/screenshot/2-json-format.png +0 -0
- package/screenshot/2-readable-format.png +0 -0
- package/screenshot/client-server-mode-1.png +0 -0
- package/screenshot/client-server-mode-2.png +0 -0
- package/screenshot/client-server-mode-production.png +0 -0
- package/screenshot/show_using_etcd.png +0 -0
- package/statically_linked_compilation.sh +0 -2
- package/util/mao_log.go +0 -42
- package/util/mao_log_test.go +0 -73
- package/util/mao_util.go +0 -53
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
package Soap
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"bytes"
|
|
5
|
-
"errors"
|
|
6
|
-
"io/ioutil"
|
|
7
|
-
"log"
|
|
8
|
-
"net/http"
|
|
9
|
-
"strconv"
|
|
10
|
-
"strings"
|
|
11
|
-
"time"
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
const (
|
|
15
|
-
SOAP_HEADER_KEY = "SOAPAction"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/* ============================== ipc ============================== */
|
|
19
|
-
SOAP_URL_WANIPConnection = "http://192.168.1.1:1900/ipc"
|
|
20
|
-
|
|
21
|
-
// ========== GetUptime ==========
|
|
22
|
-
SOAP_HEADER_VALUE_GetUptime = "urn:schemas-upnp-org:service:WANIPConnection:1#GetStatusInfo"
|
|
23
|
-
SOAP_MSG_GetUptime = "<s:Body><u:GetStatusInfo></u:GetStatusInfo></s:Body>"
|
|
24
|
-
SOAP_KEYWORD_GetUptime = "NewUptime"
|
|
25
|
-
|
|
26
|
-
// ========== GetExternalIPAddress ==========
|
|
27
|
-
SOAP_HEADER_VALUE_GetExternalIPAddress = "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetExternalIPAddress"
|
|
28
|
-
SOAP_MSG_GetExternalIPAddress = "<s:Body><u:GetExternalIPAddress></u:GetExternalIPAddress></s:Body>"
|
|
29
|
-
SOAP_KEYWORD_GetExternalIPAddress = "NewExternalIPAddress"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
/* ============================== ifc ============================== */
|
|
33
|
-
SOAP_URL_WANCommonInterfaceConfig = "http://192.168.1.1:1900/ifc"
|
|
34
|
-
|
|
35
|
-
// ========== GetTotalBytesSent ==========
|
|
36
|
-
SOAP_HEADER_VALUE_GetTotalBytesSent = "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetTotalBytesSent"
|
|
37
|
-
SOAP_MSG_GetTotalBytesSent = "<s:Body><u:GetTotalBytesSent></u:GetTotalBytesSent></s:Body>"
|
|
38
|
-
SOAP_KEYWORD_GetTotalBytesSent = "NewTotalBytesSent"
|
|
39
|
-
|
|
40
|
-
// ========== GetTotalBytesReceived ==========
|
|
41
|
-
SOAP_HEADER_VALUE_GetTotalBytesReceived = "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetTotalBytesReceived"
|
|
42
|
-
SOAP_MSG_GetTotalBytesReceived = "<s:Body><u:GetTotalBytesReceived></u:GetTotalBytesReceived></s:Body>"
|
|
43
|
-
SOAP_KEYWORD_GetTotalBytesReceived = "NewTotalBytesReceived"
|
|
44
|
-
|
|
45
|
-
// ========== GetTotalPacketsSent ==========
|
|
46
|
-
SOAP_HEADER_VALUE_GetTotalPacketsSent = "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetTotalPacketsSent"
|
|
47
|
-
SOAP_MSG_GetTotalPacketsSent = "<s:Body><u:GetTotalPacketsSent></u:GetTotalPacketsSent></s:Body>"
|
|
48
|
-
SOAP_KEYWORD_GetTotalPacketsSent = "NewTotalPacketsSent"
|
|
49
|
-
|
|
50
|
-
// ========== GetTotalPacketsReceived ==========
|
|
51
|
-
SOAP_HEADER_VALUE_GetTotalPacketsReceived = "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetTotalPacketsReceived"
|
|
52
|
-
SOAP_MSG_GetTotalPacketsReceived = "<s:Body><u:GetTotalPacketsReceived></u:GetTotalPacketsReceived></s:Body>"
|
|
53
|
-
SOAP_KEYWORD_GetTotalPacketsReceived = "NewTotalPacketsReceived"
|
|
54
|
-
)
|
|
55
|
-
|
|
56
|
-
func requestSoapData(soapUrl, soapHeader, soapBody string) (*[]byte, error) {
|
|
57
|
-
|
|
58
|
-
postData := bytes.NewReader([]byte(soapBody))
|
|
59
|
-
req, err := http.NewRequest("POST", soapUrl, postData)
|
|
60
|
-
if err != nil {
|
|
61
|
-
return nil, err
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
req.Header.Set(SOAP_HEADER_KEY, soapHeader)
|
|
65
|
-
|
|
66
|
-
client := http.Client{
|
|
67
|
-
Timeout: 1 * time.Second,
|
|
68
|
-
}
|
|
69
|
-
resp, err := client.Do(req)
|
|
70
|
-
if err != nil {
|
|
71
|
-
return nil, err
|
|
72
|
-
}
|
|
73
|
-
defer resp.Body.Close()
|
|
74
|
-
|
|
75
|
-
body, err := ioutil.ReadAll(resp.Body)
|
|
76
|
-
if err != nil {
|
|
77
|
-
log.Println(err.Error())
|
|
78
|
-
return nil, err
|
|
79
|
-
}
|
|
80
|
-
return &body, nil
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
func getDataString(data string, keyword string) (string, error) {
|
|
84
|
-
_, after, found := strings.Cut(data, "<" + keyword + ">")
|
|
85
|
-
if !found {
|
|
86
|
-
return "", errors.New("Can't find the keyword at before: " + keyword)
|
|
87
|
-
}
|
|
88
|
-
before, _, found := strings.Cut(after, "</" + keyword + ">")
|
|
89
|
-
if !found {
|
|
90
|
-
return "", errors.New("Can't find the keyword at after: " + keyword)
|
|
91
|
-
}
|
|
92
|
-
return before, nil
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
func GetTotalBytesSent() (uint64, error) {
|
|
96
|
-
body, err := requestSoapData(SOAP_URL_WANCommonInterfaceConfig, SOAP_HEADER_VALUE_GetTotalBytesSent, SOAP_MSG_GetTotalBytesSent)
|
|
97
|
-
if err != nil {
|
|
98
|
-
return 0, err
|
|
99
|
-
}
|
|
100
|
-
data, err := getDataString(string(*body), SOAP_KEYWORD_GetTotalBytesSent)
|
|
101
|
-
if err != nil {
|
|
102
|
-
return 0, err
|
|
103
|
-
}
|
|
104
|
-
uintData, err := strconv.ParseUint(data, 10, 64)
|
|
105
|
-
if err != nil {
|
|
106
|
-
return 0, err
|
|
107
|
-
}
|
|
108
|
-
return uintData, nil
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
func GetTotalBytesReceived() (uint64, error) {
|
|
112
|
-
body, err := requestSoapData(SOAP_URL_WANCommonInterfaceConfig, SOAP_HEADER_VALUE_GetTotalBytesReceived, SOAP_MSG_GetTotalBytesReceived)
|
|
113
|
-
if err != nil {
|
|
114
|
-
return 0, err
|
|
115
|
-
}
|
|
116
|
-
data, err := getDataString(string(*body), SOAP_KEYWORD_GetTotalBytesReceived)
|
|
117
|
-
if err != nil {
|
|
118
|
-
return 0, err
|
|
119
|
-
}
|
|
120
|
-
uintData, err := strconv.ParseUint(data, 10, 64)
|
|
121
|
-
if err != nil {
|
|
122
|
-
return 0, err
|
|
123
|
-
}
|
|
124
|
-
return uintData, nil
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
func GetTotalPacketsSent() (uint64, error) {
|
|
128
|
-
body, err := requestSoapData(SOAP_URL_WANCommonInterfaceConfig, SOAP_HEADER_VALUE_GetTotalPacketsSent, SOAP_MSG_GetTotalPacketsSent)
|
|
129
|
-
if err != nil {
|
|
130
|
-
return 0, err
|
|
131
|
-
}
|
|
132
|
-
data, err := getDataString(string(*body), SOAP_KEYWORD_GetTotalPacketsSent)
|
|
133
|
-
if err != nil {
|
|
134
|
-
return 0, err
|
|
135
|
-
}
|
|
136
|
-
uintData, err := strconv.ParseUint(data, 10, 64)
|
|
137
|
-
if err != nil {
|
|
138
|
-
return 0, err
|
|
139
|
-
}
|
|
140
|
-
return uintData, nil
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
func GetTotalPacketsReceived() (uint64, error) {
|
|
144
|
-
body, err := requestSoapData(SOAP_URL_WANCommonInterfaceConfig, SOAP_HEADER_VALUE_GetTotalPacketsReceived, SOAP_MSG_GetTotalPacketsReceived)
|
|
145
|
-
if err != nil {
|
|
146
|
-
return 0, err
|
|
147
|
-
}
|
|
148
|
-
data, err := getDataString(string(*body), SOAP_KEYWORD_GetTotalPacketsReceived)
|
|
149
|
-
if err != nil {
|
|
150
|
-
return 0, err
|
|
151
|
-
}
|
|
152
|
-
uintData, err := strconv.ParseUint(data, 10, 64)
|
|
153
|
-
if err != nil {
|
|
154
|
-
return 0, err
|
|
155
|
-
}
|
|
156
|
-
return uintData, nil
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
func GetUptime() (uint64, error) {
|
|
160
|
-
body, err := requestSoapData(SOAP_URL_WANIPConnection, SOAP_HEADER_VALUE_GetUptime, SOAP_MSG_GetUptime)
|
|
161
|
-
if err != nil {
|
|
162
|
-
return 0, err
|
|
163
|
-
}
|
|
164
|
-
data, err := getDataString(string(*body), SOAP_KEYWORD_GetUptime)
|
|
165
|
-
if err != nil {
|
|
166
|
-
return 0, err
|
|
167
|
-
}
|
|
168
|
-
uintData, err := strconv.ParseUint(data[:len(data)-1], 10, 64)
|
|
169
|
-
if err != nil {
|
|
170
|
-
return 0, err
|
|
171
|
-
}
|
|
172
|
-
return uintData, nil
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
func GetExternalIPAddress() (string, error) {
|
|
176
|
-
body, err := requestSoapData(SOAP_URL_WANIPConnection, SOAP_HEADER_VALUE_GetExternalIPAddress, SOAP_MSG_GetExternalIPAddress)
|
|
177
|
-
if err != nil {
|
|
178
|
-
return "", err
|
|
179
|
-
}
|
|
180
|
-
data, err := getDataString(string(*body), SOAP_KEYWORD_GetExternalIPAddress)
|
|
181
|
-
if err != nil {
|
|
182
|
-
return "", err
|
|
183
|
-
}
|
|
184
|
-
return data, nil
|
|
185
|
-
}
|
|
186
|
-
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
package Soap
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"log"
|
|
5
|
-
"reflect"
|
|
6
|
-
"runtime"
|
|
7
|
-
"testing"
|
|
8
|
-
"time"
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
func TestGetExternalIPAddress(t *testing.T) {
|
|
12
|
-
sss := runtime.FuncForPC(reflect.ValueOf(GetTotalBytesSent).Pointer()).Name()
|
|
13
|
-
log.Println(sss)
|
|
14
|
-
|
|
15
|
-
t1 := time.Now()
|
|
16
|
-
time.Sleep(2 * time.Second)
|
|
17
|
-
t2 := time.Now()
|
|
18
|
-
s := t2.Sub(t1).Seconds()
|
|
19
|
-
log.Println(s)
|
|
20
|
-
|
|
21
|
-
for {
|
|
22
|
-
break
|
|
23
|
-
|
|
24
|
-
totalBytesSent, err := GetTotalBytesSent()
|
|
25
|
-
log.Printf("TotalBytesSent: %d, err: %d", totalBytesSent, err)
|
|
26
|
-
|
|
27
|
-
totalBytesReceived, err := GetTotalBytesReceived()
|
|
28
|
-
log.Printf("TotalBytesReceived: %d, err: %d", totalBytesReceived, err)
|
|
29
|
-
|
|
30
|
-
totalPacketsSent, err := GetTotalPacketsSent()
|
|
31
|
-
log.Printf("TotalPacketsSent: %d, err: %d", totalPacketsSent, err)
|
|
32
|
-
|
|
33
|
-
totalPacketsReceived, err := GetTotalPacketsReceived()
|
|
34
|
-
log.Printf("TotalPacketsReceived: %d, err: %d", totalPacketsReceived, err)
|
|
35
|
-
|
|
36
|
-
uptime, err := GetUptime()
|
|
37
|
-
log.Printf("Uptime: %d, err: %d", uptime, err)
|
|
38
|
-
|
|
39
|
-
externalIPAddress, err := GetExternalIPAddress()
|
|
40
|
-
log.Printf("ExternalIPAddress: %s, err: %d", externalIPAddress, err)
|
|
41
|
-
|
|
42
|
-
time.Sleep(500 * time.Millisecond)
|
|
43
|
-
log.Printf("\n\n\n\n\n\n==============================")
|
|
44
|
-
}
|
|
45
|
-
}
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
package Soap
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
MaoApi "MaoServerDiscovery/cmd/api"
|
|
5
|
-
"MaoServerDiscovery/cmd/lib/InfluxDB"
|
|
6
|
-
"MaoServerDiscovery/util"
|
|
7
|
-
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
|
8
|
-
influxdb2Api "github.com/influxdata/influxdb-client-go/v2/api"
|
|
9
|
-
"time"
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
const (
|
|
13
|
-
p_TPLINK_MODULE_NAME = "TPLINK-Gateway-module"
|
|
14
|
-
|
|
15
|
-
FLAG_GATEWAY_BytesReceivedSpeed = 1 << 0
|
|
16
|
-
FLAG_GATEWAY_BytesSentSpeed = 1 << 1
|
|
17
|
-
FLAG_GATEWAY_PacketsReceivedSpeed = 1 << 2
|
|
18
|
-
FLAG_GATEWAY_PacketsSentSpeed = 1 << 3
|
|
19
|
-
FLAG_GATEWAY_Uptime = 1 << 4
|
|
20
|
-
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
type TplinkGatewayModule struct {
|
|
24
|
-
|
|
25
|
-
lastseen_BytesReceived uint64
|
|
26
|
-
lastseen_BytesReceived_timestamp time.Time
|
|
27
|
-
|
|
28
|
-
lastseen_BytesSent uint64
|
|
29
|
-
lastseen_BytesSent_timestamp time.Time
|
|
30
|
-
|
|
31
|
-
lastseen_PacketsReceived uint64
|
|
32
|
-
lastseen_PacketsReceived_timestamp time.Time
|
|
33
|
-
|
|
34
|
-
lastseen_PacketsSent uint64
|
|
35
|
-
lastseen_PacketsSent_timestamp time.Time
|
|
36
|
-
|
|
37
|
-
lastseen_Uptime_timestamp time.Time
|
|
38
|
-
|
|
39
|
-
BytesReceivedSpeed uint64
|
|
40
|
-
BytesSentSpeed uint64
|
|
41
|
-
PacketsReceivedSpeed uint64
|
|
42
|
-
PacketsSentSpeed uint64
|
|
43
|
-
Uptime uint64
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
func (t *TplinkGatewayModule) publishInfluxDB(writeAPI *influxdb2Api.WriteAPI, finishFlag uint) {
|
|
47
|
-
// write point asynchronously
|
|
48
|
-
|
|
49
|
-
if finishFlag & FLAG_GATEWAY_BytesReceivedSpeed != 0 {
|
|
50
|
-
(*writeAPI).WritePoint(
|
|
51
|
-
influxdb2.NewPointWithMeasurement(MaoApi.GATEWAY_MEASUREMENT).
|
|
52
|
-
AddTag(MaoApi.GATEWAY_TAG_GEO, "Beijing-HQ").
|
|
53
|
-
AddField(MaoApi.GATEWAY_FIELD_BytesReceivedSpeed, t.BytesReceivedSpeed).
|
|
54
|
-
AddField(MaoApi.GATEWAY_FIELD_BytesReceived, t.lastseen_BytesReceived).
|
|
55
|
-
SetTime(t.lastseen_BytesReceived_timestamp))
|
|
56
|
-
}
|
|
57
|
-
if finishFlag & FLAG_GATEWAY_BytesSentSpeed != 0 {
|
|
58
|
-
(*writeAPI).WritePoint(
|
|
59
|
-
influxdb2.NewPointWithMeasurement(MaoApi.GATEWAY_MEASUREMENT).
|
|
60
|
-
AddTag(MaoApi.GATEWAY_TAG_GEO, "Beijing-HQ").
|
|
61
|
-
AddField(MaoApi.GATEWAY_FIELD_BytesSentSpeed, t.BytesSentSpeed).
|
|
62
|
-
AddField(MaoApi.GATEWAY_FIELD_BytesSent, t.lastseen_BytesSent).
|
|
63
|
-
SetTime(t.lastseen_BytesSent_timestamp))
|
|
64
|
-
}
|
|
65
|
-
if finishFlag & FLAG_GATEWAY_PacketsReceivedSpeed != 0 {
|
|
66
|
-
(*writeAPI).WritePoint(
|
|
67
|
-
influxdb2.NewPointWithMeasurement(MaoApi.GATEWAY_MEASUREMENT).
|
|
68
|
-
AddTag(MaoApi.GATEWAY_TAG_GEO, "Beijing-HQ").
|
|
69
|
-
AddField(MaoApi.GATEWAY_FIELD_PacketsReceivedSpeed, t.PacketsReceivedSpeed).
|
|
70
|
-
AddField(MaoApi.GATEWAY_FIELD_PacketsReceived, t.lastseen_PacketsReceived).
|
|
71
|
-
SetTime(t.lastseen_PacketsReceived_timestamp))
|
|
72
|
-
}
|
|
73
|
-
if finishFlag & FLAG_GATEWAY_PacketsSentSpeed != 0 {
|
|
74
|
-
(*writeAPI).WritePoint(
|
|
75
|
-
influxdb2.NewPointWithMeasurement(MaoApi.GATEWAY_MEASUREMENT).
|
|
76
|
-
AddTag(MaoApi.GATEWAY_TAG_GEO, "Beijing-HQ").
|
|
77
|
-
AddField(MaoApi.GATEWAY_FIELD_PacketsSentSpeed, t.PacketsSentSpeed).
|
|
78
|
-
AddField(MaoApi.GATEWAY_FIELD_PacketsSent, t.lastseen_PacketsSent).
|
|
79
|
-
SetTime(t.lastseen_PacketsSent_timestamp))
|
|
80
|
-
}
|
|
81
|
-
if finishFlag & FLAG_GATEWAY_Uptime != 0 {
|
|
82
|
-
(*writeAPI).WritePoint(
|
|
83
|
-
influxdb2.NewPointWithMeasurement(MaoApi.GATEWAY_MEASUREMENT).
|
|
84
|
-
AddTag(MaoApi.GATEWAY_TAG_GEO, "Beijing-HQ").
|
|
85
|
-
AddField(MaoApi.GATEWAY_FIELD_Uptime, t.Uptime).
|
|
86
|
-
SetTime(t.lastseen_Uptime_timestamp))
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Not flush writes, avoid blocking my thread, then the lib's thread will block itself.
|
|
90
|
-
//(*writeAPI).Flush()
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
func (t *TplinkGatewayModule) pushLoop(triggerChannel *chan uint) {
|
|
94
|
-
var client *influxdb2.Client
|
|
95
|
-
var writeApi *influxdb2Api.WriteAPI
|
|
96
|
-
|
|
97
|
-
for {
|
|
98
|
-
// do...while, wait for being well configured.
|
|
99
|
-
client, writeApi = InfluxDB.CreateClientAndWriteAPI()
|
|
100
|
-
if writeApi != nil {
|
|
101
|
-
break
|
|
102
|
-
}
|
|
103
|
-
time.Sleep(1 * time.Second)
|
|
104
|
-
}
|
|
105
|
-
defer (*client).Close()
|
|
106
|
-
|
|
107
|
-
for finishFlag := range *triggerChannel {
|
|
108
|
-
t.publishInfluxDB(writeApi, finishFlag)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
func (t *TplinkGatewayModule) controlLoop(triggerChannel *chan uint) {
|
|
113
|
-
for {
|
|
114
|
-
time.Sleep(2 * time.Second)
|
|
115
|
-
var finishFlag uint = 0
|
|
116
|
-
|
|
117
|
-
newBytesReceived, err := GetTotalBytesReceived()
|
|
118
|
-
newBytesReceived_timestamp := time.Now()
|
|
119
|
-
if err == nil {
|
|
120
|
-
if newBytesReceived >= t.lastseen_BytesReceived {
|
|
121
|
-
t.BytesReceivedSpeed = uint64(float64(newBytesReceived - t.lastseen_BytesReceived) / (newBytesReceived_timestamp.Sub(t.lastseen_BytesReceived_timestamp).Seconds()))
|
|
122
|
-
} else {
|
|
123
|
-
t.BytesReceivedSpeed = uint64(float64(newBytesReceived) / (newBytesReceived_timestamp.Sub(t.lastseen_BytesReceived_timestamp).Seconds())) // statistic may overflow (rollback to 0)
|
|
124
|
-
}
|
|
125
|
-
//log.Printf("%d, %f, %f", t.BytesReceivedSpeed, float64(newBytesReceived - t.lastseen_BytesReceived), newBytesReceived_timestamp.Sub(t.lastseen_BytesReceived_timestamp).Seconds())
|
|
126
|
-
t.lastseen_BytesReceived = newBytesReceived // statistic may overflow (rollback to 0)
|
|
127
|
-
t.lastseen_BytesReceived_timestamp = newBytesReceived_timestamp
|
|
128
|
-
finishFlag |= FLAG_GATEWAY_BytesReceivedSpeed
|
|
129
|
-
} else {
|
|
130
|
-
util.MaoLogM(util.WARN, p_TPLINK_MODULE_NAME, "Fail to get TotalBytesReceived, %s", err.Error())
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
newBytesSent, err := GetTotalBytesSent()
|
|
134
|
-
newBytesSent_timestamp := time.Now()
|
|
135
|
-
if err == nil {
|
|
136
|
-
if newBytesSent >= t.lastseen_BytesSent {
|
|
137
|
-
t.BytesSentSpeed = uint64(float64(newBytesSent - t.lastseen_BytesSent) / (newBytesSent_timestamp.Sub(t.lastseen_BytesSent_timestamp).Seconds()))
|
|
138
|
-
} else {
|
|
139
|
-
t.BytesSentSpeed = uint64(float64(newBytesSent) / (newBytesSent_timestamp.Sub(t.lastseen_BytesSent_timestamp).Seconds())) // statistic may overflow (rollback to 0)
|
|
140
|
-
}
|
|
141
|
-
t.lastseen_BytesSent = newBytesSent // statistic may overflow (rollback to 0)
|
|
142
|
-
t.lastseen_BytesSent_timestamp = newBytesSent_timestamp
|
|
143
|
-
finishFlag |= FLAG_GATEWAY_BytesSentSpeed
|
|
144
|
-
} else {
|
|
145
|
-
util.MaoLogM(util.WARN, p_TPLINK_MODULE_NAME, "Fail to get TotalBytesSent, %s", err.Error())
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
newPacketsReceived, err := GetTotalPacketsReceived()
|
|
149
|
-
newPacketsReceived_timestamp := time.Now()
|
|
150
|
-
if err == nil {
|
|
151
|
-
if newPacketsReceived >= t.lastseen_PacketsReceived {
|
|
152
|
-
t.PacketsReceivedSpeed = uint64(float64(newPacketsReceived - t.lastseen_PacketsReceived) / (newPacketsReceived_timestamp.Sub(t.lastseen_PacketsReceived_timestamp).Seconds()))
|
|
153
|
-
} else {
|
|
154
|
-
t.PacketsReceivedSpeed = uint64(float64(newPacketsReceived) / (newPacketsReceived_timestamp.Sub(t.lastseen_PacketsReceived_timestamp).Seconds())) // statistic may overflow (rollback to 0)
|
|
155
|
-
}
|
|
156
|
-
t.lastseen_PacketsReceived = newPacketsReceived // statistic may overflow (rollback to 0)
|
|
157
|
-
t.lastseen_PacketsReceived_timestamp = newPacketsReceived_timestamp
|
|
158
|
-
finishFlag |= FLAG_GATEWAY_PacketsReceivedSpeed
|
|
159
|
-
} else {
|
|
160
|
-
util.MaoLogM(util.WARN, p_TPLINK_MODULE_NAME, "Fail to get TotalPacketsReceived, %s", err.Error())
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
newPacketsSent, err := GetTotalPacketsSent()
|
|
164
|
-
newPacketsSent_timestamp := time.Now()
|
|
165
|
-
if err == nil {
|
|
166
|
-
if newPacketsSent >= t.lastseen_PacketsSent {
|
|
167
|
-
t.PacketsSentSpeed = uint64(float64(newPacketsSent - t.lastseen_PacketsSent) / (newPacketsSent_timestamp.Sub(t.lastseen_PacketsSent_timestamp).Seconds()))
|
|
168
|
-
} else {
|
|
169
|
-
t.PacketsSentSpeed = uint64(float64(newPacketsSent) / (newPacketsSent_timestamp.Sub(t.lastseen_PacketsSent_timestamp).Seconds())) // statistic may overflow (rollback to 0)
|
|
170
|
-
}
|
|
171
|
-
t.lastseen_PacketsSent = newPacketsSent // statistic may overflow (rollback to 0)
|
|
172
|
-
t.lastseen_PacketsSent_timestamp = newPacketsSent_timestamp
|
|
173
|
-
finishFlag |= FLAG_GATEWAY_PacketsSentSpeed
|
|
174
|
-
} else {
|
|
175
|
-
util.MaoLogM(util.WARN, p_TPLINK_MODULE_NAME, "Fail to get TotalPacketsSent, %s", err.Error())
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
newUptime, err := GetUptime()
|
|
179
|
-
newUptime_timestamp := time.Now()
|
|
180
|
-
if err == nil {
|
|
181
|
-
t.Uptime = newUptime
|
|
182
|
-
t.lastseen_Uptime_timestamp = newUptime_timestamp
|
|
183
|
-
finishFlag |= FLAG_GATEWAY_Uptime
|
|
184
|
-
} else {
|
|
185
|
-
util.MaoLogM(util.WARN, p_TPLINK_MODULE_NAME, "Fail to get Uptime, %s", err.Error())
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
*triggerChannel <- finishFlag
|
|
189
|
-
|
|
190
|
-
util.MaoLogM(util.DEBUG, p_TPLINK_MODULE_NAME, "BytesSentSpeed: %d, BytesReceivedSpeed: %d, PacketsSentSpeed: %d, PacketsReceivedSpeed: %d, Uptime: %d",
|
|
191
|
-
t.BytesSentSpeed, t.BytesReceivedSpeed, t.PacketsSentSpeed, t.PacketsReceivedSpeed, t.Uptime)
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
func (t *TplinkGatewayModule) InitTplinkGatewayModule() bool {
|
|
196
|
-
triggerChannel := make(chan uint, 100)
|
|
197
|
-
go t.controlLoop(&triggerChannel)
|
|
198
|
-
go t.pushLoop(&triggerChannel)
|
|
199
|
-
return true
|
|
200
|
-
}
|