forked from wi1dcard/v2ray-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (57 loc) · 2.05 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
package main
import (
"fmt"
"net/http"
"os"
"time"
flags "github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var opts struct {
Listen string `short:"l" long:"listen" description:"Listen address" value-name:"[ADDR]:PORT" default:":9550"`
MetricsPath string `short:"m" long:"metrics-path" description:"Metrics path" value-name:"PATH" default:"/scrape"`
V2RayEndpoint string `short:"e" long:"v2ray-endpoint" description:"V2Ray API endpoint" value-name:"HOST:PORT" default:"127.0.0.1:8080"`
ScrapeTimeoutInSeconds int64 `short:"t" long:"scrape-timeout" description:"The timeout in seconds for every individual scrape" value-name:"N" default:"3"`
Version bool `long:"version" description:"Show version"`
}
var (
buildVersion = "dev"
buildCommit = "none"
buildDate = "unknown"
)
var exporter *Exporter
func scrapeHandler(w http.ResponseWriter, r *http.Request) {
promhttp.HandlerFor(
exporter.registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError},
).ServeHTTP(w, r)
}
func main() {
if _, err := flags.Parse(&opts); err != nil {
os.Exit(0)
}
fmt.Printf("V2Ray Exporter %v-%v (built %v)\n", buildVersion, buildCommit, buildDate)
if opts.Version {
os.Exit(0)
}
scrapeTimeout := time.Duration(opts.ScrapeTimeoutInSeconds) * time.Second
exporter = NewExporter(opts.V2RayEndpoint, scrapeTimeout)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/scrape", scrapeHandler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>V2Ray Exporter</title></head>
<body>
<h1>V2Ray Exporter ` + buildVersion + `</h1>
<p><a href='/metrics'>Exporter Metrics</a></p>
<p><a href='` + opts.MetricsPath + `'>Scrape V2Ray Metrics</a></p>
</body>
</html>
`))
if err != nil {
log.Debugf("Write() err: %s", err)
}
})
log.Infof("Server is ready to handle incoming scrape requests on %s", opts.Listen)
log.Fatal(http.ListenAndServe(opts.Listen, nil))
}