Files
monitor/backend/test/test_influxdb.go
2025-12-07 18:15:50 +08:00

112 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"context"
"fmt"
influxdb2 "github.com/influxdata/influxdb-client-go"
)
func main() {
// 使用config.json中的InfluxDB配置
url := "http://10.35.10.70:8066"
token := "aVI5qMGz6e8d4pfyhVZNYfS5we7C8Bb-5bi-V7LpL3K6CmQyudauigoxDFv1UFo2Hvda7swKEqTe8eP6xy4jBw=="
username := "monitor"
password := "monitor"
org := "AMAZEHOME"
bucket := "AMAZEHOME"
fmt.Printf("Testing InfluxDB connection to %s\n", url)
fmt.Printf("Org: %s, Bucket: %s\n", org, bucket)
fmt.Printf("Username: %s, Password: %s\n", username, password)
fmt.Printf("Token: %s\n", token)
// 测试1: 使用Token认证
fmt.Println("\n=== Test 1: Using Token Authentication ===")
client1 := influxdb2.NewClient(url, token)
defer client1.Close()
// 测试连接
health, err := client1.Health(context.Background())
if err != nil {
fmt.Printf("Health check failed: %v\n", err)
} else {
fmt.Printf("Health check result: %v\n", health)
}
// 测试2: 使用Username/Password认证通过URL嵌入
fmt.Println("\n=== Test 2: Using Username/Password Authentication (Embedded in URL) ===")
authURL := fmt.Sprintf("http://%s:%s@10.35.10.70:8066", username, password)
client2 := influxdb2.NewClient(authURL, "")
defer client2.Close()
// 测试连接
health2, err2 := client2.Health(context.Background())
if err2 != nil {
fmt.Printf("Health check failed: %v\n", err2)
} else {
fmt.Printf("Health check result: %v\n", health2)
}
// 测试3: 查询硬件数据
fmt.Println("\n=== Test 3: Querying Hardware Data ===")
// 使用client1Token认证进行查询
queryAPI := client1.QueryAPI(org)
// 查询特定设备的硬件数据
deviceID := "device-1764692967636"
query := `from(bucket: "AMAZEHOME")
|> range(start: -24h)
|> filter(fn: (r) => r["_measurement"] == "hardware")
|> filter(fn: (r) => r["device_id"] == "` + deviceID + `")
|> filter(fn: (r) => r["type"] == "cpu")
|> last()`
result, err := queryAPI.Query(context.Background(), query)
if err != nil {
fmt.Printf("Query failed: %v\n", err)
} else {
fmt.Println("Query results:")
for result.Next() {
fmt.Printf("- Field: %s, Value: %v\n", result.Record().Field(), result.Record().Value())
}
if result.Err() != nil {
fmt.Printf("Result error: %v\n", result.Err())
}
}
// 测试4: 查询所有硬件类型的最新数据
fmt.Println("\n=== Test 4: Querying All Hardware Types ===")
// 使用client1Token认证进行查询
queryAPI = client1.QueryAPI(org)
hardwareTypes := []string{"os", "cpu", "memory", "disk", "network"}
for _, hardwareType := range hardwareTypes {
fmt.Printf("\n--- %s data ---", hardwareType)
query = `from(bucket: "` + bucket + `")
|> range(start: -24h)
|> filter(fn: (r) => r["_measurement"] == "hardware")
|> filter(fn: (r) => r["device_id"] == "` + deviceID + `")
|> filter(fn: (r) => r["type"] == "` + hardwareType + `")
|> last()`
result, err := queryAPI.Query(context.Background(), query)
if err != nil {
fmt.Printf("Query failed: %v\n", err)
} else {
hasData := false
for result.Next() {
hasData = true
fmt.Printf("- Field: %s, Value: %v\n", result.Record().Field(), result.Record().Value())
}
if !hasData {
fmt.Println("No data found")
}
if result.Err() != nil {
fmt.Printf("Result error: %v\n", result.Err())
}
}
}
fmt.Println("\n=== Test Completed ===")
}