3.8 Output

When slb has finished building load table, it sends it to the output, line by line, using the output format string. This format string is similar to the format argument of printf(1): any characters, except ‘%’ are copied to the output verbatim; the ‘%’ introduces a conversion specifier. For example, ‘%i’ expands to the ID of the server this line refers to. The ‘%w’ specifier expands to the computed relative load for that server, etc. There is a number of such specifiers, they are all described in detail in Output Format String.

The default output format is ‘%i %w\n’. This produces a table, each line of which shows a server ID and its relative load. This default is suitable for testing purposes, but for real configurations you will most probably need to create a custom output format. You do so using the ‘output-format’ statement:

 
output-format "id=%i host=%h\n";

The output format string can be quite complex as shown in the following example:

 
output-format <<EOT
server 10.0.0.1
update add www.example.net 60 IN A %h
send
EOT;

This format creates, for each line from the load table, a set of commands for nsupdate(1). The ‘<<’ block introduces a here-document, a construct similar to that of shell and several other programming languages. It is discussed in here-document.

You may need to include server-dependent data in the corresponding output lines. For this purpose, slb provides server macros. Server macros are special text variables, defined in the ‘server’ block, which can be accessed from output format string.

Macros are defined using the ‘macro’ statement, whose syntax is similar to that of ‘variable’ or ‘constant’. The first argument supplies the name of the macro, and the second one, its contents, or expansion, e.g.:

 
server srv01 {
  host 192.168.10.6;
  community "public";
  macro description "some descriptive text";
}

Macros are accessed using the following conversion specifier:

 
%(name)

where name is the name of the macro. This specifier is replaced with the actual contents of the macro. The following example uses the ‘description’ macro to create a TXT record in the DNS:

 
output-format <<EOT
server 10.0.0.1
update add www.example.net 60 IN A %h
update add %i.example.net 60 IN TXT "%(description)"
send
EOT;

Sometimes you may need to produce some output immediately before the load table or after it. Two configuration statements are provided for that purpose: ‘begin-output-message’ and ‘end-output-message’. Both take a single string as argument. The string supplied with ‘begin-output-message’ is output before formatting the load table, and the one supplied with ‘end-output-message’ is output after formatting it.

Continuing our ‘nsupdate’ example, the following statement will remove all existing A records from the DNS prior to creating new ones:

 
begin-output-message <<EOT
server 10.0.0.1
update delete www.example.net
send
EOT;

You may want to output only a part of the load table. Two statements are provided for this purpose. The ‘head N’ statement outputs at most N entries from the top of the table. The ‘tail N’ statement outputs at most N entries from the bottom of the table. For example, to print only one entry corresponding to the least loaded server, use

 
head 1;

By default slb prints results to the standard output. To change this, use the ‘output-file’ statement. Its argument specifies the name of a file where slb output will be directed. If this name starts with a pipe character (‘|’), slb treats the remaining characters as the shell command. This command is executed (using /bin/sh -c), and the output is piped to its standard input. Thus, the following statement

 
output-file "| /usr/bin/nsupdate "
            "-k /usr/share/slb/Kvpn.+157+45756.private";

directs the formatted output to the standard input of nsupdate command.

Footnotes

(1)

See http://www.manpagez.com/man/8/nsupdate.