- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2016 10:37 PM
I am trying to export all upstream and downstream CI's of a specific CI.
E.g. I built a report around the business service: "x", the report filter condition can be: Parent is x OR Child is x, and sort on Parent and Child. The report ran on the CI Relations table. But this only gave me one level up and down.
I need to export multiple level.
How can this be done? Maybe Database Views???
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2016 01:03 AM
I did this once by building a UI Action to point me towards a url from the sys_ids returned from a script include which grabs all of the CIs related.
The UI Action had the script below to call the script include and then redirect the user to that list which then enables them to export it via a normal method.
function showcis(){
var cuc = new CIUtilsrelatedCIs();
var cis = cuc.cisrelatedtoCI(current.sys_id,["all"]);
action.setRedirectURL("cmdb_ci_list.do?sysparm_query=sys_idIN" + cis);
action.setReturnURL(current);
You would need to build a script include which would return the sys_ids of all the CIs related to the ci provided to it, I will see if I still have it in my library as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2016 01:03 AM
I did this once by building a UI Action to point me towards a url from the sys_ids returned from a script include which grabs all of the CIs related.
The UI Action had the script below to call the script include and then redirect the user to that list which then enables them to export it via a normal method.
function showcis(){
var cuc = new CIUtilsrelatedCIs();
var cis = cuc.cisrelatedtoCI(current.sys_id,["all"]);
action.setRedirectURL("cmdb_ci_list.do?sysparm_query=sys_idIN" + cis);
action.setReturnURL(current);
You would need to build a script include which would return the sys_ids of all the CIs related to the ci provided to it, I will see if I still have it in my library as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2016 02:55 AM
Hi mguy,
Thanks for the answer above. could you please also share the script include for the above.
Regards,
Sanjeev
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2016 03:36 AM
Sure some of this code might be a little out of date as I haven't used it for couple of years, so you will need to run through and check it's current version friendly I bascially used a copy of the out of the box CIUtils script and adds some extra functionality to return downstream CIs instead of upstream.
The Utils it takes it's origins from is mentioned here, it does the opposite of this and looks downstream as opposed to upstream (you can use the CIUtils to get the upstream business service etc if you want that as well).
anyway, hope it helps
gs.include('PrototypeServer');
var CIUtilsExtra = Class.create();
CIUtilsExtra.prototype = {
initialize : function() {
this.maxDepth = gs.getProperty('glide.relationship.max_depth',6); // how deep to look
this.currentDepth = 0;
this.CIs = new Object(); // list of affected CIs
this.maxSize = gs.getProperty('glide.relationship.threshold',5000); // how many records to return
this.added = 0; // track how many added, since can't get size() for an Object
},
/**
* Determine which CIs are affected by a specific CI
*
* Inputs:
* id is the sys_id of a configuration item (cmdb_ci)
* classArr is an array of CI class names that should be returned
*
* Returns:
* an array of sys_id values for cmdb_ci records downstream of
* (or affected by) the input item
*/
cisAffectedByCI: function(id, classArr) {
var ci = new GlideRecord('cmdb_ci');
ci.get(id);
//If no class specified then assume business service
if(classArr.toString() == 'all'){
classArr = ['cmdb_ci_server','cmdb_ci_linux_server','cmdb_ci_win_server','cmdb_ci_unix_server','cmdb_ci_esx_server','cmdb_ci_mainframe'];
}
if (classArr.toString() == 'cmdb_ci_server')
{
classArr = ['cmdb_ci_server','cmdb_ci_linux_server','cmdb_ci_win_server','cmdb_ci_unix_server','cmdb_ci_esx_server','cmdb_ci_mainframe'];
}
if (classArr.toString() == 'cmdb_ci_netgear')
{
classArr = ['cmdb_ci_netgear','cmdb_ci_ip_router','cmdb_ci_ip_switch'];
}
if(classArr == ''){
classArr = ['cmdb_ci_server','cmdb_ci_netgear','cmdb_ci_database','cmdb_ci_db_instance'];
for (var ciClass in classArr){
this._addCI(id, this.CIs);
}
}
//If class = 'ALL' then just add the CI
else if(classArr[0] == 'ALL'){
this._addCI(id, this.CIs);
}
else{
for (var ciClass in classArr){
if (ci.sys_class_name == classArr[ciClass]){
this._addCI(id, this.CIs);
}
}
}
this._addParentCIs(id, this.CIs, this.currentDepth, classArr);
var ciarr = new Array(); // CIs is an Object, convert to Array for final query
for (var i in this.CIs)
ciarr.push(i);
return ciarr; // list of affected CIs
},
/**
* Returns an XML-formatted string showing all CIs impacted by an outage to the CI given
*
* Inputs:
* id is the sys_id of the root CI
*
* Returns:
* an XML-formatted string containing cmdb_ci records downstream of
* (or affected by) the configuration item provided as input
*/
getCIXML: function(id) {
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child', id);
gr.query();
gr.next();
var str = '';
str += '<CI>';
str += '<sys_id>'+gr.child.sys_id+'</sys_id>';
str += '<name>'+gr.child.name+'</name>';
str += '<relType>SELF</relType>';
ret = this._recurs(id);
if(ret){
str += '<children>';
str += ret;
str += '</children>';
}
str += '</CI>';
return str;
},
_recurs: function(ci) {
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child', ci);
gr.query();
var str = '';
while(gr.next()){
str += '<CI>';
str += '<sys_id>'+gr.parent.sys_id+'</sys_id>';
str += '<name>'+gr.parent.name+'</name>';
str += '<relType>'+gr.type.name+'</relType>';
ret = this._recurs(gr.parent.sys_id);
if (ret) {
str += '<children>';
str += ret;
str += '</children>';
}
str += '</CI>';
}
return str;
},
_addParentCIs : function(value, CIs, currentDepth, classArr) {
if (this.added >= this.maxSize) {
// gs.print('reached threshold of ' + this.maxSize);
return;
} else {
currentDepth++;
// gs.print('current depth is ' + currentDepth);
var g = new Packages.com.glideapp.ecmdb.Relationships();
var al = g.getRelatedRecords(value, null, 'cmdb_ci', 'cmdb_ci', 'parent'); // returns ArrayList
// first add the unique cis
var kids = new GlideRecord('cmdb_ci');
kids.addQuery('sys_id', al);
kids.query();
while (kids.next()) {
var str = kids.sys_id;
if (!CIs[str]) {
var rec = new GlideRecord('cmdb_ci');
rec.get(str);
for (var ciClass in classArr){
if ((kids.sys_class_name == classArr[ciClass]) || (classArr[0] == 'ALL')){
this._addCI(str, CIs);
}
}
if (this.added >= this.maxSize)
return;
if (currentDepth < this.maxDepth)
this._addParentCIs(str, CIs, currentDepth, classArr);
}
}
}
},
_addCI: function(id, CIs) {
CIs[id] = true;
this.added++;
},
unique: function(a) {
//Check all values in the incoming array and eliminate any duplicates
var r = new Array();
o:for(var i = 0, n = a.length; i < n; i++){
for(var x = 0, y = r.length; x < y; x++){
if(r[x]==a[i]){
continue o;
}
}
r[r.length] = a[i];
}
return r;
},
type: 'CIUtilsExtra'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2017 09:02 AM
Hi mguy.
Thank you so much for your script. Anyway, you were right, it needs a small update.
In line 120, instead of having
var g = new Packages.com.glideapp.ecmdb.Relationships();
var al = g.getRelatedRecords(value, null, 'cmdb_ci', 'cmdb_ci', 'parent');
substitute it for this:
"var al = SNC.CMDBUtil.getRelatedRecords(value, "", 'cmdb_ci', 'cmdb_ci', 'parent');"