Skip to content

Commit

Permalink
Merge pull request percona#34 from roman-vynar/http-auth
Browse files Browse the repository at this point in the history
Added support for MONGODB_URI and HTTP_AUTH env vars.
  • Loading branch information
dbmurphy authored Nov 17, 2016
2 parents 22ae800 + 0cce34f commit 933fe63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ The exporter can be started by running the '*mongodb_exporter*' binary that is c

It is recommended to define the following options:

- **-mongodb.uri** - The URI of the MongoDB port (*default: mongodb://localhost:27017*)
- **-auth.user** - The optional exporter HTTP auth username (*default: none*)
- **-auth.pass** - The optional exporter HTTP auth password (*default: none*)
- **-web.listen-address** - The listen address of the exporter (*default: ":9104"*)
- **-log_dir** - The directory to write the log file (*default: /tmp*)

To define your own MongoDB URI, use environment variable `MONGODB_URI`. If set this variable takes precedence over **-mongodb.uri** flag.
For example: `export MONGODB_URI=mongodb://localhost:27017`

To enable HTTP basic authentication, set environment variable `HTTP_AUTH` to user:password pair.
For example: `export HTTP_AUTH="user:password"`

*For more options see the help page with '-h' or '--help'*

If you use [MongoDB Authorization](https://docs.mongodb.org/manual/core/authorization/), you must:
Expand All @@ -48,10 +51,10 @@ db.getSiblingDB("admin").createUser({
})
```

2. Add the username/password to the '*-mongodb.uri*' command-line option for mongodb_exporter, example:
2. Set environment variable `MONGODB_URI` before starting the exporter:

```
mongodb_exporter -mongodb.uri mongodb://mongodb_exporter:s3cr3tpassw0rd@localhost:27017
export MONGODB_URI=mongodb://mongodb_exporter:s3cr3tpassw0rd@localhost:27017
```

### Note about how this works
Expand Down
35 changes: 24 additions & 11 deletions mongodb_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"strings"

"github.com/Percona-Lab/prometheus_mongodb_exporter/collector"
"github.com/Percona-Lab/prometheus_mongodb_exporter/shared"
Expand All @@ -29,8 +30,6 @@ var (

mongodbURIFlag = flag.String("mongodb.uri", mongodbDefaultUri(), "Mongodb URI, format: [mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]")
enabledGroupsFlag = flag.String("groups.enabled", "asserts,durability,background_flushing,connections,extra_info,global_lock,index_counters,network,op_counters,op_counters_repl,memory,locks,metrics", "Comma-separated list of groups to use, for more info see: docs.mongodb.org/manual/reference/command/serverStatus/")
authUserFlag = flag.String("auth.user", "", "Username for basic auth.")
authPassFlag = flag.String("auth.pass", "", "Password for basic auth.")
)

func printVersion() {
Expand All @@ -54,17 +53,25 @@ func (h *basicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

func hasUserAndPassword() bool {
return *authUserFlag != "" && *authPassFlag != ""
}

func prometheusHandler() http.Handler {
var authUser, authPass string
httpAuth := os.Getenv("HTTP_AUTH")
if httpAuth != "" {
data := strings.SplitN(httpAuth, ":", 2)
if len(data) != 2 || data[0] == "" || data[1] == "" {
panic("HTTP_AUTH should be formatted as user:password")
}
authUser = data[0]
authPass = data[1]
fmt.Println("HTTP basic authentication is enabled")
}

handler := prometheus.Handler()
if hasUserAndPassword() {
if authUser != "" && authPass != "" {
handler = &basicAuthHandler{
handler: prometheus.Handler().ServeHTTP,
user: *authUserFlag,
password: *authPassFlag,
handler: handler.ServeHTTP,
user: authUser,
password: authPass,
}
}

Expand All @@ -73,12 +80,18 @@ func prometheusHandler() http.Handler {

func startWebServer() {
printVersion()
fmt.Printf("Listening on %s\n", *listenAddressFlag)

uri := os.Getenv("MONGODB_URI")
if uri != "" {
mongodbURIFlag = &uri
}

handler := prometheusHandler()

registerCollector()

http.Handle(*metricsPathFlag, handler)
fmt.Printf("Listening on %s\n", *listenAddressFlag)
err := http.ListenAndServe(*listenAddressFlag, nil)

if err != nil {
Expand Down

0 comments on commit 933fe63

Please sign in to comment.