This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
rancher_exporter.go
120 lines (103 loc) · 5.48 KB
/
rancher_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
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
package main
import (
"flag"
"net/http"
"os"
"regexp"
"strconv"
"github.com/infinityworks/prometheus-rancher-exporter/measure"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
const (
namespace = "rancher" // Used to prepand Prometheus metrics created by this exporter.
defaultLabelsFilter = "^io.prometheus"
)
// Runtime variables, user controllable for targeting, authentication and filtering.
var (
log = logrus.New()
metricsPath = getEnv("METRICS_PATH", "/metrics") // Path under which to expose metrics
listenAddress = getEnv("LISTEN_ADDRESS", ":9173") // Address on which to expose metrics
rancherURL = os.Getenv("CATTLE_URL") // URL of Rancher Server API e.g. http://192.168.0.1:8080/v2-beta
accessKey = os.Getenv("CATTLE_ACCESS_KEY") // Optional - Access Key for Rancher API
secretKey = os.Getenv("CATTLE_SECRET_KEY") // Optional - Secret Key for Rancher API
labelsFilter = os.Getenv("LABELS_FILTER") // Optional - Filter for Rancher label names
logLevel = getEnv("LOG_LEVEL", "info") // Optional - Set the logging level
resourceLimit = getEnv("API_LIMIT", "100") // Optional - Rancher API resource limit (default: 100)
hideSys, _ = strconv.ParseBool(getEnv("HIDE_SYS", "true")) // hideSys - Optional - Flag that indicates if the environment variable `HIDE_SYS` is set to a boolean true value
)
// Predefined variables that are used throughout the exporter
var (
agentStates = []string{"activating", "active", "reconnecting", "disconnected", "disconnecting", "finishing-reconnect", "reconnected"}
clusterStates = []string{"active", "cordoned", "degraded", "disconnected", "drained", "draining", "healthy", "initializing", "locked", "purged", "purging", "reconnecting", "reinitializing", "removed", "running", "unavailable", "unhealthy", "upgraded", "upgrading"}
hostStates = []string{"activating", "active", "deactivating", "disconnected", "error", "erroring", "inactive", "provisioned", "purged", "purging", "reconnecting", "registering", "removed", "removing", "requested", "restoring", "updating_active", "updating_inactive"}
stackStates = []string{"activating", "active", "canceled_upgrade", "canceling_upgrade", "error", "erroring", "finishing_upgrade", "removed", "removing", "requested", "restarting", "rolling_back", "updating_active", "upgraded", "upgrading"}
serviceStates = []string{"activating", "active", "canceled_upgrade", "canceling_upgrade", "deactivating", "finishing_upgrade", "inactive", "registering", "removed", "removing", "requested", "restarting", "rolling_back", "updating_active", "updating_inactive", "upgraded", "upgrading"}
healthStates = []string{"healthy", "unhealthy", "initializing", "degraded", "started-once"}
componentStatus = []string{"True", "False", "Unknown"}
nodeStates = []string{"active", "cordoned", "drained", "draining", "provisioning", "registering", "unavailable"}
endpoints = []string{"stacks", "services", "hosts"} // EndPoints the exporter will trawl
endpointsV3 = []string{"clusters", "nodes"} // EndPoints the exporter will trawl]
stackRef = make(map[string]string) // Stores the StackID and StackName as a map, used to provide label dimensions to service metrics
clusterRef = make(map[string]string) // Stores the ClusterID and ClusterName as a map, used to provide label dimensions to node metrics
)
// getEnv - Allows us to supply a fallback option if nothing specified
func getEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
func main() {
flag.Parse()
// Sets the logging value for the exporter, defaults to info
setLogLevel(logLevel)
// check the rancherURL ($CATTLE_URL) has been provided correctly
if rancherURL == "" {
log.Fatal("CATTLE_URL must be set and non-empty")
}
if labelsFilter == "" {
labelsFilter = defaultLabelsFilter
}
labelsFilterRegexp, err := regexp.Compile(labelsFilter)
if err != nil {
log.Fatal("LABELS_FILTER must be valid regular expression")
}
log.Info("Starting Prometheus Exporter for Rancher")
log.Info(
"Runtime Configuration in-use: URL of Rancher Server: ",
rancherURL,
" Access key: ",
accessKey,
" System services hidden: ",
hideSys,
" Labels filter: ",
labelsFilter,
)
// Register internal metrics used for tracking the exporter performance
measure.Init()
// Register a new Exporter
exporter := newExporter(rancherURL, accessKey, secretKey, labelsFilterRegexp, hideSys, resourceLimit)
// Register Metrics from each of the endpoints
// This invokes the Collect method through the prometheus client libraries.
prometheus.MustRegister(exporter)
// Setup HTTP handler
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>Rancher exporter</title></head>
<body>
<h1>rancher exporter</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
log.Printf("Starting Server on port %s and path %s", listenAddress, metricsPath)
log.Fatal(http.ListenAndServe(listenAddress, nil))
}