Skip to content

Commit

Permalink
Merge pull request #4 from exosite/support_config_file
Browse files Browse the repository at this point in the history
use viper to support config file
  • Loading branch information
johnlinvc authored Nov 27, 2019
2 parents 86b062c + 9c0b164 commit 7a0d3f4
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 47 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/go-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Install Promu
run: make promu

- name: Build
run: |
make build
make
zip 389ds_exporter 389ds_exporter
- name: Create Release
id: create_release
uses: actions/[email protected]
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Install Promu
run: make promu

- name: Build
run: make build
run: make
82 changes: 54 additions & 28 deletions 389ds_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,89 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/terrycain/389ds_exporter/exporter"
)

var (
listenPort = flag.String("web.listen-address", ":9496", "Bind address for prometheus HTTP metrics server")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path to expose metrics on")
ldapAddr = flag.String("ldap.addr", "localhost:389", "Address of 389ds server")
ldapUser = flag.String("ldap.user", "cn=Directory Manager", "389ds Directory Manager user")
ldapPass = flag.String("ldap.pass", "", "389ds Directory Manager password")
ldapCert = flag.String("ldap.cert", "", "Certificate for LDAP with startTLS")
ldapCertServerName = flag.String("ldap.cert-server-name", "", "ServerName for LDAP with startTLS")
ipaDomain = flag.String("ipa-domain", "", "FreeIPA domain e.g. example.org")
interval = flag.Duration("interval", 60*time.Second, "Scrape interval")
debug = flag.Bool("debug", false, "Debug logging")
jsonFormat = flag.Bool("log-json", false, "JSON formatted log messages")
)

