Need help with Regex for Event Rule

0346akashjadhav
Tera Contributor

Hi,

We have requirement to integrate one monitoring tool which will send us Assignment Group and Configuration Items fields as tags in the payload.

"Tags":"ConfigurationItem:XXX, AssignmentGroup:XXX, Host Name Tag:XXX, Application:XXX"

The positions of "Assignment Group" and "ConfigurationItem" fields is not fixed, and they can appear at the start or in middle or at end as well.

Also, the sequence of "AssignmentGroup" & "ConfigurationItem" is not fixed.

So wanted to have regex which will extract these two fields which can be used and mapped to other variables. 

2 REPLIES 2

neerajsharma27
Tera Contributor

Hello @0346akashjadhav ,

 

You want to use Script inlcude, you can use like the following and then you can use this wherever you want in you rplatform

 

var tags = current.u_tags; var confItem = ''; var asmtGroup = '';

 

var ciRegex = /ConfigurationItem:([^,]*)/i; var agRegex = /AssignmentGroup:([^,]*)/i;

var ciMatch = tags.match(ciRegex);

 

if (ciMatch) {
confItem = ciMatch[1].trim();
}

var agMatch = tags.match(agRegex);
if (agMatch) {
asmtGroup = agMatch[1].trim();
}

 

If my answer was helpful, please click the Thumb Icon and mark it as the solution. This way, it can assist others in the future.

Thanks.

Nishant8
Giga Sage

Hello @0346akashjadhav, There might be a better way to do this, but if you know that its going to be a payload in JSON format, then you can walk thru the key and value pair rather applying regEx, something like below:

var payload = {
  "Tags":"ConfigurationItem:ChangeCI, Host Name Tag:XXX, Application:XXX, AssignmentGroup:AbraKaDabra"
};
var tagsArray = payload.Tags.split(',');
var assignmentGroup = null;
var configItem = null;

for (var i = 0; i < tagsArray.length; i++) {
  var tag = tagsArray[i].trim();
  var separatorIndex = tag.indexOf(':');
  if (separatorIndex > -1) {
    var key = tag.substring(0, separatorIndex).trim();
    var value = tag.substring(separatorIndex + 1).trim();
    if (key === "AssignmentGroup") {
      assignmentGroup = value;
    } else if (key === "ConfigurationItem") {
      configItem = value;
    }
  }
}
gs.info("Assignment Group: " + assignmentGroup);
gs.info("Configuration Item: " + configItem);

 

Regards,

Nishant