diff --git a/Dockerfile b/Dockerfile index ab916aa..d5ee64a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,24 @@ -# Builder stage to -FROM golang:1.13 as builder +FROM golang:1.13 AS builder +WORKDIR /src -# Add the project in the image -ADD . /build -WORKDIR /build - -# Install needed dependencies for the build -RUN apt-get update -q && apt-get install -qy \ +# avoid downloading the dependencies on succesive builds +RUN apt-get update -qq && apt-get install -qqy \ build-essential \ libsystemd-dev -# Get dependencies and build the static binary +COPY go.mod go.sum ./ +RUN go mod download +RUN go mod verify + +COPY . . + +# Force the go compiler to use modules +ENV GO111MODULE=on RUN go test -RUN go build -a -tags static_all +RUN go build -o /bin/postfix_exporter -# Real image FROM debian:latest - EXPOSE 9154 WORKDIR / - -# Copy the binary from the build image to the real one -COPY --from=builder /build/postfix_exporter . - -ENTRYPOINT ["/postfix_exporter"] +COPY --from=builder /bin/postfix_exporter /bin/ +ENTRYPOINT ["/bin/postfix_exporter"] diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bea5d93 --- /dev/null +++ b/go.sum @@ -0,0 +1,111 @@ +github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvrbUHiqye8wRJMlnYI= +github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/coreos/go-systemd/v22 v22.0.0 h1:XJIw/+VlJ+87J+doOxznsAWIdmWuViOVhkQamW5YV28= +github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.4.1 h1:FFSuS004yOQEtDdTq+TAOLP5xUq63KqAFYyOi8zA+Y8= +github.com/prometheus/client_golang v1.4.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= +github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.9.1 h1:KOMtN28tlbam3/7ZKEYKHhKoJZYYj3gMH4uc62x7X7U= +github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8= +github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU= +golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c= +gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..5e3adb8 --- /dev/null +++ b/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "github.com/prometheus/client_golang/prometheus/promhttp" + "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() + logUnsupportedLines = app.Flag("log.unsupported", "Log all unsupported lines.").Bool() + 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() + log.Println("Reading log events from systemd") + } else { + log.Printf("Reading log events from %v", *postfixLogfilePath) + } + + exporter, err := NewPostfixExporter( + *postfixShowqPath, + *postfixLogfilePath, + journal, + *logUnsupportedLines, + ) + if err != nil { + log.Fatalf("Failed to create PostfixExporter: %s", err) + } + prometheus.MustRegister(exporter) + + http.Handle(*metricsPath, promhttp.Handler()) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + _, err = w.Write([]byte(` + + Postfix Exporter + +

Postfix Exporter

+

Metrics