func main() {
flag.Parse()
flag.String("web.listen-address", ":9496", "Bind address for prometheus HTTP metrics server")
flag.String("web.telemetry-path", "/metrics", "Path to expose metrics on")
flag.String("ldap.addr", "localhost:389", "Address of 389ds server")
flag.String("ldap.user", "cn=Directory Manager", "389ds Directory Manager user")
flag.String("ldap.pass", "", "389ds Directory Manager password")
flag.String("ldap.cert", "", "Certificate for LDAP with startTLS")
flag.String("ldap.cert-server-name", "", "ServerName for LDAP with startTLS")
flag.String("ipa-domain", "", "FreeIPA domain e.g. example.org")
flag.Duration("interval", 60*time.Second, "Scrape interval")
flag.Bool("debug", false, "Debug logging")
flag.Bool("log-json", false, "JSON formatted log messages")
flag.String("config", "", "YAML format config file with the extension (i.e. /path/to/config.yaml)")

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
configFile := viper.GetString("config")
if configFile != "" {
viper.SetConfigType("yaml")
viper.SetConfigFile(configFile)
err := viper.ReadInConfig()
if err != nil {
log.Fatal("error reading config. the error is", err)
}
}

listenPort := viper.GetString("web.listen-address")
metricsPath := viper.GetString("web.telemetry-path")
ldapAddr := viper.GetString("ldap.addr")
ldapUser := viper.GetString("ldap.user")
ldapPass := viper.GetString("ldap.pass")
ldapCert := viper.GetString("ldap.cert")
ldapCertServerName := viper.GetString("ldap.cert-server-name")
ipaDomain := viper.GetString("ipa-domain")
interval := viper.GetDuration("interval")
debug := viper.GetBool("debug")
jsonFormat := viper.GetBool("log-json")

if *debug {
if debug {
log.SetLevel(log.DebugLevel)
}
if *jsonFormat {
if jsonFormat {
log.SetFormatter(&log.JSONFormatter{})
}

if *ldapPass == "" {
if ldapPass == "" {
log.Fatal("ldapPass cannot be empty")
}
if *ipaDomain == "" {
if ipaDomain == "" {
log.Fatal("ipaDomain cannot be empty")
}

if (*ldapCert == "") != (*ldapCertServerName == "") {
if (ldapCert == "") != (ldapCertServerName == "") {
log.Fatal("ldapCert & ldapCertServerName must come together")
}

log.Info("Starting prometheus HTTP metrics server on ", *listenPort)
go StartMetricsServer(*listenPort)
log.Info("Starting prometheus HTTP metrics server on ", listenPort)
go StartMetricsServer(listenPort, metricsPath)

log.Info("Starting 389ds scraper for ", *ldapAddr)
for range time.Tick(*interval) {
log.Info("Starting 389ds scraper for ", ldapAddr)
log.Debug("Starting metrics scrape")
exporter.ScrapeMetrics(ldapAddr, ldapUser, ldapPass, ldapCert, ldapCertServerName, ipaDomain)
for range time.Tick(interval) {
log.Debug("Starting metrics scrape")
exporter.ScrapeMetrics(*ldapAddr, *ldapUser, *ldapPass, *ldapCert, *ldapCertServerName, *ipaDomain)
exporter.ScrapeMetrics(ldapAddr, ldapUser, ldapPass, ldapCert, ldapCertServerName, ipaDomain)
}
}

func StartMetricsServer(bindAddr string) {
func StartMetricsServer(bindAddr, metricsPath string) {
d := http.NewServeMux()
d.Handle(*metricsPath, promhttp.Handler())
d.Handle(metricsPath, promhttp.Handler())
d.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Consul Exporter</title></head>
<body>
<h1>Consul Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</dl>
<h2>Build</h2>
<pre>` + version.Info() + ` ` + version.BuildContext() + `</pre>
Expand Down
7 changes: 1 addition & 6 deletions exporter/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package exporter
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"strconv"
Expand Down Expand Up @@ -124,10 +123,6 @@ func init() {
)
}

func objectClass(name string) string {
return fmt.Sprintf("(objectClass=%v)", name)
}

func ScrapeMetrics(ldapAddr, ldapUser, ldapPass, ldapCert, ldapCertServerName, ipaDomain string) {
start := time.Now()
if err := scrapeAll(ldapAddr, ldapUser, ldapPass, ldapCert, ldapCertServerName, ipaDomain); err != nil {
Expand Down Expand Up @@ -281,7 +276,7 @@ func ldapSubordinatesQuery(l *ldap.Conn, baseDN, searchFilter string) (float64,
}

if len(sr.Entries) == 0 {
return -1, errors.New(fmt.Sprintf("No entries contain numSubordinates for %s (%s)", baseDN, searchFilter))
return -1, fmt.Errorf("no entries contain numSubordinates for %s (%s)", baseDN, searchFilter)
}

val := sr.Entries[0].GetAttributeValue("numSubordinates")
Expand Down
14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,46 @@ require (
cloud.google.com/go/pubsub v1.1.0 // indirect
cloud.google.com/go/storage v1.4.0 // indirect
dmitri.shuralyov.com/gpu/mtl v0.0.0-20191126053124-fec1b7b612ab // indirect
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/creack/pty v1.1.9 // indirect
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 // indirect
github.com/envoyproxy/go-control-plane v0.9.1 // indirect
github.com/go-gl/glfw v0.0.0-20191125211704-12ad95a8df72 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect
github.com/google/go-github/v25 v25.1.3 // indirect
github.com/google/pprof v0.0.0-20191105193234-27840fff0d09 // indirect
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf // indirect
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/json-iterator/go v1.1.8 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/kardianos/govendor v1.0.9 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pty v1.1.8 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/prometheus/client_golang v1.2.1
github.com/prometheus/common v0.7.0
github.com/prometheus/procfs v0.0.8 // indirect
github.com/prometheus/promu v0.5.0 // indirect
github.com/rogpeppe/go-internal v1.5.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.5.0
github.com/stretchr/objx v0.2.0 // indirect
go.opencensus.io v0.22.2 // indirect
golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c // indirect
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect
golang.org/x/mobile v0.0.0-20191123054942-d9e324ca8c38 // indirect
golang.org/x/net v0.0.0-20191125084936-ffdde1057850 // indirect
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirect
golang.org/x/oauth2 v0.0.0-20191122200657-5d9234df094c // indirect
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e // indirect
golang.org/x/sys v0.0.0-20191127021746-63cb32ae39b2 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
golang.org/x/tools v0.0.0-20191125224844-73cd2cc3b550 // indirect
golang.org/x/tools v0.0.0-20191127064951-724660f1afeb // indirect
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/genproto v0.0.0-20191115221424-83cc0476cb11 // indirect
Expand Down
Loading

0 comments on commit 7a0d3f4

Please sign in to comment.