How to manage LOG files on a router


In this page I describe how to manage LOG files on a router (Asus RT-AC56U) - we use an Entware package, so please refer to this page on how to install Entware on a router.
On a router we can install any type of daemon, and most daemons create and manage a LOG file which can become bigger and bigger and take up a lot of space on the router's internal storage. The management of LOG files is of vital importance especially when dealing with limited storage, such as that of a router, so managing a LOG file becomes vital to avoid taking up the storage on a router. To manage the LOG files we will use a Linux application called "LOGROTATE" which allows, if executed, to properly manage the LOG files, following the indications of a properly configured configuration file.


How to install the application LOGROTATE

In order to install the "LOGROTATE" application with the Entware packages, we must execute the following command :
opkg install logrotate
The above-mentioned package contains the executable file "/opt/sbin/logrotate" and the configuration file "/opt/etc/logrotate.conf". We create a backup copy of the "/opt/etc/logrotate.conf" configuration file.
The following is a sample content of the above-mentioned configuration file "/opt/etc/logrotate.conf" :

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /opt/etc/logrotate.d

# system-specific logs may be also be configured here.

Refer to this page for further details about configuration file "logrotate.conf".
In the above-mentioned example:

  • LOG files will be "rotated" (or archived) each week;
  • they will be "rotated" on basis 4 (rotation is based on 4 files) before being removed;
  • an empty LOG file will be created after rotation of oldest ones;
  • we will use a prefix on file name, based on creation file, on LOG files already archived;
  • we will include files placed on folder "/opt/etc/logrotate.d" for the rotation of log files.

Obviously in the above example file, there is no reference to an already existing .LOG file, we will include it now.


How to setup a configuration file for LOGROTATE

As an example we take the LOG file of the Transmission daemon (refer to this page for further details on how to install and configure the above-mentioned Transmission daemon). To find out where it is located, use the following command "ps | grep transmission-daemon"; in the output line containing the Transmission daemon, after the "-e" parameter we can see the path and the LOG file of the above-mentioned daemon. We complete the configuration file "logrotate.conf" in order to have a reference to the above LOG file of the Transmission daemon.

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# Some packages can drop log rotation information into this directory
include /opt/etc/logrotate.d

# system-specific logs may be also be configured here.

"/opt/var/log/transmission.log" {
    create 666 nobody nobody
    daily
    missingok
    nocompress
    nomail
    notifempty
    rotate 10
    size 100k
    su nobody nobody
}

In the above-mentioned configuration file, we have created a dedicated section for the LOG file of the Transmission with the path "/opt/var/log/transmission.log" that is the path where the LOG file of the Transmission is placed. Within this section we include the parameters and configuration options dedicated to the above LOG file, these parameters have priority over those ones outside the above-mentioned section. The parameters provide the following configuration:

  • After the rotation, a new empty LOG file is created, with the access permissions to the file set to "666" (same parameter using the "chmod" command), and with the owner permissions to the file set to "nobody nobody" (same parameter used by the "chown" command);
  • LOG files will be rotated every day;
  • If LOG file is missing, do not show any error message;
  • Do not use compression of LOG files;
  • Do not send email of oldest LOG files;
  • Do not rotate LOG file if it is empty;
  • LOG files will be rotated for 10 times before being removed;
  • LOG files will be rotated only if they arrive to occupy more than 100 kilobytes in size;
  • LOG files already rotated will have owner permissions set to "nobody nobody" (same parameter used by command "chown").

How to execute the application LOGROTATE

In the above-mentioned configuration file we have set the rotation of the LOG files every day, so every day we will run the "LOGROTATE" application.
We create a script file, for example we call it "daily.sh", with the following content :

#!/bin/sh

chmod 644 /opt/etc/logrotate.conf
/opt/sbin/logrotate /opt/etc/logrotate.conf