Name
sn_agent.MonitoringSync
Description
No description available
Script
var MonitoringSync = Class.create();
MonitoringSync.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
NO_AGENT: "No agent",
//define the database view for http entrypoints
ENTRYPOINT_DB_VIEW: 'sn_itmon_http_entrypoint',
addTestResult: function() {
var checkId = this.getParameter('sysparm_check_id');
var checkDef = this.getParameter('sysparm_check_def');
var ci = this.getParameter('sysparm_ci');
var tableName = this.getParameter('sysparm_table_name');
var proxyAgent = this.getParameter('sysparm_agent_proxy');
var credentialId = this.getParameter('sysparm_cred_id');
var credentialsAliasId = this.getParameter('sysparm_cred_alias_id');
var command = this.getParameter('sysparm_command');
var policyId = this.getParameter('sysparm_policy_sys_id');
var dbViewPrefix = this.getParameter('sysparm_prefix');
return this.internalAddTestResults(checkId, checkDef, ci, tableName, proxyAgent, credentialId, credentialsAliasId, command, policyId, dbViewPrefix);
},
// This function is for dependency injection; to use directly in order to run a check and get the test result record id.
// Currently used locally in this class and in tests: in file: CheckInstanceExecutor.java
internalAddTestResults: function(checkId, checkDef, ci, tableName, proxyAgent, credentialId, credentialsAliasId, command, policyId, dbViewPrefix) {
var agentSysId = "";
if (dbViewPrefix != "") {
var dbViewDetails = this.getDBViewDetails(ci, tableName, dbViewPrefix);
ci = dbViewDetails["ciSysId"];
tableName = dbViewDetails["ciClassName"];
agentSysId = dbViewDetails["agentSysId"];
}
var clientParams = this.getClientParams(policyId, ci, proxyAgent, tableName, command, checkId);
if (proxyAgent) {
agentSysId = proxyAgent;
} else if (dbViewPrefix == "") {
var agentIds = Object.keys(clientParams);
if (agentIds.length > 0) {
agentSysId = this.getAgentSysIdByAgentId(agentIds[0]);
} else {
return this.NO_AGENT;
}
}
var sysIdField = tableName == this.ENTRYPOINT_DB_VIEW ? "endpoint_sys_id" : "sys_id";
var cis = new GlideRecord(tableName);
cis.addQuery(sysIdField, ci);
var testResultRecordId = this.addTestResultRecord(checkId, checkDef, agentSysId);
var agentHandler = new AgentNowHandler();
var testResultJson = this.buildTestResultJson(checkId, checkDef, testResultRecordId, credentialId, credentialsAliasId);
var requestId = agentHandler.runCheck(cis, testResultJson, agentHandler.getPolicyPerMid(clientParams), 0);
if (gs.nil(requestId)) {
gs.error("nil request id returned by agent handler for test check run");
return null;
}
var testResultGr = new GlideRecord("sn_agent_test_result");
if (!testResultGr.get(testResultRecordId)) {
gs.error("error getting test result record");
return null;
}
testResultGr.setValue("agent_request", requestId);
testResultGr.update();
return testResultRecordId;
},
addTestResultRecord: function(checkId, checkDef, agentSysId) {
var gr = new GlideRecord("sn_agent_test_result");
if (checkDef === "true") {
gr.setValue("check_def", checkId);
} else {
gr.setValue("check_instance", checkId);
}
gr.setValue("agent", agentSysId);
gr.setValue("status", "1");
return gr.insert();
},
buildTestResultJson: function(checkId, checkDef, recordId, credentialId, credentialsAliasId) {
var check = this.getCheckJson(checkId, checkDef, credentialId, credentialsAliasId);
check.testResultId = recordId;
return check;
},
getCheckJson: function(checkId, checkDef, credentialId, credentialsAliasId) {
var gr;
if (checkDef === "true") {
gr = new GlideRecord("sn_agent_check_def");
} else {
gr = new GlideRecord("sn_agent_check");
}
var aliasApplicativeId = "";
if (credentialsAliasId) {
var aliasRecord = new GlideRecord("sys_alias");
aliasRecord.get(credentialsAliasId);
aliasApplicativeId = aliasRecord.getValue("id");
}
if (gr.get(checkId)) {
var accUtils = new ACCConfigUtils();
var checkJson = accUtils.getCheckJson(gr, credentialId, aliasApplicativeId);
checkJson['check_type_id'] = "a32f261967133300b7b72dbd2685eff9";
return checkJson;
}
return {};
},
getPayload: function(value) {
var payload = new XMLDocument2();
var element = payload.createElementWithTextValue("output", value);
return payload.toString();
},
checkAgentStatus: function() {
var ciSysId = this.getParameter('sysparm_ci');
var agentGr = new GlideRecord("sn_agent_cmdb_ci_agent");
agentGr.get(ciSysId);
return agentGr.agent_extended_info.status;
},
checkProgress: function() {
var testResultId = this.getParameter('sysparm_test_result');
var gr = new GlideRecord('sn_agent_test_result');
gr.addQuery('sys_id', testResultId);
gr.query();
if (!gr.next())
return "error";
var status = gr.getValue("status");
if (status == "3") //error
return gr.getValue("error_msg");
return status;
},
getTimeout: function() {
var checkId = this.getParameter('sysparm_check_id');
var timeout = 0;
var gr = new GlideRecord('sn_agent_check_def');
if (gr.get('sys_id', checkId)) {
var result = parseInt(gr.getValue("timeout"));
if (!isNaN(result)) {
timeout = result;
}
}
if (GlidePluginManager.isActive('com.sn_itom_cloud_services')) {
var testId = this.getParameter('sysparm_test_id');
var testGr = new GlideRecord('sn_agent_test_result');
if (testGr.get('sys_id', testId)) {
var extendedInfoId = testGr.getValue('agent');
var extendedInfoGr = new GlideRecord('sn_agent_ci_extended_info');
if (extendedInfoGr.get('sys_id', extendedInfoId)) {
if (global.JSUtil.getBooleanValue(extendedInfoGr, "use_cloud_services")) {
timeout += parseInt(gs.getProperty('sn_agent.agent_cloud_services_timeout', 120));
}
}
}
}
return "" + timeout;
},
getOutput: function() {
var testResultId = this.getParameter('sysparm_test_result');
var gr = new GlideRecord('sn_agent_test_result');
gr.addQuery('sys_id', testResultId);
gr.query();
var res = {};
if (gr.next()) {
res.check_status = gr.getValue("check_status");
res.output = gr.getValue("output");
return JSON.stringify(res);
}
return JSON.stringify(res);
},
updateTestResultError: function() {
var testResultId = this.getParameter('sysparm_test_result');
var errorMsg = "Timeout - no response from MID... Validate MID is up and try again";
var gr = new GlideRecord('sn_agent_test_result');
gr.addQuery('sys_id', testResultId);
gr.query();
if (gr.next()) {
gr.setValue("status", "3");
gr.setValue("error_msg", errorMsg);
gr.update();
}
return errorMsg;
},
getPolicyDetails: function() {
var isProxy = false;
var policyId = this.getParameter('sysparm_policy_id');
var checkId = this.getParameter('sysparm_check_id');
var gr = new GlideRecord("sn_agent_policy");
if (gr.get(policyId)) {
if (gr.single_proxy_agent || gr.proxy_advanced || gr.proxy_script_advanced || gr.proxy_cluster) //if proxy, policy uses proxy functionality
isProxy = true;
}
var result = {};
if (gr.monitored_ci_type_script) {
var evaluator = new GlideScopedEvaluator();
var monitoredCis = evaluator.evaluateScript(gr, 'monitored_ci_script');
if (monitoredCis == null) {
gs.error("The monitored CI type from script is invalid");
} else {
result.tableName = monitoredCis.getTableName();
result.filter = "" + this.getSysIdsFilter(monitoredCis);
}
} else if (gr.monitored_ci_type_group) {
var helper = new PolicyClientsGeneratorNG();
monitoredCis = helper.getCisFromGroup(gr.monitored_ci_group);
if (monitoredCis == null) {
gs.error("The monitored CI type from group is invalid");
} else {
result.tableName = monitoredCis.getTableName();
result.filter = "" + monitoredCis.getEncodedQuery();
}
} else {
result.tableName = gr.getValue("table");
result.filter = gr.getValue("filter");
}
if (gr.is_service_filter) {
var monitoredCisIds = [];
var monitoredCIsGr = new GlideRecord(result.tableName);
if (result.filter)
monitoredCIsGr.addEncodedQuery(result.filter);
monitoredCIsGr.query();
while (monitoredCIsGr.next()) {
monitoredCisIds.push(monitoredCIsGr.getValue("sys_id"));
}
var monitoredCIsStr = monitoredCisIds.join(",");
if (monitoredCisIds.length > 0) {
var serviceFilterQuery = gr.service_filter;
var serviceRecord = new GlideRecord("cmdb_ci_service");
if (serviceFilterQuery)
serviceRecord.addEncodedQuery(serviceFilterQuery);
var svcCiAssocQuery = serviceRecord.addJoinQuery("svc_ci_assoc", "sys_id", "service_id");
svcCiAssocQuery.addCondition("ci_id", "IN", monitoredCIsStr);
serviceRecord.query();
var serviceIds = [];
while (serviceRecord.next()) {
var serviceId = serviceRecord.getValue("sys_id");
serviceIds.push(serviceId);
}
if (serviceIds.length == 0) {
result.filter = "sys_id=none";
} else {
var filteredCIs = [];
var assocRecord = new GlideRecord("svc_ci_assoc");
assocRecord.addQuery("ci_id", "IN", monitoredCisIds);
assocRecord.addQuery("service_id", "IN", serviceIds);
assocRecord.query();
while (assocRecord.next()) {
filteredCIs.push(assocRecord.getValue("ci_id"));
}
if (filteredCIs.lenght == 0) {
result.filter = "sys_id=none";
} else {
result.filter = "sys_idIN" + filteredCIs.join(",");
}
}
}
}
result.isProxy = isProxy;
result.ciType = this.getCiTypeByTableName(result.tableName);
var credName = gr.getValue("cred_alias");
result.credentialId = MonitoringUtils.getCredIdFromCredName(credName);
result.credentialName = credName;
result.credentialsAliasId = gr.getValue("credential_alias");
result.credentialsAliasName = gr.getDisplayValue("credential_alias");
if (checkId)
result.isCheckUsingSecureParam = this.isCheckUsingSecureParam(checkId, false);
return JSON.stringify(result);
},
getSysIdsFilter: function(monitoredCis) {
var filter = '';
while (monitoredCis.next()) {
filter = filter + monitoredCis.sys_id;
if (monitoredCis.hasNext()) {
filter = filter + ',';
}
}
filter = 'sys_idIN' + filter;
return filter;
},
isCheckUsingSecureParam: function(checkId, isDef) {
var checkSecureParamsGr;
if (isDef) {
checkSecureParamsGr = new GlideRecord("sn_agent_check_secure_param_def");
checkSecureParamsGr.addQuery("check_def", checkId);
} else {
checkSecureParamsGr = new GlideRecord("sn_agent_check_secure_param");
checkSecureParamsGr.addQuery("check", checkId);
}
checkSecureParamsGr.query();
return (checkSecureParamsGr.getRowCount() > 0);
},
getClientParams: function(policyId, ci, proxyAgent, tableName, command, checkId) {
var policyClientsGenerator = new PolicyClientsGeneratorNG();
var sysIdField = tableName == this.ENTRYPOINT_DB_VIEW ? "endpoint_sys_id" : "sys_id";
var clientParams = "";
var tableGr = new GlideRecord(tableName);
tableGr.addQuery(sysIdField, ci);
tableGr.query();
var policyGr = undefined;
if (policyId) {
policyGr = new GlideRecord("sn_agent_policy");
policyGr.addQuery("sys_id", policyId);
policyGr.query();
policyGr.next();
}
clientParams = policyClientsGenerator.getClientsByCisForCheck(policyGr, tableGr, checkId, command, true, proxyAgent);
return clientParams;
},
getCiTypeByGroup: function() {
var checkGroup = this.getParameter('sysparm_check_group');
var checkDefId = this.getParameter('sysparm_check_def_id');
var tableName;
var groupGR = new GlideRecord("sn_agent_check_group");
if (groupGR.get(checkGroup)) {
tableName = groupGR.getValue("ci_type");
} else {
tableName = "cmdb_ci_computer";
}
var result = {};
result.tableName = tableName;
result.ciType = this.getCiTypeByTableName(tableName);
if (checkDefId)
result.isCheckUsingSecureParam = this.isCheckUsingSecureParam(checkDefId, true);
return JSON.stringify(result);
},
getDBViewDetails: function(ci, tableName, prefix) {
var ciSysId = "";
var ciClassName = "";
var agentSysId = "";
var gr = new GlideRecord(tableName);
if (gr.get(ci)) {
ciSysId = gr.getValue(prefix + "sys_id");
ciClassName = gr.getValue(prefix + "sys_class_name");
agentSysId = gr.getValue("agent_sys_id");
}
return {
ciSysId: ciSysId,
ciClassName: ciClassName,
agentSysId: agentSysId
};
},
getCiTypeByTableName: function(tableName) {
var ciType = "";
if (tableName == "sn_agent_cmdb_ci_agent") {
ciType = "agent";
} else {
//now check if its in hardware hierarchy
var baseTables = new GlideTableHierarchy(tableName).getTables();
for (var i = 0; i < baseTables.length; i++) {
var currTable = baseTables[i];
if (currTable == "cmdb_ci_hardware") {
ciType = "host";
break;
}
if (currTable == "cmdb_ci_appl") {
ciType = "app";
break;
}
}
}
return ciType;
},
getAgentSysIdByAgentId: function(agentId) {
var agentSysIs = "";
var agentGr = new GlideRecord("sn_agent_ci_extended_info");
agentGr.addQuery("agent_id", agentId);
agentGr.query();
if (agentGr.next()) {
agentSysIs = agentGr.getUniqueValue();
}
return agentSysIs;
},
type: 'MonitoringSync'
});
Sys ID
2a6223ba539153006dfeddeeff7b1293