Name

global.CreatePatternForGenericApplication

Description

Create classifier, horizontal probe + pattern for generic application, and create empty application NDL

Script

var CreatePatternForGenericApplication = Class.create();

CreatePatternForGenericApplication.prototype = Object.extendsObject(AbstractAjaxProcessor,{
  
  HORIZONTAL_PROBE_NAME : "HorizontalDiscoveryProbe-Horizontal Patt",
  HORIZONTAL_PROBE_ID : "4f64c6389f230200fe2ab0aec32e7068",
  EXPLORATION_PHASE : "exploration",
  ACTIVE : "1",
  APPLICATION : "1",
  SUCCESS : "Success",//Should not translate this is checked and expected in the UI JS validation
  
  getCurrentScopeName : function() {
  	return gs.getCurrentApplicationScope();
  },
  
  synchCiWithMid : function() {
  	var ptl = new PatternLibrary();
  	ptl.notifyMIDsCitChanged();
  	return this.SUCCESS;
  },
  
  isMultipleProcesses : function() {
  	var hostId = SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_host_id'), null);
  	var processName =  SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_process_name'), null);
  	var runningProcessCommand =  SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_running_process_command'), null);
  	var epIds =  SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_endpoints_ids'), null);
  	
  	var inParams = "hostId[" +hostId+ "], processName[" + processName + "], runningProcessCommand[" +runningProcessCommand + "], epIds[" + epIds +"]";
  	/* gs.print( "@@@@@ with in isMultipleProcesses -" + inParams); */
  	
  	var errMessage;
  	
  	if (!hostId || !epIds || !processName || !runningProcessCommand ) {
  		errMessage = gs.getMessage('Error: isMultipleProcesses- expect to get {0}', inParams);
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	/* Retrieve the ports of all endtry points of the GA */
  	var arrayIds = epIds.split();
  	var endPointGR = new GlideRecord('cmdb_ci_endpoint');
  	endPointGR.addQuery('sys_id', "IN", arrayIds);
  	endPointGR.query();
  	
  	var ports = [];
  	while (endPointGR.next()) {
  		ports.push(':'+endPointGR.port+':');
  	}
  	
  	/* Extract the target process that should comply with the processName+porunningProcessCommandrt */
  	var processGr = new GlideRecord('cmdb_running_process');
  	processGr.addQuery('computer', hostId);

  	var portsQuery = null;
  	if (ports.length > 0){
  		portsQuery = processGr.addQuery('listening_on', "CONTAINS", ports[0]);
  	} else {
  		errMessage = gs.getMessage('Error: isMultipleProcesses- no port was found');
  		this.errorMsg(errMessage);
  		return errMessage;	
  	}
  	
  	for (var i = 1; i < ports.length; i++) {
  		portsQuery.addOrCondition('listening_on', "CONTAINS", ports[i]);
  	}
  	
  	processGr.query();
  	while (processGr.next()) {
  		if (processGr.name.trim() !== processName.trim() || processGr.command.trim() !== runningProcessCommand.trim() ){
  			return this.SUCCESS;	
  		} 
  	} 
  	
  	return "GA is not a multi process, child context replaced with parent";
  },
  
  validateNoExistNameForCiTypeAndPatternAndClassification : function() {
  	
  	var patternName = SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_pattern_name'), null);
  	var classifierName =  SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_classifier_name'), null);
  	var tableName =  SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_table_name'), null);
  	var errMessage;
  		
  	if (gs.tableExists(tableName)) {
  		errMessage = gs.getMessage('Error: There is already existing table with the name: {0}', [tableName]);
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	var patterGr = new GlideRecord('sa_pattern');
  	patterGr.addQuery('name', patternName);
  	patterGr.query();
  	if (patterGr.next()) {
  		errMessage = gs.getMessage('Error: There is already pattern with this name: {0}', [patternName]);
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	var classificationGr = new GlideRecord('discovery_classy_proc');
  	classificationGr.addQuery('name', classifierName);
  	classificationGr.query();
  	if (classificationGr.next()) {
  		errMessage = gs.getMessage('Error: There is already process classifier with this name: {0}', [classifierName]);
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	return this.SUCCESS;
  },
  
  clientProcess : function() {

  	var classifierName = SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_classifier_name'), null);
  	var table = SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_table'), null);
  	var condition = this.getParameter('sysparm_condition'); //Condition can contains special chars so do not sanitize
  	var relType = SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_rel_type'),null);
  	var patternName = SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_pattern_name'), null);
  	var defaultIdentificationSectionName = SNC.GlideHTMLSanitizer.sanitize(this.getParameter('sysparm_default_identification_section_name'),null);
  
  	//create pattern
  	var patternVal = this.createPattern(patternName,table, defaultIdentificationSectionName);
  	if (patternVal.indexOf('Error:') != -1) {
  		return patternVal;
  	}
  	else if (GlideStringUtil.nil(patternVal)) {
  		return gs.getMessage('Error: Failed to create pattern');
  	}
  	
  		
  	//create classifier
  	var classifierVal = this.createClassifier(classifierName, table, condition, relType);
  	
  	if (GlideStringUtil.notNil(classifierVal) && classifierVal.indexOf('Error:') == -1) {
  		return this.createHorizontalProbe(patternVal, classifierVal);	
  	}
  	
  	return classifierVal;
  },
  
  createClassifier : function(name, table, condition, rel_type) {
  	
  	var errMessage;
  	
  	var classificationGr = new GlideRecord('discovery_classy_proc');
  	classificationGr.addQuery('name', name);
  	classificationGr.query();
  	if (classificationGr.next()) {
  		errMessage = gs.getMessage('Error: Duplicate process classifier name: {0}', [name]);
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	if (GlideStringUtil.nil(name)) {
  		errMessage = gs.getMessage('Error: The classifier name must not be empty');
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	if (GlideStringUtil.nil(table)) {
  		errMessage = gs.getMessage('Error: The table must not be not empty');
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	var tableGr = new GlideRecord(table);
  	if (!tableGr.isValid()) {
  		errMessage = gs.getMessage('Error: There is no table {0}', [table]);
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	if (GlideStringUtil.nil(condition)) {
  		errMessage = gs.getMessage('Error: The condition must not be empty');
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	var relGr = new GlideRecord('cmdb_rel_type');
  	relGr.addQuery('sys_id', rel_type);
  	relGr.query();
  	if (!relGr.next()) {
  		errMessage = gs.getMessage('Error: There is no relation type with sys ID: {0}', [rel_type]);
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	classificationGr.initialize();
  	classificationGr.setValue('name',name);
  	classificationGr.setValue('table', table);
  	classificationGr.setValue('condition', condition.trim());
  	classificationGr.setValue('relation_type', rel_type);
  	classificationGr.setValue('active', this.ACTIVE);
  	classificationGr.setValue('order',10000);
  	return classificationGr.update();
  	
  },
  
  createHorizontalProbe : function(patternID, classificationSysID) {
  	var probeGr = new GlideRecord('discovery_classifier_probe');
  	probeGr.setValue('name', this.HORIZONTAL_PROBE_NAME);
  	probeGr.setValue('child',this.HORIZONTAL_PROBE_ID);
  	probeGr.setValue('classy', classificationSysID);
  	probeGr.setValue('phase', this.EXPLORATION_PHASE);
  	probeGr.setValue('active', this.ACTIVE);
  	probeGr.setValue('pattern',patternID);
  	probeGr.update();
  	return this.SUCCESS;
  },
  
  createEmptyNDL : function(name, ciType, defaultIdentificationSectionName, patternGr) {
  	var errMessage = gs.getMessage('Error: ');
  	if (GlideStringUtil.nil(defaultIdentificationSectionName)) {
  		errMessage += gs.getMessage('The default identification section cannot be empty');
  		this.errorMsg(errMessage);
  		return errMessage;
  	}
  	
  	var gr = new GlideRecord(ciType);
  	if (gr.isValid()) {
  		var patternLibrary = new SNC.GlidePatternLibrary();
  		var ndl = patternLibrary.createEmptyNdlForGenericApplication(name, ciType, defaultIdentificationSectionName,  patternGr);
  		return ndl;
  	}
  	errMessage += gs.getMessage('There is no CI Type table: {0}', [ciType]);
  	this.errorMsg(errMessage);
  	return errMessage;
  },
  
  createPattern : function(name, ciType, defaultIdentificationSectionName) {
  	var patternGr = new GlideRecord('sa_pattern');
  	var ndl = this.createEmptyNDL(name,ciType, defaultIdentificationSectionName,patternGr);
  	if (ndl.indexOf('Error:') != -1) {
  		return ndl;
  	}
  	patternGr.setValue('ndl',ndl);
  	patternGr.setValue('name', name);
  	patternGr.setValue('ci_type', ciType);
  	patternGr.setValue('cpattern_type', this.APPLICATION);
  	patternGr.setValue('enforce_proc_classy', 'true');
  	return patternGr.update();
  },
  
  errorMsg : function(msg) {
  	gs.error(msg);
  },
  
  type: 'CreatePatternForGenericApplication'
});

Sys ID

90cf9cf69f200300579e91aec32e701d

Offical Documentation

Official Docs: