5.4 Expression

The following statement creates a named expression:

Config: expression name expr

Define the expression name to be expr.

Named expressions can be used as load estimation functions in server statements (see section expression) and invoked from the output format string (see section Output Format String).

Config: default-expression name

Declares the default expression. The name refers to a named expression declared elsewhere in the configuration file.

The default expression is used to compute relative load of servers whose ‘server’ declaration lacks explicit ‘expression’ definition (see server-expression).

The expr in the ‘expression’ statement is an arithmetical expression, which evaluates to a floating point number. The expression consists of terms, function calls and operators. The terms are floating point numbers, variable and constant names. The names refer to constants or SNMP variables defined in the definition of the server, for which this expression is being evaluated.

The following operations are allowed in expression:

Arithmetic operations

+

Addition

-

Subtraction

*

Multiplication

/

Division

**

Power

Named expression reference

A named expression reference is a reference to an expression defined by a ‘expression’ statement elsewhere in the configuration file. The reference has the following syntax:

 
@name

where name is the name of the expression as given by the first argument of its definition (see section Expression).

Ternary conditional operator

The ternary conditional operator is used to select a value based on a condition. It has the following form:

 
cond ? expr1 : expr2

The ternary operator evaluates to expr1 if cond yields ‘true’ (i.e. returns a non-null value) and to expr2 otherwise.

The condition cond is an expression which, apart from the arithmetic operators above, can use the following comparison and logical operations:

5.4.1 Comparisons

a == b

True if a equals b

a != b

True if the operands are not equal.

a < b

True if a is less than b.

a <= b

True if a is less than or equal to b.

a > b

True if a is greater than b.

a >= b

True if a is greater than or equal to b.

Logical operations

!expr

True if expr is false.

a && b

Logical and: true if both a and b are true, false otherwise.

a || b

Logical or: true if at least one of a or b are true.

Both logical ‘and’ and ‘or’ implement boolean shortcut evaluation: their second argument (b) is evaluated only when the first one is not sufficient to compute the result.

Precedence

The following table lists all operators in order of decreasing precedence:

Operators Description
(...) Grouping
? Ternary operator
** Power (right-associative)
- Unary negation
* / Multiplication, division
+ - Addition, subtraction
< <= >= > Relational operators (non-associative)
== != Equality comparison (non-associative)
! Boolean negation
&& Logical ‘and’.
|| Logical ‘or

When operators of equal precedence are used together they are evaluated from left to right (i.e., they are left-associative), except for comparison operators, which are non-associative and for the power operator, which is right-associative (these are explicitly marked as such in the above table). This means, for example that you cannot write:

 
(5 <= x <= 10) ? x : y

Instead, you should write:

 
(5 <= x && x <= 10) ? x : y

5.4.2 Function calls

Function calls have the following syntax:

 
name(arglist)

where name stands for the function name and arglist denotes the argument list: a comma-separated list of expressions which are evaluated and supply actual arguments for the function.

The following functions are supported by SLB version 1.0:

function: d (x)

Returns the derivative of x, i.e. the speed of its change per second, measured between the two last wakeups of slb (see section wakeup).

Notice, that this function is available only in standalone mode (see section standalone mode).

function: max (x0, ..., xn)

Returns the maximum value of its arguments. Any number of arguments can be given.

function: min (x0, ..., xn)

Returns the minimum value of its arguments.

function: avg (x0, ..., xn)

Returns the average value of its arguments.

function: log (x)

Returns the natural logarithm of x.

function: log10 (x)

Returns the decimal logarithm of x.

function: exp (x)

Returns the value of e (the base of natural logarithms) raised to the power of x.

function: pow (x, y)

Returns the value of x raised to the power of y. This function is provided for the sake of completeness, as it is entirely equivalent to ‘x ** y’.

function: sqrt (x)

Returns the non-negative square root of x.

function: abs (x)

Returns the absolute value of x.

function: ceil (x)

Returns the smallest integral value that is not less than x.

 
ceil(0.5) ⇒ 1.0
ceil(-0.5) ⇒ 0
function: floor (x)

Returns the largest integral value that is not greater than x.

 
floor(0.5) ⇒ 0.0
floor(-0.5) ⇒ -1.0
function: trunc (x)

Rounds x to the nearest integer not larger in absolute value.

function: round (x)

Rounds x to the nearest integer, but round halfway cases away from zero:

 
round(0.5) ⇒ 1.0
round(-0.5) ⇒ -1.0