Skip to content

Commit b734a73

Browse files
committed
expose puppet file locations as commandline arguments
the location of the run report changed with puppet7. the debian package patches puppet to look for the configuration file under /etc/puppet and the report is stored under /var/cache/puppet. other OS packages might use different paths as well. this change exposes the already present file locations to be adjusted via commandline arguments. the default values remain the same. Signed-off-by: Gordon Bleux <33967640+UiP9AV6Y@users.noreply.github.com>
1 parent 4cf299b commit b734a73

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

puppet-agent-exporter.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ 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+
}
40+
3441
func setupLogger() func() {
3542
var logger *zap.Logger
3643
var err error
@@ -78,22 +85,24 @@ var rootTemplate = template.Must(template.New("/").Parse(`<html>
7885
</html>
7986
`))
8087

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

8491
prometheus.DefaultRegisterer.MustRegister(puppetconfig.Collector{
85-
Logger: lgr,
92+
Logger: lgr,
93+
ConfigPath: cfg.puppetConfigFile,
8694
})
8795
prometheus.DefaultRegisterer.MustRegister(puppetreport.Collector{
88-
Logger: lgr,
96+
Logger: lgr,
97+
ReportPath: cfg.puppetReportFile,
8998
})
9099

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

98107
resultCh := make(chan bool)
99108
go func() {
@@ -125,10 +134,16 @@ func run(ctx context.Context, listenAddress, telemetryPath string) (ok bool) {
125134

126135
func main() {
127136
// 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-
)
137+
var cfg config
138+
139+
kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").
140+
Default(":9819").StringVar(&cfg.listenAddress)
141+
kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").
142+
Default("/metrics").StringVar(&cfg.telemetryPath)
143+
kingpin.Flag("puppet.report-file", "Path to the Puppet run report.").
144+
Default("/opt/puppetlabs/puppet/cache/state/last_run_report.yaml").StringVar(&cfg.puppetReportFile)
145+
kingpin.Flag("puppet.config-file", "Path to the Puppet configuration.").
146+
Default("/etc/puppetlabs/puppet/puppet.conf").StringVar(&cfg.puppetConfigFile)
132147
kingpin.HelpFlag.Short('h')
133148
kingpin.Parse()
134149

@@ -138,7 +153,7 @@ func main() {
138153
ctx, onExit := setupInterruptContext()
139154
defer onExit()
140155

141-
if ok := run(ctx, *listenAddress, *telemetryPath); !ok {
156+
if ok := run(ctx, &cfg); !ok {
142157
os.Exit(1)
143158
}
144159
}

0 commit comments

Comments
 (0)