forked from camptocamp/prometheus-puppetdb-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (101 loc) · 4.52 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
108
109
110
111
112
113
114
115
package main
import (
"net/http"
"os"
"runtime"
"strings"
"time"
"github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/camptocamp/prometheus-puppetdb-exporter/internal/exporter"
)
// Config stores handler's configuration
type Config struct {
Version bool `long:"version" description:"Show version."`
PuppetDBUrl string `short:"u" long:"puppetdb-url" description:"PuppetDB base URL." env:"PUPPETDB_URL" required:"true" default:"https://puppetdb:8081/pdb/query"`
CertFile string `long:"cert-file" description:"A PEM encoded certificate file." env:"PUPPETDB_CERT_FILE"`
KeyFile string `long:"key-file" description:"A PEM encoded private key file." env:"PUPPETDB_KEY_FILE"`
CACertFile string `long:"ca-file" description:"A PEM encoded CA's certificate." env:"PUPPETDB_CA_FILE"`
SSLSkipVerify bool `long:"ssl-skip-verify" description:"Skip SSL verification." env:"PUPPETDB_SSL_SKIP_VERIFY"`
ScrapeInterval string `long:"scrape-interval" description:"Duration between two scrapes." env:"PUPPETDB_SCRAPE_INTERVAL" default:"5s"`
ListenAddress string `long:"listen-address" description:"Address to listen on for web interface and telemetry." env:"PUPPETDB_LISTEN_ADDRESS" default:"0.0.0.0:9635"`
MetricPath string `long:"metric-path" description:"Path under which to expose metrics." env:"PUPPETDB_METRIC_PATH" default:"/metrics"`
Verbose bool `long:"verbose" description:"Enable debug mode" env:"PUPPETDB_VERBOSE"`
UnreportedNode string `long:"unreported-node" description:"Tag nodes as unreported if the latest report is older than the defined duration." env:"PUPPETDB_UNREPORTED_NODE" default:"2h"`
Categories string `long:"categories" description:"Report metrics categories to scrape." env:"REPORT_METRICS_CATEGORIES" default:"resources,time,changes,events"`
BasicAuth bool `long:"basic-auth" description:"Enable http basic auth" env:"PUPPETDB_BASIC_AUTH"`
AuthUser string `long:"user" description:"Http basic auth user" env:"PUPPETDB_USER"`
AuthPass string `long:"pass" description:"Http basic auth password" env:"PUPPETDB_PASS"`
}
var (
// VERSION, BUILD_DATE, GIT_COMMIT are filled in by the build script
version = "<<< filled in by build >>>"
buildDate = "<<< filled in by build >>>"
commitSha1 = "<<< filled in by build >>>"
)
func main() {
var c Config
parser := flags.NewParser(&c, flags.Default)
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
log.Printf("PuppetDB Metrics Exporter %s build date: %s sha1: %s Go: %s",
version, buildDate, commitSha1,
runtime.Version(),
)
if c.Verbose {
log.SetLevel(log.DebugLevel)
log.Debugln("Enabling debug output")
} else {
log.SetLevel(log.InfoLevel)
}
if c.Version {
return
}
interval, err := time.ParseDuration(c.ScrapeInterval)
if err != nil {
log.Fatalf("failed to parse scrape interval duration: %s", err)
}
// Create a map[string]struct{} of categories to provide an efficient way to
// find if a category exists in the list of categories.
cats := strings.Split(c.Categories, ",")
categories := make(map[string]struct{}, len(cats))
for _, category := range cats {
categories[category] = struct{}{}
}
authType := "x509"
if c.BasicAuth {
authType = "basic"
}
exp, err := exporter.NewPuppetDBExporter(c.PuppetDBUrl, c.CertFile, c.CACertFile, c.KeyFile, c.SSLSkipVerify, authType, c.AuthUser, c.AuthPass, categories)
if err != nil {
log.Fatalf("failed to initialize exporter: %s", err)
}
go exp.Scrape(interval, c.UnreportedNode, categories)
buildInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "puppetdb_exporter_build_info",
Help: "puppetdb exporter build informations",
}, []string{"version", "commit_sha", "build_date", "golang_version"})
buildInfo.WithLabelValues(version, commitSha1, buildDate, runtime.Version()).Set(1)
prometheus.MustRegister(buildInfo)
http.Handle(c.MetricPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Prometheus PuppetDB Exporter v` + version + `</title></head>
<body>
<h1>Prometheus PuppetDB Exporter ` + version + `</h1>
<p><a href='` + c.MetricPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Infof("Providing metrics at %s%s", c.ListenAddress, c.MetricPath)
log.Fatal(http.ListenAndServe(c.ListenAddress, nil))
}