-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
75 lines (62 loc) · 2.13 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
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
"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:"Display the version and exit"`
}
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() {
var err error
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, err = NewExporter(opts.V2RayEndpoint, scrapeTimeout)
if err != nil {
os.Exit(1)
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc(opts.MetricsPath, 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 {
logrus.Debugf("Write() err: %s", err)
}
})
logrus.Infof("Server is ready to handle incoming scrape requests.")
logrus.Fatal(http.ListenAndServe(opts.Listen, nil))
defer exporter.conn.Close()
}