-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
213 lines (188 loc) · 5.09 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type APIBoxList []struct {
Box struct {
ID int `json:"id"`
} `json:"storagebox"`
}
type APIBoxDetail struct {
Box Storagebox `json:"storagebox"`
}
type APIError struct {
Error struct {
Status int `json:"status"`
Code string `json:"code"`
} `json:"error"`
}
type Storagebox struct {
ID int `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
Product string `json:"product"`
Cancelled bool `json:"cancelled"`
Locked bool `json:"locked"`
LinkedServer int `json:"linked_server"`
PaidUntil string `json:"paid_until"`
DiskQuota float64 `json:"disk_quota"`
DiskUsage float64 `json:"disk_usage"`
DiskUsageData float64 `json:"disk_usage_data"`
DiskUsageSnapshots float64 `json:"disk_usage_snapshots"`
Webdav bool `json:"webdav"`
Samba bool `json:"samba"`
SSH bool `json:"ssh"`
BackupService bool `json:"backup_service"`
ExternalReachability bool `json:"external_reachability"`
Zfs bool `json:"zfs"`
Server string `json:"server"`
}
var (
hetznerUsername string
hetznerPassword string
boxes []Storagebox
labels = []string{"id", "name", "product", "server"}
diskQuota = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "storagebox",
Name: "disk_quota",
Help: "Total diskspace in MB",
},
labels,
)
diskUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "storagebox",
Name: "disk_usage",
Help: "Total used diskspace in MB",
},
labels,
)
diskUsageData = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "storagebox",
Name: "disk_usage_data",
Help: "Used diskspace by files in MB",
},
labels,
)
diskUsageSnapshots = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "storagebox",
Name: "disk_usage_snapshots",
Help: "Used diskspace by snapshots in MB",
},
labels,
)
)
func updateBoxes() {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://robot-ws.your-server.de/storagebox", nil)
req.SetBasicAuth(hetznerUsername, hetznerPassword)
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
bodyText, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var apiErr APIError
err = json.Unmarshal(bodyText, &apiErr)
if err != nil {
log.Fatal(err)
}
log.Printf("API Error: %d - %s", apiErr.Error.Status, apiErr.Error.Code)
return
}
var apiResponse APIBoxList
err = json.Unmarshal(bodyText, &apiResponse)
if err != nil {
log.Fatal(err)
}
for _, entry := range apiResponse {
req, err := http.NewRequest("GET", fmt.Sprintf("https://robot-ws.your-server.de/storagebox/%d", entry.Box.ID), nil)
req.SetBasicAuth(hetznerUsername, hetznerPassword)
resp, err = client.Do(req)
if err != nil {
fmt.Println(err)
return
}
bodyText, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var apiErr APIError
err = json.Unmarshal(bodyText, &apiErr)
if err != nil {
log.Fatal(err)
}
log.Printf("API Error: %d - %s", apiErr.Error.Status, apiErr.Error.Code)
return
}
var box APIBoxDetail
err = json.Unmarshal(bodyText, &box)
if err != nil {
log.Fatal(err)
}
boxes = append(boxes, box.Box)
}
}
func updateMetrics() {
for {
updateBoxes()
for _, box := range boxes {
diskQuota.With(prometheus.Labels{
"id": strconv.Itoa(box.ID),
"name": box.Name,
"product": box.Product,
"server": box.Server,
}).Set(box.DiskQuota)
diskUsage.With(prometheus.Labels{
"id": strconv.Itoa(box.ID),
"name": box.Name,
"product": box.Product,
"server": box.Server,
}).Set(box.DiskUsage)
diskUsageData.With(prometheus.Labels{
"id": strconv.Itoa(box.ID),
"name": box.Name,
"product": box.Product,
"server": box.Server,
}).Set(box.DiskUsageData)
diskUsageSnapshots.With(prometheus.Labels{
"id": strconv.Itoa(box.ID),
"name": box.Name,
"product": box.Product,
"server": box.Server,
}).Set(box.DiskUsageSnapshots)
}
// Try to avoid rate limiting
// Limit is 200req / 1h
time.Sleep(60 * time.Second)
}
}
const (
listenAddr = ":9509"
)
func main() {
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(diskQuota)
prometheus.MustRegister(diskUsage)
prometheus.MustRegister(diskUsageData)
prometheus.MustRegister(diskUsageSnapshots)
go updateMetrics()
fmt.Printf("Listening on %q", listenAddr)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(listenAddr, nil)
}