selectservers
Prototype: selectservers(hostlist, port, query, regex, maxbytes, array)
Return type: int
Description: Returns the number of tcp servers from hostlist
which
respond with a reply matching regex
to a query
send to port
, and
populates array
with their names.
The regular expression is anchored. If query
is empty, then no
reply checking is performed (any server reply is deemed to be satisfactory),
otherwise at most maxbytes
bytes are read from the server and matched.
This function allows discovery of all the TCP ports that are active and functioning from an ordered list, and builds an array of their names. This allows maintaining a list of pretested failover alternatives.
Arguments:
hostlist
:string
- CFEngine list identifier, the list of hosts or addresses to contact - in the range:@[(][a-zA-Z0-9_$(){}\[\].:]+[)]
port
:string
- Port number or service name. - in the range:.*
query
:string
- A query string - in the range:.*
regex
: regular expression - A regular expression to match success - in the range:.*
maxbyes
:int
- Maximum number of bytes to read from server - in the range:0,99999999999
array
:string
- Name for array of results - in the range:[a-zA-Z0-9_$(){}\[\].:]+
Example:
bundle agent example
{
vars:
"hosts" slist => { "slogans.iu.hio.no", "eternity.iu.hio.no", "nexus.iu.hio.no" };
"fhosts" slist => { "www.cfengine.com", "www.cfengine.org" };
"up_servers" int => selectservers("@(hosts)","80","","","100","alive_servers");
"has_favicon" int =>
selectservers(
"@(hosts)", "80",
"GET /favicon.ico HTTP/1.0$(const.n)Host: www.cfengine.com$(const.n)$(const.n)",
"(?s).*OK.*",
"200", "favicon_servers");
classes:
"someone_alive" expression => isgreaterthan("$(up_servers)","0");
"has_favicon" expression => isgreaterthan("$(has_favicon)","0");
reports:
"Number of active servers $(up_servers)";
someone_alive::
"First server $(alive_servers[0]) fails over to $(alive_servers[1])";
has_favicon::
"At least $(favicon_servers[0]) has a favicon.ico";
}
If there is a multi-line response from the server, special care must be
taken to ensure that newlines are matched, too. Note the use of (?s)
in the example, which allows .
to also match newlines in the
multi-line HTTP response.