25.5. Working with Queues in Rsyslog Queues are used to pass content, mostly syslog messages, between components of rsyslog . With queues, rsyslog is capable of processing multiple messages simultaneously and to apply several actions to a single message at once. The data flow inside rsyslog can be illustrated as follows: Figure 25.1. Message Flow in Rsyslog Whenever rsyslog receives a message, it passes this message to the preprocessor and then places it into the main message queue . Messages wait there to be dequeued and passed to the rule processor . The rule processor is a parsing and filtering engine. Here, the rules defined in /etc/rsyslog.conf are applied. Based on these rules, the rule processor evaluates which actions are to be performed. Each action has its own action queue. Messages are passed through this queue to the respective action processor which creates the final output. Note that at this point, several actions can run simultaneously on one message. For this purpose, a message is duplicated and passed to multiple action processors. Only one queue per action is possible. Depending on configuration, the messages can be sent right to the action processor without action queuing. This is the behavior of direct queues (see below). In case the output action fails, the action processor notifies the action queue, which then takes an unprocessed element back and after some time interval, the action is attempted again. To sum up, there are two positions where queues stand in rsyslog : either in front of the rule processor as a single main message queue or in front of various types of output actions as action queues . Queues provide two main advantages that both lead to increased performance of message processing: they serve as buffers that decouple producers and consumers in the structure of rsyslog they allow for parallelization of actions performed on messages Apart from this, queues can be configured with several directives to provide optimal performance for your system. These configuration options are covered in the following sections. Warning If an output plug-in is unable to deliver a message, it is stored in the preceding message queue. If the queue fills, the inputs block until it is no longer full. This will prevent new messages from being logged via the blocked queue. In the absence of separate action queues this can have severe consequences, such as preventing SSH logging, which in turn can prevent SSH access. Therefore it is advised to use dedicated action queues for outputs which are forwarded over a network or to a database. 25.5.1. Defining Queues Based on where the messages are stored, there are several types of queues: direct , in-memory , disk , and disk-assisted in-memory queues that are most widely used. You can choose one of these types for the main message queue and also for action queues. Add the following into /etc/rsyslog.conf : USD object QueueType queue_type Here, you can apply the setting for the main message queue (replace object with MainMsg ) or for an action queue (replace object with Action ). Replace queue_type with one of direct , linkedlist or fixedarray (which are in-memory queues), or disk . The default setting for a main message queue is the FixedArray queue with a limit of 10,000 messages. Action queues are by default set as Direct queues. Direct Queues For many simple operations, such as when writing output to a local file, building a queue in front of an action is not needed. To avoid queuing, use: USD object QueueType Direct Replace object with MainMsg or with Action to use this option to the main message queue or for an action queue respectively. With direct queue, messages are passed directly and immediately from the producer to the consumer. Disk Queues Disk queues store messages strictly on a hard drive, which makes them highly reliable but also the slowest of all possible queuing modes. This mode can be used to prevent the loss of highly important log data. However, disk queues are not recommended in most use cases. To set a disk queue, type the following into /etc/rsyslog.conf : USD object QueueType Disk Replace object with MainMsg or with Action to use this option to the main message queue or for an action queue respectively. Disk queues are written in parts, with a default size 10 Mb. This default size can be modified with the following configuration directive: USD object QueueMaxFileSize size where size represents the specified size of disk queue part. The defined size limit is not restrictive, rsyslog always writes one complete queue entry, even if it violates the size limit. Each part of a disk queue matches with an individual file. The naming directive for these files looks as follows: USD object QueueFilename name This sets a name prefix for the file followed by a 7-digit number starting at one and incremented for each file. In-memory Queues With in-memory queue, the enqueued messages are held in memory which makes the process very fast. The queued data is lost if the computer is power cycled or shut down. However, you can use the USDActionQueueSaveOnShutdown setting to save the data before shutdown. There are two types of in-memory queues: FixedArray queue - the default mode for the main message queue, with a limit of 10,000 elements. This type of queue uses a fixed, pre-allocated array that holds pointers to queue elements. Due to these pointers, even if the queue is empty a certain amount of memory is consumed. However, FixedArray offers the best run time performance and is optimal when you expect a relatively low number of queued messages and high performance. LinkedList queue - here, all structures are dynamically allocated in a linked list, thus the memory is allocated only when needed. LinkedList queues handle occasional message bursts very well. In general, use LinkedList queues when in doubt. Compared to FixedArray, it consumes less memory and lowers the processing overhead. Use the following syntax to configure in-memory queues: USD object QueueType LinkedList USD object QueueType FixedArray Replace object with MainMsg or with Action to use this option to the main message queue or for an action queue respectively. Disk-Assisted In-memory Queues Both disk and in-memory queues have their advantages and rsyslog lets you combine them in disk-assisted in-memory queues . To do so, configure a normal in-memory queue and then add the USDobjectQueueFileName directive to define a file name for disk assistance. This queue then becomes disk-assisted , which means it couples an in-memory queue with a disk queue to work in tandem. The disk queue is activated if the in-memory queue is full or needs to persist after shutdown. With a disk-assisted queue, you can set both disk-specific and in-memory specific configuration parameters. This type of queue is probably the most commonly used, it is especially useful for potentially long-running and unreliable actions. To specify the functioning of a disk-assisted in-memory queue, use the so-called watermarks : USD object QueueHighWatermark number USD object QueueLowWatermark number Replace object with MainMsg or with Action to use this option to the main message queue or for an action queue respectively. Replace number with a number of enqueued messages. When an in-memory queue reaches the number defined by the high watermark, it starts writing messages to disk and continues until the in-memory queue size drops to the number defined with the low watermark. Correctly set watermarks minimize unnecessary disk writes, but also leave memory space for message bursts since writing to disk files is rather lengthy. Therefore, the high watermark must be lower than the whole queue capacity set with USDobjectQueueSize . The difference between the high watermark and the overall queue size is a spare memory buffer reserved for message bursts. On the other hand, setting the high watermark too low will turn on disk assistance unnecessarily often. Example 25.12. Reliable Forwarding of Log Messages to a Server Rsyslog is often used to maintain a centralized logging system, where log messages are forwarded to a server over the network. To avoid message loss when the server is not available, it is advisable to configure an action queue for the forwarding action. This way, messages that failed to be sent are stored locally until the server is reachable again. Note that such queues are not configurable for connections using the UDP protocol. To establish a fully reliable connection, for example when your logging server is outside of your private network, consider using the RELP protocol described in Section 25.7.4, "Using RELP" . Procedure 25.2. Forwarding To a Single Server Suppose the task is to forward log messages from the system to a server with host name example.com , and to configure an action queue to buffer the messages in case of a server outage. To do so, perform the following steps: Use the following configuration in /etc/rsyslog.conf or create a file with the following content in the /etc/rsyslog.d/ directory: Where: USDActionQueueType enables a LinkedList in-memory queue, USDActionFileName defines a disk storage, in this case the backup files are created in the /var/lib/rsyslog/ directory with the example_fwd prefix, the USDActionResumeRetryCount -1 setting prevents rsyslog from dropping messages when retrying to connect if server is not responding, enabled USDActionQueueSaveOnShutdown saves in-memory data if rsyslog shuts down, the last line forwards all received messages to the logging server, port specification is optional. With the above configuration, rsyslog keeps messages in memory if the remote server is not reachable. A file on disk is created only if rsyslog runs out of the configured memory queue space or needs to shut down, which benefits the system performance. Procedure 25.3. Forwarding To Multiple Servers The process of forwarding log messages to multiple servers is similar to the procedure: Each destination server requires a separate forwarding rule, action queue specification, and backup file on disk. For example, use the following configuration in /etc/rsyslog.conf or create a file with the following content in the /etc/rsyslog.d/ directory:

Commands:
USDActionQueueType LinkedList USDActionQueueFileName example_fwd USDActionResumeRetryCount -1 USDActionQueueSaveOnShutdown on *.* @@example.com:6514
USDActionQueueType LinkedList USDActionQueueFileName example_fwd1 USDActionResumeRetryCount -1 USDActionQueueSaveOnShutdown on *.* @@example1.com USDActionQueueType LinkedList USDActionQueueFileName example_fwd2 USDActionResumeRetryCount -1 USDActionQueueSaveOnShutdown on *.* @@example2.com