Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup mtail #388

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion cmdeploy/src/cmdeploy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pathlib import Path

from chatmaild.config import Config, read_config
from pyinfra import host
from pyinfra import host, facts
from pyinfra.facts.files import File
from pyinfra.facts.systemd import SystemdEnabled
from pyinfra.operations import apt, files, pip, server, systemd
Expand Down Expand Up @@ -441,6 +441,68 @@ def check_config(config):
return config


def deploy_mtail(config):
if host.get_fact(facts.server.Arch) == "x86_64":
files.download(
"https://github.com/google/mtail/releases/download/v3.0.7/mtail_3.0.7_linux_amd64.tar.gz",
"/tmp/mtail_3.0.7_linux_amd64.tar.gz",
user="root",
group="root",
sha256sum="624c31a563acd76440e316bba6cdca604f5ec4106a0b25f2b185ab2b1dbb7f4a",
)
server.shell(
name="Setup mtail",
commands=[
"tar -xf /tmp/mtail_3.0.7_linux_amd64.tar.gz mtail --to-stdout >/usr/local/bin/mtail",
"chmod +x /usr/local/bin/mtail",
],
)
elif host.get_fact(facts.server.Arch) == "aarch64":
files.download(
"https://github.com/google/mtail/releases/download/v3.0.7/mtail_3.0.7_linux_arm64.tar.gz",
"/tmp/mtail_3.0.7_linux_arm64.tar.gz",
user="root",
group="root",
sha256sum="0a1da15d41ac04e4f51385f4a00c819dd1f551826341560a9082dd7fd4efab79",
)
server.shell(
name="Setup mtail",
commands=[
"tar -xf /tmp/mtail_3.0.7_linux_arm64.tar.gz mtail --to-stdout >/usr/local/bin/mtail",
"chmod +x /usr/local/bin/mtail",
],
)

files.template(
src=importlib.resources.files(__package__).joinpath("mtail/mtail.service.j2"),
dest="/etc/systemd/system/mtail.service",
user="root",
group="root",
mode="644",
address="127.0.0.1", # TODO read from config
port=3903,
)

mtail_conf = files.put(
name="Mtail configuration",
src=importlib.resources.files(__package__).joinpath(
"mtail/delivered_mail.mtail"
),
dest="/etc/mtail/delivered_mail.mtail",
user="root",
group="root",
mode="644",
)

systemd.service(
name="Start and enable mtail",
service="mtail.service",
running=True,
enabled=True,
restarted=mtail_conf.changed,
)


def deploy_chatmail(config_path: Path) -> None:
"""Deploy a chat-mail instance.

Expand Down Expand Up @@ -635,3 +697,5 @@ def deploy_chatmail(config_path: Path) -> None:
name="Ensure cron is installed",
packages=["cron"],
)

deploy_mtail(config)
44 changes: 44 additions & 0 deletions cmdeploy/src/cmdeploy/mtail/delivered_mail.mtail
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
counter delivered_mail
/saved mail to INBOX$/ {
delivered_mail++
}

counter quota_exceeded
/Quota exceeded \(mailbox for user is full\)$/ {
quota_exceeded++
}

# Essentially the number of outgoing messages.
counter dkim_signed
/DKIM-Signature field added/ {
dkim_signed++
}

counter created_accounts
counter created_ci_accounts
counter created_nonci_accounts

/: Created address: (?P<addr>.*)$/ {
created_accounts++

$addr =~ /ci-/ {
created_ci_accounts++
} else {
created_nonci_accounts++
}
}

counter postfix_timeouts
/timeout after DATA/ {
postfix_timeouts++
}

counter postfix_noqueue
/postfix\/.*NOQUEUE/ {
postfix_noqueue++
}

counter warning_count
/warning/ {
warning_count++
}
Loading