+ + `)) + if err != nil { + panic(err) + } + }) + ctx, cancelFunc := context.WithCancel(context.Background()) + defer cancelFunc() + go exporter.StartMetricCollection(ctx) + log.Print("Listening on ", *listenAddress) + log.Fatal(http.ListenAndServe(*listenAddress, nil)) +} diff --git a/postfix_exporter.go b/postfix_exporter.go index ef5adf0..18f906c 100644 --- a/postfix_exporter.go +++ b/postfix_exporter.go @@ -19,12 +19,9 @@ import ( "context" "errors" "fmt" - "github.com/alecthomas/kingpin" "io" "log" "net" - "net/http" - "os" "regexp" "strconv" "strings" @@ -32,7 +29,6 @@ import ( "github.com/hpcloud/tail" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" ) var ( @@ -138,37 +134,39 @@ func CollectTextualShowqFromReader(file io.Reader, ch chan<- prometheus.Metric) for scanner.Scan() { matches := messageLine.FindStringSubmatch(scanner.Text()) if matches != nil { - // Derive the name of the message queue. - queue := "other" - if matches[1] == "*" { - queue = "active" - } else if matches[1] == "!" { - queue = "hold" - } - - // Parse the message size. - size, err := strconv.ParseFloat(matches[2], 64) - if err != nil { - return err - } - - // Parse the message date. Unfortunately, the - // output contains no year number. Assume it - // applies to the last year for which the - // message date doesn't exceed time.Now(). - date, err := time.ParseInLocation("Mon Jan 2 15:04:05", - matches[3], location) - if err != nil { - return err - } - date = date.AddDate(now.Year(), 0, 0) - if date.After(now) { - date = date.AddDate(-1, 0, 0) - } - - sizeHistogram.WithLabelValues(queue).Observe(size) - ageHistogram.WithLabelValues(queue).Observe(now.Sub(date).Seconds()) + continue } + + // Derive the name of the message queue. + queue := "other" + if matches[1] == "*" { + queue = "active" + } else if matches[1] == "!" { + queue = "hold" + } + + // Parse the message size. + size, err := strconv.ParseFloat(matches[2], 64) + if err != nil { + return err + } + + // Parse the message date. Unfortunately, the + // output contains no year number. Assume it + // applies to the last year for which the + // message date doesn't exceed time.Now(). + date, err := time.ParseInLocation("Mon Jan 2 15:04:05", + matches[3], location) + if err != nil { + return err + } + date = date.AddDate(now.Year(), 0, 0) + if date.After(now) { + date = date.AddDate(-1, 0, 0) + } + + sizeHistogram.WithLabelValues(queue).Observe(size) + ageHistogram.WithLabelValues(queue).Observe(now.Sub(date).Seconds()) } sizeHistogram.Collect(ch) @@ -260,16 +258,6 @@ func CollectBinaryShowqFromReader(file io.Reader, ch chan<- prometheus.Metric) e return scanner.Err() } -// CollectShowqFromFile collects Postfix queue statistics from a file. -//func CollectShowqFromFile(path string, ch chan<- prometheus.Metric) error { -// fd, err := os.Open(path) -// if err != nil { -// return err -// } -// defer fd.Close() -// return CollectShowqFromReader(fd, ch) -//} - // CollectShowqFromSocket collects Postfix queue statistics from a socket. func CollectShowqFromSocket(path string, ch chan<- prometheus.Metric) error { fd, err := net.Dial("unix", path) @@ -300,194 +288,130 @@ var ( // CollectFromLogline collects metrict from a Postfix log line. func (e *PostfixExporter) CollectFromLogLine(line string) { // Strip off timestamp, hostname, etc. - if logMatches := logLine.FindStringSubmatch(line); logMatches != nil { - process := logMatches[1] - subprocess := logMatches[3] - remainder := logMatches[4] - switch process { - case "postfix": - // Group patterns to check by Postfix service. - if subprocess == "cleanup" { - if strings.Contains(remainder, ": message-id=<") { - e.cleanupProcesses.Inc() - } else if strings.Contains(remainder, ": reject: ") { - e.cleanupRejects.Inc() - } else if strings.Contains(remainder, "message not accepted") { - e.cleanupNotAccepted.Inc() - } else { - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues(subprocess).Inc() - } - } else if subprocess == "lmtp" { - if lmtpMatches := lmtpPipeSMTPLine.FindStringSubmatch(remainder); lmtpMatches != nil { - pdelay, err := strconv.ParseFloat(lmtpMatches[2], 64) - if err != nil { - log.Printf("Couldn't convert LMTP pdelay: %v", err) - } - e.lmtpDelays.WithLabelValues("before_queue_manager").Observe(pdelay) - adelay, err := strconv.ParseFloat(lmtpMatches[3], 64) - if err != nil { - log.Printf("Couldn't convert LMTP adelay: %v", err) - } - e.lmtpDelays.WithLabelValues("queue_manager").Observe(adelay) - sdelay, err := strconv.ParseFloat(lmtpMatches[4], 64) - if err != nil { - log.Printf("Couldn't convert LMTP adelay: %v", err) - } - e.lmtpDelays.WithLabelValues("connection_setup").Observe(sdelay) - xdelay, err := strconv.ParseFloat(lmtpMatches[5], 64) - if err != nil { - log.Printf("Couldn't convert LMTP xdelay: %v", err) - } - e.lmtpDelays.WithLabelValues("transmission").Observe(xdelay) - } else { - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues(subprocess).Inc() - } - } else if subprocess == "pipe" { - if pipeMatches := lmtpPipeSMTPLine.FindStringSubmatch(remainder); pipeMatches != nil { - pdelay, err := strconv.ParseFloat(pipeMatches[2], 64) - if err != nil { - log.Printf("Couldn't convert PIPE pdelay: %v", err) - } - e.pipeDelays.WithLabelValues(pipeMatches[1], "before_queue_manager").Observe(pdelay) - adelay, err := strconv.ParseFloat(pipeMatches[3], 64) - if err != nil { - log.Printf("Couldn't convert PIPE adelay: %v", err) - } - e.pipeDelays.WithLabelValues(pipeMatches[1], "queue_manager").Observe(adelay) - sdelay, err := strconv.ParseFloat(pipeMatches[4], 64) - if err != nil { - log.Printf("Couldn't convert PIPE sdelay: %v", err) - } - e.pipeDelays.WithLabelValues(pipeMatches[1], "connection_setup").Observe(sdelay) - xdelay, err := strconv.ParseFloat(pipeMatches[5], 64) - if err != nil { - log.Printf("Couldn't convert PIPE xdelay: %v", err) - } - e.pipeDelays.WithLabelValues(pipeMatches[1], "transmission").Observe(xdelay) - } else { - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues(subprocess).Inc() - } - } else if subprocess == "qmgr" { - if qmgrInsertMatches := qmgrInsertLine.FindStringSubmatch(remainder); qmgrInsertMatches != nil { - size, err := strconv.ParseFloat(qmgrInsertMatches[1], 64) - if err != nil { - log.Printf("Couldn't convert QMGR size: %v", err) - } - e.qmgrInsertsSize.Observe(size) - nrcpt, err := strconv.ParseFloat(qmgrInsertMatches[2], 64) - if err != nil { - log.Printf("Couldn't convert QMGR nrcpt: %v", err) - } - e.qmgrInsertsNrcpt.Observe(nrcpt) - } else if strings.HasSuffix(remainder, ": removed") { - e.qmgrRemoves.Inc() - } else { - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues(subprocess).Inc() - } - } else if subprocess == "smtp" { - if smtpMatches := lmtpPipeSMTPLine.FindStringSubmatch(remainder); smtpMatches != nil { - pdelay, err := strconv.ParseFloat(smtpMatches[2], 64) - if err != nil { - log.Printf("Couldn't convert SMTP pdelay: %v", err) - } - e.smtpDelays.WithLabelValues("before_queue_manager").Observe(pdelay) - adelay, err := strconv.ParseFloat(smtpMatches[3], 64) - if err != nil { - log.Printf("Couldn't convert SMTP adelay: %v", err) - } - e.smtpDelays.WithLabelValues("queue_manager").Observe(adelay) - sdelay, err := strconv.ParseFloat(smtpMatches[4], 64) - if err != nil { - log.Printf("Couldn't convert SMTP sdelay: %v", err) - } - e.smtpDelays.WithLabelValues("connection_setup").Observe(sdelay) - xdelay, err := strconv.ParseFloat(smtpMatches[5], 64) - if err != nil { - log.Printf("Couldn't convert SMTP xdelay: %v", err) - } - e.smtpDelays.WithLabelValues("transmission").Observe(xdelay) + logMatches := logLine.FindStringSubmatch(line) - if smtpMatches := smtpStatusDeferredLine.FindStringSubmatch(remainder); smtpMatches != nil { - e.smtpStatusDeferred.Inc() - } - } else if smtpTLSMatches := smtpTLSLine.FindStringSubmatch(remainder); smtpTLSMatches != nil { - e.smtpTLSConnects.WithLabelValues(smtpTLSMatches[1:]...).Inc() - } else if smtpMatches := smtpConnectionTimedOut.FindStringSubmatch(remainder); smtpMatches != nil { - e.smtpConnectionTimedOut.Inc() - } else { - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues(subprocess).Inc() - } - } else if subprocess == "smtpd" { - if strings.HasPrefix(remainder, "connect from ") { - e.smtpdConnects.Inc() - } else if strings.HasPrefix(remainder, "disconnect from ") { - e.smtpdDisconnects.Inc() - } else if smtpdFCrDNSErrorsLine.MatchString(remainder) { - e.smtpdFCrDNSErrors.Inc() - } else if smtpdLostConnectionMatches := smtpdLostConnectionLine.FindStringSubmatch(remainder); smtpdLostConnectionMatches != nil { - e.smtpdLostConnections.WithLabelValues(smtpdLostConnectionMatches[1]).Inc() - } else if smtpdProcessesSASLMatches := smtpdProcessesSASLLine.FindStringSubmatch(remainder); smtpdProcessesSASLMatches != nil { - e.smtpdProcesses.WithLabelValues(smtpdProcessesSASLMatches[1]).Inc() - } else if strings.Contains(remainder, ": client=") { - e.smtpdProcesses.WithLabelValues("").Inc() - } else if smtpdRejectsMatches := smtpdRejectsLine.FindStringSubmatch(remainder); smtpdRejectsMatches != nil { - e.smtpdRejects.WithLabelValues(smtpdRejectsMatches[1]).Inc() - } else if smtpdSASLAuthenticationFailuresLine.MatchString(remainder) { - e.smtpdSASLAuthenticationFailures.Inc() - } else if smtpdTLSMatches := smtpdTLSLine.FindStringSubmatch(remainder); smtpdTLSMatches != nil { - e.smtpdTLSConnects.WithLabelValues(smtpdTLSMatches[1:]...).Inc() - } else { - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues(subprocess).Inc() - } + if logMatches == nil { + // Unknown log entry format. + e.addToUnsupportedLine(line, "") + return + } + process := logMatches[1] + remainder := logMatches[4] + switch process { + case "postfix": + // Group patterns to check by Postfix service. + subprocess := logMatches[3] + switch subprocess { + case "cleanup": + if strings.Contains(remainder, ": message-id=<") { + e.cleanupProcesses.Inc() + } else if strings.Contains(remainder, ": reject: ") { + e.cleanupRejects.Inc() } else { - // Unknown Postfix service. - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues(subprocess).Inc() + e.addToUnsupportedLine(line, subprocess) } - case "opendkim": - if opendkimMatches := opendkimSignatureAdded.FindStringSubmatch(remainder); opendkimMatches != nil { - e.opendkimSignatureAdded.WithLabelValues(opendkimMatches[1], opendkimMatches[2]).Inc() + case "lmtp": + if lmtpMatches := lmtpPipeSMTPLine.FindStringSubmatch(remainder); lmtpMatches != nil { + addToHistogramVec(e.lmtpDelays, lmtpMatches[2], "LMTP pdelay", "before_queue_manager") + addToHistogramVec(e.lmtpDelays, lmtpMatches[3], "LMTP adelay", "queue_manager") + addToHistogramVec(e.lmtpDelays, lmtpMatches[4], "LMTP sdelay", "connection_setup") + addToHistogramVec(e.lmtpDelays, lmtpMatches[5], "LMTP xdelay", "transmission") } else { - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) + e.addToUnsupportedLine(line, subprocess) + } + case "pipe": + if pipeMatches := lmtpPipeSMTPLine.FindStringSubmatch(remainder); pipeMatches != nil { + addToHistogramVec(e.pipeDelays, pipeMatches[2], "PIPE pdelay", pipeMatches[1], "before_queue_manager") + addToHistogramVec(e.pipeDelays, pipeMatches[3], "PIPE adelay", pipeMatches[1], "queue_manager") + addToHistogramVec(e.pipeDelays, pipeMatches[4], "PIPE sdelay", pipeMatches[1], "connection_setup") + addToHistogramVec(e.pipeDelays, pipeMatches[5], "PIPE xdelay", pipeMatches[1], "transmission") + } else { + e.addToUnsupportedLine(line, subprocess) + } + case "qmgr": + if qmgrInsertMatches := qmgrInsertLine.FindStringSubmatch(remainder); qmgrInsertMatches != nil { + addToHistogram(e.qmgrInsertsSize, qmgrInsertMatches[1], "QMGR size") + addToHistogram(e.qmgrInsertsNrcpt, qmgrInsertMatches[2], "QMGR nrcpt") + } else if strings.HasSuffix(remainder, ": removed") { + e.qmgrRemoves.Inc() + } else { + e.addToUnsupportedLine(line, subprocess) + } + case "smtp": + if smtpMatches := lmtpPipeSMTPLine.FindStringSubmatch(remainder); smtpMatches != nil { + addToHistogramVec(e.smtpDelays, smtpMatches[2], "before_queue_manager") + addToHistogramVec(e.smtpDelays, smtpMatches[3], "queue_manager") + addToHistogramVec(e.smtpDelays, smtpMatches[4], "connection_setup") + addToHistogramVec(e.smtpDelays, smtpMatches[5], "transmission") + if smtpMatches := smtpStatusDeferredLine.FindStringSubmatch(remainder); smtpMatches != nil { + e.smtpStatusDeferred.Inc() } - e.unsupportedLogEntries.WithLabelValues(process).Inc() + } else if smtpTLSMatches := smtpTLSLine.FindStringSubmatch(remainder); smtpTLSMatches != nil { + e.smtpTLSConnects.WithLabelValues(smtpTLSMatches[1:]...).Inc() + } else if smtpMatches := smtpConnectionTimedOut.FindStringSubmatch(remainder); smtpMatches != nil { + e.smtpConnectionTimedOut.Inc() + } else { + e.addToUnsupportedLine(line, subprocess) + } + case "smtpd": + if strings.HasPrefix(remainder, "connect from ") { + e.smtpdConnects.Inc() + } else if strings.HasPrefix(remainder, "disconnect from ") { + e.smtpdDisconnects.Inc() + } else if smtpdFCrDNSErrorsLine.MatchString(remainder) { + e.smtpdFCrDNSErrors.Inc() + } else if smtpdLostConnectionMatches := smtpdLostConnectionLine.FindStringSubmatch(remainder); smtpdLostConnectionMatches != nil { + e.smtpdLostConnections.WithLabelValues(smtpdLostConnectionMatches[1]).Inc() + } else if smtpdProcessesSASLMatches := smtpdProcessesSASLLine.FindStringSubmatch(remainder); smtpdProcessesSASLMatches != nil { + e.smtpdProcesses.WithLabelValues(smtpdProcessesSASLMatches[1]).Inc() + } else if strings.Contains(logMatches[2], ": client=") { + e.smtpdProcesses.WithLabelValues("").Inc() + } else if smtpdRejectsMatches := smtpdRejectsLine.FindStringSubmatch(remainder); smtpdRejectsMatches != nil { + e.smtpdRejects.WithLabelValues(smtpdRejectsMatches[1]).Inc() + } else if smtpdSASLAuthenticationFailuresLine.MatchString(remainder) { + e.smtpdSASLAuthenticationFailures.Inc() + } else if smtpdTLSMatches := smtpdTLSLine.FindStringSubmatch(remainder); smtpdTLSMatches != nil { + e.smtpdTLSConnects.WithLabelValues(smtpdTLSMatches[1:]...).Inc() + } else { + e.addToUnsupportedLine(line, subprocess) } default: - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues(process).Inc() + e.addToUnsupportedLine(line, subprocess) } - } else { + case "opendkim": + if opendkimMatches := opendkimSignatureAdded.FindStringSubmatch(remainder); opendkimMatches != nil { + e.opendkimSignatureAdded.WithLabelValues(opendkimMatches[1], opendkimMatches[2]).Inc() + } else { + e.addToUnsupportedLine(line, process) + } + default: // Unknown log entry format. - if e.logUnsupportedLines { - log.Printf("Unsupported Line: %v", line) - } - e.unsupportedLogEntries.WithLabelValues("").Inc() + e.addToUnsupportedLine(line, "") } } +func (e *PostfixExporter) addToUnsupportedLine(line string, subprocess string) { + if e.logUnsupportedLines { + log.Printf("Unsupported Line: %v", line) + } + e.unsupportedLogEntries.WithLabelValues(subprocess).Inc() +} + +func addToHistogram(h prometheus.Histogram, value, fieldName string) { + float, err := strconv.ParseFloat(value, 64) + if err != nil { + log.Printf("Couldn't convert value '%s' for %v: %v", value, fieldName, err) + } + h.Observe(float) +} +func addToHistogramVec(h *prometheus.HistogramVec, value, fieldName string, labels ...string) { + float, err := strconv.ParseFloat(value, 64) + if err != nil { + log.Printf("Couldn't convert value '%s' for %v: %v", value, fieldName, err) + } + h.WithLabelValues(labels...).Observe(float) +} + // CollectLogfileFromFile tails a Postfix log file and collects entries from it. func (e *PostfixExporter) CollectLogfileFromFile(ctx context.Context) { gaugeVec := prometheus.NewGaugeVec( @@ -786,63 +710,3 @@ func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) { ch <- e.smtpConnectionTimedOut e.opendkimSignatureAdded.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() - logUnsupportedLines = app.Flag("log.unsupported", "Log all unsupported lines.").Bool() - 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() - log.Println("Reading log events from systemd") - } else { - log.Printf("Reading log events from %v", *postfixLogfilePath) - } - - exporter, err := NewPostfixExporter( - *postfixShowqPath, - *postfixLogfilePath, - journal, - *logUnsupportedLines, - ) - if err != nil { - log.Fatalf("Failed to create PostfixExporter: %s", err) - } - prometheus.MustRegister(exporter) - - http.Handle(*metricsPath, promhttp.Handler()) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - _, err = w.Write([]byte(` - - Postfix Exporter - -

Postfix Exporter

-

Metrics

- - `)) - if err != nil { - panic(err) - } - }) - ctx, cancelFunc := context.WithCancel(context.Background()) - defer cancelFunc() - go exporter.StartMetricCollection(ctx) - log.Print("Listening on ", *listenAddress) - log.Fatal(http.ListenAndServe(*listenAddress, nil)) -}