Logfiles mit newsyslog verwalten

Logfiles können schnell mal das /var/log Verzeichnis füllen und so die /var Partition schnell komplett vollschreiben. Daher ist es wichtig, dass die Logfiles regelmässig gezippt/rotiert und gelöscht werden. Man kann dies mit logrotate oder (in FreeBSD bereits vorinstalliert) newsyslog machen.

Als Beispiel nehme ich mal das /var/log/imapd.log :

$ ll /var/log/imapd.log* 
-rw-r--r-- 1 root wheel 342702118 Jul 5 16:25 /var/log/imapd.log

Um das File nun zu rotieren, wie folgt vorgehen:

vi /etc/newsyslog.conf

Das Configfile ist eigentlich selbsterklärend:

# Entries which do not specify the '/pid_file' field will cause the
# syslogd process to be signalled when that log file is rotated. This
# action is only appropriate for log files which are written to by the
# syslogd process (ie, files listed in /etc/syslog.conf). If there
# is no process which needs to be signalled when a given log file is
# rotated, then the entry for that file should include the 'N' flag.
#
# The 'flags' field is one or more of the letters: BCDGJNUXZ or a '-'.
#
# Note: some sites will want to select more restrictive protections than the
# defaults. In particular, it may be desirable to switch many of the 644
# entries to 640 or 600. For example, some sites will consider the
# contents of maillog, messages, and lpd-errs to be confidential. In the
# future, these defaults may change to more conservative ones.
#
# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]
/var/log/all.log 600 7 * @T00 J
/var/log/amd.log 644 7 100 * J
[...]

Ansonsten mach man newsyslog.conf für weitere Infos. Speziell zu beachten sind die Flags. Möchte man zum Beispiel Patterns (z.B. *) benutzen, benötigt man den Flag G.

Das könnte dann so aussehen:

/var/log/httpd-*.log    root:wheel  644 7   *   @T00    GBJC  /var/run/httpd.pid 1

Nun aber weiter mit imapd.log – einfach folgendes eintragen:

# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]
/var/log/imapd.log 644 3 100 * JC
  • J indicates that newsyslog(8) should attempt to save disk
    space by compressing the rotated log file using bzip2(1).
  • C indicates that the log file should be created if it does
    not already exist, and if the -C option was also speci-
    fied on the command line.

Bei einer normalen Systemconfig wird newsyslog normalerweise automatisch regelmässig per Cron ausgeführt (man braucht selber nichts weiter zu konfigurieren).

Man kann kann es aber auch manuell ausführen. Also testen wir unseren Eintrag:

$ newsyslog

Das dauert nun eine Weile. Und jetzt… tadaaaaa…

$ ll imapd.log*
-rw-r--r-- 1 root wheel 15019 Jul 5 16:58 imapd.log
-rw-r--r-- 1 root wheel 11897353 Jul 5 16:56 imapd.log.0.bz2

Nun ist wichtig, dass man alle Logfiles erfasst, damit nichts überbordet.

 

nach oben