-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
161 lines (143 loc) · 4.48 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
package main
import (
"hash/crc32"
"io/ioutil"
"net/http"
"strconv"
"time"
"log"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace = "repo"
)
func doProbe(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "Target parameter is missing", http.StatusBadRequest)
return
}
arch := r.URL.Query().Get("arch")
if arch == "" {
http.Error(w, "Arch parameter is missing", http.StatusBadRequest)
return
}
repodata, _, err := fetch("http://" + target + "/" + arch + "-repodata")
if err != nil {
log.Printf("Error fetching repodata: %s", err)
http.Error(w, "Error fetching repodata: "+err.Error(), http.StatusPreconditionFailed)
}
_, stagedataStatusCode, err := fetch("http://" + target + "/" + arch + "-stagedata")
if err != nil {
log.Printf("Error fetching stagedata: %s", err)
http.Error(w, "Error fetching stagedata: "+err.Error(), http.StatusPreconditionFailed)
}
otimes, c, err := fetch("http://" + target + "/otime")
if err != nil {
log.Println("Error fetching origin timestamp file: %s", err)
http.Error(w, "Error fetching origin time: "+err.Error(), http.StatusPreconditionFailed)
}
var otime float64
if c == 200 {
// If this fails it will just stay at zero; acceptable.
otime, err = strconv.ParseFloat(strings.TrimSpace(string(otimes)), 64)
if err != nil {
log.Println("Error parsing otime", err)
}
}
stimeStarts, c, err := fetch("http://" + target + "/stime-start")
if err != nil {
http.Error(w, "Error fetching origin time: "+err.Error(), http.StatusPreconditionFailed)
}
var stimeStart float64
if c == 200 {
// If this fails it will just stay at zero; acceptable.
stimeStart, err = strconv.ParseFloat(strings.TrimSpace(string(stimeStarts)), 64)
if err != nil {
log.Println("Error parsing stimeStart", err)
}
}
stimeEnds, c, err := fetch("http://" + target + "/stime-end")
if err != nil {
http.Error(w, "Error fetching origin time: "+err.Error(), http.StatusPreconditionFailed)
}
var stimeEnd float64
if c == 200 {
// If this fails it will just stay at zero; acceptable.
stimeEnd, err = strconv.ParseFloat(strings.TrimSpace(string(stimeEnds)), 64)
if err != nil {
log.Println("Error parsing stimeEnd", err)
}
}
var (
rdatachecksum = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "", "repodata_checksum"),
Help: "CRC32 of the repodata",
},
)
repostaged = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "", "is_staged"),
Help: "Non-zero if a stagedata file is present on the repo",
},
)
repoOriginTime = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "", "origin_time"),
Help: "A Unix Timestamp updated every minute on the origin",
},
)
repoSyncStartTime = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "", "sync_start_time"),
Help: "A Unix timestamp written by the mirror when it last started a sync",
},
)
repoSyncEndTime = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "", "sync_end_time"),
Help: "A Unix timestamp written by the mirror when it last finished a sync",
},
)
)
rdatachecksum.Set(float64(crc32.ChecksumIEEE(repodata)))
if stagedataStatusCode == 200 {
repostaged.Set(1)
}
repoOriginTime.Set(otime)
repoSyncStartTime.Set(stimeStart)
repoSyncEndTime.Set(stimeEnd)
registry := prometheus.NewRegistry()
registry.MustRegister(rdatachecksum, repostaged, repoOriginTime)
if stimeStart > 0 && stimeEnd > 0 {
registry.Register(repoSyncStartTime)
registry.Register(repoSyncEndTime)
}
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP(w, r)
}
func fetch(url string) ([]byte, int, error) {
c := http.Client{Timeout: time.Second * 10}
resp, err := c.Get(url)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
return bytes, resp.StatusCode, err
}
func main() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/probe", doProbe)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>XBPS Repo Exporter</title></head>
<body>
<h1>XBPS Repo Exporter</h1>
</body>
</html>`))
})
http.ListenAndServe(":1234", nil)
}