Skip to content

Commit 0b400af

Browse files
authored
Merge pull request #1 from cernops/feat/config_section
Feat/config section
2 parents 5b97982 + d8899ac commit 0b400af

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

puppet-agent-exporter.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ import (
3131
"golang.org/x/term"
3232
)
3333

34+
type config struct {
35+
listenAddress string
36+
telemetryPath string
37+
puppetReportFile string
38+
puppetConfigFile string
39+
puppetConfigSection string
40+
}
41+
3442
func setupLogger() func() {
3543
var logger *zap.Logger
3644
var err error
@@ -78,22 +86,25 @@ var rootTemplate = template.Must(template.New("/").Parse(`<html>
7886
</html>
7987
`))
8088

81-
func run(ctx context.Context, listenAddress, telemetryPath string) (ok bool) {
89+
func run(ctx context.Context, cfg *config) (ok bool) {
8290
lgr := zap.S()
8391

8492
prometheus.DefaultRegisterer.MustRegister(puppetconfig.Collector{
85-
Logger: lgr,
93+
Logger: lgr,
94+
ConfigPath: cfg.puppetConfigFile,
95+
ConfigSection: cfg.puppetConfigSection,
8696
})
8797
prometheus.DefaultRegisterer.MustRegister(puppetreport.Collector{
88-
Logger: lgr,
98+
Logger: lgr,
99+
ReportPath: cfg.puppetReportFile,
89100
})
90101

91102
mux := http.NewServeMux()
92-
mux.Handle(telemetryPath, promhttp.Handler())
103+
mux.Handle(cfg.telemetryPath, promhttp.Handler())
93104
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
94-
_ = rootTemplate.Execute(writer, telemetryPath)
105+
_ = rootTemplate.Execute(writer, cfg.telemetryPath)
95106
})
96-
server := &http.Server{Addr: listenAddress, Handler: mux}
107+
server := &http.Server{Addr: cfg.listenAddress, Handler: mux}
97108

98109
resultCh := make(chan bool)
99110
go func() {
@@ -125,10 +136,18 @@ func run(ctx context.Context, listenAddress, telemetryPath string) (ok bool) {
125136

126137
func main() {
127138
// Flag definitions copied from github.com/prometheus/node_exporter
128-
var (
129-
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9819").String()
130-
telemetryPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
131-
)
139+
var cfg config
140+
141+
kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").
142+
Default(":9819").StringVar(&cfg.listenAddress)
143+
kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").
144+
Default("/metrics").StringVar(&cfg.telemetryPath)
145+
kingpin.Flag("puppet.report-file", "Path to the Puppet run report.").
146+
Default("/opt/puppetlabs/puppet/cache/state/last_run_report.yaml").StringVar(&cfg.puppetReportFile)
147+
kingpin.Flag("puppet.config-file", "Path to the Puppet configuration.").
148+
Default("/etc/puppetlabs/puppet/puppet.conf").StringVar(&cfg.puppetConfigFile)
149+
kingpin.Flag("puppet.config-section", "Stanza to consider in the Puppet configuration.").
150+
Default("main").StringVar(&cfg.puppetConfigSection)
132151
kingpin.HelpFlag.Short('h')
133152
kingpin.Parse()
134153

@@ -138,7 +157,7 @@ func main() {
138157
ctx, onExit := setupInterruptContext()
139158
defer onExit()
140159

141-
if ok := run(ctx, *listenAddress, *telemetryPath); !ok {
160+
if ok := run(ctx, &cfg); !ok {
142161
os.Exit(1)
143162
}
144163
}

puppetconfig/collector.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ var configDesc = prometheus.NewDesc(
2727
)
2828

2929
type Collector struct {
30-
Logger Logger
31-
ConfigPath string
30+
Logger Logger
31+
ConfigPath string
32+
ConfigSection string
3233
}
3334

3435
func (c Collector) Describe(ch chan<- *prometheus.Desc) {
@@ -41,8 +42,9 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
4142
c.Logger.Errorw("puppet_open_config_failed", "err", err)
4243
return
4344
}
44-
server := config.Section("main").Key("server").String()
45-
environment := config.Section("main").Key("environment").String()
45+
section := c.configSection()
46+
server := config.Section(section).Key("server").String()
47+
environment := config.Section(section).Key("environment").String()
4648
ch <- prometheus.MustNewConstMetric(configDesc, prometheus.GaugeValue, 1, server, environment)
4749
}
4850

@@ -53,6 +55,13 @@ func (c Collector) configPath() string {
5355
return "/etc/puppetlabs/puppet/puppet.conf"
5456
}
5557

58+
func (c Collector) configSection() string {
59+
if c.ConfigSection != "" {
60+
return c.ConfigSection
61+
}
62+
return "main"
63+
}
64+
5665
type Logger interface {
5766
Errorw(msg string, keysAndValues ...interface{})
5867
Panicw(msg string, keysAndValues ...interface{})

0 commit comments

Comments
 (0)