Files
monitor/backend/test_influxdb.go
2025-12-02 23:20:10 +08:00

73 lines
1.9 KiB
Go
Raw Permalink 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"
"time"
influxdb2 "github.com/influxdata/influxdb-client-go"
)
func main() {
// 使用配置文件中的InfluxDB配置
url := "http://10.35.10.130:8066"
token := "aVI5qMGz6e8d4pfyhVZNYfS5we7C8Bb-5bi-V7LpL3K6CmQyudauigoxDFv1UFo2Hvda7swKEqTe8eP6xy4jBw=="
username := "admin"
password := "Wxf26051"
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.130: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: Trying to Write Data Point ===")
// 使用client2进行测试
writeAPI := client2.WriteAPIBlocking(org, bucket)
point := influxdb2.NewPoint(
"test_metric",
map[string]string{"test_tag": "test_value"},
map[string]interface{}{"value": 1.0},
time.Now(),
)
err = writeAPI.WritePoint(context.Background(), point)
if err != nil {
fmt.Printf("Write failed: %v\n", err)
} else {
fmt.Println("Write successful!")
}
fmt.Println("\n=== Test Completed ===")
}