Name
sn_itom_pde.PANParsePatternForCommands
Description
No description available
Script
var PANParsePatternForCommands = Class.create();
PANParsePatternForCommands.prototype = {
initialize: function() {
this.pattern_table = 'sa_pattern';
this.command_list_table = 'pd_command_list';
this.pattern_to_shared_libraries_table = 'pd_pattern_to_shared_library_mapping';
this.os_class = '';
},
getOSClass: function(sys_id) {
this.parseOSClass(sys_id);
return this.os_class + '';
},
setOSClass: function(os_class) {
this.os_class = os_class;
},
parseOSClass: function(rec) {
var ndl = '';
var step = '';
var appl_os_class = '';
var pattern = new GlideRecord('sa_pattern');
if (global.JSUtil.notNil(rec)) {
pattern.addQuery('sys_id', rec);
}
pattern.addQuery('active', true);
pattern.query();
while (pattern.next()) {
ndl = pattern.ndl;
var lines = ndl.split('\n');
if (pattern.cpattern_type == 3) {
this.os_class = pattern.ci_type;
return;
}
if (pattern.cpattern_type == 1) {
lines.forEach(function(line) {
//Grab applicable OS Class
if (line.match(/apply_to_os_families = ".*"/gi)) {
var os_class = line.split('apply_to_os_families =');
appl_os_class = os_class[1].trim();
}
});
}
if (appl_os_class != '') {
this.os_class = appl_os_class;
return;
}
}
},
parsePattern: function(rec) {
//initialize
var newcmd = '';
var newname = '';
var step = '';
var condition = '';
var cmd = '';
var ndl = '';
var name = '';
var snmp_tab_oid = '';
var isShell = '';
var isSnmpScalar = '';
var isSnmpTabular = '';
var isWmi = '';
var namespace = '';
var wmiQuery = '';
var insideSNMPQuery = 'out';
var commandType = '';
var isDisabled = false;
//set os class from this.os_class
var appl_os_class = this.os_class;
//check if pattern is valid and active
var pattern = new GlideRecord(this.pattern_table);
if (global.JSUtil.notNil(rec)) {
pattern.addQuery('sys_id', rec);
}
pattern.addQuery('active', true);
pattern.query();
while (pattern.next()) {
ndl = pattern.ndl;
var lines = ndl.split('\n');
var bracketCounter = 0;
for (i in lines) {
var line = lines[i];
if (bracketCounter < 0)
bracketCounter = 0;
if (line.indexOf('{') > 0) {
bracketCounter++;
}
if (line.indexOf('step') > 0) {
step = 'in';
}
if(line.includes("disabled = \"true\"") && step == "in"){
isDisabled = true;
}
if (line.indexOf('condition =') > 0) {
condition = 'in';
}
if (line.indexOf('run_snmp_to_var') > 0) {
insideSNMPQuery = 'in';
}
if (line.indexOf('}') >= 0) {
--bracketCounter;
if (bracketCounter == 1)
step = 'out';
if (!gs.nil(newcmd)) {
gs.debug('CVT: Name: ' + newname);
gs.debug('CVT: Command: ' + newcmd);
//Insert or update only if the step is not disabled
if(!isDisabled)
this.insertOrUpdateCommand(pattern, newname, newcmd, appl_os_class, commandType, condition);
//reset all the variables
newname = '';
newcmd = '';
namespace = '';
wmiQuery = '';
insideSNMPQuery = 'out';
snmp_tab_oid = '';
commandType = '';
condition = '';
isDisabled = false;
}
}
// Grab names from steps
if (line.match(/name = ".*"/gi) && step == 'in' && newcmd == '' && insideSNMPQuery == 'out') {
name = line.split('=');
newname = name[1].trim();
newname = newname.replace(/"/g, '');
}
// Grab actual command Calls
if (line.match(/cmd = ".*"/gi) && step == 'in') {
cmd = line.split('cmd =');
newcmd = cmd[1].trim();
newcmd = newcmd.replace(/"/g, '');
commandType = 'shell';
}
//Grab WMI query: namespace
if (line.match(/namespace = ".*"/gi) && step == 'in') {
var space = line.split('namespace =');
namespace = space[1].trim();
namespace = namespace.replace(/\\\\/g, '\\');
if (namespace.indexOf('\\') != 1) {
var res = namespace.split("\"");
namespace = '\\' + res[1];
namespace = "\"" + namespace + "\"";
}
gs.debug("CVT: namespace is :" + namespace);
}
//Grab WMI query: query and build newcmd
if (line.match(/query = ".*"/gi) && step == 'in') {
var query = line.split('query =');
wmiQuery = query[1].trim();
gs.debug("CVT : Query is:" + query);
if (namespace != '' && wmiQuery != '') {
newcmd = namespace + " " + wmiQuery;
commandType = 'wmi';
}
}
//Grab SNMP query scalar
if (line.match(/snmp_object_ids = ".*"/gi) && step == 'in' && newcmd == '' && insideSNMPQuery == 'in') {
cmd = line.split('=');
newcmd = cmd[1].trim();
newcmd = newcmd.replace(/"/g, '');
commandType = 'snmp';
}
//Grab SNMP query tabular oid
if (line.match(/name = ".*"/gi) && step == 'in' && newcmd == '' && insideSNMPQuery == 'in') {
cmd = line.split('=');
snmp_tab_oid = cmd[1].trim();
snmp_tab_oid = snmp_tab_oid.replace(/"/g, '');
}
//Grab SNMP query tabular oid columns
if (line.match(/col_names = ".*"/gi) && step == 'in' && snmp_tab_oid != '' && insideSNMPQuery == 'in') {
cmd = line.split('=');
var col = cmd[1].trim();
col = col.replace(/"/g, '');
newcmd = snmp_tab_oid + " " + col;
commandType = 'snmp';
}
}
}
},
//The below logic will make sure that we have only one command record for concerned steps in shared libraries, which are used in multiple patterns.
//One records with comma separted applicable os class.
insertOrUpdateCommand: function(pattern, newname, newcmd, appl_os_class, commandType, condition) {
var command_list = new GlideRecord('pd_command_list');
command_list.addQuery('pattern', pattern.sys_id);
command_list.addQuery('step_name', newname);
command_list.addQuery('command', newcmd);
command_list.addQuery('ci_type', pattern.ci_type);
command_list.query();
if (command_list.next()) {
if (global.JSUtil.nil(command_list.applicable_os_class))
command_list.applicable_os_class = appl_os_class;
else {
if (!command_list.applicable_os_class.includes(appl_os_class))
command_list.applicable_os_class = command_list.applicable_os_class + ',' + appl_os_class;
}
command_list.is_outdated = false;
command_list.update();
} else {
command_list.initialize();
command_list.pattern = pattern.sys_id;
command_list.step_name = newname;
command_list.ci_type = pattern.ci_type;
command_list.command = newcmd;
command_list.command_type = commandType;
command_list.applicable_os_class = appl_os_class;
command_list.is_outdated = false;
if (condition == 'in' && newname.toLowerCase().includes('failover'))
command_list.is_failover_step = true;
else
command_list.is_failover_step = false;
command_list.insert();
}
},
type: 'PANParsePatternForCommands'
};
Sys ID
fda21ea0475a51507cb2e977746d4321