Document toolboxDocument toolbox

Using List Searches

You can search for values in a NetMRI list from inside a policy rule. This allows better integration of policies and scripting (for more information, see Job Scripting ). Using this feature, you can also manage a single list and avoid using large collections of similar rules.
A basic list search appears as:

<ListSearch list-name="list-name" search-columns="search1, search2, ..." result-columns="result1,result2, ..." >
    expression for search1 expression for search2
    ...
    expression for searchX
</ListSearch>

List-name is the name of the list shown on the Configuration Management > Job Management > Lists page; search1, search2,... is a comma-delimited list of column names to search; result1, result2,... is a comma-delimited list of result columns to retrieve. (If any column names contain commas, you may use a delimiter attribute to change the comma to another character.)
The rule evaluates each expression, and matches them with the search columns, in the defined order. Then, the rule creates variables, names them for the result columns and sets them to contain the values found in those columns. By default, the rule returns the first matching row; instead, you may use the result-mode attribute (set to all) to retrieve all matching rows. In that case, the variables will contain arrays of the values, with one entry per row.
Consider a policy in which device names must begin with a three-letter site code followed by a hyphen "-" character.
In NetMRI, you define a list titled Site Details with two columns: 'Site Code' and 'Location.' A sample list could appear as follows:

Site Code

Location

Site Code

Location

dca

Washington, DC

iad

Herndon, VA

sjc

San Jose, CA

The following rule is defined in the Raw XML Editor. The rule parses the site code from the device name, and the rule uses it to look up the ‘Location.’ The rule then uses it to check that the SNMP location ends with that city name:

 

<PolicyRuleLogic editor="raw-xml"

xmlns="http://www.infoblox.com/NetworkAutomation/1.0/ScriptXml">

<If>

<Expr op="matches">

<Expr field="DeviceName"/>

<Expr value="^([a-z]{3})-"/>

<!--This is the regex used to match against the Device Name field --!>

</Expr>

<Then>

<!--

The device name appears to be the correct format, so we look up the location via the parsed site code. Here, we rely on the fact that the return value of the ListSearch is the count of the matching rows, and that 0 is considered false.

We evaluate the ListSearch first; if no result is returned, the ConfigFileCheck will not execute due to the short-circuiting of the boolean expression (Expr). If a result is returned, the ListSearch call will set a variable named "Location" containing the result of the first match to "Site Code".

Then, we do a ConfigFileCheck to see if the config file contains exactly one line that starts with "snmp-server location" and ends with the location. To do this, we construct a result expression using the Location variable.

We can construct a more descriptive message for the PolicyRuleFail, that includes the current site code and location, as well as the intended location, but did not for simplicity here.

-->

<If>

<Expr op="and">

<ListSearch list-name="Site Details" search-columns="Site Code" result-columns="Location">

<Expr variable="_match_1"/>

</ListSearch>

<ConfigFileCheck op="contains-one">

<Expr op="concat"><Expr value="^snmp-server location .*"/>

<Expr variable="Location"/></Expr>

</ConfigFileCheck>

</Expr>

<Then><PolicyRulePass/></Then>

<Else><PolicyRuleFail>The incorrect location, or none at all, is

configured.</PolicyRuleFail>

</Else>

</If>

</Then>

<Else>

<!--

The device name did not match the correct format, so fail the rule.

-->

<PolicyRuleFail>The device name does not begin with a three-letter site code.

</PolicyRuleFail>

</Else>

</If>

</PolicyRuleLogic>