forked from wulabing/justmysocks_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (87 loc) · 2.96 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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"io/ioutil"
"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"})
)
func fetchJustMySocksAPIData(apiAddress string, service string, id string) (*JustMySocksData, error) {
var data JustMySocksData
resp, err := http.Get(fmt.Sprintf("%s?service=%s&id=%s", apiAddress, service, id))
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &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://justmysocks6.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))
}