Name
sn_agent.MidResourceCache
Description
No description available
Script
var MidResourceCache = Class.create();
// MidResourceCache sends a probe to mids, containing a list of resources to cache. The mids will download the resources
// to their filesystem.
MidResourceCache.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
triggerSyncForAllMids: function() {
gs.addInfoMessage(gs.getMessage("Syncing all resources to all MIDs"));
this.triggerSyncForMidName("*");
},
triggerSyncForMidId: function(midId) {
var gr = new GlideRecord("ecc_agent");
if (gr.get(midId)) {
this.triggerSyncForMidName(gr.getValue("name"));
} else {
gs.error("MidResourceCache: couldn't find mid by id");
}
},
triggerSyncForMidName: function(midName) {
gs.info("MidResourceCache: triggerSyncForMidName: " + midName);
var command = "sync_resource_cache";
var id = "sync_resource_cache";
var mid = "mid.server." + midName;
var gr = new GlideRecord("sn_agent_mid_cached_resource");
var resources = [];
gr.addActiveQuery();
gr.query();
while (gr.next()) {
var res = {};
var url = gr.getValue("url");
res.url = this.interpolateSysPropVars(url);
res.url = this.interpolateTernaryOperators(res.url);
res.cacheDir = gr.getValue("cache_directory");
resources.push(res);
}
var payload = new XMLDocument2();
var element = payload.createElementWithTextValue("output", JSON.stringify(resources));
payload = payload.toString();
var priority = 2;
var agentCorrelator = "";
new MonitoringConfig().sendProbe(command, id, mid, payload, priority, agentCorrelator);
},
// Replaces all sys_properties labled with ${sys_prop:} with their value if exists
interpolateSysPropVars: function(text) {
var matches = this.matchAll(text, /\${sys_prop:(.*?)}/g);
var varToValue = {};
for (var i = 0; i < matches.length; i++) {
var groups = matches[i];
var propName = groups[1];
// Special case for dev_mode as it doesn't exist OOB
if (propName == "sn_agent.dev_mode")
varToValue[groups[0]] = gs.getProperty('sn_agent.dev_mode', 'false');
else {
var gr = new GlideRecord("sys_properties");
gr.addQuery("name", propName);
gr.query();
if (gr.next())
varToValue[groups[0]] = gr.getValue("value");
else
gs.error("couldn't find sys prop: " + groups[1]);
}
}
return this.replaceStringWithArrayVals(text, varToValue);
},
// Evaluates and replaces all ternary operators. Currently only supports sys_property evaluations
interpolateTernaryOperators: function(text) {
var matches = this.matchAll(text, /\${(true|false)\s?\?\s?(.*?)\s?:\s?(.*?)}/g);
var varToValue = {};
for (var i = 0; i < matches.length; i++) {
var groups = matches[i];
var bool = groups[1];
varToValue[groups[0]] = bool == "true" ? groups[2] : groups[3];
}
return this.replaceStringWithArrayVals(text, varToValue);
},
// regex must have the global flag set. regex is stateful - it should be recreated for every call
// Returns an array of matches, where each element is an array of groups in the match.
matchAll: function(text, regex) {
var match = regex.exec(text);
var matches = [];
while (match !== null) {
matches.push(match);
match = regex.exec(text);
}
return matches;
},
// replaces all the values in the array within the text according to key
replaceStringWithArrayVals: function(text, arr) {
var answer = text;
for (var key in arr) {
if (arr.hasOwnProperty(key)) {
var value = arr[key];
answer = answer.replaceAll(key, value);
}
}
return answer;
},
type: 'MidResourceCache'
});
Sys ID
93d1eae5773281105e2e08ed7f5a99a7