Name

global.TrackedFileHelper

Description

Helper script include for tracked configuration files

Script

var TrackedFileHelper = Class.create();
TrackedFileHelper.prototype = {
  initialize: function() {
  	this.table_name = 'sa_tracked_file_definition';
  },

  /**
   * Returns all tracked file definitions as an array of JSON objects.
   *
   * @param activeOnly - if true, returns only active definitions.
   * @param fields - string list of fields to return. All fields are returned when null or undefined. No field
   * returned when empty.
   * @param displayValue - if true, returns the display value of fields (for example: name instead of sys_id)
   */
  getAllTrackedFileDefinitions: function(activeOnly, fields, displayValue) {
  	if (activeOnly &&
  		gs.getProperty('glide.discovery.enable_file_tracking', 'false') == 'false') {
  		// In case the feature is disabled, all tracked file definitions are considered inactive
  		return JSON.stringify([]);
  	}

  	if (fields == undefined) {
  		var td = GlideTableDescriptor.get(this.table_name);
  		fields = td.getActiveFieldNames();
  	}

  	var gr = new GlideRecord(this.table_name);
  	if (activeOnly)
  		gr.addQuery('active', true);

  	var results = [];
  	gr.query();
  	while (gr.next()) {
  		var record = {};
  		for (var i = 0 ; i < fields.size() ; i++) {
  			var fieldName = fields.get(i);
  			record[fieldName] = getValueOrDisplayValue(gr, fieldName, displayValue);
  		}

  		results.push(record);
  	}

  	return JSON.stringify(results);
  },

  // Notify MID Servers of changes in Tracked File Definition
  notifyMIDsOnChange : function() {
  	gs.log('######  Tracked File Definition changed  ######');
  	new MIDNotificationHandler().notifyMIDServers('trackedFileDefinitionChanged');
  },

  /**
   * Customer optional logic for getting tracked configuration file CIs by host sys_id and parent
   * CI type, used when fetching files for the MID Server's payload.
   *
   * @return - a list of configuration file CI sys_ids
   */
  getTrackedConfigFileCis : function(hostSysId, parentCiType) {
  	return null;
  },

  /**
   * Processing logic for the payload coming from the MID Server.
   * Called from HorizontalDiscoverySensor.
   */
  processPayload : function(payload) {
  	if (gs.getProperty('glide.discovery.enable_file_tracking', 'false') == 'false')
  		return payload;

  	var payloadObj = JSON.parse(payload);
  	var items = payloadObj.items;
  	for (var i = 0 ; i < items.length ; i++) {
  		var item = items[i];
  		if (GlideDBObjectManager.get().isInstanceOf(item.className, 'cmdb_ci_config_file_tracked')) {
  			var values = item.values;
  			if (values.tracking_status == '2') {
  				values.content = gs.getMessage("File content is not saved by definition");
  			} else if (values.tracking_status == '3') {
  				values.content = gs.getMessage("File size exceeds the maximum defined limit");
  			}
  		}
  	}

  	return JSON.stringify(payloadObj);
  },

  type: 'TrackedFileHelper'
};

function getValueOrDisplayValue(gr, fieldName, displayValue) {
  if (displayValue) {
  	// This is an ugly WA, to solve DEF0083085
  	// displayValue is required for other fields as well (i.e. "pattern"), for future parsing.
  	// Hence, boolean String value ("true" / "false") for "save_content" field is enforced to return in English
  	// so parsing of this value won't be influenced by language
  	if (fieldName == "save_content") {
  		return (gr.getValue(fieldName) == "1")? "true": "false";
  	}
  	else
  		return gr.getDisplayValue(fieldName);
  }
  else
  	return gr.getValue(fieldName);
}

Sys ID

3a4d9f2ac3caa200adce9624a1d3ae7c

Offical Documentation

Official Docs: