Name
sn_agent.PolicyHierarchyDiffUtils
Description
No description available
Script
var PolicyHierarchyDiffUtils = Class.create();
PolicyHierarchyDiffUtils.prototype = {
initialize: function() {
this.CHILD_PROPERTY = "#child_property#";
this.PARENT_PROPERTY = "#parent_property#";
this.FIELDS = "fields";
this.NAME = "name";
this.CHECKS = "checks";
this.PARAMS = "params";
this.SECURE_PARAMS = "secure_params";
},
createMsgStrDiff: function(childId) {
var diff = this.compareChildToParent(childId);
if (Object.keys(diff).length == 0)
return "";
var logStr = "";
var policyFieldsChanges = diff[this.FIELDS];
if (policyFieldsChanges) {
for (var field in policyFieldsChanges) {
logStr += this.getDiffMsg(field, policyFieldsChanges[field]) + "<br/>";
}
}
var checksChanges = diff[this.CHECKS];
if (checksChanges) {
logStr += "<br/>";
logStr += gs.getMessage("Differences on policy's checks :");
for (var checkIdx in checksChanges) {
logStr += "<br/>";
var check = checksChanges[checkIdx];
var differencesOnCheckMsg = gs.getMessage("Differences on the check: {0}", GlideStringUtil.escapeHTML(check[this.NAME]));
logStr += this.getSpace(1) + differencesOnCheckMsg + "<br/>";
var checkFieldsChanges = check[this.FIELDS];
if (checkFieldsChanges) {
for (field in checkFieldsChanges) {
if (field == this.CHILD_PROPERTY) {
logStr += this.getSpace(2) + gs.getMessage("This check and its parameters (if any) exist only on this child policy") + "<br/>";
} else if (field == this.PARENT_PROPERTY) {
logStr += this.getSpace(2) + gs.getMessage("This check and its parameters (if any) exist only on the parent policy") + "<br/>";
} else {
logStr += this.getSpace(2) + this.getDiffMsg(field, checkFieldsChanges[field]) + "<br/>";
}
}
}
var paramsChanges = check[this.PARAMS];
if (paramsChanges) {
logStr += "<br/>";
logStr += this.getSpace(2) + gs.getMessage("Differences on check's parameters:") + "<br/>";
for (var paramIdx in paramsChanges) {
var param = paramsChanges[paramIdx];
var differencesOnParamMsg = gs.getMessage("Differences on the parameter: {0}", GlideStringUtil.escapeHTML(param[this.NAME]));
logStr += this.getSpace(3) + differencesOnParamMsg + "<br/>";
var paramFieldsChanges = param[this.FIELDS];
if (paramFieldsChanges) {
for (field in paramFieldsChanges) {
if (field == this.CHILD_PROPERTY) {
logStr += this.getSpace(4) + gs.getMessage("This parameter exists only on this child policy") + "<br/>";
} else if (field == this.PARENT_PROPERTY) {
logStr += this.getSpace(4) + gs.getMessage("This parameter exists only on the parent policy") + "<br/>";
} else {
logStr += this.getSpace(4) + this.getDiffMsg(field, paramFieldsChanges[field]) + "<br/>";
}
}
}
}
}
var secureParamsChanges = check[this.SECURE_PARAMS];
if (secureParamsChanges) {
logStr += "<br/>";
logStr += this.getSpace(2) + gs.getMessage("Differences on check's secure parameters:") + "<br/>";
for (paramIdx in secureParamsChanges) {
param = secureParamsChanges[paramIdx];
var differencesOnSecureParamMsg = gs.getMessage("Differences on the secure parameter: {0}", GlideStringUtil.escapeHTML(param[this.NAME]));
logStr += this.getSpace(3) + differencesOnSecureParamMsg + "<br/>";
paramFieldsChanges = param[this.FIELDS];
if (paramFieldsChanges) {
for (field in paramFieldsChanges) {
if (field == this.CHILD_PROPERTY) {
logStr += this.getSpace(4) + gs.getMessage("This secure parameter exists only on this child policy") + "<br/>";
} else if (field == this.PARENT_PROPERTY) {
logStr += this.getSpace(4) + gs.getMessage("This secure parameter exists only on the parent policy") + "<br/>";
} else {
logStr += this.getSpace(4) + this.getDiffMsg(field, paramFieldsChanges[field]) + "<br/>";
}
}
}
}
}
}
}
//if we have diff, add prefix to it.
if (logStr)
logStr = gs.getMessage("Differences between this policy and its parent:") + "<br/>" + "<br/>" + logStr;
return logStr;
},
getDiffMsg: function(field, diff) {
return gs.getMessage("Field: '{0}', Child value: {1}, Parent value: {2}", [field, this.adjustPresentationValue(diff.child), this.adjustPresentationValue(diff.parent)]);
},
adjustPresentationValue: function(value) {
if (!value || value == 'null')
return "<-empty-value->";
return "'" + value + "'";
},
getSpace: function(length) {
var result = "";
for (var i = 0; i < length; i++) {
result += "------";
}
return result;
},
compareChildToParent: function(childPolicyId) {
var childPolicyGr = new GlideRecord("sn_agent_policy");
childPolicyGr.get(childPolicyId);
var publishedPolicyId = childPolicyGr.getValue("published_parent");
var policyWrapper = {};
var ignoreFieldPol = this.getMapFromArray(["is_draft", "publish_status", "published_parent", "relationship_state", "checks_str", "order", "name", "related_policy", "inherit_from_parent", "publish_error"]);
var ignoreFieldsCheck = this.getMapFromArray(["is_draft", "published_parent", "monitoring_policy", "published_check"]);
var ignoreFieldsParams = this.getMapFromArray(["is_draft", "check", "published_parent", "published_param"]);
var ignoreFieldsSecureParams = this.getMapFromArray(["is_draft", "check", "published_parent", "published_secure_param"]);
var policyDiff = this.compareParentToChildRecord(childPolicyGr, ignoreFieldPol);
if (Object.keys(policyDiff).length > 0)
policyWrapper[this.FIELDS] = policyDiff;
var checks = [];
var checkRecord = new GlideRecord("sn_agent_check");
checkRecord.addQuery("monitoring_policy", childPolicyId);
checkRecord.query();
var checkParentIds = [];
while (checkRecord.next()) {
var checkName = checkRecord.getValue("name");
var checkParentId = checkRecord.getValue("published_parent");
var checkChildId = checkRecord.getValue("sys_id");
var checkDif = this.compareParentToChildRecord(checkRecord, ignoreFieldsCheck);
var paramsDiffArr = [];
var secParamsDiffArr = [];
//search for diff in params only if this check exists also on parent
if (checkParentId) {
checkParentIds.push(checkParentId);
this.handleParams(paramsDiffArr, "sn_agent_check_param", ignoreFieldsParams, checkParentId, checkChildId);
this.handleParams(secParamsDiffArr, "sn_agent_check_secure_param", ignoreFieldsSecureParams, checkParentId, checkChildId);
}
var checkWrapper = {};
if (Object.keys(checkDif).length > 0 || paramsDiffArr.length > 0 || secParamsDiffArr.length > 0) {
checkWrapper[this.NAME] = checkName;
if (paramsDiffArr.length > 0)
checkWrapper[this.PARAMS] = paramsDiffArr;
if (secParamsDiffArr.length > 0)
checkWrapper[this.SECURE_PARAMS] = secParamsDiffArr;
if (Object.keys(checkDif).length > 0)
checkWrapper[this.FIELDS] = checkDif;
checks.push(checkWrapper);
}
}
//look for checks that were created for parent and are missing on child
var publishedChecksGr = new GlideRecord("sn_agent_check");
publishedChecksGr.addQuery("monitoring_policy", publishedPolicyId);
publishedChecksGr.addQuery("sys_id", "NOT IN", checkParentIds);
publishedChecksGr.query();
while (publishedChecksGr.next()) {
checkWrapper = {};
checkWrapper[this.NAME] = publishedChecksGr.getValue("name");
var fields = {};
fields[this.PARENT_PROPERTY] = true;
checkWrapper[this.FIELDS] = fields;
checks.push(checkWrapper);
}
if (checks.length > 0) {
policyWrapper[this.CHECKS] = checks;
}
return policyWrapper;
},
handleParams: function(paramsDiffArr, tableName, ignoreFieldsParams, checkParentId, checkChildId) {
var paramsParentIds = [];
var paramRecord = new GlideRecord(tableName);
paramRecord.addQuery("check", checkChildId);
paramRecord.query();
while (paramRecord.next()) {
var paramsWrapper = {};
var paramName = paramRecord.getValue("name");
var paramParent = paramRecord.getValue("published_parent");
paramsParentIds.push(paramParent);
var paramDif = this.compareParentToChildRecord(paramRecord, ignoreFieldsParams);
if (Object.keys(paramDif).length > 0) {
paramsWrapper[this.NAME] = paramName;
paramsWrapper["fields"] = paramDif;
paramsDiffArr.push(paramsWrapper);
}
}
//look for paramns that were created for parent and are missing on child
var publishedParamsGr = new GlideRecord(tableName);
publishedParamsGr.addQuery("check", checkParentId);
publishedParamsGr.addQuery("sys_id", "NOT IN", paramsParentIds);
publishedParamsGr.query();
paramsWrapper = {};
while (publishedParamsGr.next()) {
paramsWrapper[this.NAME] = publishedParamsGr.getValue("name");
var fields = {};
fields[this.PARENT_PROPERTY] = true;
paramsWrapper[this.FIELDS] = fields;
paramsDiffArr.push(paramsWrapper);
}
},
getMapFromArray: function(array) {
var map = {};
for (var index in array)
map[array[index]] = true;
return map;
},
compareParentToChildRecord: function(childGr, ignoreFieldsMap) {
var diff = {};
var parentId = childGr.getValue("published_parent");
if (parentId) {
var parentGr = new GlideRecord(childGr.getTableName());
parentGr.get(parentId);
for (var field in childGr) {
//check if the field shouldn't be ignored
if (!field.startsWith("sys") && !ignoreFieldsMap[field]) {
//in case of script values, the display value is not a string and contains different non ASCII chars, if not removed script fields will appear different even when look the same
var parentValue = this.removeNonASCIChars(parentGr.getDisplayValue(field));
var childValue = this.removeNonASCIChars(childGr.getDisplayValue(field));
if (parentValue != childValue) {
diff[childGr[field].getLabel()] = {
child: GlideStringUtil.escapeHTML(childValue),
parent: GlideStringUtil.escapeHTML(parentValue)
};
}
}
}
} else {
diff[this.CHILD_PROPERTY] = true;
}
return diff;
},
removeNonASCIChars: function(value) {
return ("" + value).replace(/[^\x20-\x7E]/g, '');
},
type: 'PolicyHierarchyDiffUtils'
};
Sys ID
d243d53eb761a010ff78dc55ce11a9bf