docker-mailserver / docker-mailserver

Production-ready fullstack but simple mail server (SMTP, IMAP, LDAP, Antispam, Antivirus, etc.) running inside a container.
https://docker-mailserver.github.io/docker-mailserver/latest/
MIT License
14.72k stars 1.84k forks source link

feature request: Vector for log management #3561

Open polarathene opened 1 year ago

polarathene commented 1 year ago

Context

Vector for easier and better documented configuration, offering more flexible log management than the current rsyslog package supports (syslog focused only):

Another maintainer approving interest in adopting Vector: https://github.com/docker-mailserver/docker-mailserver/pull/3134#issuecomment-1451031308

Description

This has been considered for a while. I just haven't properly documented / discussed an issue for going forward.

It's not likely to be a quick and easy change to implement, tests will likely need to be updated and current logging configuration reviewd/revised. Docs should also be added to better communicate the support to users that want to leverage it.

Alternatives

I think there was some improvements that could be done with rsyslog config, but beyond that there were not many other appealing options within a container that I came across.

Applicable Users

Anyone that needs better control of log management than we currently have. I am particularly interested in it as our current docs are lacking in this area, our log support with DMS is rather limited AFAIK.

IIRC, there was a concern with how supervisord handled logging which @georglauterbach raised upstream but his fix was rejected. I believe this was related to ANSI-based coloured logs and a workaround was implemented at least for docker logs stdout. With Vector the log level can be treated as metadata or streams, similar to what syslog supports, but IIRC syslog was less flexibility on "facilities"/tagging for grouping streams, especially when handling multi-line logs.

Are you going to implement it?

Yes, because I know the probability of someone else doing it is low and I can learn from it.

What are you going to contribute?

If this is ever contributed, it's probably going to be handled by me πŸ˜…

This is a low priority task due to effort required and ROI for users + maintainers vs other tasks in my backlog. I'm raising this FR for the benefit of others that may want to try tackle it if I don't get around to it.

For reference this PR adjusted services to centralize logging to /var/log/mail directory (which we support as a volume to persist for logs).


There are some concerns (as noted by the linked context resources).

Feature is better to delay until Dockerfile sees some more housekeeping and the testsuite is converted to the compose.yaml revision (presently still converting tests to setup.bash helper + refactoring tests)

polarathene commented 1 year ago

Relevant changes to consider (dug up from a local branch experiment back in March 2023). I probably have some notes elsewhere laying around on my disk, if I find anything else that's relevant I'll update it here πŸ‘


Including Vector

While there is no official package in Debian, Vector supports a variety of install methods including their own third-party repo if preferrable.

Since we have packages.sh that may be a good approach. Alternatively, especially for ease of use (by not invalidating the packages.sh layer during implementation), one could copy the binary over like we do with the ClamAV database.

Dockerfile:

# Copy binary over from published DockerHub release:
# - https://hub.docker.com/r/timberio/vector
# - NOTE: Adjust the tag to current release tag (eg `:0.33.0-debian`), otherwise it won't be properly version pinned:
# - Besides `-debian` variant, they also offer `-alpine`, `-distroless-static`, `-distroless-libc`
# - They also now have `0.33.X-variant` and `0.X-variant` which will provide the latest minor or patch release
COPY --link --from=docker.io/timberio/vector:0.X-debian /usr/bin/vector /usr/bin/vector

The image can be deployed as a separate container for side-car usage with the /var/log/mail directory and file source config, but since metadata is lost (especially for /var/log/mail/mail.log), this isn't as helpful. Vector can configure some parsing to infer log level of some entries based on content, same with service and facility. Vector docs for Docker image deployment.


Stashed changes

Rsyslog

This was probably just experimenting with rsyslog config. It was a stashed change with the only context "rsyslog file source" (EDIT: This snippet is roughly what was also shared here).

target/scripts/build/dms.conf:

module(load="imfile" Mode="inotify") #needs to be done just once

# File 1
input(type="imfile"
      File="/var/log/supervisor/postfix.log"
      Tag="tag1"
      Facility="mail")

input(type="imfile"
      File="/var/log/supervisor/postgrey.log"
      Facility="mail")

Amavis

There was also a similar one where it looks like I was experimenting with Amavis log configuration (applied to this DMS config):

$log_level = 4;
# $LOGDIR = "/var/log";
# $logfile = "/dev/stdout";
# $do_syslog = 0;

Test suite adjustment

A change for tests to be more explicit about services being searched via their own log instead of generic /var/log/mail/mail.log output, such as a supervisord stdout log file.

