forked from kbudde/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (124 loc) · 3.52 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
package main
import (
"bytes"
"context"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
var (
is_conf = kingpin.Flag(
"gi",
"Enable config file.",
).Default("true").Bool()
config_file = kingpin.Flag(
"config-path",
"Path to config file.",
).Default("conf/rabbitmq.conf").String()
)
const (
defaultLogLevel = log.InfoLevel
serviceName = "RabbitMQ_exporter"
)
func initLogger() {
log.SetLevel(getLogLevel())
if strings.ToUpper(config.OutputFormat) == "JSON" {
log.SetFormatter(&log.JSONFormatter{})
} else {
// The TextFormatter is default, you don't actually have to do this.
log.SetFormatter(&log.TextFormatter{})
}
}
func main() {
//log.AddFlags(kingpin.CommandLine)
//kingpin.Version(version.Print("node_exporter_sd"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln(*is_conf)
if *is_conf {
initConfigFromFile(*config_file)
} else {
initConfig()
}
initLogger()
initClient()
exporter := newExporter()
prometheus.MustRegister(exporter)
log.WithFields(log.Fields{
"VERSION": Version,
"REVISION": Revision,
"BRANCH": Branch,
"BUILD_DATE": BuildDate,
// "RABBIT_PASSWORD": config.RABBIT_PASSWORD,
}).Info("Starting RabbitMQ exporter")
log.WithFields(log.Fields{
"PUBLISH_ADDR": config.PublishAddr,
"PUBLISH_PORT": config.PublishPort,
"RABBIT_URL": config.RabbitURL,
"RABBIT_USER": config.RabbitUsername,
"OUTPUT_FORMAT": config.OutputFormat,
"RABBIT_CAPABILITIES": formatCapabilities(config.RabbitCapabilities),
"RABBIT_EXPORTERS": config.EnabledExporters,
"CAFILE": config.CAFile,
"CERTFILE": config.CertFile,
"KEYFILE": config.KeyFile,
"SKIPVERIFY": config.InsecureSkipVerify,
"EXCLUDE_METRICS": config.ExcludeMetrics,
"SKIP_QUEUES": config.SkipQueues.String(),
"INCLUDE_QUEUES": config.IncludeQueues,
"SKIP_VHOST": config.SkipVHost.String(),
"INCLUDE_VHOST": config.IncludeVHost,
"RABBIT_TIMEOUT": config.Timeout,
"MAX_QUEUES": config.MaxQueues,
// "RABBIT_PASSWORD": config.RABBIT_PASSWORD,
}).Info("Active Configuration")
handler := http.NewServeMux()
handler.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>RabbitMQ Exporter</title></head>
<body>
<h1>RabbitMQ Exporter</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>`))
})
server := &http.Server{Addr: config.PublishAddr + ":" + config.PublishPort, Handler: handler}
go func() {
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
<-runService()
log.Info("Shutting down")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if err := server.Shutdown(ctx); err != nil {
log.Fatal(err)
}
cancel()
}
func getLogLevel() log.Level {
lvl := strings.ToLower(os.Getenv("LOG_LEVEL"))
level, err := log.ParseLevel(lvl)
if err != nil {
level = defaultLogLevel
}
return level
}
func formatCapabilities(caps rabbitCapabilitySet) string {
var buffer bytes.Buffer
first := true
for k := range caps {
if !first {
buffer.WriteString(",")
}
first = false
buffer.WriteString(string(k))
}
return buffer.String()
}