- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 12-11-2020 02:54 AM
Hi Folks,
This article is to identify which commands a particular pattern runs in discovery. Though this is available in SNOW docs, but is not very specific or in detail.
This would be another additional way to check quickly running a script on the instance and you can see what all commands used in a particular pattern.
Target Audience
Anyone using Discovery product using Patterns on their SNOW instance.
Overview
This script covers only the command that are part of standard "Parse Command Output" operation in pattern and doesn't have capability to fetch commands run from EVAL Scripts or any other code blocks.
You can get the list of commands for one particular pattern by giving the pattern sys id in the variable "args" in below script. You can get the sys id of the pattern quickly by right clicking on the pattern record and click "Copy sys_id".
Script Output also prints the Scope of the respective pattern which tells you whether it is part of family release or Store Release.
If Scope is Global it is a family release and if it is "Discovery and Service Mapping Patterns" it is indeed the Patterns store app name. If you have patterns from other store apps respective name gets displayed.
To run this script in your instance open "System Definition->Scripts-Background" in filter navigator and copy this script and click Run script.
Note: If the main pattern sys id is provided, it fetches the shared libraries and extension sections that are part of the pattern and lists commands from all of them.
Ex: Below is the sample output for a simple shared library.
*** Script: *********************************************************************************************************
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: Pattern[Shared Library]: Linux - Network Scope: Global
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: Command: "uname -r |cut -d'.' -f1"
*** Script: Command: "ifconfig -a | awk '{line=$0} {printf \"%s\",line!=\"\"?line:\"\\n\"}'"
*** Script: Command: "ip route list | awk ' /^default/ {print $3}'"
*** Script: Command: "ip -4 -o addr show | awk '{print $2,$4}'"
*** Script: Command: "ip -6 -o a| awk '{print $2,$4}'"
*** Script: Command: "ip=$(ip -6 -o addr show | awk '{print $4}' | tail -1 | cut -d'/' -f2);i=0;mask='';full_octets=$(($ip/8));partial_octet=$(($ip%8));for ((i=0;i<4;i+=1)); do if [ $i -lt $full_octets ]; then mask+=255; elif [ $i -eq $full_octets ]; then mask+=$((256 - 2**(8-$partial_octet))); else mask+=0; fi; test $i -lt 3 && mask+=. ;done;echo $mask;"
*** Script: Command: "route -n"
*** Script: Command: "ip r"
*** Script: Command: "ip r"
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: *********************************************************************************************************
Script
gs.log('*********************************************************************************************************');
var args = '';
PatternRelations(args, false);
ParsePatternExtensions(args);
gs.log('*********************************************************************************************************');
function ParsePatternExtensions(parPattern) {
var gPatternExtRec = new GlideRecord('sa_pattern_extension');
gPatternExtRec.addQuery('pattern', parPattern);
gPatternExtRec.query();
var totalExts = gPatternExtRec.getRowCount();
if (totalExts != 0) {
gs.log('Available Pattern Extensions are ' + totalExts);
while (gPatternExtRec.next()) {
var extSysID = gPatternExtRec.getValue('extension');
PatternRelations(extSysID, true);
}
}
}
function PatternRelations(patternSysId, isLib) {
var gPatternRec = new GlideRecord('sa_pattern');
gPatternRec.addQuery('sys_id', patternSysId);
if (isLib)
gPatternRec.addQuery('ndl', 'CONTAINS', 'runcmd_to_var').addOrCondition('ndl', 'CONTAINS', 'refid');
gPatternRec.query();
if (gPatternRec.getRowCount() != 0) {
if (gPatternRec.next()) {
gs.log('-----------------------------------------------------------------------------------------------------');
var pattern_name = gPatternRec.getValue('name');
var pattern_text = gPatternRec.getValue('ndl');
var pattern_type = gPatternRec.getValue('cpattern_type');
var sysscope_Ref = gPatternRec.sys_scope.getRefRecord();
var sys_scope = sysscope_Ref.getValue('name');
if (pattern_type != '2')
gs.log('Pattern: ' + pattern_name + ' Scope: ' + sys_scope);
else
gs.log('Pattern[Shared Library]: ' + pattern_name + ' Scope: ' + sys_scope);
gs.log('-----------------------------------------------------------------------------------------------------');
var pattern_steps = pattern_text.split('step'); // Fetches all steps of pattern
for (var index in pattern_steps) {
if (pattern_steps[index].contains('runcmd_to_var')) { // Steps that run a command is only of our interest
var command = '';
var index1 = pattern_steps[index].indexOf('cmd = ') + 6;
var index2 = pattern_steps[index].lastIndexOf('var_names') - 1;
command = pattern_steps[index].substring(index1, index2);
if (command.length > 0)
gs.log('Command: ' + command);
}
if (pattern_steps[index].contains('refid = ')) {
var code_lines = pattern_steps[index].split('\n');
for (var idx in code_lines) { // Making a recursive call to parse the shared library that came across in sequence.
if (code_lines[idx].contains('refid')) {
var index1 = code_lines[idx].indexOf('"') + 1;
var index2 = code_lines[idx].lastIndexOf('"');
libSysID = code_lines[idx].substring(index1, index2);
PatternRelations(libSysID, true);
}
}
}
}
gs.log('-----------------------------------------------------------------------------------------------------\n');
}
}
}
- 1,825 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
You can check this advanced blog to list more no of operations that are run in a pattern.