Name
global.FixPatternsModelBasic
Description
Abstract method holder for migrate probe to pattern scripts
Script
var FixPatternsModelBasic = Class.create();
FixPatternsModelBasic.prototype = Object.extendsObject(DiscoveryMigrateToPatterns, {
INSERT : "insert",
UPDATE : "update",
REL_TABLE : "cmdb_rel_ci",
CHUNK_SIZE : 50,
batchUtil : new SNC.BatchCommandsUtil(),
jsonArr : [],
routeFlag : false,
updateMac : false,
classifierCheck : true,
dscyTables : ['dscy_route_next_hop', 'dscy_route_interface'],
scriptNames : {
WINDOWS: 'Windows Migration',
UNIX: 'Unix Migration',
NETWORK: 'Network Migration',
LB: 'Load Balancer Migration',
APP: 'Application Migration',
},
addRelationToCi : function(tableName, fieldName, hostClassName, relType) {
var tableGr = new GlideRecord(tableName);
if (hostClassName)
tableGr.addQuery(fieldName + '.sys_class_name', 'IN', hostClassName);
else
tableGr.addJoinQuery("cmdb_ci_computer", fieldName, "sys_id").addCondition('os', 'CONTAINS', 'windows');
tableGr.query();
while (tableGr.next()) {
var child = tableGr.getValue('sys_id');
var parent = tableGr.getValue(fieldName);
this.addRelation(parent, child, relType);
}
},
addIpAddress : function(hostClassName) {
var adapterGr = new GlideRecord('cmdb_ci_network_adapter');
if (hostClassName)
adapterGr.addQuery('cmdb_ci.sys_class_name', hostClassName);
else
adapterGr.addJoinQuery("cmdb_ci_computer", "cmdb_ci", "sys_id").addCondition('os', 'CONTAINS', 'windows');
adapterGr.query();
while (adapterGr.next()) {
var hostSysID = adapterGr.getValue('cmdb_ci');
var adapterSysId = adapterGr.getValue('sys_id');
var ipAddressGr = new GlideRecord('cmdb_ci_ip_address');
ipAddressGr.addQuery('nic', adapterSysId);
ipAddressGr.query();
while(ipAddressGr.next()) {
var ipAddrSysId = ipAddressGr.getValue('sys_id');
this.addRelation(hostSysID, ipAddrSysId, this.OWNS);
}
}
},
addRelation : function(parent, child, relationType) {
var jsObj = {};
if (parent != null && child != null && relationType != null) {
jsObj['parent'] = parent;
jsObj['child'] = child;
jsObj['type'] = relationType;
this.jsonArr.push(jsObj);
this.saveRelationsToCMDBRelCI();
}
},
flipRelationship : function(parentClass, childClass, relationType) {
var grRel = new GlideRecord(this.REL_TABLE);
grRel.addQuery('parent.sys_class_name', parentClass);
grRel.addQuery('child.sys_class_name', childClass);
grRel.addQuery('type', relationType);
grRel.query();
while (grRel.next()) {
var jsObj = {};
var parent = grRel.getValue('parent');
var child = grRel.getValue('child');
var sysId = grRel.getValue('sys_id');
jsObj.parent = child;
jsObj.child = parent;
jsObj.type = relationType;
jsObj.sys_id = sysId;
this.jsonArr.push(jsObj);
if (this.jsonArr.length >= this.CHUNK_SIZE)
this.batchUpdateRecords(this.REL_TABLE);
}
this.batchUpdateRecords(this.REL_TABLE);
},
flipLayeredRelationship : function(parentClass, childClass, relationType, ciClass, parentField, childField) {
var grParent = new GlideRecord(parentClass);
if (ciClass)
grParent.addQuery(parentField + '.sys_class_name', ciClass);
else
grParent.addJoinQuery("cmdb_ci_computer", parentField ,"sys_id").addCondition('os', 'CONTAINS', 'windows');
grParent.query();
while (grParent.next()) {
var grRel = new GlideRecord(this.REL_TABLE);
grRel.addQuery('parent', grParent.sys_id + '');
grRel.addQuery('child.sys_class_name', childClass);
grRel.addQuery('type', relationType);
grRel.query();
while (grRel.next()) {
var grChild = new GlideRecord(childClass);
grChild.get(grRel.child + '');
if (grChild.getValue(childField) == grParent.getValue(parentField)) {
var jsObj = {};
var parent = grRel.getValue('parent');
var child = grRel.getValue('child');
var sysId = grRel.getValue('sys_id');
jsObj.parent = child;
jsObj.child = parent;
jsObj.type = relationType;
jsObj.sys_id = sysId;
this.jsonArr.push(jsObj);
if (this.jsonArr.length >= this.CHUNK_SIZE)
this.batchUpdateRecords(this.REL_TABLE);
}
}
}
this.batchUpdateRecords(this.REL_TABLE);
},
changeRelationship : function(parentClass, childClass, relationTypeOld, relationTypeNew) {
var grRel = new GlideRecord(this.REL_TABLE);
grRel.addQuery('parent.sys_class_name', parentClass);
grRel.addQuery('child.sys_class_name', childClass);
grRel.addQuery('type', relationTypeOld);
grRel.query();
while (grRel.next()) {
var jsObj = {};
var parent = grRel.getValue('parent');
var child = grRel.getValue('child');
var sysId = grRel.getValue('sys_id');
jsObj.parent = parent;
jsObj.child = child;
jsObj.type = relationTypeNew;
jsObj.sys_id = sysId;
this.jsonArr.push(jsObj);
if (this.jsonArr.length >= this.CHUNK_SIZE)
this.batchUpdateRecords(this.REL_TABLE);
}
this.batchUpdateRecords(this.REL_TABLE);
},
updateFileSystems : function(hostClassName) {
var jsObj = {};
var table = "cmdb_ci_file_system";
var driveLetter;
var fileSystem = new GlideRecord(table);
if (hostClassName)
fileSystem.addQuery('computer.sys_class_name', 'IN', hostClassName);
else
fileSystem.addJoinQuery("cmdb_ci_computer", "computer", "sys_id").addCondition('os', 'CONTAINS', 'windows');
fileSystem.query();
while (fileSystem.next()) {
if ((fileSystem.computer.os.match(/2012|2016|2019/) || parseFloat(fileSystem.computer.os_version) > 6.2) && fileSystem.computer.sys_class_name != 'cmdb_ci_hyper_v_server') {
// Use Windows - Storage 2012 format (ex. C)
driveLetter = /(\w+):\\/.exec(fileSystem.name);
} else {
// Use Windows - Storage 2008 format (ex. C:)
driveLetter = /(\w+:)\\/.exec(fileSystem.name);
}
if (driveLetter) {
jsObj = {};
jsObj.name = driveLetter[1];
jsObj.source = fileSystem.getValue('computer');
jsObj.sys_id = fileSystem.getValue('sys_id');
this.jsonArr.push(jsObj);
if (this.jsonArr.length >= this.CHUNK_SIZE)
this.batchUpdateRecords(table);
}
}
this.batchUpdateRecords(table);
},
fixDestIpNetwork : function(hostClassName) {
for (var i in this.dscyTables) {
var table = this.dscyTables[i];
var routeTable = new GlideMultipleUpdate(table);
if (hostClassName)
routeTable.addQuery('cmdb_ci.sys_class_name', 'IN', hostClassName);
else
routeTable.addJoinQuery("cmdb_ci_computer", 'cmdb_ci', "sys_id").addCondition('os', 'CONTAINS', 'windows');
routeTable.changeValue('dest_ip_network', '0.0.0.0/0.0.0.0', '0.0.0.0/0');
}
},
updateRouteInterfaceName : function(hostClassName, field) {
var jsObj = {};
var table = 'dscy_route_interface';
var newName, tempName;
this.routeFlag = true;
var dscyRouteTable = new GlideRecord(table);
if (hostClassName)
dscyRouteTable.addQuery('cmdb_ci.sys_class_name', 'IN', hostClassName);
else
dscyRouteTable.addJoinQuery("cmdb_ci_computer", 'cmdb_ci', "sys_id").addCondition('os', 'CONTAINS', 'windows');
dscyRouteTable.addQuery('name', '');
dscyRouteTable.addQuery('install_status', '!=', 100);
dscyRouteTable.query();
while (dscyRouteTable.next()) {
newName = '';
switch (field) {
case 'router_interface':
newName = dscyRouteTable.router_interface.name;
break;
case 'dest_ip_network':
tempName = dscyRouteTable.getValue('dest_ip_network');
if (tempName);
newName = tempName + "[Direct]";
break;
case 'short_description':
var riTable = new GlideRecord('dscy_router_interface');
riTable.get(dscyRouteTable.getValue('router_interface'));
if (!riTable.isValidRecord())
break;
var naTable = new GlideRecord('cmdb_ci_network_adapter');
naTable.addQuery('ip_address', riTable.getValue('ip_address'));
naTable.addQuery('cmdb_ci', riTable.getValue('cmdb_ci'));
naTable.addQuery('install_status', '!=', 100);
naTable.query();
if (naTable.next()) {
tempName = naTable.getValue('short_description');
if (tempName)
newName = tempName.substring(tempName.indexOf(']') + 1).trim();
}
break;
default:
}
if (newName && newName != '') {
jsObj = {};
jsObj.name = newName;
jsObj.source = dscyRouteTable.getValue('cmdb_ci');
jsObj.altSource = dscyRouteTable.getValue('dest_ip_network');
jsObj.altSourceInterface = dscyRouteTable.getValue('router_interface');
jsObj.sys_id = dscyRouteTable.getValue('sys_id');
this.jsonArr.push(jsObj);
if (this.jsonArr.length >= this.CHUNK_SIZE)
this.batchUpdateRecords(table);
}
}
this.batchUpdateRecords(table);
this.routeFlag = false;
},
updateRouteInterfaceMacAddress : function(hostClassName) {
var jsObj = {};
var table = 'dscy_route_interface';
this.routeFlag = true;
this.updateMac = true;
var dscyRouteTable = new GlideRecord(table);
if (hostClassName)
dscyRouteTable.addQuery('cmdb_ci.sys_class_name', 'IN', hostClassName);
else
dscyRouteTable.addJoinQuery("cmdb_ci_computer", 'cmdb_ci', "sys_id").addCondition('os', 'CONTAINS', 'windows');
dscyRouteTable.addQuery('install_status', '!=', 100);
dscyRouteTable.query();
while (dscyRouteTable.next()) {
var riTable = new GlideRecord('dscy_router_interface');
riTable.get(dscyRouteTable.getValue('router_interface'));
if (!riTable.isValidRecord())
continue;
var dscyMac = dscyRouteTable.mac_address || '';
var riMac = riTable.mac_address || '';
if (dscyMac == riMac)
continue;
jsObj = {};
jsObj.name = riTable.getValue('mac_address');
jsObj.source = dscyRouteTable.getValue('cmdb_ci');
jsObj.altSource = dscyRouteTable.getValue('dest_ip_network');
jsObj.altSourceInterface = dscyRouteTable.getValue('router_interface');
jsObj.sys_id = dscyRouteTable.getValue('sys_id');
this.jsonArr.push(jsObj);
if (this.jsonArr.length >= this.CHUNK_SIZE)
this.batchUpdateRecords(table);
}
this.batchUpdateRecords(table);
this.routeFlag = false;
this.updateMac = false;
},
checkForExistingRecordsInCmdbRelCI : function(operation) {
var candidates = {};
var deleteList = [];
var encodedQuery;
var relGr = new GlideRecord(this.REL_TABLE);
for (var i = 0; i < this.jsonArr.length; i++) {
relGr.initialize();
var parent = this.jsonArr[i].parent;
var child = this.jsonArr[i].child;
var type = this.jsonArr[i].type;
candidates[parent + child + type] = this.jsonArr[i];
relGr.addQuery('parent', parent);
relGr.addQuery('child',child);
relGr.addQuery('type',type);
if (!encodedQuery)
encodedQuery = relGr.getEncodedQuery();
else
encodedQuery += "^NQ" + relGr.getEncodedQuery();
}
relGr.initialize();
relGr.addEncodedQuery(encodedQuery);
relGr.query();
while (relGr.next()) {
var parentVal = relGr.getValue('parent');
var childVal = relGr.getValue('child');
var typeVal = relGr.getValue('type');
var key = parentVal + childVal + typeVal;
if (operation == this.UPDATE)
deleteList.push(candidates[key].sys_id);
delete candidates[key];
}
this.deleteDuplicateRecords(deleteList, this.REL_TABLE);
this.resetArray(candidates);
},
checkForExistingRecordsInRouteTable : function(operation, tableName) {
var candidates = {};
var deleteList = [];
var fieldName, key, encodedQuery;
var tableGr = new GlideRecord(tableName);
if (this.updateMac)
fieldName = 'mac_address';
else
fieldName = 'name';
for (var i = 0; i < this.jsonArr.length; i++) {
tableGr.initialize();
var name = this.jsonArr[i].name;
var source = this.jsonArr[i].source;
var altSource = this.jsonArr[i].altSource;
var altSourceInterface = this.jsonArr[i].altSourceInterface;
candidates[name + source + altSource + altSourceInterface] = this.jsonArr[i];
tableGr.addQuery(fieldName, name);
tableGr.addQuery('cmdb_ci', source);
tableGr.addQuery('dest_ip_network', altSource);
tableGr.addQuery('router_interface', altSourceInterface);
if (!encodedQuery)
encodedQuery = tableGr.getEncodedQuery();
else
encodedQuery += "^NQ" + tableGr.getEncodedQuery();
}
tableGr.initialize();
tableGr.addEncodedQuery(encodedQuery);
tableGr.query();
while (tableGr.next()) {
var nameVal = tableGr.getValue(fieldName);
var sourceVal = tableGr.getValue('cmdb_ci');
var altRefVal = tableGr.getValue('dest_ip_network');
var altRefInterfaceVal = tableGr.getValue('router_interface');
key = nameVal + sourceVal + altRefVal + altRefInterfaceVal;
if (operation == this.UPDATE)
deleteList.push(candidates[key].sys_id);
delete candidates[key];
}
this.deleteDuplicateRecords(deleteList, tableName);
this.resetArray(candidates);
},
checkForExistingRecordsInTable : function(operation, tableName) {
var candidates = {};
var deleteList = [];
var key, fieldRef, encodedQuery;
var tableToField = { cmdb_ci_lvm_pool_member: 'pool', cmdb_ci_dns_name: 'ip_address' };
fieldRef = tableToField[tableName] || 'computer';
var tableGr = new GlideRecord(tableName);
for (var i = 0; i < this.jsonArr.length; i++) {
tableGr.initialize();
var name = this.jsonArr[i].name;
var source = this.jsonArr[i].source;
candidates[name + source] = this.jsonArr[i];
tableGr.addQuery('name', name);
tableGr.addQuery(fieldRef, source);
if (!encodedQuery)
encodedQuery = tableGr.getEncodedQuery();
else
encodedQuery += "^NQ" + tableGr.getEncodedQuery();
}
tableGr.initialize();
tableGr.addEncodedQuery(encodedQuery);
tableGr.query();
while (tableGr.next()) {
var nameVal = tableGr.getValue('name');
var sourceVal = tableGr.getValue(fieldRef);
key = nameVal + sourceVal;
if (operation == this.UPDATE)
deleteList.push(candidates[key].sys_id);
delete candidates[key];
}
this.deleteDuplicateRecords(deleteList, tableName);
this.resetArray(candidates);
},
deleteDuplicateRecords : function(list, table) {
if (list.length > 0) {
var tableGrDelete = new GlideMultipleDelete(table);
tableGrDelete.addQuery('sys_id', 'IN', list);
tableGrDelete.execute();
this.infoMsg("Deleted batch of " + tableGrDelete.getUpdateCount() + " " + table + " records");
}
},
resetArray : function(candidateList) {
this.jsonArr.length = 0;
this.jsonArr = Object.keys(candidateList).filter(function(key4Insert) {
return candidateList[key4Insert];
}).map(function(key4Insert) { return candidateList[key4Insert]; });
},
saveRelationsToCMDBRelCI : function() {
if (this.jsonArr.length > this.CHUNK_SIZE) {
this.checkForExistingRecordsInCmdbRelCI(this.INSERT);
var count = this.batchUtil.batchInsertMultiple(JSON.stringify(this.jsonArr), this.REL_TABLE, '');
this.infoMsg("Created batch of " + count + " relationships");
this.jsonArr.length = 0;
}
},
saveLeftOversRelationsToCMDBRelCI : function() {
if (this.jsonArr.length > 0) {
this.checkForExistingRecordsInCmdbRelCI(this.INSERT);
var count = this.batchUtil.batchInsertMultiple(JSON.stringify(this.jsonArr), this.REL_TABLE, '');
this.infoMsg("Created batch of " + count + " relationships");
this.jsonArr.length = 0;
}
},
batchUpdateRecords : function(tableName) {
if (this.jsonArr.length > 0) {
if (tableName == this.REL_TABLE)
this.checkForExistingRecordsInCmdbRelCI(this.UPDATE);
else if (this.routeFlag)
this.checkForExistingRecordsInRouteTable(this.UPDATE, tableName);
else
this.checkForExistingRecordsInTable(this.UPDATE, tableName);
var count = this.updateMultipleObjects(this.jsonArr, tableName, '');
this.infoMsg("Updated batch of " + count + " " + tableName + " records");
this.jsonArr.length = 0;
}
},
updateMultipleObjects : function(jsonArr, tableName, domainId) {
var updateRecordsCount = 0;
var key, dbUpdate, jsonObject, fieldValue;
for (var i = 0; i < jsonArr.length; i++) {
dbUpdate = new GlideDBUpdate(tableName);
jsonObject = jsonArr[i];
for (key in jsonObject) {
if (key == "source" || key.startsWith("altSource") || jsonObject[key] == null)
continue;
fieldValue = jsonObject[key].toString();
if (key == "sys_id")
dbUpdate.setPrimaryKeyValue(fieldValue);
else if (key == "name" && this.updateMac)
dbUpdate.setValue("mac_address", fieldValue);
else
dbUpdate.setValue(key, fieldValue);
}
dbUpdate.execute();
updateRecordsCount++;
}
return updateRecordsCount;
},
addNetworkAdapter : function(hostClassName) {
this.addRelationToCi('cmdb_ci_network_adapter', 'cmdb_ci', hostClassName, this.OWNS);
},
addFileSystem : function(hostClassName) {
this.addRelationToCi('cmdb_ci_file_system', 'computer', hostClassName, this.CONTAINS);
},
addStorage : function(hostClassName) {
this.addRelationToCi('cmdb_ci_storage_device', 'computer', hostClassName, this.CONTAINS);
},
addMemoryModule : function(hostClassName) {
this.addRelationToCi('cmdb_ci_memory_module', 'cmdb_ci', hostClassName, this.CONTAINS);
},
addStoragePool : function(hostClassName) {
this.addRelationToCi('cmdb_ci_storage_pool', 'hosted_by', hostClassName, this.CONTAINS);
},
addDiskPartitions : function(hostClassName) {
this.addRelationToCi('cmdb_ci_disk_partition', 'computer', hostClassName, this.CONTAINS);
},
addExitInterfaceRule : function(hostClassName) {
this.addRelationToCi('dscy_route_interface', 'cmdb_ci', hostClassName, this.USES);
},
addNextHopRule : function(hostClassName) {
this.addRelationToCi('dscy_route_next_hop', 'cmdb_ci', hostClassName, this.USES);
},
addRouterInterface : function(hostClassName) {
this.addRelationToCi('dscy_router_interface', 'cmdb_ci', hostClassName, this.USES);
},
addHBA : function(hostClassName) {
this.addRelationToCi('cmdb_ci_storage_hba', 'computer', hostClassName, this.OWNS);
},
addFiberChannel : function(hostClassName) {
this.addRelationToCi('cmdb_ci_fc_port', 'computer', hostClassName, this.OWNS);
},
addPatches : function(hostClassName) {
this.addRelationToCi('cmdb_ci_patches', 'cmdb_ci', hostClassName, this.OWNS);
},
addHyperVComponentsToPool: function(hostClassName) {
this.addRelationToCi('cmdb_ci_hyper_v_rpool_comp', 'resource_pool', hostClassName, this.CONTAINS);
},
addSwitchPartitions : function(hostClassName) {
this.addRelationToCi('dscy_swtch_partition', 'cmdb_ci', hostClassName, this.CONTAINS);
},
addSwitchPort : function(hostClassName) {
this.addRelationToCi('dscy_switchport', 'cmdb_ci', hostClassName, this.CONTAINS);
},
addSwitchForwardingRule : function(hostClassName) {
this.addRelationToCi('dscy_swtch_fwd_rule', 'cmdb_ci', hostClassName, this.CONTAINS);
},
addVlans : function(hostClassName) {
this.addRelationToCi('cmdb_ci_lb_vlan', 'load_balancer', hostClassName, this.OWNS);
},
addNetworkInterface : function(hostClassName) {
this.addRelationToCi('cmdb_ci_lb_interface', 'load_balancer', hostClassName, this.OWNS);
},
addPools : function(hostClassName) {
this.addRelationToCi('cmdb_ci_lb_pool','load_balancer', hostClassName, this.OWNS);
},
addPoolMember : function(hostClassName) {
var tableGr = new GlideRecord('cmdb_ci_lb_pool');
tableGr.addQuery('load_balancer.sys_class_name', hostClassName);
tableGr.query();
while (tableGr.next()) {
var poolID = tableGr.getValue('sys_id');
var poolMemGr = new GlideRecord('cmdb_ci_lb_pool_member');
poolMemGr.addQuery('pool', poolID);
poolMemGr.query();
while (poolMemGr.next()) {
var poolMemberID = poolMemGr.getValue('sys_id');
this.addRelation(poolID, poolMemberID, this.OWNS);
}
}
},
updateDNSIPAddresses : function() {
var jsObj = {};
var ipDnsGr = new GlideRecord('cmdb_ip_address_dns_name');
var ipGr = new GlideRecord('cmdb_ci_ip_address');
var table = 'cmdb_ci_dns_name';
var dnsGr = new GlideRecord(table);
dnsGr.addNullQuery('ip_address');
dnsGr.query();
while (dnsGr.next()) {
ipDnsGr.initialize();
ipDnsGr.addQuery('dns_name', dnsGr.getValue('sys_id'));
ipDnsGr.addNotNullQuery('ip_address');
ipDnsGr.query();
while (ipDnsGr.next()) {
ipGr.initialize();
ipGr.get('sys_id', ipDnsGr.getValue('ip_address'));
var ip = ipGr.getValue('ip_address');
if (ip && ipGr.getValue('install_status') != 100) {
jsObj = {};
jsObj.name = dnsGr.getValue('name');
jsObj.source = ip;
jsObj.ip_address = ip;
jsObj.sys_id = dnsGr.getValue('sys_id');
this.jsonArr.push(jsObj);
break;
}
}
if (this.jsonArr.length >= this.CHUNK_SIZE)
this.batchUpdateRecords(table);
}
this.batchUpdateRecords(table);
},
quickPrerequisiteCheck : function() {
this.MIGRATE = true;
this.classifierCheck = true;
this.recordArray = [];
this.horizontalProbeCheck(this.MIGRATE_SOURCE);
this.relationshipTypeCheck(this.MIGRATE_SOURCE);
this.scriptIncludeCheck(this.MIGRATE_SOURCE);
},
msToTime : function (duration) {
var milliseconds = parseInt((duration%1000)/100);
var seconds = parseInt((duration/1000)%60);
var minutes = parseInt((duration/(1000*60))%60);
var hours = parseInt((duration/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
},
getStepsCompleted : function() {
var logGr = new GlideRecord('probe_to_pattern_log');
if (this.logSysId && logGr.get('sys_id', this.logSysId) && logGr.status == "In Progress")
return Number(logGr.steps_completed);
return 0;
},
updateStepsCompleted : function(stepsCompleted) {
var logGr = new GlideRecord('probe_to_pattern_log');
if (this.logSysId && logGr.get('sys_id', this.logSysId)) {
logGr.steps_completed = stepsCompleted;
logGr.update();
}
},
type: 'FixPatternsModelBasic'
});
Sys ID
a1794bd09f2313004deb91aec32e7085