Skip to content

Commit e9f6a0b

Browse files
committed
!!! CLEANUP | Move host & apiKey to query parameters
This moves the host & apiKey used for the metrics into query parameters instead of static command line arguments / environment variables. This is done in order to use the same mailcow-exporter instance for multiple targets while keeping the configuration in the prometheus config files. If you are updating from an earlier version, you will have to move these values from environment / command line arguments into your prometheus configuration as shown in the README file.
1 parent 2a28a2a commit e9f6a0b

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

.readme/api-key.png

54.8 KB
Loading

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ RUN cd /build \
55
&& go build -o /mailcow-exporter /build/main.go \
66
&& rm -Rf /build
77

8-
ENTRYPOINT /mailcow-exporter --host=$MAILCOW_HOST --api-key=$MAILCOW_API_KEY
8+
ENTRYPOINT /mailcow-exporter

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,32 @@
55

66
## Usage
77

8-
In order to use the exporter, you will need to get a readonly API token
9-
for the instance first. To extract that, log into the management interface
10-
and create one under 'Access > API'.
8+
```bash
9+
# As a docker container
10+
$ docker run -p '9099:9099' thej6s/mailcow-exporter
1111

12+
# Natively
13+
$ ./mailcow-exporter
1214
```
13-
$ docker run \
14-
-e 'MAILCOW_HOST=mail.mydomain.com' \
15-
-e 'MAILCOW_API_KEY=...' \
16-
-p '9099:9099' \
17-
thej6s/mailcow-exporter
15+
16+
The `/metrics` endpoint requires `host` and `apiKey` URL parameters. `host` is the
17+
hostname of your mailcow instance. `apiKey` should be a readonly API key that can
18+
be generated by logging into the mailcow management interface and navigating to
19+
'Access > API'.
20+
21+
![Visualization of where to find the API Key](./.readme/api-key)
22+
23+
The following prometheus configuration can be used in order to pass these information
24+
to the endpoint:
25+
26+
```yaml
27+
scrape_configs:
28+
- job_name: 'mailcow'
29+
static_configs:
30+
- targets: [ 'mailcow_exporter:9099' ]
31+
params:
32+
host: [ 'mail.example.com' ]
33+
apiKey: [ 'YOUR-APIKEY-HERE' ]
1834
```
1935
2036
## Example metrics

main.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
)
1414

1515
var (
16-
apiKey = flag.String("api-key", "", "API Key to use for the requests")
17-
host = flag.String("host", "", "The host of the mailcow instance")
1816
listen = flag.String("listen", ":9099", "Host and port to listen on")
1917
)
2018

@@ -60,22 +58,25 @@ func collectMetrics(host string, apiKey string) (*prometheus.Registry, error) {
6058
return registry, nil
6159
}
6260

63-
// Command line argument parsing
64-
func init() {
61+
func main() {
6562
flag.Parse()
66-
if *apiKey == "" || *host == "" {
67-
log.Fatal("Both --api-key and --host must be specified")
68-
}
69-
}
7063

71-
func main() {
7264
http.HandleFunc("/metrics", func(response http.ResponseWriter, request *http.Request) {
73-
registry, err := collectMetrics(*host, *apiKey)
65+
host := request.URL.Query().Get("host")
66+
apiKey := request.URL.Query().Get("apiKey")
67+
if host == "" || apiKey == "" {
68+
response.WriteHeader(http.StatusBadRequest)
69+
response.Write([]byte("Query parameters `host` & `apiKey` are required"))
70+
return
71+
}
72+
73+
registry, err := collectMetrics(host, apiKey)
7474
if err != nil {
7575
response.WriteHeader(http.StatusInternalServerError)
7676
response.Write([]byte(err.Error()))
7777
return
7878
}
79+
7980
promhttp.HandlerFor(
8081
registry,
8182
promhttp.HandlerOpts{},

0 commit comments

Comments
 (0)