From 43221afdb0039727598871d16c837bc6d79a09fc Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Thu, 21 Feb 2019 00:03:41 +0100 Subject: [PATCH] Move main function to main.go --- main.go | 63 +++++++++++++++++++++++++++++++++++++++++++++ postfix_exporter.go | 56 ---------------------------------------- 2 files changed, 63 insertions(+), 56 deletions(-) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..27f6737 --- /dev/null +++ b/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "log" + "net/http" + "os" + + "github.com/alecthomas/kingpin" + "github.com/prometheus/client_golang/prometheus" +) + +func main() { + var ( + 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, app) + + kingpin.MustParse(app.Parse(os.Args[1:])) + + var journal *Journal + if systemdEnable { + var err error + journal, err = NewJournal(systemdUnit, systemdSlice, systemdJournalPath) + if err != nil { + log.Fatalf("Error opening systemd journal: %s", err) + } + defer journal.Close() + } + + exporter, err := NewPostfixExporter( + *postfixShowqPath, + *postfixLogfilePath, + journal, + ) + if err != nil { + log.Fatalf("Failed to create PostfixExporter: %s", err) + } + prometheus.MustRegister(exporter) + + http.Handle(*metricsPath, prometheus.Handler()) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + _, err = w.Write([]byte(` + + Postfix Exporter + +

Postfix Exporter

+

Metrics

+ + `)) + if err != nil { + panic(err) + } + }) + + log.Print("Listening on ", *listenAddress) + log.Fatal(http.ListenAndServe(*listenAddress, nil)) +} diff --git a/postfix_exporter.go b/postfix_exporter.go index 5ec0e19..cd22bff 100644 --- a/postfix_exporter.go +++ b/postfix_exporter.go @@ -18,12 +18,9 @@ import ( "bytes" "errors" "fmt" - "github.com/alecthomas/kingpin" "io" "log" "net" - "net/http" - "os" "regexp" "strconv" "strings" @@ -622,56 +619,3 @@ func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) { e.smtpdTLSConnects.Collect(ch) e.unsupportedLogEntries.Collect(ch) } - -func main() { - var ( - 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, app) - - kingpin.MustParse(app.Parse(os.Args[1:])) - - var journal *Journal - if systemdEnable { - var err error - journal, err = NewJournal(systemdUnit, systemdSlice, systemdJournalPath) - if err != nil { - log.Fatalf("Error opening systemd journal: %s", err) - } - defer journal.Close() - } - - exporter, err := NewPostfixExporter( - *postfixShowqPath, - *postfixLogfilePath, - journal, - ) - if err != nil { - log.Fatalf("Failed to create PostfixExporter: %s", err) - } - prometheus.MustRegister(exporter) - - http.Handle(*metricsPath, prometheus.Handler()) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - _, err = w.Write([]byte(` - - Postfix Exporter - -

Postfix Exporter

-

Metrics

- - `)) - if err != nil { - panic(err) - } - }) - - log.Print("Listening on ", *listenAddress) - log.Fatal(http.ListenAndServe(*listenAddress, nil)) -}