misc: add logtracker.hourly timer

This commit is contained in:
Christophe Siraut 2020-09-02 08:47:32 +02:00
parent 9c55841291
commit 63f80851ac
8 changed files with 56 additions and 29 deletions

View File

@ -1,3 +0,0 @@
#!/bin/sh
sudo -u logtracker logtracker purge

View File

@ -0,0 +1,19 @@
[Unit]
Description=logtracker hourly tasks
After=network.target logtracker.service
[Service]
Type=oneshot
Environment=LANG=C.UTF-8
User=logtracker
Group=logtracker
ExecStart=/usr/bin/logtracker-manage collect_mail
ExecStart=/usr/bin/logtracker-manage clean_mail
ExecStart=/usr/bin/logtracker-manage clean_journal
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGQUIT
ProtectSystem=strict
ProtectHome=yes
ProtectDevices=yes
NoNewPrivileges=yes
PrivateTmp=yes

View File

@ -0,0 +1,8 @@
[Unit]
Description=Run logtracker tasks
[Timer]
OnCalendar=hourly
[Install]
WantedBy=timers.target

4
debian/rules vendored
View File

@ -8,3 +8,7 @@ export PYBUILD_INSTALL_ARGS_python3=--install-scripts=/usr/lib/logtracker/
%:
dh $@ --with python3 --buildsystem=pybuild
override_dh_installsystemd:
dh_installsystemd
dh_installsystemd -p logtracker --name=logtracker-hourly

View File

@ -11,6 +11,6 @@ class Command(BaseCommand):
def handle(self, *args, **options):
with connection.cursor() as cursor:
cursor.execute(
"DELETE FROM journal_entry WHERE id not in (SELECT id FROM journal_entry ORDER BY id desc LIMIT %s)",
"DELETE FROM journal_entry WHERE id not in (SELECT id FROM journal_entry ORDER BY timestamp desc LIMIT %s)",
[options['keep']],
)

View File

@ -0,0 +1,16 @@
from django.core.management.base import BaseCommand, CommandError
from django.db import connection
class Command(BaseCommand):
help = "Remove old entries"
def add_arguments(self, parser):
parser.add_argument("--keep", type=int, default="100000")
def handle(self, *args, **options):
with connection.cursor() as cursor:
cursor.execute(
"DELETE FROM mail_mail WHERE id not in (SELECT id FROM journal_entry ORDER BY timestamp desc LIMIT %s)",
[options['keep']],
)

View File

@ -1,25 +0,0 @@
from django.core.management.base import BaseCommand, CommandError
from logtracker.data.models import Entry, Mail, Sender
from logtracker.collectors import parse
from logtracker.utils import lock
class Command(BaseCommand):
help = 'Collect entries from exim4 log'
def add_arguments(self, parser):
parser.add_argument('-l', '--logfile', default='/var/log/exim4/mainlog')
parser.add_argument('--lockfile', default='/var/lock/mainlog.lock')
def handle(self, logfile, *args, **options):
with lock(options.get('lockfile')):
new_mails = []
for stamp, identifier, action, data in parse(logfile):
mail, created = Mail.objects.get_or_create(identifier=identifier)
entry = Entry(mail=mail, action=action, data=data, stamp=stamp)
entry.save()
if created:
new_mails.append(mail)
for mail in new_mails:
mail.handle_exim_data()

View File

@ -0,0 +1,8 @@
from django.core.management.base import BaseCommand, CommandError
from logtracker.mail.utils import extract_exim_emails
class Command(BaseCommand):
help = 'Collect exim entries from journal entries'
def handle(self, *args, **options):
extract_exim_emails()