Compress and delete old remote logs
Some checks failed
continuous-integration/drone/push Build is failing

Logrotate is not used because I didn't found an easy way to configure it
to handle the compression/deletion of log files already rotated by
rsyslog (it is probably possible, but I found the script to be easier).
This commit is contained in:
jeltz 2021-03-01 03:58:58 +01:00
parent f7183095c1
commit c65b3f090b
6 changed files with 122 additions and 0 deletions

View file

@ -1,4 +1,7 @@
---
rsyslog_inputs: []
rsyslog_collector_base_dir: /var/log/remote
rsyslog_collector_rotate_path: /usr/local/sbin/rotate_remote_logs
rsyslog_collector_keep_days: 0
rsyslog_collector_compress_days: 1
...

View file

@ -0,0 +1,62 @@
#!/usr/bin/env python3
import argparse
import datetime
import logging
import pathlib
import subprocess
def compress_file(filename):
subprocess.run(["xz", "-z", str(filename)])
def find_files(base_dir, extension, days):
delta = datetime.timedelta(days=days)
now = datetime.datetime.now()
for path in base_dir.rglob(f"*{extension}"):
stem = path.name.removesuffix(extension)
date = datetime.datetime.fromisoformat(stem)
if date < now - delta:
yield path
def compress_logs(base_dir, days):
for path in find_files(base_dir, ".log", days):
logging.info("Compressing log file %s", str(path))
compress_file(path)
def remove_logs(base_dir, days):
for path in find_files(base_dir, ".log.xz", days):
logging.info("Removing log file %s", str(path))
path.unlink()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--compress-days", type=int, default=0)
parser.add_argument("--keep-days", type=int, default=0)
parser.add_argument(
"--base-dir", type=pathlib.Path, default="/var/log/remote"
)
args = parser.parse_args()
logging.basicConfig(
format="[%(asctime)s] %(levelname)s %(message)s", level=logging.INFO
)
logging.info("Rotate script started")
if args.compress_days > 0:
compress_logs(args.base_dir, args.compress_days)
if args.keep_days > 0:
remove_logs(args.base_dir, args.keep_days)
logging.info("Rotate script done")
if __name__ == "__main__":
main()

View file

@ -0,0 +1,5 @@
---
- name: Run systemd daemon-reload
systemd:
daemon_reload: true
...

View file

@ -24,4 +24,34 @@
group: root
mode: u=rw,g=r,o=r
notify: Restart rsyslog
- name: Install rotate script
become: true
copy:
src: rotate
dest: "{{ rsyslog_collector_rotate_path }}"
owner: root
group: root
mode: u=rwx,g=rx,o=
- name: Install timer and service for rotate script
become: true
template:
src: "{{ item }}.j2"
dest: "/etc/systemd/system/{{ item }}"
owner: root
group: root
mode: u=rw,g=r,o=
loop:
- rotate-remote-logs.timer
- rotate-remote-logs.service
notify:
- Run systemd daemon-reload
- name: Enable timer for log rotation
become: true
systemd:
name: rotate-remote-logs.timer
enabled: true
state: started
...

View file

@ -0,0 +1,12 @@
{{ ansible_managed | comment }}
[Unit]
Description=Rotate remote logs
[Service]
User=root
Type=OneShot
ExecStart={{ rsyslog_collector_rotate_path }} \
--base-dir {{ rsyslog_collector_keep_days }} \
--compress-days {{ rsyslog_collector_compress_days }} \
--keep-days {{ rsyslog_collector_base_dir }}

View file

@ -0,0 +1,10 @@
{{ ansible_managed | comment }}
[Unit]
Description=Rotate remote logs daily
[Timer]
OnCalendar=daily
[Install]
WantedBy=timers.target