Probably a bit of changes since, but gives a rough overview of approach taken to better focus the scope of log content (helpful for troubleshooting).

Setup

Add these log vars to this setup.bash helper method (_test/test_helper/common.bash is the legacy path as well until all tests are converted over to test/helper/setup.bash_):

# Log files for tests to grep:
export LOGFILE_AMAVIS=/var/log/mail/mail.log
export LOGFILE_POSTFIX=/var/log/supervisor/postfix.log
export LOGFILE_POSTGREY=/var/log/supervisor/postgrey.log

Helpers

This is specifically interested in Postfix logs: /var/log/mail.log => "${LOGFILE_POSTFIX}"

https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/helper/sending.bash#L74

Amavis

Replace all 4 /var/log/mail/mail.log instances with ${LOGFILE_AMAVIS}:

https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/tests/parallel/set3/mta/smtp_delivery.bats#L227-L248

Postfix

This would require the following addition to main.cf (_detailed in orignal FR description above regarding info from this PR_):

# Logging (instead of syslog default, output to stdout for supervisor)
maillog_file = /dev/stdout

Only requires Postfix logs: /var/log/mail/mail.log => "${LOGFILE_POSTFIX}"

virtual_transport alternative support:

https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/tests/parallel/set3/mta/lmtp_ip.bats#L46

All 6 instances here (1st one requires quotes to grep to adjust to double quotes for variable expansion):

https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/tests/parallel/set3/mta/smtp_delivery.bats#L147-L198

And this one:

https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/tests/parallel/set3/mta/smtp_delivery.bats#L221-L225

PERMIT_DOCKER test (also need to adjust quotes):

https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/tests/serial/permit_docker.bats#L73

Postscreen: https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/tests/parallel/set1/spam_virus/postscreen.bats#L40-L61

Postgrey

Log stdout to service specific file (replace these two lines, this is the only service that lacks that):

stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log

Replace these two lines with "${LOGFILE_POSTGREY}"

https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/tests/parallel/set1/spam_virus/postgrey_enabled.bats#L64

https://github.com/docker-mailserver/docker-mailserver/blob/bd96c1161e9f9b32bb0b3aaec4b930980a56945f/test/tests/parallel/set1/spam_virus/postgrey_enabled.bats#L126

georglauterbach commented 1 year ago

I like this and will assist with integration.

polarathene commented 1 year ago

I like this and will assist with integration.

Awesome, no rush as I'm not sure if it's the right time to bring it in.


UPDATE: Review is underway for the syslog sink PR.

polarathene commented 9 months ago

Review feedback that heavily documents logging config in DMS (related to Debian 11 => Debian 12 /etc/rsyslog.conf change):

An earlier comment above has referenced this resource, but it's worth noting here for me. Notably the implicit Amavis syslog debian config.


Aug 2024 update:

Related to the v14 release with rsyslog changes (as per our CHANGELOG.md for v14), as the log format changed I documented some useful information here (notably regarding logger): https://github.com/docker-mailserver/docker-mailserver/pull/4035#pullrequestreview-2079576115

While that notes issues with logger with the two syslog RFC format options, if neither of those are provided the log lines generated seemed compatible with what other services were generating:

$ logger --priority mail.error --id=1337 --tag dms-changedetector "This is an example error message!"

# It would be processed via rsyslog to a log line like this (but with the timestamp prepended):
mail dms-changedetector[1337]: This is an example error message!

That ends up in the filtered /var/log/mail.log (symlink) / /var/log/mail/mail.log (created by rsyslogd, with fallback via touch in start-mailserver.sh) and in this case also /var/log/syslog. I noticed that when the facility was changed from mail to another like cron or uucp that it was still being adjusted to mail instead of keeping the intended facility.. Potentially a misconfiguration with rsyslog?

For reference we don't seem to do much with rsyslog config, but it's manipulated a bit in the Dockerfile:

https://github.com/docker-mailserver/docker-mailserver/blob/2f8ad142eccfe3d6291373629c5b1dbf89b71f52/Dockerfile#L185-L188

https://github.com/docker-mailserver/docker-mailserver/blob/2f8ad142eccfe3d6291373629c5b1dbf89b71f52/Dockerfile#L242-L243

https://github.com/docker-mailserver/docker-mailserver/blob/2f8ad142eccfe3d6291373629c5b1dbf89b71f52/Dockerfile#L248-L249

https://github.com/docker-mailserver/docker-mailserver/blob/2f8ad142eccfe3d6291373629c5b1dbf89b71f52/Dockerfile#L262-L266

Log management and config as a whole should probably be reorganized once this feature is ready to be tackled.