- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 11-18-2020 06:07 AM
Hi Folks,
This article is to identify which all CI relationships a particular pattern creates. Though this is available in SNOW docs, but sometimes we might not get for every pattern or would not be able to find one easily.
This would be another additional way to check quickly running a script on the instance and you can see what all relations gets created out of a particular pattern.
Target Audience
Anyone using Discovery product using Patterns on their SNOW instance.
Overview
This script will get you ONLY list of patterns that creates CI relationships as not every pattern will have steps related to relation creation. This script covers only the CI relationships that are created through patterns and not through Pre Post Scripts or any other code blocks.
You can get the CI relationships for one particular pattern by giving the main CI type name in the first argument.
If that one particular pattern is a shared library you have to fill the sys id in 2nd argument. You can get the sys id of the pattern (shared library) quickly by right clicking on the pattern record and click "Copy sys_id".
By default below script gives you output for all patterns.
Script Output also prints the Scope of the respective pattern which tells you whether it is part of family release or Patterns 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.
Ex: Below is the sample output how it looks like for specific scenarios mentioned above. Output format is
Relation name - Parent CI Name & Child CI Name.
1. If a particular pattern is selected with main CI type as first argument
Modify below line in the script with the CI type name.
PatternRelations('cmdb_ci_lb_service', '');
Output:
*** Script: Patterns creating relations are 1 only out of total 730 patterns.
*** Script: *********************************************************************************************************
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: Pattern: Amazon AWS - LB Service (LP) Scope: Discovery and Service Mapping Patterns
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: Hosted on::Hosts - Load Balancer Service & Cloud Load Balancer
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: *********************************************************************************************************
2. If a particular shared library is selected in 2nd argument.
Modify below line in the script with the sys id of shared library or extension section.
PatternRelations('', 'd879b24c1b2d90907e0e31901a4bcb45');
Output:
*** Script: Patterns creating relations are 1 only out of total 730 patterns.
*** Script: *********************************************************************************************************
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: Pattern: Get Cassandra Cluster and Cluster Nodes - Unix Scope: Discovery and Service Mapping Patterns
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: Hosted on::Hosts - Cassandra Keyspace & Cassandra Cluster
*** Script: Cluster of::Cluster - Cassandra Cluster Node & Cassandra Cluster
*** Script: Hosted on::Hosts - Cassandra Instance & Cassandra Cluster Node
*** Script: -----------------------------------------------------------------------------------------------------
*** Script: *********************************************************************************************************
Similar output can be seen listing all patterns if script run with both arguments being empty.
Imp Notes
- This script [Script 2] will not intelligently consider all the extensions or shared libraries if exists for a pattern. So in order to get that you need to individually find those using sys id of shared libraries and extensions as mentioned above.
- I'm working on this and will post the updated script which I will handle this as well. - Completed and available in Script 1.
- Ensure that either both arguments are empty or one of it being empty as mentioned in above examples. Avoid running script filling both arguments
Script 1: Provide the sys id of the main pattern [Not shared library].
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', 'relation_type').addOrCondition('ndl', 'CONTAINS', 'refid');
gPatternRec.query();
if (gPatternRec.getRowCount() != 0) {
while (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('relation_reference') || pattern_steps[index].contains('refid')) { // Steps that create relation is only of our interest
var code_lines = pattern_steps[index].split('\n');
var table1_name = '', table2_name = '', relation_type = '';
// Parsing the relation information from the step.
for (var idx in code_lines) {
if (code_lines[idx].contains('table1_name')) {
var index1 = code_lines[idx].indexOf('"') + 1;
var index2 = code_lines[idx].lastIndexOf('"');
table1_name = code_lines[idx].substring(index1, index2);
}
if (code_lines[idx].contains('table2_name')) {
var index1 = code_lines[idx].indexOf('"') + 1;
var index2 = code_lines[idx].lastIndexOf('"');
table2_name = code_lines[idx].substring(index1, index2);
}
if (code_lines[idx].contains('relation_type')) {
var index1 = code_lines[idx].indexOf('"') + 1;
var index2 = code_lines[idx].lastIndexOf('"');
relation_type = code_lines[idx].substring(index1, index2);
}
if (code_lines[idx].contains('refid')) { // Making a recursive call to parse the shared library that came across in sequence.
var index1 = code_lines[idx].indexOf('"') + 1;
var index2 = code_lines[idx].lastIndexOf('"');
libSysID = code_lines[idx].substring(index1, index2);
PatternRelations(libSysID, true);
}
}
if (relation_type.length > 0)
gs.log(relation_type + ' - ' + GetLabel(table1_name) + ' & ' + GetLabel(table2_name));
}
}
gs.log('-----------------------------------------------------------------------------------------------------\n');
}
}
}
function GetLabel(tableName) {
var label = tableName;
var vCIGR = new GlideRecord('sys_db_object');
vCIGR.addQuery('name', tableName);
vCIGR.query();
if (vCIGR.next())
var label = vCIGR.getValue('label');
return label;
}
Script 2:
PatternRelations('', '');
function PatternRelations(tableName, libSysId) {
var gPatternRec = new GlideRecord('sa_pattern');
// Only steps which have 'relation_type' makes relation b/w CIs, so filtering out other patterns.
gPatternRec.addQuery('ndl', 'CONTAINS', 'relation_type');
if (tableName)
gPatternRec.addQuery('ci_type', tableName);
if (libSysId)
gPatternRec.addQuery('sys_id', libSysId);
gPatternRec.query();
var gTotalPatterns = new GlideRecord('sa_pattern');
gTotalPatterns.query();
if (gPatternRec.getRowCount() != 0) {
gs.log("Patterns creating relations are " + gPatternRec.getRowCount() + " only out of total " + gTotalPatterns.getRowCount() + " patterns.\n");
gs.log('*********************************************************************************************************');
while (gPatternRec.next()) {
gs.log('-----------------------------------------------------------------------------------------------------');
var pattern_name = gPatternRec.getValue('name');
var pattern_text = gPatternRec.getValue('ndl');
var sysscope_Ref = gPatternRec.sys_scope.getRefRecord();
var sys_scope = sysscope_Ref.getValue('name');
gs.log('Pattern: ' + 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('relation_reference')) { // Steps that create relation is only of our interest
var code_lines = pattern_steps[index].split('\n');
var table1_name = '', table2_name = '', relation_type = '';
// Parsing the relation information from the step.
for (var idx in code_lines) {
if (code_lines[idx].contains('table1_name')) {
var index1 = code_lines[idx].indexOf('"') + 1;
var index2 = code_lines[idx].lastIndexOf('"');
table1_name = code_lines[idx].substring(index1, index2);
}
if (code_lines[idx].contains('table2_name')) {
var index1 = code_lines[idx].indexOf('"') + 1;
var index2 = code_lines[idx].lastIndexOf('"');
table2_name = code_lines[idx].substring(index1, index2);
}
if (code_lines[idx].contains('relation_type')) {
var index1 = code_lines[idx].indexOf('"') + 1;
var index2 = code_lines[idx].lastIndexOf('"');
relation_type = code_lines[idx].substring(index1, index2);
}
}
if (relation_type.length > 0)
gs.log(relation_type + ' - ' + GetLabel(table1_name) + ' & ' + GetLabel(table2_name));
}
}
gs.log('-----------------------------------------------------------------------------------------------------\n');
}
gs.log('*********************************************************************************************************\n\n');
}
else
gs.log('No Results for the query');
}
function GetLabel(tableName) {
var label = tableName;
var vCIGR = new GlideRecord('sys_db_object');
vCIGR.addQuery('name', tableName);
vCIGR.query();
if (vCIGR.next())
var label = vCIGR.getValue('label');
return label;
}
- 949 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Checkout this blog to list commands and operations that are run in a pattern.