Scheduled Task policy | obtaining instance parameters in Perl script

Good afternoon,

while setting up a Scheduled Task policy I specified:

1. an instance parameter NODE_NAME

2. another parameter PORT

as Task type in the policy I am using Perl script.

I assigned the policy to the node with an agent and defined 3 instances, for simplicity:

[instance 1]

NODE_NAME = serverA

PORT = 123

[instance 2]

NODE_NAME = serverB

PORT = 456

[instance 3]

NODE_NAME = serverC

PORT = 789

I would want to process each instance separately inside the perl script embedded in the Scheduled Task policy.

My impression was that inside the embedded script I should be able to obtain all of the instance parameter values using something like:

my $node_names_str = $OVOSystem->ParameterGetString("NODE_NAME", "%%NODE_NAME%%");

`/opt/microfocus/usr/lpp/OV/bin/ovlogmsg -w -a "System" -c "TEST" -msg "$node_names_str" -t`; <- outputs collected parameter values to System.txt

Unfortunately, $node_names_str will always contain only the value of the first instance, second and third is never obtained. This is also seen in the translated policy <id>_data.xml file, where the part '%%NODE_NAME%%' is replaced with 'serverA' - subsequent calls to $OVOSystem->ParameterGetString("NODE_NAME", "%%NODE_NAME%%"); will also yield the first instance value.

Would you please happen to know, whether there is some way to iterate over the instance values inside the embedded perl script, so that I can process all of the instances there?

Please note that I specifically wish to work with instance parameters as these are easier to read and provide better overview than setting non-instance parameter with values separated by commas such as "serverA, serverB, serverC" - this is not plausible.

  • 0  

    Hello,

    That's a good question.  I'm not sure about the answer. I normally copy one of the sample policies to get it to work, but in this case all the examples I've seen don't do any multi-instance processing. 

    Did you manage to get this working?

    Thanks.

  • Verified Answer

    +1 in reply to   

    Good afternoon,

    I managed to get this working via workaround within the perl script by parsing the .xml file of the policy that contains the parameter values:

    1. Obtain policy string: my $policy_str = `ovpolicy -list -poltype schedtmpl -polname $Policy->Name() -level 1`;

    2. Parse the policy template id: my $id = $1 if ($policy_str =~ /.*policy id.*: "(.*)"/);

    3. Utilize XML parser module - for my purposes I opted for XML::Simple - use XML::Simple qw(:strict); - which is bundled with the Operations Agent

    4. Compose xml filepath using the policy template id obtained: my $xml_filepath = "/var/opt/OV/datafiles/policies/schedtmpl/${policy_tmpl_id}_params.xml";

    5. Open the xml file for parsing: my $xml_ref = XMLin($xml_filepath, ForceArray => 0, KeyAttr => [], ForceContent => 0);

    Using Data::Dumper to print out the $xml_ref it can be seen that the output hash is quite easy to navigate:

     {
              'values' => {
                            'parameter' => {
                                             'instances' => {
                                                              'instance' => [
                                                                              {
                                                                                'value' => 'ananas',
                                                                                'parameters' => {
                                                                                                  'parameter' => [
                                                                                                                   {
                                                                                                                     'name' => 'PORT',
                                                                                                                     'value' => '443'
                                                                                                                   },
                                                                                                                   {
                                                                                                                     'value' => '0',
                                                                                                                     'name' => 'CRITICAL_DAYS_LEFT'
                                                                                                                   },
                                                                                                                   {
                                                                                                                     'value' => '100',
                                                                                                                     'name' => 'WARNING_DAYS_LEFT'
                                                                                                                   }
                                                                                                                 ]
                                                                                                }
                                                                              }
                                                                            ]
                                                            },
                                             'name' => 'NODE_NAME'
                                           }
                          },
              'definitions' => {
                                 'parameter' => {
                                                  'name' => 'NODE_NAME',
                                                  'label' => 'Node Name',
    
                                                  'type' => 'STRING',
                                                  'description' => 'Name of the node to connect to - fqdn of the node in OBM. Should have an \'IP FQDN\' entry in /etc/hosts file',
                                                  'multiinstance' => 'yes',
                                                  'value' => {},
                                                  'parameters' => {
                                                                    'parameter' => [
                                                                                     {
                                                                                       'value' => '443',
                                                                                       'description' => 'Port to connect to with openssl utility',
                                                                                       'type' => 'NUMERIC',
                                                                                       'name' => 'PORT',
                                                                                       'label' => 'Port',
                                                                                       'order' => '1'
                                                                                     },
                                                                                     {
                                                                                       'description' => 'Critical event should occur if the days left value in ceritifcate expiration is less than this
     value and than the Warning_Days_Left',
                                                                                       'type' => 'NUMERIC',
                                                                                       'value' => '0',
                                                                                       'order' => '2',
                                                                                       'label' => 'Critical Days Left',
                                                                                       'name' => 'CRITICAL_DAYS_LEFT'
                                                                                     },
                                                                                     {
                                                                                       'description' => 'Warning event should occur if the certificate expiration is less than this value and greater t
    han the Critical_Days_Left',
                                                                                       'type' => 'NUMERIC',
                                                                                       'value' => '100',
                                                                                       'order' => '3',
                                                                                       'name' => 'WARNING_DAYS_LEFT',
                                                                                       'label' => 'Warning Days Left'
                                                                                     }
                                                                                   ]
                                                                  }
                                                }
                               }
            };

    6. It's then possible to straightforwardly loop through the individual instances as well as it's parameters by for example:

    my $xml_ref = XMLin($xml_filepath, ForceArray => 0, KeyAttr => [], ForceContent => 0);
    my $instances_arr_ref = $xml_ref->{values}->{parameter}->{instances}->{instance};
    
    my $instances_size = scalar(@{$instances_arr_ref});
    
    for ($i=0; $i<$instances_size; $i++) {
        my $instance_ref = $instances_arr_ref->[$i];
        my $node_name = $instance_ref->{value};
        # ... do something with node name
    
        my $parameters_arr_ref = $instance_ref->{parameters}->{parameter};
        my $parameters_size = scalar(@{$parameters_arr_ref});
        
        for ($j=0; $j < $parameters_size; $j++) {
            my $parameter_ref = $parameters_arr_ref->[$j];
            if ( $parameter_ref->{name} eq "PORT" ) {
                my $port = $parameter_ref->{value};
                # ... do something with param
            }
            elsif ( $parameter_ref->{name} eq "CRITICAL_DAYS_LEFT" ) {
                my $critical_days_left = $parameter_ref->{value};
                # ... do something with other param
            }
            elsif ( $parameter_ref->{name} eq "WARNING_DAYS_LEFT" ) {
                my $warning_days_left = $parameter_ref->{value};
                # ... do something with another param
            }
        }
    }

    It's however unfortunate that $OVOSystem or $Policy object does not seem to provide a simple way to obtain an array of all instance parameters by parameter name, such as $Policy->GetParameters(%%NODE_NAME%%) ... [ananas, banana, lemon].

  • 0   in reply to 

    Hello there,

    That's really nice solution.

    Thank you for sharing.

  • 0 in reply to   

    Thank you, feel free to consider this post answered, unless someone can come up with a simpler solution.