File Operations (xm_fileop)

This module provides functions and procedures to manipulate files. Coupled with a Schedule block, this module allows various log rotation and retention policies to be implemented, including:

  • log file retention based on file size,

  • log file retention based on file age, and

  • cyclic log file rotation and retention.

Rotating, renaming, or removing the file written by om_file is also supported with the help of the om_file reopen() procedure.

Configuration

The xm_fileop module accepts only the common module directives.

Functions

The following functions are exported by xm_fileop.

boolean dir_exists(string path)

Return TRUE if path exists and is a directory. On error undef is returned and an error is logged.

string dir_temp_get()

Return the name of a directory suitable as a temporary storage location.

string file_basename(string file)

Strip the directory name from the full file path. For example, file_basename('/var/log/app.log') will return app.log.

datetime file_ctime(string file)

Return the creation or inode-changed time of file. On error undef is returned and an error is logged.

string file_dirname(string file)

Return the directory name of the full file path. For example, file_dirname('/var/log/app.log') will return /var/log. Returns an empty string if file does not contain any directory separators.

boolean file_exists(string file)

Return TRUE if file exists and is a regular file.

binary file_hash(string file, string digest)

Return the calculated hash of file using digest algorithm. Available digest values are blake2b512, blake2s256, gost, md4, md5, rmd160, sha1, sha224, sha256, sha384, sha512 (see openssl dgst command in openssl’s manual). On error undef is returned and an error is logged.

integer file_inode(string file)

Return the inode number of file. On error undef is returned and an error is logged.

datetime file_mtime(string file)

Return the last modification time of file. On error undef is returned and an error is logged.

string file_read(string file)

Return the contents of file as a string value. On error undef is returned and an error is logged.

integer file_size(string file)

Return the size of file, in bytes. On error undef is returned and an error is logged.

string file_type(string file)

Return the type of file. The following string values can be returned: FILE, DIR, CHAR, BLOCK, PIPE, LINK, SOCKET, and UNKNOWN. On error undef is returned and an error is logged.

Procedures

The following procedures are exported by xm_fileop.

dir_make(string path);

Create a directory recursively (like mkdir -p). It succeeds if the directory already exists. An error is logged if the operation fails.

dir_remove(string file);

Remove the directory from the filesystem.

file_append(string src, string dst);

Append the contents of the file src to dst. The dst file will be created if it does not exist. An error is logged if the operation fails.

file_chmod(string file, integer mode);

Change the permissions of file. This function is only implemented on POSIX systems where chmod() is available in the underlying operating system. An error is logged if the operation fails.

file_chown(string file, integer uid, integer gid);

Change the ownership of file. This function is only implemented on POSIX systems where chown() is available in the underlying operating system. An error is logged if the operation fails.

file_chown(string file, string user, string group);

Change the ownership of file. This function is only implemented on POSIX systems where chown() is available in the underlying operating system. An error is logged if the operation fails.

file_copy(string src, string dst);

Copy the file src to dst. If file dst already exists, its contents will be overwritten. An error is logged if the operation fails.

file_cycle(string file);

Do a cyclic rotation on file. The file will be moved to "file.1". If "file.1" already exists it will be moved to "file.2", and so on. Wildcards are supported in the file path and filename. The backslash (\) must be escaped if used as the directory separator with wildcards (for example, C:\\test\\*.log). This procedure will reopen the LogFile if it is cycled. An error is logged if the operation fails.

file_cycle(string file, integer max);

Do a cyclic rotation on file as described above. The max argument specifies the maximum number of files to keep. For example, if max is 5, "file.6" will be deleted.

file_cycle(string file, boolean copy_truncate);

Perform a cyclic rotation on file. If copy_truncate is set to FALSE or not specified, the file will be moved to "file.1". If copy_truncate is set to TRUE, the original file will be copied to "file.1", and then truncated to zero. If "file.1" already exists it will be moved to "file.2", and so on. Wildcards are supported in the file path and filename. The backslash (\) must be escaped if used as the directory separator with wildcards (for example, C:\\test\\*.log). If copy_truncate is FALSE, this procedure will reopen the LogFile if it is cycled. An error is logged if the operation fails.

Be aware, that if copy_truncate is TRUE, there is a small time delay between copying the file and truncating it, so some logging data may be lost.
file_cycle(string file, integer max, boolean copy_truncate);

Perform a cyclic rotation on file as described above. The max argument specifies the maximum number of files to keep. For example, if max is 5, "file.6" will be deleted.

file_remove(string file);

Remove file. It is possible to specify a wildcard in the filename (but not in the path). The backslash (\) must be escaped if used as the directory separator with wildcards (for example, C:\\test\\*.log). This procedure will reopen the LogFile if it is removed. An error is logged if the operation fails. Calling this procedure while the file is still open by nxlog or other processes, may produce undefined behavior, including silently failing to remove the file (known issue on Windows Server).

file_remove(string file, datetime older);

Remove file if its creation time is older than the value specified in older. It is possible to specify a wildcard in the filename (but not in the path). The backslash (\) must be escaped if used as the directory separator with wildcards (for example, C:\\test\\*.log). This procedure will reopen the LogFile if it is removed. An error is logged if the operation fails. Calling this procedure while the file is still open by nxlog or other processes, may produce undefined behavior, including silently failing to remove the file (known issue on Windows Server).

file_rename(string old, string new);

Rename the file old to new. If the file new exists, it will be overwritten. Moving files or directories across devices may not be possible. This procedure will reopen the LogFile if it is renamed. An error is logged if the operation fails.

file_touch(string file);

Update the last modification time of file or create the file if it does not exist. An error is logged if the operation fails.

file_truncate(string file);

Truncate file to zero length. If the file does not exist, it will be created. An error is logged if the operation fails.

file_truncate(string file, integer offset);

Truncate file to the size specified in offset. If the file does not exist, it will be created. An error is logged if the operation fails.

file_write(string file, string value);

Write value into file. The file will be created if it does not exist. An error is logged if the operation fails.

Examples

Example 1. Rotation of the NXLog Agent internal log file

In this example, the internal log file is rotated based on time and size.

nxlog.conf
#define LOGFILE C:\Program Files\nxlog\data\nxlog.log
define LOGFILE /var/log/nxlog/nxlog.log

<Extension fileop>
    Module      xm_fileop

    # Check the log file size every hour and rotate if larger than 1 MB
    <Schedule>
        Every   1 hour
        Exec    if (file_size('%LOGFILE%') >= 1M) \
                    file_cycle('%LOGFILE%', 2);
    </Schedule>

    # Rotate log file every week on Sunday at midnight
    <Schedule>
        When    @weekly
        Exec    file_cycle('%LOGFILE%', 2);
    </Schedule>
</Extension>