-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
192 lines (177 loc) · 5.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
pb "github.com/minhdanh/speedtest-to-prom/internal/prometheus"
prometheus "github.com/minhdanh/speedtest-to-prom/pkg/prometheus"
)
// SpeedTestResult represents the JSON structure
type SpeedTestResult struct {
Ping struct {
Jitter float64 `json:"jitter"`
Latency float64 `json:"latency"`
Low float64 `json:"low"`
High float64 `json:"high"`
} `json:"ping"`
Download struct {
Bandwidth int64 `json:"bandwidth"`
Bytes int64 `json:"bytes"`
Elapsed int64 `json:"elapsed"`
Latency struct {
Iqm float64 `json:"iqm"`
Low float64 `json:"low"`
High float64 `json:"high"`
Jitter float64 `json:"jitter"`
} `json:"latency"`
} `json:"download"`
Upload struct {
Bandwidth int64 `json:"bandwidth"`
Bytes int64 `json:"bytes"`
Elapsed int64 `json:"elapsed"`
Latency struct {
Iqm float64 `json:"iqm"`
Low float64 `json:"low"`
High float64 `json:"high"`
Jitter float64 `json:"jitter"`
} `json:"latency"`
} `json:"upload"`
PacketLoss float64 `json:"packetLoss"`
ISP string `json:"isp"`
Server struct {
ID int `json:"id"`
Name string `json:"name"`
Location string `json:"location"`
} `json:"server"`
Result struct {
ID string `json:"id"`
URL string `json:"url"`
} `json:"result"`
}
// parseLabels parses label string in format "key1=value1,key2=value2"
func parseLabels(labelStr string) ([]*pb.Label, error) {
var labels []*pb.Label
if labelStr == "" {
return labels, nil
}
pairs := strings.Split(labelStr, ",")
for _, pair := range pairs {
kv := strings.Split(pair, "=")
if len(kv) != 2 {
return nil, fmt.Errorf("invalid label format: %s", pair)
}
labels = append(labels, &pb.Label{
Name: strings.TrimSpace(kv[0]),
Value: strings.TrimSpace(kv[1]),
})
}
return labels, nil
}
func main() {
// Define command-line flags
labelsFlag := flag.String("labels", "", "Additional labels in format 'key1=value1,key2=value2'")
flag.Parse()
// Read JSON from stdin
reader := bufio.NewReader(os.Stdin)
var input bytes.Buffer
_, err := io.Copy(&input, reader)
if err != nil {
log.Fatalf("Error reading input: %v", err)
}
var result SpeedTestResult
if err := json.Unmarshal(input.Bytes(), &result); err != nil {
log.Fatalf("Error parsing JSON: %v", err)
}
// Parse additional labels
additionalLabels, err := parseLabels(*labelsFlag)
if err != nil {
log.Fatalf("Error parsing labels: %v", err)
}
// Combine default and additional labels
labels := []*pb.Label{
{Name: "isp", Value: result.ISP},
{Name: "server_id", Value: strconv.Itoa(result.Server.ID)},
{Name: "server_name", Value: result.Server.Name},
{Name: "result_id", Value: result.Result.ID},
{Name: "result_url", Value: result.Result.URL},
}
labels = append(labels, additionalLabels...)
// Create metrics map with labels
metrics := map[string][]prometheus.MetricValue{
"speedtest_ping_latency": {
{
Value: result.Ping.Latency,
Labels: map[string]string{
"jitter": fmt.Sprintf("%.2f", result.Ping.Jitter),
"low": fmt.Sprintf("%.2f", result.Ping.Low),
"high": fmt.Sprintf("%.2f", result.Ping.High),
},
},
},
"speedtest_download_bandwidth": {
{
Value: float64(result.Download.Bandwidth),
Labels: map[string]string{
"bytes": fmt.Sprintf("%d", result.Download.Bytes),
"elapsed": fmt.Sprintf("%d", result.Download.Elapsed),
},
},
},
"speedtest_download_latency": {
{
Value: result.Download.Latency.Iqm,
Labels: map[string]string{
"jitter": fmt.Sprintf("%.2f", result.Download.Latency.Jitter),
"low": fmt.Sprintf("%.2f", result.Download.Latency.Low),
"high": fmt.Sprintf("%.2f", result.Download.Latency.High),
"iqm": fmt.Sprintf("%.2f", result.Download.Latency.Iqm),
},
},
},
"speedtest_upload_bandwidth": {
{
Value: float64(result.Upload.Bandwidth),
Labels: map[string]string{
"bytes": fmt.Sprintf("%d", result.Upload.Bytes),
"elapsed": fmt.Sprintf("%d", result.Upload.Elapsed),
},
},
},
"speedtest_upload_latency": {
{
Value: result.Upload.Latency.Iqm,
Labels: map[string]string{
"jitter": fmt.Sprintf("%.2f", result.Upload.Latency.Jitter),
"low": fmt.Sprintf("%.2f", result.Upload.Latency.Low),
"high": fmt.Sprintf("%.2f", result.Upload.Latency.High),
"iqm": fmt.Sprintf("%.2f", result.Upload.Latency.Iqm),
},
},
},
"speedtest_packet_loss": {
{
Value: result.PacketLoss,
Labels: map[string]string{},
},
},
}
// Get credentials from environment variables
promUrl := os.Getenv("PROM_URL")
promUsername := os.Getenv("PROM_USERNAME")
promPassword := os.Getenv("PROM_PASSWORD")
if promUsername == "" || promPassword == "" {
log.Fatal("PROM_USERNAME and PROM_PASSWORD environment variables must be set")
}
err = prometheus.PushPrometheusMetrics(promUrl, promUsername, promPassword, metrics, labels)
if err != nil {
log.Fatalf("Error pushing metrics: %v", err)
}
log.Println("Metrics pushed successfully")
}