Load Balancer Discovery Pattern Extension steps - CIs not created

Marcin Witoslaw
Tera Contributor

I have created an Extension steps for AVI Load Balancer Controller - Session based pattern. Requirement is to get markers from JSON payload for Load Balancer Services and Load Balancer pools and insert them into cmdb_key_value table. All steps in Pattern debug works ok except the insert step. Configuration Item sys id of Load Balancer Service  is needed for cmdb_key_value table. I dont know how achieve that? I have used EVAL script for configuration_item field no success . I have created a simple script include and call it from the pattern no success as well.Please refer to the attached screenshots. And also my script include which is working in background script.

**********************************************************************************************************

findByObjectId: function(objectId) {
    if (!objectId)
        return "";

 

    objectId = ("" + objectId).trim();

 

    var gr = new GlideRecord("cmdb_ci_lb_service");
    gr.addQuery("object_id", objectId);
    gr.setLimit(1);
    gr.query();

 

    if (gr.next())
        return gr.getUniqueValue();

 

    return "";
}
************************************************************************************
My EVAL script (second solution) without script include : 
var rtrn = "";
var objID = CTX.getAttribute("matched_service_markers[].u_vs_uuid");
var port = CTX.getAttribute("matched_service_markers[].port");
if (objID) {var fullLookupId = port ? (objID + "-" + port) : objID;
var gr = new GlideRecord("cmdb_ci_lb_service");
gr.addQuery("object_id", fullLookupId);
gr.setLimit(1);gr.query();
if (gr.next()) { rtrn = gr.getUniqueValue(); } }
rtrn;
 
Anyone please help .
5 REPLIES 5

Vishnu-K
Kilo Sage

Hi @Marcin Witoslaw ,

The best approach here is to use a Pre Sensor Script in the Pattern Pre/Post Scripts table (sa_pattern_prepost_script). This runs before the Identification Engine processes the payload, so you can resolve the configuration_item sys_id and inject it directly into the payload.

 

How to Set It Up

1. Navigate to the table Go to sa_pattern_prepost_script.list and click New.

2. Fill in the record

  • Name: give it a meaningful name
  • Pattern: select your AVI Load Balancer pattern
  • Type: set to Pre Sensor
  • Active: checked

3. Write your logic in the Script field In the script, you have access to the full payload object before IRE touches it. The idea is to loop through the payload items, find your cmdb_key_value entries, perform a GlideRecord lookup on cmdb_ci_lb_service using the object_id, and inject the resolved sys_id into the configuration_item field of the payload directly.

 

4. Return the payload Returning the modified payload as a JSON string is mandatory for Pre Sensor scripts — otherwise your changes won't be picked up.

 

Since the script runs before IRE processes the payload, any values you inject are treated as if they came from the pattern itself. This completely bypasses the problem you faced with EVAL, where the CI may not yet be committed when the insert step executes.

 

Hope this helps!

If it helped you please do mark it as helpful and accept the solution

 

Thanks,

Vishnu

Thank you for a quick reply Vishnu-K . I have created the Pre script and it was succesfull.However No records created in cmdb_key_value . Can you have a look ?

var rtrn = {};var payloadObj = JSON.parse(payload || "{}");

if (payloadObj && payloadObj.items) {
var items = payloadObj.items;

for (var i = 0; i < items.length; i++) {
var currentItem = items[i];

if (currentItem.className == "cmdb_key_value") {
var values = currentItem.values;
var tempId = values.configuration_item;

if (tempId) {
var targetTable = "";

if (tempId.indexOf('virtualservice-') > -1) {
targetTable = "cmdb_ci_lb_service";
} else if (tempId.indexOf('pool-') > -1) {
targetTable = "cmdb_ci_lb_pool";
}

if (targetTable) {
var gr = new GlideRecord(targetTable);

if (gr.get("object_id", tempId)) {
values.configuration_item = gr.getUniqueValue();
prePostNodeLogger.info(prePostLogPrefix + " Successfully Resolved: " + tempId);
} else {
prePostNodeLogger.warn(prePostLogPrefix + " Match Failed in DB for: " + tempId);
}
}
}
}
}
}rtrn = {
'status': {
'message': 'AVI Resolution Completed',
'isSuccess': true
},
'patternId': patternId,
'payload': JSON.stringify(payloadObj)
};

 

bold line reflects to cmdb_key_value table but its not transformed yet. 

Hi @Marcin Witoslaw ,

Can you log the payload and share it here to see what might be wrong .

 

Regards,

Vishnu

Please see the attached JSON payload. I have changed my pre sensor script : 

var rtrn = {};

// 1. Parse the payload
var payloadObj = JSON.parse(payload || "{}");
gs.info("AVI_DEBUG_PAYLOAD: " + JSON.stringify(payloadObj));
prePostNodeLogger.info(prePostLogPrefix + " Full Input Payload: " + JSON.stringify(payloadObj));

if (payloadObj && payloadObj.items) {
    var items = payloadObj.items;

    for (var i = 0; i < items.length; i++) {
        var currentItem = items[i];
        if (currentItem.className == "cmdb_ci_lb_service" || currentItem.className == "cmdb_ci_lb_pool") {

            var values = currentItem.values || {};
            var tempId = values.object_id;

            if (tempId) {
                var targetTable = "";
                if (tempId.indexOf('virtualservice-') > -1) {
                    targetTable = "cmdb_ci_lb_service";
                } else if (tempId.indexOf('pool-') > -1) {
                    targetTable = "cmdb_ci_lb_pool";
                }

                if (targetTable) {
                    prePostNodeLogger.info(prePostLogPrefix +
                        " AVI_DEBUG_LOOKUP_START: idx=" + i +
                        " class=" + currentItem.className +
                        " table=" + targetTable +
                        " object_id=" + tempId
                    );

                    var gr = new GlideRecord(targetTable);
                    gr.addQuery("object_id", tempId);
                    gr.setLimit(1);
                    gr.query();

                    if (gr.next()) {
                        values.configuration_item = gr.getUniqueValue();

                        prePostNodeLogger.info(prePostLogPrefix +
                            " AVI_DEBUG_LOOKUP_OK: idx=" + i +
                            " object_id=" + tempId +
                            " sys_id=" + gr.getUniqueValue()
                        );
                    } else {
                        prePostNodeLogger.warn(prePostLogPrefix +
                            " DB Lookup Failed for: " + tempId
                        );
                    }
                }
            }
        }
    }
}

rtrn = {
    'status': {
        'message': 'AVI Resolution Completed',
        'isSuccess': true
    },
    'patternId': patternId,
    'payload': JSON.stringify(payloadObj)
};