-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
107 lines (87 loc) · 2.45 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
// splunk_exporter
// Prometheus exporter for Splunk API data
//
// Copyright 2019, MongoDB, Inc.
//
// All rights reserved - Do Not Redistribute
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"github.com/ehershey/splunk-exporter/handler"
"gopkg.in/yaml.v2"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
configFile = kingpin.Flag("config", "Config File").Short('c').Default(fmt.Sprintf("%s.yml", path.Base(os.Args[0]))).String()
localCertFile = kingpin.Flag("localCert", "Splunk x509 cert File").Short('x').String()
)
func main() {
// Process command-line arguments
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
// Process config
cfg := newDefaultConfig()
configContent, err := ioutil.ReadFile(*configFile)
if err != nil {
log.Fatalf("Can't load config: %v", err)
}
err = yaml.Unmarshal([]byte(configContent), &cfg)
if err != nil {
log.Fatalf("Can't load config: %v", err)
}
// Set up the logger
//if err := logging.Initialize(cfg.Logging); err != nil {
//log.Fatalf("Can't initialize logging: %v", err)
//}
defer zap.L().Sync()
// build an http client
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
// Read in a custom cert file
if *localCertFile != "" {
certs, err := ioutil.ReadFile(*localCertFile)
if err != nil {
log.Fatalf("Failed to append %q to RootCAs: %v", localCertFile, err)
}
// Append our cert to the system pool
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
log.Println("Cert append failed, using system certs only")
}
}
// Trust the augmented cert pool in our client
config := &tls.Config{
InsecureSkipVerify: true,
}
tr := &http.Transport{TLSClientConfig: config}
client := &http.Client{Transport: tr}
// BasicAuth: [ cfg.username, cfg.password ]
// }
// Set up prom handler for splunk data
splunkHandler, err := handler.NewSplunkHandler(client, cfg.Username, cfg.Password)
if err != nil {
zap.S().Fatalw("Can't initialize splunk handler.",
"err", err)
}
zap.S().Info("Splunk exporter started...")
// Expose the exporter's own metrics
http.Handle(cfg.MetricsPath, promhttp.Handler())
// Expose the Splunk metrics
http.Handle(cfg.ScrapePath, splunkHandler)
err = http.ListenAndServe(cfg.ListenAddress, nil)
if err != nil {
zap.S().Fatalw("HTTP ListenAndServe() error:",
"err", err,
)
}
}