新增Windows代理程序支持
This commit is contained in:
@@ -3,17 +3,16 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 使用配置文件中的InfluxDB配置
|
||||
url := "http://10.35.10.130:8066"
|
||||
// 使用config.json中的InfluxDB配置
|
||||
url := "http://10.35.10.70:8066"
|
||||
token := "aVI5qMGz6e8d4pfyhVZNYfS5we7C8Bb-5bi-V7LpL3K6CmQyudauigoxDFv1UFo2Hvda7swKEqTe8eP6xy4jBw=="
|
||||
username := "admin"
|
||||
password := "Wxf26051"
|
||||
username := "monitor"
|
||||
password := "monitor"
|
||||
org := "AMAZEHOME"
|
||||
bucket := "AMAZEHOME"
|
||||
|
||||
@@ -37,7 +36,7 @@ func main() {
|
||||
|
||||
// 测试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)
|
||||
authURL := fmt.Sprintf("http://%s:%s@10.35.10.70:8066", username, password)
|
||||
client2 := influxdb2.NewClient(authURL, "")
|
||||
defer client2.Close()
|
||||
|
||||
@@ -49,23 +48,63 @@ func main() {
|
||||
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)
|
||||
// 测试3: 查询硬件数据
|
||||
fmt.Println("\n=== Test 3: Querying Hardware Data ===")
|
||||
// 使用client1(Token认证)进行查询
|
||||
queryAPI := client1.QueryAPI(org)
|
||||
|
||||
point := influxdb2.NewPoint(
|
||||
"test_metric",
|
||||
map[string]string{"test_tag": "test_value"},
|
||||
map[string]interface{}{"value": 1.0},
|
||||
time.Now(),
|
||||
)
|
||||
// 查询特定设备的硬件数据
|
||||
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()`
|
||||
|
||||
err = writeAPI.WritePoint(context.Background(), point)
|
||||
result, err := queryAPI.Query(context.Background(), query)
|
||||
if err != nil {
|
||||
fmt.Printf("Write failed: %v\n", err)
|
||||
fmt.Printf("Query failed: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("Write successful!")
|
||||
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 ===")
|
||||
// 使用client1(Token认证)进行查询
|
||||
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 ===")
|
||||
|
||||
Reference in New Issue
Block a user