forked from oliver006/redis_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (118 loc) · 3.43 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
package main
import (
"crypto/tls"
"github.com/oliver006/redis_exporter/config"
"io/ioutil"
"net/http"
"os"
"runtime"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
log "github.com/sirupsen/logrus"
)
var (
// BuildVersion, BuildDate, BuildCommitSha are filled in by the build script
BuildVersion = "<<< filled in by build >>>"
BuildDate = "<<< filled in by build >>>"
BuildCommitSha = "<<< filled in by build >>>"
)
func getEnv(key string, defaultVal string) string {
if envVal, ok := os.LookupEnv(key); ok {
return envVal
}
return defaultVal
}
func getEnvBool(key string, defaultVal bool) bool {
if envVal, ok := os.LookupEnv(key); ok {
envBool, err := strconv.ParseBool(envVal)
if err == nil {
return envBool
}
}
return defaultVal
}
func main() {
conf := config.New()
switch conf.LogFormat {
case "json":
log.SetFormatter(&log.JSONFormatter{})
default:
log.SetFormatter(&log.TextFormatter{})
}
log.Printf("Redis Metrics Exporter %s build date: %s sha1: %s Go: %s GOOS: %s GOARCH: %s",
BuildVersion, BuildDate, BuildCommitSha,
runtime.Version(),
runtime.GOOS,
runtime.GOARCH,
)
if conf.IsDebug {
log.SetLevel(log.DebugLevel)
log.Debugln("Enabling debug output")
} else {
log.SetLevel(log.InfoLevel)
}
if conf.ShowVersion {
return
}
to, err := time.ParseDuration(conf.ConnectionTimeout)
if err != nil {
log.Fatalf("Couldn't parse connection timeout duration, err: %s", err)
}
var tlsClientCertificates []tls.Certificate
if (conf.TlsClientKeyFile != "") != (conf.TlsClientCertFile != "") {
log.Fatal("TLS client key file and cert file should both be present")
}
if conf.TlsClientKeyFile != "" && conf.TlsClientCertFile != "" {
cert, err := tls.LoadX509KeyPair(conf.TlsClientCertFile, conf.TlsClientKeyFile)
if err != nil {
log.Fatalf("Couldn't load TLS client key pair, err: %s", err)
}
tlsClientCertificates = append(tlsClientCertificates, cert)
}
var ls []byte
if conf.ScriptPath != "" {
if ls, err = ioutil.ReadFile(conf.ScriptPath); err != nil {
log.Fatalf("Error loading script file %s err: %s", conf.ScriptPath, err)
}
}
registry := prometheus.NewRegistry()
if !conf.RedisMetricsOnly {
registry = prometheus.DefaultRegisterer.(*prometheus.Registry)
}
exp, err := NewRedisExporter(
conf.RedisAddr,
ExporterOptions{
Password: conf.RedisPwd,
Namespace: conf.Namespace,
ConfigCommandName: conf.ConfigCommand,
CheckKeys: conf.CheckKeys,
CheckSingleKeys: conf.CheckSingleKeys,
LuaScript: ls,
InclSystemMetrics: conf.InclSystemMetrics,
IsTile38: conf.IsTile38,
ExportClientList: conf.ExportClientList,
SkipTLSVerification: conf.SkipTLSVerification,
SetClientName: conf.SetClientName,
ClientCertificates: tlsClientCertificates,
ConnectionTimeouts: to,
MetricsPath: conf.MetricPath,
RedisMetricsOnly: conf.RedisMetricsOnly,
Registry: registry,
},
)
if err != nil {
log.Fatal(err)
}
go func(registry *prometheus.Registry){
for {
err = push.New(conf.PushGatewayAddr, conf.AppName).Gatherer(registry).Push()
if err != nil {
log.Infof("Error pushing metrics to push gateway. Error: %+v", err)
}
time.Sleep(time.Duration(conf.PushIntervalInSec) * time.Second)
}
}(registry)
log.Fatal(http.ListenAndServe(conf.ListenAddress, exp))
}