-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
393 lines (358 loc) · 9.19 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/expfmt"
)
const (
defaultListenAddr = "127.0.0.1:9676"
defaultInterval = 10
GPLv2 = "https://www.ohse.de/uwe/licenses/GPL-2"
)
type ServerEntry struct {
Server_IP string
Server_number int
Server_name string
Product string
Dc string
Traffic string
Flatrate bool
Status string
Throttled bool
Canceled bool
Paid_until string
IP []string
Subnet []struct {
IP string
Mask string
}
}
type ServerList []struct {
Server ServerEntry
}
type Traffic struct {
Traffic struct {
Type string
From string
To string
Data map[string]struct {
In float64
Out float64
Sum float64
}
}
}
type APIError struct {
Error struct {
Status int `json:"status"`
Code string `json:"code"`
} `json:"error"`
}
type TrafficInfo struct {
address string
input float64
output float64
total float64
server_number int
server_name string
dns_name string
product string
}
var (
hetznerUsername string
hetznerPassword string
labels = []string{"address", "dns_name", "server_name", "server_number", "product"}
inputGB = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "hetzner_traffic",
Name: "input_gb",
Help: "Input traffic in GB",
},
labels,
)
outputGB = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "hetzner_traffic",
Name: "output_gb",
Help: "Output traffic in GB",
},
labels,
)
totalGB = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "hetzner_traffic",
Name: "total_gb",
Help: "Used total traffic (input and output) in GB",
},
labels,
)
flagOneshot = flag.Bool("1", false, "collect and output the metrics once, and exit.")
flagVersion = flag.Bool("version", false, "show version information and exit.")
flagLicense = flag.Bool("license", false, "show license information and exit.")
flagLogUpdates = flag.Bool("log-updates", false, "log updates.")
flagType = flag.String("type", "day", "day, month or year.")
flagInterval = flag.Int("interval", defaultInterval, "run updates against the API every ... minutes.")
flagListen = flag.String("listen", defaultListenAddr,
"Address on which to expose metrics and web interface.")
)
func basicRequest(client *http.Client, method, apiurl string, data io.Reader) ([]byte, error) {
req, err := http.NewRequest(method, apiurl, data)
if err != nil {
log.Fatal(err) // broken URL, bad method, can't continue.
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(hetznerUsername, hetznerPassword)
resp, err := client.Do(req)
if err != nil {
return []byte{}, err
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
if resp.StatusCode != 200 {
var apiErr APIError
err = json.Unmarshal(bodyText, &apiErr)
if err != nil {
return []byte{}, err
}
return []byte{},
fmt.Errorf("API error: %d - %v", apiErr.Error.Status, apiErr.Error.Code)
}
return bodyText, nil
}
func handleRDNS(client *http.Client) (map[string]string, error) {
out := make(map[string]string)
bodyText, err := basicRequest(client, "GET", "https://robot-ws.your-server.de/rdns", nil)
if err != nil {
return out, err
}
var t []struct {
Rdns struct {
IP string
Ptr string
}
}
err = json.Unmarshal(bodyText, &t)
if err != nil {
log.Fatal(err)
}
for _, entry := range t {
out[entry.Rdns.IP] = entry.Rdns.Ptr
}
return out, nil
}
func getTraffic(client *http.Client, par url.Values) (Traffic, error) {
var trafficresponse Traffic
bodyText, err := basicRequest(client, "POST", "https://robot-ws.your-server.de/traffic",
strings.NewReader(par.Encode()))
if err != nil {
return trafficresponse, err
}
err = json.Unmarshal(bodyText, &trafficresponse)
return trafficresponse, err
}
func updateIPs() ([]TrafficInfo, error) {
client := &http.Client{}
out := make([]TrafficInfo, 0)
/* part1: get the server list */
bodyText, err := basicRequest(client, "GET", "https://robot-ws.your-server.de/server", nil)
if err != nil {
return out, err
}
stich := time.Now()
// hetzner returns the traffic after the hour is finished. we avoid data
// loss by going back one hour.
var from string
var to string
if *flagType=="day" {
stich = stich.Add(-1* time.Hour)
stichString := stich.Format("2006-01-02")
from = stichString+"T00"
to = stichString+"T24"
} else if *flagType=="month" {
stich = stich.Add(-1* time.Hour)
stichString := stich.Format("2006-01")
from = stichString+"-01"
to = stichString+"-31"
} else {
stich = stich.Add(-1* time.Hour)
stichString := stich.Format("2006")
from = stichString+"-01-01"
to = stichString+"-12-31"
}
var serverlistresponse ServerList
err = json.Unmarshal(bodyText, &serverlistresponse)
if err != nil {
return out, err
}
/* build back link list and params for part3 */
ipToServer := make(map[string]ServerEntry)
par := url.Values{}
par.Set("type", *flagType)
par.Set("from", from)
par.Set("to", to)
for _, entry := range serverlistresponse {
for _, ip := range entry.Server.IP {
ipToServer[ip] = entry.Server
par.Add("ip[]", ip)
}
}
for _, entry := range serverlistresponse {
for _, sub := range entry.Server.Subnet {
t := sub.IP + "/" + sub.Mask
ipToServer[t] = entry.Server
par.Add("subnet[]", sub.IP)
}
}
/* part2: get the revdns list */
rdns, err := handleRDNS(client)
if err != nil {
return out, err
}
/* part3: get the traffic */
trafficresponse, err := getTraffic(client, par)
if err != nil {
return out, err
}
for key, entry := range trafficresponse.Traffic.Data {
var ti TrafficInfo
ti.address = key
ti.input = entry.In
ti.output = entry.Out
ti.total = entry.Sum
s, ok := ipToServer[key]
if ok {
ti.server_number = s.Server_number
ti.server_name = s.Server_name
ti.product = s.Product
}
r, ok := rdns[key]
if !ok {
tmp := strings.Split(key, "/")
r, ok = rdns[tmp[0]]
}
if ok {
ti.dns_name = r
}
out = append(out, ti)
}
return out, nil
}
func updateMetrics(oneshot bool) {
interval := *flagInterval
if interval < 1 {
interval = 1
} else if interval>60 {
interval = 60
}
for {
start := time.Now()
if *flagLogUpdates {
log.Printf("update starts\n")
}
tiList, err := updateIPs()
if err != nil {
log.Printf("update failed: %s\n", err)
// do not run against API rate limits.
time.Sleep(time.Duration(interval) * 60 * time.Second)
continue
}
end := time.Now()
inputGB.Reset()
outputGB.Reset()
totalGB.Reset()
var curTotal float64 = 0.0
for _, ti := range tiList {
inputGB.With(prometheus.Labels{
"address": ti.address,
"server_number": strconv.Itoa(ti.server_number),
"server_name": ti.server_name,
"dns_name": ti.dns_name,
"product": ti.product,
}).Add(ti.input)
outputGB.With(prometheus.Labels{
"address": ti.address,
"server_number": strconv.Itoa(ti.server_number),
"server_name": ti.server_name,
"dns_name": ti.dns_name,
"product": ti.product,
}).Add(ti.output)
totalGB.With(prometheus.Labels{
"address": ti.address,
"server_number": strconv.Itoa(ti.server_number),
"server_name": ti.server_name,
"dns_name": ti.dns_name,
"product": ti.product,
}).Add(ti.total)
curTotal += ti.total
}
if *flagLogUpdates {
d := end.Sub(start)
log.Printf("update ended: total=%v, dur=%v\n", curTotal, d)
}
if oneshot {
return
}
time.Sleep(time.Duration(interval) * 60 * time.Second)
}
}
func handleOneshot() {
updateMetrics(true)
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
}
gathering, err := gatherers.Gather()
if err != nil {
log.Fatalf("Gather failed: %v\n", err)
}
for _, mf := range gathering {
_, err := expfmt.MetricFamilyToText(os.Stdout, mf)
if err != nil {
log.Fatalf("Export failed: %v\n", err)
}
}
}
func main() {
flag.Parse()
if *flagVersion {
fmt.Printf("%s: version %s\n", os.Args[0], versionString)
os.Exit(0)
}
if *flagLicense {
fmt.Printf("%s: version %s\n\nThis software is published under the terms of the GPL version 2.\nA copy is at %s.\n",
os.Args[0], versionString, GPLv2)
os.Exit(0)
}
if *flagType!="day" && *flagType != "year" && *flagType!="month" {
log.Fatalf("bad --type option, %v not in (day,month,year)",*flagType);
}
hetznerUsername = os.Getenv("HETZNER_USER")
hetznerPassword = os.Getenv("HETZNER_PASS")
if hetznerUsername == "" || hetznerPassword == "" {
log.Fatal("Please provide HETZNER_USER and HETZNER_PASS as environment variables")
}
prometheus.MustRegister(inputGB)
prometheus.MustRegister(outputGB)
prometheus.MustRegister(totalGB)
if *flagOneshot {
handleOneshot()
os.Exit(0)
}
go updateMetrics(false)
fmt.Printf("Listening on %q\n", *flagListen)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*flagListen, nil))
}