-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmtproto_proxy_exporter.go
93 lines (77 loc) · 1.95 KB
/
mtproto_proxy_exporter.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/Ty3uK/mtproto_proxy_exporter/config"
metricsPkg "github.com/Ty3uK/mtproto_proxy_exporter/metrics"
statsPkg "github.com/Ty3uK/mtproto_proxy_exporter/stats"
)
var (
configPath = flag.String("config", "", "YML config path")
help = flag.Bool("help", false, "prints help")
versionCmd = flag.Bool("version", false, "prints information about version and build")
stats = statsPkg.Stats{}
metrics = metricsPkg.Metrics{
List: make(map[string]metricsPkg.Item),
}
version = "current"
build = "master build"
)
func run() {
for {
err := stats.GetData(config.Config.StatsAddress)
if err != nil {
log.Printf("error: could not get stats data: %v\n", err)
} else {
for _, item := range metrics.List {
item.SetValue(
stats.GetNumberItem(item.StatName),
)
}
}
time.Sleep(time.Duration(config.Config.Interval) * time.Second)
}
}
func initFromConfig() {
fmt.Println("LISTENING ON :", config.Config.Address)
fmt.Println("SCAN INTERVAL :", config.Config.Interval)
fmt.Println("REQUEST TIMEOUT :", config.Config.RequestTimeout)
fmt.Println()
for _, configItem := range config.Config.Metrics {
err := metrics.AddItem(
configItem.StatName,
configItem.Name,
configItem.Help,
)
if err != nil {
log.Printf("could not add metrics item: %v\n", err)
continue
}
fmt.Println("FROM MTPROTO :", configItem.StatName)
fmt.Println("TO PROMETHEUS :", configItem.Name)
fmt.Println()
}
}
func main() {
flag.Parse()
if *versionCmd {
config.PrintVersion(version, build)
return
}
if *help {
config.PrintHelp()
return
}
err := config.InitFromFile(*configPath)
if err != nil {
log.Fatalf("could not init config from file: %v", err)
}
initFromConfig()
http.Handle("/metrics", promhttp.Handler())
go run()
log.Fatal(http.ListenAndServe(config.Config.Address, nil))
}