Name

sn_em_ai.EvtMgmtEmEvent

Description

No description available

Script

var EvtMgmtEmEvent = Class.create();
EvtMgmtEmEvent.prototype = {
  initialize: function(event) {
  	
  	this.arrayUtil = new global.ArrayUtil();
  	
  	this.CONSTS = {
  		TABLE: 'em_event',
  		SEVERITY: {
  			CLEAR: "0",
  			OK: "5",
  			WARNING: "4",
  			MINOR: "3",
  			MAJOR: "2",
  			CRITICAL: "1",
  			values: ["0", "1", "2", "3", "4", "5"]
  		},
  		STATE: {
  			READY: 'Ready',
  			PROCESSED: 'Processed',
  			IGNORED: 'Ignored',
  			ERROR: 'Error'
  		},
  		RESOLUTION_STATE: {
  			NEW: 'New',
  			CLOSING: 'Closing'
  		},
  		CLASSIFICATION: {
  			IT: "0",
  			SECURITY: "1",
  			METRIC: "2",
  			ANOMALY: "3",
  			values: ["0", "1", "2", "3"]
  		}
  	};

  	this.fields = {};
  	
  	if (!event || (typeof event !== 'object') || event instanceof Array) {
  		event = {};
  	}
  	this.construct(event);
  	
  }, // end initialize

  type: 'EvtMgmtEmEvent',
  
  /**
   * EmEvent constructor.
   * @event - Object: An object that describes the new event.
   *   Any key in the object that is not directly mapped into em_event field will be saved in the event's additional info field.
   */
  construct: function(event) {
  	this.setDescription(event.description);
  	this.setMetricName(event.metric_name);
  	this.setMessageKey(event.message_key);
  	this.setSource(event.source);
  	this.setType(event.type);
  	this.setResource(event.resource);
  	this.setTimeOfEvent(event.time_of_event);
  	this.setEventClass(event.event_class);
  	this.setCmdbCi(event.cmdb_ci);
  	this.setSeverity(event.severity);
  	this.setAdditionalInfo(event.additional_info);
  	this.setResolutionState(event.resolution_state);
  	this.setSysDomain(event.domain);
  	this.setNode(event.node);
  	this.setCiType(event.ci_type);
  	this.setCiIdentifier(event.ci_identifier);
  	this.setBucket(event.bucket);
  	
  	// Remove fields that are under the event's meta data and are not desired to be saved in additional info
  	if (event.hasOwnProperty('meta_data')) {
  		delete event.meta_data;
  	}
  	
  	// Save in "event.additional_info" field any other field that is on the event object, but not in the em_event schema
  	for (var key in event) {
  		// Add key-value to "additional info" only if it not part of em_event schema
  		if (event.hasOwnProperty(key) && this.fields.hasOwnProperty(key) === false) {
  			this.addToAdditionalInfo(key, event[key]);
  		}
  	} 
  },
  
  /**
   * Creates a new event in em_event.
   */
  createEvent: function() {
  	if (this.hasOwnProperty('fields')) {
  		var fieldValue;
  		var gr = new GlideRecord(this.CONSTS.TABLE);
  		gr.initialize();
  		
  		for (var field in this.fields) {
  			if (this.fields.hasOwnProperty(field) && (typeof this.fields[field] !== 'undefined')) {
  				fieldValue = this.fields[field];
  				gr.setValue(field, fieldValue);
  			}
  		}

  		gr.insert();
  	}
  },
  
  
  /** Setters / Getters **/
  
  getTableConsts: function() {
  	return this.CONSTS;
  },

  setTimeOfEvent: function(timeOfEvent) {
  	this.fields.time_of_event = String(timeOfEvent) || new GlideDateTime().getValue();
  },

  getTimeOfEvent: function() {
  	return this.fields.time_of_event;
  },
  
  setBucket: function(bucket) {
  	bucket = parseInt(Number(bucket), 10);
  	this.fields.bucket = isNaN(bucket) ? undefined : bucket;
  },
  
  getBucket: function(bucket) {
  	return this.fields.bucket;
  },

  setSeverity: function(severity) {
  	if (Number(severity) && this.arrayUtil.contains(this.CONSTS.SEVERITY.values, severity)) {
  		// MUST be saved as a STRING, not a number!
  		// parseInt so "3.0" will become 3, then convert to String, to save as "3"
  		this.fields.severity = String(parseInt(severity, 10));
  	}
  	else if (typeof severity === 'string' && this.CONSTS.SEVERITY.hasOwnProperty(severity.toUpperCase())) {
  		// The given severity argument is a string
  		this.fields.severity = this.CONSTS.SEVERITY[severity.toUpperCase()];
  	} else {
  		// Default value
  		this.fields.severity = this.CONSTS.SEVERITY.CLEAR;
  	}
  },

  getSeverity: function() {
  	return this.fields.severity;
  },

  setResolutionState: function(resolutionState) {
  	if (typeof resolutionState === 'string' && this.CONSTS.RESOLUTION_STATE.hasOwnProperty(resolutionState.toUpperCase())) {
  		this.fields.resolution_state = this.CONSTS.RESOLUTION_STATE[resolutionState.toUpperCase()];
  	} else {
  		// Default value
  		this.fields.resolution_state = this.CONSTS.RESOLUTION_STATE.NEW;
  	}
  },

  getResolutionState: function() {
  	return this.fields.resolution_state;
  },

  setCmdbCi: function(cmdb_ci) {
  	this.fields.cmdb_ci = cmdb_ci ? String(cmdb_ci) : undefined;
  },

  getCmdbCi: function() {
  	return this.fields.cmdb_ci;
  },

  setNode: function(node) {
  	this.fields.node = node ? String(node) : undefined;
  },

  getNode: function() {
  	return this.fields.node;
  },

  setSysDomain: function(sysDomain) {
  	this.fields.sys_domain = sysDomain ? String(sysDomain) : undefined;
  },

  getSysDomain: function() {
  	return this.fields.sys_domain;
  },
  
  addToAdditionalInfo: function(key, value) {
  	if (key && value && String(key)) {
  		var additionalInfo = {};
  		try {
  			additionalInfo = this.getAdditionalInfo();
  			additionalInfo[String(key)] = String(value);
  		} catch (e) {
  			// Revert back to original additional info on failure
  			additionalInfo = this.getAdditionalInfo();
  		} finally {
  			this.setAdditionalInfo(additionalInfo);
  		}
  	}
  },

  setAdditionalInfo: function(additionalInfo) {
  	try {
  		if (additionalInfo) {
  			// JSON.stringify can fail on circular reference or for BigInt value
  			additionalInfo = JSON.stringify(additionalInfo);
  		}
  	} catch (e) {
  		additionalInfo = undefined;
  	} finally {
  		this.fields.additional_info = additionalInfo;	
  	}
  },

  getAdditionalInfo: function() {
  	return this.fields.additional_info ? JSON.parse(this.fields.additional_info) : {};
  },

  setDescription: function(description) {
  	this.fields.description = description ? String(description) : undefined;
  },

  getDescription: function() {
  	return this.fields.description ;
  },

  setResource: function(resource) {
  	this.fields.resource = resource ? String(resource) : undefined;
  },

  getResource: function() {
  	return this.fields.resource;
  },

  setCiType: function(ciType) {
  	this.fields.ci_type = ciType ? String(ciType) : undefined;
  },

  getCiType: function() {
  	return this.fields.ci_type;
  },

  setCiIdentifier: function(ciIdentifier) {
  	this.fields.ci_identifier = ciIdentifier ? String(ciIdentifier) : undefined;
  },

  getCiIdentifier: function() {
  	return this.fields.ci_identifier;
  },

  setMetricName: function(metricName) {
  	this.fields.metric_name = metricName ? String(metricName) : undefined;
  },

  getMetricName: function() {
  	return this.fields.metric_name;
  },

  setMessageKey: function(messageKey) {
  	this.fields.message_key = messageKey ? String(messageKey) : undefined;
  },

  getMessageKey: function() {
  	return this.fields.message_key;
  },

  setSource: function(source) {
  	this.fields.source = source ? String(source) : undefined;
  },

  getSource: function() {
  	return this.fields.source;
  },

  setType: function(type) {
  	this.fields.type = type ? String(type) : undefined;
  },

  getType: function() {
  	return this.fields.type;
  },

  setEventClass: function(eventClass) {
  	this.fields.event_class = eventClass ? String(eventClass) : undefined;
  },

  getEventClass: function() {
  	return this.fields.event_class;
  },
};

Sys ID

2c0f19d0b74310107c038229ce11a99f

Offical Documentation

Official Docs: