Distributed Detection Node
- Provide HTTP/Websocket services, and use whether the Websocket is connected as the basis for judging whether it is online.
- Use MongoDB database to store the indicator data pushed by the client and support horizontally scalable cluster services.
- Provide some interfaces to provide data for Prometheus.
- Provide some management interfaces or functions, such as data cleaning, persistence, etc.
- MongoDB time series
- MongoDB time series user document
- MongoDB Go Driver time series collection
- Prometheus Pushgateway
Note: MongoDB 5.0 version is too low, the time series function is incomplete, and there are bugs. You need to use the latest version 7.0 or above.
docker pull mongodb/mongodb-community-server:7.0.12-ubuntu2204
docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:7.0.12-ubuntu2204
go build -ldflags "-X main.version=v0.0.1" -o ddn ./app/ddn/ddn.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=v0.0.1" -o ddn ./app/ddn/ddn.go
Create a JSON configuration file, as shown below:
{
"Addr": "0.0.0.0:9521",
"LogLevel": "info",
"LogFile": "./test.log",
"MongoDB": {
"URI": "mongodb://127.0.0.1:27017/",
"Database": "health_monitoring",
"ExpireTime": 86400
},
"IP2LocationDB": {
"DatabasePath": "./IP2LOCATION-LITE-DB5.BIN/IP2LOCATION-LITE-DB5.BIN"
},
"Prometheus": {
"JobName": "test"
},
"Chain": {
"Rpc": "https://rpc-testnet.dbcwallet.io",
"ChainId": 19850818,
"PrivateKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"ReportContract": {
"AbiFile": "./dbc/ai_abi.json",
"ContractAddress": "0xb616A0dad9af4cA23234b65D27176be2c09c720c"
},
"MachineInfoContract": {
"AbiFile": "./dbc/0xE676096cA8B957e914094c5e044Fcf99f5dbf3C0.json",
"ContractAddress": "0xE676096cA8B957e914094c5e044Fcf99f5dbf3C0"
}
},
"NotifyThirdParty": {
"OfflineNotify": "https://nodeapi.deeplink.cloud/api/cyc/notifyOffline"
}
}
Use the command ddn -config ./config.json
to run.
The program will start a WebSocket service, which can be connected using ws://192.168.1.159:9521/websocket
.
WebSocket sets up a heartbeat service, that is, the client sends a ping message and the service replies with a pong message. If there is no ping message within 30 seconds, the connection will be disconnected by the server. Please send ping messages in time, which is both a heartbeat and can ensure the stability and reliability of long connections.
WebSocket messages use UTF-8 text format, mainly in JSON format. For specific examples, please see Test Case
The request message sent by the client to the server mainly consists of two parts: Header and Body.
Field | Description | Type | Remarks | |
Header | version | Protocol version, temporarily use 0 | uint32 | |
timestamp | Timestamp | int64 | ||
id | Message sequence number, a pair of request and response sequence numbers are the same | uint64 | ||
type | Message body type, 0 - reserved, 1 - online, 2 - machine information | uint32 | ||
pub_key | Public key, verify the security and integrity of the message, not required for the time being | []byte | ||
sign | Signature, verify the security and integrity of the message, not required for the time being | []byte | ||
Body | body | Message body, the real message is encoded in JSON, the encrypted byte array | []byte |
The message body currently has the following types:
- 0 - meaningless.
- 1 - Online, indicating that the WebSocket connection belongs to that device or node.
{
"machine_id": "123456789",
"project": "deeplink",
"staking_type": 0
}
- 2 - DeepLink short-term rental equipment information, regularly send graphics card and other machine information.
{
"cpu_type": "13th Gen Intel(R) Core(TM) i5-13400F",
"cpu_rate": 2500,
"gpu_names": [
"NVIDIA GeForce RTX 4060"
],
"gpu_memory_total": [
8
],
"memory_total": 16,
"wallet": "xxxxxx"
}
- 3 - Notify message.
{
"unregister": {
"message": "machine unregistered, notify from node server"
}
}
- 4 - DeepLink bandwidth mining equipment information.
{
"cpu_cores": 1,
"memory_total": 2,
"hdd": 50,
"bandwidth": 10,
"wallet": "xxxxxx"
}
The format structure of the response message body returned by the server to the client is similar, except that there are two more fields, Code and Message, than the request.
Field | Description | Type | Remarks | |
Header | version | Protocol version, temporarily use 0 | uint32 | |
timestamp | Timestamp | int64 | ||
id | Message sequence number, a pair of request and response sequence numbers are the same | uint64 | ||
type | Message body type, 0 - reserved, 1 - online, 2 - machine information | uint32 | ||
pub_key | Public key, verify the security and integrity of the message, temporarily not required | []byte | ||
sign | Signature, verify the security and integrity of the message, temporarily not required | []byte | ||
Code | code | Error code, 0 means normal | uint32 | |
Message | message | Error message | string | |
Body | body | Message body, the real message is encoded through JSON, encrypted byte array | []byte |
Assuming that the HTTP address of this service is set to 192.168.1.159:9527
, when you need to provide monitoring data for Prometheus, you only need to add the following scrape_config
to the Prometheus configuration:
# A scrape configuration containing exactly one endpoint to scrape:
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
metrics_path: "/metrics/prometheus"
static_configs:
- targets: ["192.168.1.159:9527"]
- Product uses the IP2Location LITE database for IP geolocation.