diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b528b7d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +# Created by .ignore support plugin (hsz.mobi) +.idea/ +*.iml +.git/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index f238da4..7dd92f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,12 @@ # Editor files *~ --.idea/ +.idea/ # Test binary, build with `go test -c` *.test # Binaries postfix_exporter + +*.iml +vendor/ diff --git a/Dockerfile b/Dockerfile index b07e198..d5ee64a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,24 @@ -# Builder stage to -FROM golang:1.12 as builder +FROM golang:1.13 AS builder +WORKDIR /src -# Add the project in the image -ADD . /go/src/github.com/kumina/postfix_exporter -WORKDIR /go/src/github.com/kumina/postfix_exporter - -# 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 -RUN go get -d ./... -RUN go build -a -tags static_all +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 -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 /go/src/github.com/kumina/postfix_exporter/postfix_exporter . - -ENTRYPOINT ["/postfix_exporter"] +COPY --from=builder /bin/postfix_exporter /bin/ +ENTRYPOINT ["/bin/postfix_exporter"] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3d5e663 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module github.com/kumina/postfix_exporter + +go 1.13 + +require ( + github.com/alecthomas/kingpin v2.2.6+incompatible + github.com/coreos/go-systemd/v22 v22.0.0 + github.com/fsnotify/fsnotify v1.4.7 // indirect + github.com/hpcloud/tail v1.0.0 + github.com/prometheus/client_golang v1.4.1 + github.com/prometheus/client_model v0.2.0 + github.com/stretchr/testify v1.4.0 + gopkg.in/fsnotify.v1 v1.4.7 // indirect + gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect +) 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/nosystemd.go b/nosystemd.go index 33c1a16..901d270 100644 --- a/nosystemd.go +++ b/nosystemd.go @@ -1,15 +1,14 @@ -// +build nosystemd +// +build nosystemd !linux // This file contains stubs to support non-systemd use package main -import( +import ( "io" "github.com/alecthomas/kingpin" ) - type Journal struct { io.Closer Path string diff --git a/postfix_exporter.go b/postfix_exporter.go index ff5357b..18f906c 100644 --- a/postfix_exporter.go +++ b/postfix_exporter.go @@ -16,14 +16,12 @@ package main import ( "bufio" "bytes" + "context" "errors" "fmt" - "github.com/alecthomas/kingpin" "io" "log" "net" - "net/http" - "os" "regexp" "strconv" "strings" @@ -31,7 +29,6 @@ import ( "github.com/hpcloud/tail" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" ) var ( @@ -44,9 +41,10 @@ var ( // PostfixExporter holds the state that should be preserved by the // Postfix Prometheus metrics exporter across scrapes. type PostfixExporter struct { - showqPath string - journal *Journal - tailer *tail.Tail + showqPath string + journal *Journal + tailer *tail.Tail + logUnsupportedLines bool // Metrics that should persist after refreshes, based on logs. cleanupProcesses prometheus.Counter @@ -59,6 +57,7 @@ type PostfixExporter struct { qmgrRemoves prometheus.Counter smtpDelays *prometheus.HistogramVec smtpTLSConnects *prometheus.CounterVec + smtpConnectionTimedOut prometheus.Counter smtpDeferreds prometheus.Counter smtpdConnects prometheus.Counter smtpdDisconnects prometheus.Counter @@ -69,6 +68,8 @@ type PostfixExporter struct { smtpdSASLAuthenticationFailures prometheus.Counter smtpdTLSConnects *prometheus.CounterVec unsupportedLogEntries *prometheus.CounterVec + smtpStatusDeferred prometheus.Counter + opendkimSignatureAdded *prometheus.CounterVec } // CollectShowqFromReader parses the output of Postfix's 'showq' command @@ -133,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) @@ -255,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) @@ -277,178 +270,173 @@ func CollectShowqFromSocket(path string, ch chan<- prometheus.Metric) error { // Patterns for parsing log messages. var ( - logLine = regexp.MustCompile(` ?postfix/(\w+)\[\d+\]: (.*)`) + logLine = regexp.MustCompile(` ?(postfix|opendkim)(/(\w+))?\[\d+\]: (.*)`) lmtpPipeSMTPLine = regexp.MustCompile(`, relay=(\S+), .*, delays=([0-9\.]+)/([0-9\.]+)/([0-9\.]+)/([0-9\.]+), `) qmgrInsertLine = regexp.MustCompile(`:.*, size=(\d+), nrcpt=(\d+) `) + smtpStatusDeferredLine = regexp.MustCompile(`, status=deferred`) smtpTLSLine = regexp.MustCompile(`^(\S+) TLS connection established to \S+: (\S+) with cipher (\S+) \((\d+)/(\d+) bits\)$`) - smtpDeferredsLine = regexp.MustCompile(`status=deferred`) + smtpConnectionTimedOut = regexp.MustCompile(`^connect\s+to\s+(.*)\[(.*)\]:(\d+):\s+(Connection timed out)$`) smtpdFCrDNSErrorsLine = regexp.MustCompile(`^warning: hostname \S+ does not resolve to address `) smtpdProcessesSASLLine = regexp.MustCompile(`: client=.*, sasl_username=(\S+)`) smtpdRejectsLine = regexp.MustCompile(`^NOQUEUE: reject: RCPT from \S+: ([0-9]+) `) smtpdLostConnectionLine = regexp.MustCompile(`^lost connection after (\w+) from `) smtpdSASLAuthenticationFailuresLine = regexp.MustCompile(`^warning: \S+: SASL \S+ authentication failed: `) smtpdTLSLine = regexp.MustCompile(`^(\S+) TLS connection established from \S+: (\S+) with cipher (\S+) \((\d+)/(\d+) bits\)$`) + opendkimSignatureAdded = regexp.MustCompile(`^[\w\d]+: DKIM-Signature field added \(s=(\w+), d=(.*)\)$`) ) // CollectFromLogline collects metrict from a Postfix log line. -func (e *PostfixExporter) CollectFromLogline(line string) { +func (e *PostfixExporter) CollectFromLogLine(line string) { // Strip off timestamp, hostname, etc. - if logMatches := logLine.FindStringSubmatch(line); logMatches != nil { + logMatches := logLine.FindStringSubmatch(line) + + 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. - if logMatches[1] == "cleanup" { - if strings.Contains(logMatches[2], ": message-id=<") { + subprocess := logMatches[3] + switch subprocess { + case "cleanup": + if strings.Contains(remainder, ": message-id=<") { e.cleanupProcesses.Inc() - } else if strings.Contains(logMatches[2], ": reject: ") { + } else if strings.Contains(remainder, ": reject: ") { e.cleanupRejects.Inc() - } else if strings.Contains(logMatches[2], "message not accepted") { - e.cleanupNotAccepted.Inc() } else { - e.unsupportedLogEntries.WithLabelValues(logMatches[1]).Inc() + e.addToUnsupportedLine(line, subprocess) } - } else if logMatches[1] == "lmtp" { - if lmtpMatches := lmtpPipeSMTPLine.FindStringSubmatch(logMatches[2]); 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) + 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 { - e.unsupportedLogEntries.WithLabelValues(logMatches[1]).Inc() + e.addToUnsupportedLine(line, subprocess) } - } else if logMatches[1] == "pipe" { - if pipeMatches := lmtpPipeSMTPLine.FindStringSubmatch(logMatches[2]); 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) + 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.unsupportedLogEntries.WithLabelValues(logMatches[1]).Inc() + e.addToUnsupportedLine(line, subprocess) } - } else if logMatches[1] == "qmgr" { - if qmgrInsertMatches := qmgrInsertLine.FindStringSubmatch(logMatches[2]); 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(logMatches[2], ": removed") { + 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.unsupportedLogEntries.WithLabelValues(logMatches[1]).Inc() + e.addToUnsupportedLine(line, subprocess) } - } else if logMatches[1] == "smtp" { - if smtpMatches := lmtpPipeSMTPLine.FindStringSubmatch(logMatches[2]); smtpMatches != nil { - pdelay, err := strconv.ParseFloat(smtpMatches[2], 64) - if err != nil { - log.Printf("Couldn't convert SMTP pdelay: %v", err) + 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.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) - if smtpDeferredsMatches := smtpDeferredsLine.FindStringSubmatch( - logMatches[2]); smtpDeferredsMatches != nil { - e.smtpDeferreds.Inc() - } - } else if smtpTLSMatches := smtpTLSLine.FindStringSubmatch(logMatches[2]); smtpTLSMatches != nil { + } 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.unsupportedLogEntries.WithLabelValues(logMatches[1]).Inc() + e.addToUnsupportedLine(line, subprocess) } - } else if logMatches[1] == "smtpd" { - if strings.HasPrefix(logMatches[2], "connect from ") { + case "smtpd": + if strings.HasPrefix(remainder, "connect from ") { e.smtpdConnects.Inc() - } else if strings.HasPrefix(logMatches[2], "disconnect from ") { + } else if strings.HasPrefix(remainder, "disconnect from ") { e.smtpdDisconnects.Inc() - } else if smtpdFCrDNSErrorsLine.MatchString(logMatches[2]) { + } else if smtpdFCrDNSErrorsLine.MatchString(remainder) { e.smtpdFCrDNSErrors.Inc() - } else if smtpdLostConnectionMatches := smtpdLostConnectionLine.FindStringSubmatch(logMatches[2]); smtpdLostConnectionMatches != nil { + } else if smtpdLostConnectionMatches := smtpdLostConnectionLine.FindStringSubmatch(remainder); smtpdLostConnectionMatches != nil { e.smtpdLostConnections.WithLabelValues(smtpdLostConnectionMatches[1]).Inc() - } else if smtpdProcessesSASLMatches := smtpdProcessesSASLLine.FindStringSubmatch(logMatches[2]); smtpdProcessesSASLMatches != nil { + } 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(logMatches[2]); smtpdRejectsMatches != nil { + } else if smtpdRejectsMatches := smtpdRejectsLine.FindStringSubmatch(remainder); smtpdRejectsMatches != nil { e.smtpdRejects.WithLabelValues(smtpdRejectsMatches[1]).Inc() - } else if smtpdSASLAuthenticationFailuresLine.MatchString(logMatches[2]) { + } else if smtpdSASLAuthenticationFailuresLine.MatchString(remainder) { e.smtpdSASLAuthenticationFailures.Inc() - } else if smtpdTLSMatches := smtpdTLSLine.FindStringSubmatch(logMatches[2]); smtpdTLSMatches != nil { + } else if smtpdTLSMatches := smtpdTLSLine.FindStringSubmatch(remainder); smtpdTLSMatches != nil { e.smtpdTLSConnects.WithLabelValues(smtpdTLSMatches[1:]...).Inc() } else { - e.unsupportedLogEntries.WithLabelValues(logMatches[1]).Inc() + e.addToUnsupportedLine(line, subprocess) } - } else { - // Unknown Postfix service. - e.unsupportedLogEntries.WithLabelValues(logMatches[1]).Inc() + default: + 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. - 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() error { +func (e *PostfixExporter) CollectLogfileFromFile(ctx context.Context) { + gaugeVec := prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "postfix", + Subsystem: "", + Name: "up", + Help: "Whether scraping Postfix's metrics was successful.", + }, + []string{"path"}) + gauge := gaugeVec.WithLabelValues(e.tailer.Filename) for { select { case line := <-e.tailer.Lines: - e.CollectFromLogline(line.Text) - default: - return nil + e.CollectFromLogLine(line.Text) + case <-ctx.Done(): + gauge.Set(0) + return } + gauge.Set(1) } } // NewPostfixExporter creates a new Postfix exporter instance. -func NewPostfixExporter(showqPath string, logfilePath string, journal *Journal) (*PostfixExporter, error) { +func NewPostfixExporter(showqPath string, logfilePath string, journal *Journal, logUnsupportedLines bool) (*PostfixExporter, error) { var tailer *tail.Tail if logfilePath != "" { var err error @@ -462,9 +450,10 @@ func NewPostfixExporter(showqPath string, logfilePath string, journal *Journal) } } return &PostfixExporter{ - showqPath: showqPath, - tailer: tailer, - journal: journal, + logUnsupportedLines: logUnsupportedLines, + showqPath: showqPath, + tailer: tailer, + journal: journal, cleanupProcesses: prometheus.NewCounter(prometheus.CounterOpts{ Namespace: "postfix", @@ -534,6 +523,11 @@ func NewPostfixExporter(showqPath string, logfilePath string, journal *Journal) Name: "smtp_deferred_messages_total", Help: "Total number of messages that have been deferred on SMTP.", }), + smtpConnectionTimedOut: prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: "postfix", + Name: "smtp_connection_timed_out_total", + Help: "Total number of messages that have been deferred on SMTP.", + }), smtpdConnects: prometheus.NewCounter(prometheus.CounterOpts{ Namespace: "postfix", Name: "smtpd_connects_total", @@ -589,6 +583,19 @@ func NewPostfixExporter(showqPath string, logfilePath string, journal *Journal) Help: "Log entries that could not be processed.", }, []string{"service"}), + smtpStatusDeferred: prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: "postfix", + Name: "smtp_status_deferred", + Help: "Total number of messages deferred.", + }), + opendkimSignatureAdded: prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: "opendkim", + Name: "signatures_added_total", + Help: "Total number of messages signed.", + }, + []string{"subject", "domain"}, + ), }, nil } @@ -615,9 +622,52 @@ func (e *PostfixExporter) Describe(ch chan<- *prometheus.Desc) { e.smtpdRejects.Describe(ch) ch <- e.smtpdSASLAuthenticationFailures.Desc() e.smtpdTLSConnects.Describe(ch) + ch <- e.smtpStatusDeferred.Desc() e.unsupportedLogEntries.Describe(ch) } +func (e *PostfixExporter) foreverCollectFromJournal(ctx context.Context) { + gauge := prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "postfix", + Subsystem: "", + Name: "up", + Help: "Whether scraping Postfix's metrics was successful.", + }, + []string{"path"}).WithLabelValues(e.journal.Path) + select { + case <-ctx.Done(): + gauge.Set(0) + return + default: + err := e.CollectLogfileFromJournal() + if err != nil { + log.Printf("Couldn't read journal: %v", err) + gauge.Set(0) + } else { + gauge.Set(1) + } + } +} + +func (e *PostfixExporter) StartMetricCollection(ctx context.Context) { + if e.journal != nil { + e.foreverCollectFromJournal(ctx) + } else { + e.CollectLogfileFromFile(ctx) + } + + prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "postfix", + Subsystem: "", + Name: "up", + Help: "Whether scraping Postfix's metrics was successful.", + }, + []string{"path"}) + return +} + // Collect metrics from Postfix's showq socket and its log file. func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) { err := CollectShowqFromSocket(e.showqPath, ch) @@ -636,29 +686,6 @@ func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) { e.showqPath) } - var src string - if e.journal != nil { - err = e.CollectLogfileFromJournal() - src = e.journal.Path - } else { - err = e.CollectLogfileFromFile() - src = e.tailer.Filename - } - if err == nil { - ch <- prometheus.MustNewConstMetric( - postfixUpDesc, - prometheus.GaugeValue, - 1.0, - src) - } else { - log.Printf("Failed to scrape log: %s", err) - ch <- prometheus.MustNewConstMetric( - postfixUpDesc, - prometheus.GaugeValue, - 0.0, - src) - } - ch <- e.cleanupProcesses ch <- e.cleanupRejects ch <- e.cleanupNotAccepted @@ -678,58 +705,8 @@ func (e *PostfixExporter) Collect(ch chan<- prometheus.Metric) { e.smtpdRejects.Collect(ch) ch <- e.smtpdSASLAuthenticationFailures e.smtpdTLSConnects.Collect(ch) + ch <- e.smtpStatusDeferred 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, 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) - } - }) - - log.Print("Listening on ", *listenAddress) - log.Fatal(http.ListenAndServe(*listenAddress, nil)) + ch <- e.smtpConnectionTimedOut + e.opendkimSignatureAdded.Collect(ch) } diff --git a/postfix_exporter_test.go b/postfix_exporter_test.go new file mode 100644 index 0000000..59b9a6f --- /dev/null +++ b/postfix_exporter_test.go @@ -0,0 +1,161 @@ +package main + +import ( + "github.com/hpcloud/tail" + "github.com/prometheus/client_golang/prometheus" + io_prometheus_client "github.com/prometheus/client_model/go" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestPostfixExporter_CollectFromLogline(t *testing.T) { + type fields struct { + showqPath string + journal *Journal + tailer *tail.Tail + cleanupProcesses prometheus.Counter + cleanupRejects prometheus.Counter + cleanupNotAccepted prometheus.Counter + lmtpDelays *prometheus.HistogramVec + pipeDelays *prometheus.HistogramVec + qmgrInsertsNrcpt prometheus.Histogram + qmgrInsertsSize prometheus.Histogram + qmgrRemoves prometheus.Counter + smtpDelays *prometheus.HistogramVec + smtpTLSConnects *prometheus.CounterVec + smtpDeferreds prometheus.Counter + smtpdConnects prometheus.Counter + smtpdDisconnects prometheus.Counter + smtpdFCrDNSErrors prometheus.Counter + smtpdLostConnections *prometheus.CounterVec + smtpdProcesses *prometheus.CounterVec + smtpdRejects *prometheus.CounterVec + smtpdSASLAuthenticationFailures prometheus.Counter + smtpdTLSConnects *prometheus.CounterVec + unsupportedLogEntries *prometheus.CounterVec + } + type args struct { + line []string + count int + } + tests := []struct { + name string + fields fields + args args + }{ + { + name: "Single line", + args: args{ + line: []string{ + "Feb 11 16:49:24 letterman postfix/qmgr[8204]: AAB4D259B1: removed", + }, + count: 1, + }, + }, + { + name: "Multiple lines", + args: args{ + line: []string{ + "Feb 11 16:49:24 letterman postfix/qmgr[8204]: AAB4D259B1: removed", + "Feb 11 16:49:24 letterman postfix/qmgr[8204]: C2032259E6: removed", + "Feb 11 16:49:24 letterman postfix/qmgr[8204]: B83C4257DC: removed", + "Feb 11 16:49:24 letterman postfix/qmgr[8204]: 721BE256EA: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: CA94A259EB: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: AC1E3259E1: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: D114D221E3: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: A55F82104D: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: D6DAA259BC: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: E3908259F0: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: 0CBB8259BF: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: EA3AD259F2: removed", + "Feb 11 16:49:25 letterman postfix/qmgr[8204]: DDEF824B48: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 289AF21DB9: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 6192B260E8: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: F2831259F4: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 09D60259F8: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 13A19259FA: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 2D42722065: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 746E325A0E: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 4D2F125A02: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: E30BC259EF: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: DC88924DA1: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 2164B259FD: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 8C30525A14: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: 8DCCE25A15: removed", + "Feb 11 16:49:26 letterman postfix/qmgr[8204]: C5217255D5: removed", + "Feb 11 16:49:27 letterman postfix/qmgr[8204]: D8EE625A28: removed", + "Feb 11 16:49:27 letterman postfix/qmgr[8204]: 9AD7C25A19: removed", + "Feb 11 16:49:27 letterman postfix/qmgr[8204]: D0EEE2596C: removed", + "Feb 11 16:49:27 letterman postfix/qmgr[8204]: DFE732172E: removed", + }, + count: 31, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := &PostfixExporter{ + showqPath: tt.fields.showqPath, + journal: tt.fields.journal, + tailer: tt.fields.tailer, + cleanupProcesses: tt.fields.cleanupProcesses, + cleanupRejects: tt.fields.cleanupRejects, + cleanupNotAccepted: tt.fields.cleanupNotAccepted, + lmtpDelays: tt.fields.lmtpDelays, + pipeDelays: tt.fields.pipeDelays, + qmgrInsertsNrcpt: tt.fields.qmgrInsertsNrcpt, + qmgrInsertsSize: tt.fields.qmgrInsertsSize, + qmgrRemoves: tt.fields.qmgrRemoves, + smtpDelays: tt.fields.smtpDelays, + smtpTLSConnects: tt.fields.smtpTLSConnects, + smtpDeferreds: tt.fields.smtpDeferreds, + smtpdConnects: tt.fields.smtpdConnects, + smtpdDisconnects: tt.fields.smtpdDisconnects, + smtpdFCrDNSErrors: tt.fields.smtpdFCrDNSErrors, + smtpdLostConnections: tt.fields.smtpdLostConnections, + smtpdProcesses: tt.fields.smtpdProcesses, + smtpdRejects: tt.fields.smtpdRejects, + smtpdSASLAuthenticationFailures: tt.fields.smtpdSASLAuthenticationFailures, + smtpdTLSConnects: tt.fields.smtpdTLSConnects, + unsupportedLogEntries: tt.fields.unsupportedLogEntries, + } + counter := testCounter{} + e.qmgrRemoves = &counter + for _, line := range tt.args.line { + e.CollectFromLogLine(line) + } + assert.Equal(t, tt.args.count, counter.Count(), "Wrong number of lines counted") + if counter.Count() != tt.args.count { + t.Fatal("Counter wrong: ") + } + }) + } +} + +type testCounter struct { + count int +} + +func (t *testCounter) setCount(count int) { + t.count = count +} + +func (t *testCounter) Count() int { + return t.count +} + +func (t *testCounter) Add(add float64) { +} +func (t *testCounter) Collect(c chan<- prometheus.Metric) { +} +func (t *testCounter) Describe(c chan<- *prometheus.Desc) { +} +func (t *testCounter) Desc() *prometheus.Desc { + return nil +} +func (t *testCounter) Inc() { + t.count++ +} +func (t *testCounter) Write(x *io_prometheus_client.Metric) error { + return nil +} diff --git a/systemd.go b/systemd.go index d9d02f7..e459c8b 100644 --- a/systemd.go +++ b/systemd.go @@ -1,4 +1,4 @@ -// +build !nosystemd +// +build !nosystemd,linux package main @@ -9,7 +9,7 @@ import ( "time" "github.com/alecthomas/kingpin" - "github.com/coreos/go-systemd/sdjournal" + "github.com/coreos/go-systemd/v22/sdjournal" ) // Journal represents a lockable systemd journal. @@ -112,7 +112,7 @@ func (e *PostfixExporter) CollectLogfileFromJournal() error { if c == 0 { break } - e.CollectFromLogline(m) + e.CollectFromLogLine(m) } return nil