pax_global_header 0000666 0000000 0000000 00000000064 14133511076 0014513 g ustar 00root root 0000000 0000000 52 comment=580003c91bbfdca0ffab80fb07639d77a28a7f22 prometheus-postfix-exporter-0.3.0/ 0000775 0000000 0000000 00000000000 14133511076 0017246 5 ustar 00root root 0000000 0000000 prometheus-postfix-exporter-0.3.0/.dockerignore 0000664 0000000 0000000 00000000101 14133511076 0021712 0 ustar 00root root 0000000 0000000 # Created by .ignore support plugin (hsz.mobi) .idea/ *.iml .git/ prometheus-postfix-exporter-0.3.0/.gitignore 0000664 0000000 0000000 00000000164 14133511076 0021237 0 ustar 00root root 0000000 0000000 # Editor files *~ .idea/ # Test binary, build with `go test -c` *.test # Binaries postfix_exporter *.iml vendor/ prometheus-postfix-exporter-0.3.0/.travis.yml 0000664 0000000 0000000 00000001401 14133511076 0021353 0 ustar 00root root 0000000 0000000 language: go matrix: include: - go: 1.16.x env: VET=1 GO111MODULE=on - go: 1.16.x env: RACE=1 GO111MODULE=on - go: 1.16.x env: RUN386=1 - go: 1.15.x env: VET=1 GO111MODULE=on - go: 1.15.x env: RACE=1 GO111MODULE=on - go: 1.15.x env: RUN386=1 - go: 1.14.x env: VET=1 GO111MODULE=on - go: 1.14.x env: RACE=1 GO111MODULE=on - go: 1.14.x env: RUN386=1 - go: 1.13.x env: VET=1 GO111MODULE=on - go: 1.13.x env: RACE=1 GO111MODULE=on - go: 1.13.x env: RUN386=1 - go: 1.12.x env: GO111MODULE=on - go: 1.11.x env: GO111MODULE=on - go: stable addons: apt: packages: - libsystemd-dev env: global: GO111MODULE: on prometheus-postfix-exporter-0.3.0/CHANGELOG.md 0000664 0000000 0000000 00000000452 14133511076 0021060 0 ustar 00root root 0000000 0000000 ## 0.1.3 / 2021-05-02 * [BUGFIX] Fix default for mail log path (/var/log/mail.log) ## 0.1.2 / 2018-05-04 * [ENHANCEMENT] Build tag for systemd ## 0.1.1 / 2018-04-19 * [BUGFIX] Non-updating metrics from systemd-journal fix ## 0.1.0 / 2018-02-23 * [ENHANCEMENT] Initial release, add changelog prometheus-postfix-exporter-0.3.0/Dockerfile 0000664 0000000 0000000 00000000754 14133511076 0021246 0 ustar 00root root 0000000 0000000 FROM golang:1.16 AS builder WORKDIR /src # avoid downloading the dependencies on succesive builds RUN apt-get update -qq && apt-get install -qqy \ build-essential \ libsystemd-dev 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 FROM debian:latest EXPOSE 9154 WORKDIR / COPY --from=builder /bin/postfix_exporter /bin/ ENTRYPOINT ["/bin/postfix_exporter"] prometheus-postfix-exporter-0.3.0/LICENSE 0000664 0000000 0000000 00000026135 14133511076 0020262 0 ustar 00root root 0000000 0000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. prometheus-postfix-exporter-0.3.0/README.md 0000664 0000000 0000000 00000006554 14133511076 0020537 0 ustar 00root root 0000000 0000000 # Prometheus Postfix exporter Prometheus metrics exporter for [the Postfix mail server](http://www.postfix.org/). This exporter provides histogram metrics for the size and age of messages stored in the mail queue. It extracts these metrics from Postfix by connecting to a UNIX socket under `/var/spool`. It also counts events by parsing Postfix's log entries, using regular expression matching. The log entries are retrieved from the systemd journal, the Docker logs, or from a log file. ## Options These options can be used when starting the `postfix_exporter` | Flag | Description | Default | |--------------------------|------------------------------------------------------|-----------------------------------| | `--web.listen-address` | Address to listen on for web interface and telemetry | `9154` | | `--web.telemetry-path` | Path under which to expose metrics | `/metrics` | | `--postfix.showq_path` | Path at which Postfix places its showq socket | `/var/spool/postfix/public/showq` | | `--postfix.logfile_path` | Path where Postfix writes log entries | `/var/log/mail.log` | | `--log.unsupported` | Log all unsupported lines | `false` | | `--docker.enable` | Read from the Docker logs instead of a file | `false` | | `--docker.container.id` | The container to read Docker logs from | `postfix` | | `--systemd.enable` | Read from the systemd journal instead of file | `false` | | `--systemd.unit` | Name of the Postfix systemd unit | `postfix.service` | | `--systemd.slice` | Name of the Postfix systemd slice. | `""` | | `--systemd.journal_path` | Path to the systemd journal | `""` | ## Events from Docker Postfix servers running in a [Docker](https://www.docker.com/) container can be monitored using the `--docker.enable` flag. The default container ID is `postfix`, but can be customized with the `--docker.container.id` flag. The default is to connect to the local Docker, but this can be customized using [the `DOCKER_HOST` and similar](https://pkg.go.dev/github.com/docker/docker/client?tab=doc#NewEnvClient) environment variables. ## Events from log file The log file is tailed when processed. Rotating the log files while the exporter is running is OK. The path to the log file is specified with the `--postfix.logfile_path` flag. ## Events from systemd Retrieval from the systemd journal is enabled with the `--systemd.enable` flag. This overrides the log file setting. It is possible to specify the unit (with `--systemd.unit`) or slice (with `--systemd.slice`). Additionally, it is possible to read the journal from a directory with the `--systemd.journal_path` flag. ## Build options Default the exporter is build with systemd journal functionality (but it is disabled at default). Because the systemd headers are required for building with systemd, there is an option to build the exporter without systemd. Use the build tag `nosystemd`. ``` go build -tags nosystemd ``` prometheus-postfix-exporter-0.3.0/build_static.sh 0000775 0000000 0000000 00000000435 14133511076 0022255 0 ustar 00root root 0000000 0000000 #!/bin/sh docker run -i -v `pwd`:/postfix_exporter golang:1.16 /bin/sh << 'EOF' set -ex # Install prerequisites for the build process. apt-get update -q apt-get install -yq libsystemd-dev cd /postfix_exporter go get -d ./... go build -a -tags static_all strip postfix_exporter EOF prometheus-postfix-exporter-0.3.0/go.mod 0000664 0000000 0000000 00000001146 14133511076 0020356 0 ustar 00root root 0000000 0000000 module github.com/kumina/postfix_exporter go 1.16 require ( github.com/Microsoft/go-winio v0.5.0 // indirect github.com/alecthomas/kingpin v2.2.6+incompatible github.com/coreos/go-systemd/v22 v22.0.0 github.com/docker/distribution v2.7.1+incompatible // indirect github.com/docker/docker v1.13.1 github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-units v0.4.0 // indirect github.com/nxadm/tail v1.4.8 github.com/opencontainers/go-digest v1.0.0 // indirect github.com/prometheus/client_golang v1.4.1 github.com/prometheus/client_model v0.2.0 github.com/stretchr/testify v1.4.0 ) prometheus-postfix-exporter-0.3.0/go.sum 0000664 0000000 0000000 00000027476 14133511076 0020421 0 ustar 00root root 0000000 0000000 github.com/Microsoft/go-winio v0.5.0 h1:Elr9Wn+sGKPlkaBvwu4mTrxtmOp3F3yV9qhaHbXGjwU= github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84= 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/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug= github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo= github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= 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/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/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= 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/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.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/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= 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 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU= 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-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/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/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/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= prometheus-postfix-exporter-0.3.0/logsource.go 0000664 0000000 0000000 00000003226 14133511076 0021602 0 ustar 00root root 0000000 0000000 package main import ( "context" "fmt" "io" "github.com/alecthomas/kingpin" ) // A LogSourceFactory provides a repository of log sources that can be // instantiated from command line flags. type LogSourceFactory interface { // Init adds the factory's struct fields as flags in the // application. Init(*kingpin.Application) // New attempts to create a new log source. This is called after // flags have been parsed. Returning `nil, nil`, means the user // didn't want this log source. New(context.Context) (LogSourceCloser, error) } type LogSourceCloser interface { io.Closer LogSource } var logSourceFactories []LogSourceFactory // RegisterLogSourceFactory can be called from module `init` functions // to register factories. func RegisterLogSourceFactory(lsf LogSourceFactory) { logSourceFactories = append(logSourceFactories, lsf) } // InitLogSourceFactories runs Init on all factories. The // initialization order is arbitrary, except `fileLogSourceFactory` is // always last (the fallback). The file log source must be last since // it's enabled by default. func InitLogSourceFactories(app *kingpin.Application) { RegisterLogSourceFactory(&fileLogSourceFactory{}) for _, f := range logSourceFactories { f.Init(app) } } // NewLogSourceFromFactories iterates through the factories and // attempts to instantiate a log source. The first factory to return // success wins. func NewLogSourceFromFactories(ctx context.Context) (LogSourceCloser, error) { for _, f := range logSourceFactories { src, err := f.New(ctx) if err != nil { return nil, err } if src != nil { return src, nil } } return nil, fmt.Errorf("no log source configured") } prometheus-postfix-exporter-0.3.0/logsource_docker.go 0000664 0000000 0000000 00000004653 14133511076 0023136 0 ustar 00root root 0000000 0000000 // +build !nodocker package main import ( "bufio" "context" "io" "log" "strings" "github.com/alecthomas/kingpin" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) // A DockerLogSource reads log records from the given Docker // journal. type DockerLogSource struct { client DockerClient containerID string reader *bufio.Reader } // A DockerClient is the client interface that client.Client // provides. See https://pkg.go.dev/github.com/docker/docker/client type DockerClient interface { io.Closer ContainerLogs(context.Context, string, types.ContainerLogsOptions) (io.ReadCloser, error) } // NewDockerLogSource returns a log source for reading Docker logs. func NewDockerLogSource(ctx context.Context, c DockerClient, containerID string) (*DockerLogSource, error) { r, err := c.ContainerLogs(ctx, containerID, types.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, Follow: true, Tail: "0", }) if err != nil { return nil, err } logSrc := &DockerLogSource{ client: c, containerID: containerID, reader: bufio.NewReader(r), } return logSrc, nil } func (s *DockerLogSource) Close() error { return s.client.Close() } func (s *DockerLogSource) Path() string { return "docker:" + s.containerID } func (s *DockerLogSource) Read(ctx context.Context) (string, error) { line, err := s.reader.ReadString('\n') if err != nil { return "", err } return strings.TrimSpace(line), nil } // A dockerLogSourceFactory is a factory that can create // DockerLogSources from command line flags. type dockerLogSourceFactory struct { enable bool containerID string } func (f *dockerLogSourceFactory) Init(app *kingpin.Application) { app.Flag("docker.enable", "Read from Docker logs. Environment variable DOCKER_HOST can be used to change the address. See https://pkg.go.dev/github.com/docker/docker/client?tab=doc#NewEnvClient for more information.").Default("false").BoolVar(&f.enable) app.Flag("docker.container.id", "ID/name of the Postfix Docker container.").Default("postfix").StringVar(&f.containerID) } func (f *dockerLogSourceFactory) New(ctx context.Context) (LogSourceCloser, error) { if !f.enable { return nil, nil } log.Println("Reading log events from Docker") c, err := client.NewEnvClient() if err != nil { return nil, err } return NewDockerLogSource(ctx, c, f.containerID) } func init() { RegisterLogSourceFactory(&dockerLogSourceFactory{}) } prometheus-postfix-exporter-0.3.0/logsource_docker_test.go 0000664 0000000 0000000 00000003623 14133511076 0024171 0 ustar 00root root 0000000 0000000 // +build !nodocker package main import ( "context" "io" "io/ioutil" "strings" "testing" "github.com/docker/docker/api/types" "github.com/stretchr/testify/assert" ) func TestNewDockerLogSource(t *testing.T) { ctx := context.Background() c := &fakeDockerClient{} src, err := NewDockerLogSource(ctx, c, "acontainer") if err != nil { t.Fatalf("NewDockerLogSource failed: %v", err) } assert.Equal(t, []string{"acontainer"}, c.containerLogsCalls, "A call to ContainerLogs should be made.") if err := src.Close(); err != nil { t.Fatalf("Close failed: %v", err) } assert.Equal(t, 1, c.closeCalls, "A call to Close should be made.") } func TestDockerLogSource_Path(t *testing.T) { ctx := context.Background() c := &fakeDockerClient{} src, err := NewDockerLogSource(ctx, c, "acontainer") if err != nil { t.Fatalf("NewDockerLogSource failed: %v", err) } defer src.Close() assert.Equal(t, "docker:acontainer", src.Path(), "Path should be set by New.") } func TestDockerLogSource_Read(t *testing.T) { ctx := context.Background() c := &fakeDockerClient{ logsReader: ioutil.NopCloser(strings.NewReader("Feb 13 23:31:30 ahost anid[123]: aline\n")), } src, err := NewDockerLogSource(ctx, c, "acontainer") if err != nil { t.Fatalf("NewDockerLogSource failed: %v", err) } defer src.Close() s, err := src.Read(ctx) if err != nil { t.Fatalf("Read failed: %v", err) } assert.Equal(t, "Feb 13 23:31:30 ahost anid[123]: aline", s, "Read should get data from the journal entry.") } type fakeDockerClient struct { logsReader io.ReadCloser containerLogsCalls []string closeCalls int } func (c *fakeDockerClient) ContainerLogs(ctx context.Context, containerID string, opts types.ContainerLogsOptions) (io.ReadCloser, error) { c.containerLogsCalls = append(c.containerLogsCalls, containerID) return c.logsReader, nil } func (c *fakeDockerClient) Close() error { c.closeCalls++ return nil } prometheus-postfix-exporter-0.3.0/logsource_file.go 0000664 0000000 0000000 00000003770 14133511076 0022605 0 ustar 00root root 0000000 0000000 package main import ( "context" "io" "log" "github.com/alecthomas/kingpin" "github.com/nxadm/tail" ) // A FileLogSource can read lines from a file. type FileLogSource struct { tailer *tail.Tail } // NewFileLogSource creates a new log source, tailing the given file. func NewFileLogSource(path string) (*FileLogSource, error) { tailer, err := tail.TailFile(path, tail.Config{ ReOpen: true, // reopen the file if it's rotated MustExist: true, // fail immediately if the file is missing or has incorrect permissions Follow: true, // run in follow mode Location: &tail.SeekInfo{Whence: io.SeekEnd}, // seek to end of file Logger: tail.DiscardingLogger, }) if err != nil { return nil, err } return &FileLogSource{tailer}, nil } func (s *FileLogSource) Close() error { defer s.tailer.Cleanup() go func() { // Stop() waits for the tailer goroutine to shut down, but it // can be blocking on sending on the Lines channel... for range s.tailer.Lines { } }() return s.tailer.Stop() } func (s *FileLogSource) Path() string { return s.tailer.Filename } func (s *FileLogSource) Read(ctx context.Context) (string, error) { select { case line, ok := <-s.tailer.Lines: if !ok { return "", io.EOF } return line.Text, nil case <-ctx.Done(): return "", ctx.Err() } } // A fileLogSourceFactory is a factory than can create log sources // from command line flags. // // Because this factory is enabled by default, it must always be // registered last. type fileLogSourceFactory struct { path string } func (f *fileLogSourceFactory) Init(app *kingpin.Application) { app.Flag("postfix.logfile_path", "Path where Postfix writes log entries.").Default("/var/log/mail.log").StringVar(&f.path) } func (f *fileLogSourceFactory) New(ctx context.Context) (LogSourceCloser, error) { if f.path == "" { return nil, nil } log.Printf("Reading log events from %s", f.path) return NewFileLogSource(f.path) } prometheus-postfix-exporter-0.3.0/logsource_file_test.go 0000664 0000000 0000000 00000003233 14133511076 0023636 0 ustar 00root root 0000000 0000000 package main import ( "context" "fmt" "io/ioutil" "os" "sync" "testing" "time" "github.com/stretchr/testify/assert" ) func TestFileLogSource_Path(t *testing.T) { path, close, err := setupFakeLogFile() if err != nil { t.Fatalf("setupFakeTailer failed: %v", err) } defer close() src, err := NewFileLogSource(path) if err != nil { t.Fatalf("NewFileLogSource failed: %v", err) } defer src.Close() assert.Equal(t, path, src.Path(), "Path should be set by New.") } func TestFileLogSource_Read(t *testing.T) { ctx := context.Background() path, close, err := setupFakeLogFile() if err != nil { t.Fatalf("setupFakeTailer failed: %v", err) } defer close() src, err := NewFileLogSource(path) if err != nil { t.Fatalf("NewFileLogSource failed: %v", err) } defer src.Close() s, err := src.Read(ctx) if err != nil { t.Fatalf("Read failed: %v", err) } assert.Equal(t, "Feb 13 23:31:30 ahost anid[123]: aline", s, "Read should get data from the journal entry.") } func setupFakeLogFile() (string, func(), error) { f, err := ioutil.TempFile("", "filelogsource") if err != nil { return "", nil, err } ctx, cancel := context.WithCancel(context.Background()) var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() defer os.Remove(f.Name()) defer f.Close() for { // The tailer seeks to the end and then does a // follow. Keep writing lines so we know it wakes up and // returns lines. fmt.Fprintln(f, "Feb 13 23:31:30 ahost anid[123]: aline") select { case <-time.After(10 * time.Millisecond): // continue case <-ctx.Done(): return } } }() return f.Name(), func() { cancel() wg.Wait() }, nil } prometheus-postfix-exporter-0.3.0/logsource_systemd.go 0000664 0000000 0000000 00000007207 14133511076 0023355 0 ustar 00root root 0000000 0000000 // +build !nosystemd,linux package main import ( "context" "fmt" "io" "log" "time" "github.com/alecthomas/kingpin" "github.com/coreos/go-systemd/v22/sdjournal" ) // timeNow is a test fake injection point. var timeNow = time.Now // A SystemdLogSource reads log records from the given Systemd // journal. type SystemdLogSource struct { journal SystemdJournal path string } // A SystemdJournal is the journal interface that sdjournal.Journal // provides. See https://pkg.go.dev/github.com/coreos/go-systemd/sdjournal?tab=doc type SystemdJournal interface { io.Closer AddMatch(match string) error GetEntry() (*sdjournal.JournalEntry, error) Next() (uint64, error) SeekRealtimeUsec(usec uint64) error Wait(timeout time.Duration) int } // NewSystemdLogSource returns a log source for reading Systemd // journal entries. `unit` and `slice` provide filtering if non-empty // (with `slice` taking precedence). func NewSystemdLogSource(j SystemdJournal, path, unit, slice string) (*SystemdLogSource, error) { logSrc := &SystemdLogSource{journal: j, path: path} var err error if slice != "" { err = logSrc.journal.AddMatch("_SYSTEMD_SLICE=" + slice) } else if unit != "" { err = logSrc.journal.AddMatch("_SYSTEMD_UNIT=" + unit) } if err != nil { logSrc.journal.Close() return nil, err } // Start at end of journal if err := logSrc.journal.SeekRealtimeUsec(uint64(timeNow().UnixNano() / 1000)); err != nil { logSrc.journal.Close() return nil, err } if r := logSrc.journal.Wait(1 * time.Second); r < 0 { logSrc.journal.Close() return nil, err } return logSrc, nil } func (s *SystemdLogSource) Close() error { return s.journal.Close() } func (s *SystemdLogSource) Path() string { return s.path } func (s *SystemdLogSource) Read(ctx context.Context) (string, error) { c, err := s.journal.Next() if err != nil { return "", err } if c == 0 { return "", io.EOF } e, err := s.journal.GetEntry() if err != nil { return "", err } ts := time.Unix(0, int64(e.RealtimeTimestamp)*int64(time.Microsecond)) return fmt.Sprintf( "%s %s %s[%s]: %s", ts.Format(time.Stamp), e.Fields["_HOSTNAME"], e.Fields["SYSLOG_IDENTIFIER"], e.Fields["_PID"], e.Fields["MESSAGE"], ), nil } // A systemdLogSourceFactory is a factory that can create // SystemdLogSources from command line flags. type systemdLogSourceFactory struct { enable bool unit, slice, path string } func (f *systemdLogSourceFactory) Init(app *kingpin.Application) { app.Flag("systemd.enable", "Read from the systemd journal instead of log").Default("false").BoolVar(&f.enable) app.Flag("systemd.unit", "Name of the Postfix systemd unit.").Default("postfix.service").StringVar(&f.unit) app.Flag("systemd.slice", "Name of the Postfix systemd slice. Overrides the systemd unit.").Default("").StringVar(&f.slice) app.Flag("systemd.journal_path", "Path to the systemd journal").Default("").StringVar(&f.path) } func (f *systemdLogSourceFactory) New(ctx context.Context) (LogSourceCloser, error) { if !f.enable { return nil, nil } log.Println("Reading log events from systemd") j, path, err := newSystemdJournal(f.path) if err != nil { return nil, err } return NewSystemdLogSource(j, path, f.unit, f.slice) } // newSystemdJournal creates a journal handle. It returns the handle // and a string representation of it. If `path` is empty, it connects // to the local journald. func newSystemdJournal(path string) (*sdjournal.Journal, string, error) { if path != "" { j, err := sdjournal.NewJournalFromDir(path) return j, path, err } j, err := sdjournal.NewJournal() return j, "journald", err } func init() { RegisterLogSourceFactory(&systemdLogSourceFactory{}) } prometheus-postfix-exporter-0.3.0/logsource_systemd_test.go 0000664 0000000 0000000 00000007225 14133511076 0024414 0 ustar 00root root 0000000 0000000 // +build !nosystemd,linux package main import ( "context" "io" "os" "testing" "time" "github.com/coreos/go-systemd/v22/sdjournal" "github.com/stretchr/testify/assert" ) func TestNewSystemdLogSource(t *testing.T) { j := &fakeSystemdJournal{} src, err := NewSystemdLogSource(j, "apath", "aunit", "aslice") if err != nil { t.Fatalf("NewSystemdLogSource failed: %v", err) } assert.Equal(t, []string{"_SYSTEMD_SLICE=aslice"}, j.addMatchCalls, "A match should be added for slice.") assert.Equal(t, []uint64{1234567890000000}, j.seekRealtimeUsecCalls, "A call to SeekRealtimeUsec should be made.") assert.Equal(t, []time.Duration{1 * time.Second}, j.waitCalls, "A call to Wait should be made.") if err := src.Close(); err != nil { t.Fatalf("Close failed: %v", err) } assert.Equal(t, 1, j.closeCalls, "A call to Close should be made.") } func TestSystemdLogSource_Path(t *testing.T) { j := &fakeSystemdJournal{} src, err := NewSystemdLogSource(j, "apath", "aunit", "aslice") if err != nil { t.Fatalf("NewSystemdLogSource failed: %v", err) } defer src.Close() assert.Equal(t, "apath", src.Path(), "Path should be set by New.") } func TestSystemdLogSource_Read(t *testing.T) { ctx := context.Background() j := &fakeSystemdJournal{ getEntryValues: []sdjournal.JournalEntry{ { Fields: map[string]string{ "_HOSTNAME": "ahost", "SYSLOG_IDENTIFIER": "anid", "_PID": "123", "MESSAGE": "aline", }, RealtimeTimestamp: 1234567890000000, }, }, nextValues: []uint64{1}, } src, err := NewSystemdLogSource(j, "apath", "aunit", "aslice") if err != nil { t.Fatalf("NewSystemdLogSource failed: %v", err) } defer src.Close() s, err := src.Read(ctx) if err != nil { t.Fatalf("Read failed: %v", err) } assert.Equal(t, "Feb 13 23:31:30 ahost anid[123]: aline", s, "Read should get data from the journal entry.") } func TestSystemdLogSource_ReadEOF(t *testing.T) { ctx := context.Background() j := &fakeSystemdJournal{ nextValues: []uint64{0}, } src, err := NewSystemdLogSource(j, "apath", "aunit", "aslice") if err != nil { t.Fatalf("NewSystemdLogSource failed: %v", err) } defer src.Close() _, err = src.Read(ctx) assert.Equal(t, io.EOF, err, "Should interpret Next 0 as EOF.") } func TestMain(m *testing.M) { // We compare Unix timestamps to date strings, so make it deterministic. os.Setenv("TZ", "UTC") timeNow = func() time.Time { return time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC) } defer func() { timeNow = time.Now }() os.Exit(m.Run()) } type fakeSystemdJournal struct { getEntryValues []sdjournal.JournalEntry getEntryError error nextValues []uint64 nextError error addMatchCalls []string closeCalls int seekRealtimeUsecCalls []uint64 waitCalls []time.Duration } func (j *fakeSystemdJournal) AddMatch(match string) error { j.addMatchCalls = append(j.addMatchCalls, match) return nil } func (j *fakeSystemdJournal) Close() error { j.closeCalls++ return nil } func (j *fakeSystemdJournal) GetEntry() (*sdjournal.JournalEntry, error) { if len(j.getEntryValues) == 0 { return nil, j.getEntryError } e := j.getEntryValues[0] j.getEntryValues = j.getEntryValues[1:] return &e, nil } func (j *fakeSystemdJournal) Next() (uint64, error) { if len(j.nextValues) == 0 { return 0, j.nextError } v := j.nextValues[0] j.nextValues = j.nextValues[1:] return v, nil } func (j *fakeSystemdJournal) SeekRealtimeUsec(usec uint64) error { j.seekRealtimeUsecCalls = append(j.seekRealtimeUsecCalls, usec) return nil } func (j *fakeSystemdJournal) Wait(timeout time.Duration) int { j.waitCalls = append(j.waitCalls, timeout) return 0 } prometheus-postfix-exporter-0.3.0/main.go 0000664 0000000 0000000 00000003512 14133511076 0020522 0 ustar 00root root 0000000 0000000 package main import ( "context" "log" "net/http" "os" "github.com/alecthomas/kingpin" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { var ( ctx = context.Background() 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() logUnsupportedLines = app.Flag("log.unsupported", "Log all unsupported lines.").Bool() ) InitLogSourceFactories(app) kingpin.MustParse(app.Parse(os.Args[1:])) logSrc, err := NewLogSourceFromFactories(ctx) if err != nil { log.Fatalf("Error opening log source: %s", err) } defer logSrc.Close() exporter, err := NewPostfixExporter( *postfixShowqPath, logSrc, *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(`