-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
100 lines (92 loc) · 3.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"io"
"log"
"net/http"
"os"
"time"
)
type JustMySocksData struct {
MonthlyBWLimitB uint64 `json:"monthly_bw_limit_b"`
BWCounterB uint64 `json:"bw_counter_b"`
BWResetDayOfMonth int `json:"bw_reset_day_of_month"`
}
var (
monthlyBWLimitB = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "justmysocks_monthly_bw_limit_bytes",
Help: "Monthly bandwidth limit in bytes",
},
[]string{"service"})
bwCounterB = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "justmysocks_bw_counter_bytes",
Help: "Bandwidth bandwidth in bytes",
}, []string{"service"})
bwResetDayOfMonth = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "justmysocks_bw_reset_day_of_month",
Help: "Day of month for bandwidth reset",
}, []string{"service"})
)
var httpClient = &http.Client{
Timeout: 10 * time.Second,
}
func fetchJustMySocksAPIData(apiAddress string, service string, id string) (*JustMySocksData, error) {
resp, err := httpClient.Get(fmt.Sprintf("%s?service=%s&id=%s", apiAddress, service, id))
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println("Error closing response body: ", err)
}
}(resp.Body)
var data JustMySocksData
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}
func updateMetrics(apiAddress string, service string, id string) {
data, err := fetchJustMySocksAPIData(apiAddress, service, id)
if err != nil {
log.Println("Error fetching data: ", err)
} else {
log.Println("Fetched data: ", *data)
monthlyBWLimitB.WithLabelValues(service).Set(float64(data.MonthlyBWLimitB))
bwCounterB.WithLabelValues(service).Set(float64(data.BWCounterB))
bwResetDayOfMonth.WithLabelValues(service).Set(float64(data.BWResetDayOfMonth))
}
}
var (
// Default port allocation https://github.com/prometheus/prometheus/wiki/Default-port-allocations
listenAddress = flag.String("web.listen-address", ":10001", "Address to listen on for web interface and telemetry.")
apiAddress = flag.String("api-address", "https://justmysocks5.net/members/getbwcounter.php", "Address of JustMySocks API")
service = flag.String("service", "", "JustMySocks service number")
id = flag.String("id", "", "JustMySocks UUID")
updateInterval = flag.Duration("update-interval", 5*time.Minute, "Update interval for metrics")
)
func main() {
flag.Parse()
log.SetOutput(os.Stdout)
http.Handle("/metrics", promhttp.Handler())
go func() {
log.Println("Starting justmysocks metrics update loop")
log.Println("Update interval:", *updateInterval)
for {
updateMetrics(*apiAddress, *service, *id)
// Update every 5 minutes,Becasue the data is updated every 5 minutes by justmysocks
time.Sleep(*updateInterval)
}
}()
prometheus.MustRegister(monthlyBWLimitB, bwCounterB, bwResetDayOfMonth)
log.Printf("Listening on %s...", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}