From f51b1f882d9e785172120ea864497b6878a4bfaf Mon Sep 17 00:00:00 2001 From: Bart Vercoulen Date: Mon, 17 Dec 2018 17:01:01 +0100 Subject: [PATCH] Refactored to kingpin and fixed a EOF bug in CollectShowqFromReader. --- nosystemd.go | 9 +++++++-- postfix_exporter.go | 21 +++++++++++---------- systemd.go | 12 ++++++------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/nosystemd.go b/nosystemd.go index cfbffce..33c1a16 100644 --- a/nosystemd.go +++ b/nosystemd.go @@ -3,14 +3,19 @@ package main -import "io" +import( + "io" + + "github.com/alecthomas/kingpin" +) + type Journal struct { io.Closer Path string } -func systemdFlags(enable *bool, unit, slice, path *string) {} +func systemdFlags(enable *bool, unit, slice, path *string, app *kingpin.Application) {} func NewJournal(unit, slice, path string) (*Journal, error) { return nil, nil diff --git a/postfix_exporter.go b/postfix_exporter.go index adcac92..b853209 100644 --- a/postfix_exporter.go +++ b/postfix_exporter.go @@ -17,8 +17,8 @@ import ( "bufio" "bytes" "errors" - "flag" "fmt" + "github.com/alecthomas/kingpin" "io" "log" "net" @@ -78,7 +78,7 @@ type PostfixExporter struct { func CollectShowqFromReader(file io.Reader, ch chan<- prometheus.Metric) error { reader := bufio.NewReader(file) buf, err := reader.Peek(128) - if err != nil { + if err != nil && err != io.EOF { log.Printf("Could not read postfix output, %v", err) } if bytes.IndexByte(buf, 0) >= 0 { @@ -663,16 +663,17 @@ func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) { func main() { var ( - listenAddress = flag.String("web.listen-address", ":9154", "Address to listen on for web interface and telemetry.") - metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") - postfixShowqPath = flag.String("postfix.showq_path", "/var/spool/postfix/public/showq", "Path at which Postfix places its showq socket.") - postfixLogfilePath = flag.String("postfix.logfile_path", "/var/log/postfix_exporter_input.log", "Path where Postfix writes log entries. This file will be truncated by this exporter.") - - systemdEnable bool + app = kingpin.New("postfix_exporter", "Prometheus metrics exporter for postfix") + listenAddress = app.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9154").String() + metricsPath = app.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String() + postfixShowqPath = app.Flag("postfix.showq_path", "Path at which Postfix places its showq socket.").Default("/var/spool/postfix/public/showq").String() + postfixLogfilePath = app.Flag("postfix.logfile_path", "Path where Postfix writes log entries. This file will be truncated by this exporter.").Default("/var/log/postfix_exporter_input.log").String() + systemdEnable bool systemdUnit, systemdSlice, systemdJournalPath string ) - systemdFlags(&systemdEnable, &systemdUnit, &systemdSlice, &systemdJournalPath) - flag.Parse() + systemdFlags(&systemdEnable, &systemdUnit, &systemdSlice, &systemdJournalPath, app) + + kingpin.MustParse(app.Parse(os.Args[1:])) var journal *Journal if systemdEnable { diff --git a/systemd.go b/systemd.go index 9a78ff8..d9d02f7 100644 --- a/systemd.go +++ b/systemd.go @@ -3,12 +3,12 @@ package main import ( - "flag" "fmt" "log" "sync" "time" + "github.com/alecthomas/kingpin" "github.com/coreos/go-systemd/sdjournal" ) @@ -88,11 +88,11 @@ func (j *Journal) NextMessage() (s string, c uint64, err error) { } // systemdFlags sets the flags for use with systemd -func systemdFlags(enable *bool, unit, slice, path *string) { - flag.BoolVar(enable, "systemd.enable", false, "Read from the systemd journal instead of log") - flag.StringVar(unit, "systemd.unit", "postfix.service", "Name of the Postfix systemd unit.") - flag.StringVar(slice, "systemd.slice", "", "Name of the Postfix systemd slice. Overrides the systemd unit.") - flag.StringVar(path, "systemd.journal_path", "", "Path to the systemd journal") +func systemdFlags(enable *bool, unit, slice, path *string, app *kingpin.Application) { + app.Flag("systemd.enable", "Read from the systemd journal instead of log").Default("false").BoolVar(enable) + app.Flag("systemd.unit", "Name of the Postfix systemd unit.").Default("postfix.service").StringVar(unit) + app.Flag("systemd.slice", "Name of the Postfix systemd slice. Overrides the systemd unit.").Default("").StringVar(slice) + app.Flag("systemd.journal_path", "Path to the systemd journal").Default("").StringVar(path) } // CollectLogfileFromJournal Collects entries from the systemd journal.