- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
[asset|aid=340|format=Small|formatter=asset_lightbox|title=nerdette.jpg|width=150|height=113|align=right]Suppose your boss wanted to automatically scan every week to look for any "dog" computers you might have. For the moment, a dog computer is defined as any computer with either less than 1 gigabyte of RAM or a CPU with a clock rate lower than one gigahertz. How would you do this?
The obvious approach is to make a scheduled job that runs the query and then perhaps sends the results directly to your boss in an email. For the purposes of this post, I'm going to ignore the details of how to make a scheduled job or to have it send an email — here I want to talk about the script that would do the work. How would I write a function that returns this information?
If you're familiar with coding GlideRecord queries, the tricky bit here is the "or" part. If you coded it like this...
var answer = lookForDogs();
gs.log('\n' + answer);
function lookForDogs() {
var MIN_RAM = 1000; // in megabytes
var MIN_CPU_SPEED = 1000; // in MHz
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery ('ram', '<', MIN_RAM );
gr.addQuery ('cpu_speed', '<', MIN_CPU_SPEED);
gr.query();
var answer = '';
while (gr.next())
answer += 'Dog: ' + gr.name + '(' + gr.ram + ' mb RAM, ' + gr.cpu_speed + ' MHz CPU speed)' + '\n';
return answer;
}
...you'd get a (hopefully very short!) list of the really, really doggy computers — those that have both less than 1 gigabyte of RAM and CPUs with clock rates less than 1 gigahertz. So how do you get the computers with either condition being met? You do it like this:
var answer = lookForDogs();
gs.log('\n' + answer);
function lookForDogs() {
var MIN_RAM = 1000; // in megabytes
var MIN_CPU_SPEED = 1000; // in MHz
var gr = new GlideRecord('cmdb_ci_computer');
var cond = gr.addQuery ('ram', '<', MIN_RAM );
cond.addOrCondition ('cpu_speed', '<', MIN_CPU_SPEED);
gr.query();
var answer = '';
while (gr.next())
answer += 'Dog: ' + gr.name + '(' + gr.ram + ' mb RAM, ' + gr.cpu_speed + ' MHz CPU speed)' + '\n';
return answer;
}
The tricky little bit is in the middle of the function. This time when we add the query condition for the RAM, we save the result (in the previous example we paid no attention to it). What's in that result is a QueryCondition object, which holds the result of the addQuery() invocation. These are always created, but most of the time you don't need them. This time, however, we need to make use of a feature of that QueryCondition object: the addOrCondition() method you see invoked on the next line in the script. You can specify the "or" in exactly the same ways that you can specify the usual "and" conditions.
One additional little detail: the addOrCondtion() method also returns a QueryCondition object. This provides you with the means to specify an arbitrarily complex set of query conditions, with any combination of "and" and "or" conditions, nested any way you'd like them to be. This isn't a capability you'll need often, but when you do need it, it's there!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.