
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Recently, a customer asked me if it is possible to write a probe and sensor to get an XML file from a Windows target. Getting the file was the easy part. Parsing the returned results in the sensor was a new learning experience for me. Since I couldn't seem to find it documented anywhere else, this seemed like the best place. That's right... from the developer, to me, to you in just a few hours.
Retrieving the XML file is the same as with any other text file. Create a probe under Discovery Definition> Probes, and click New. Define the probe like this:
Name: Windows - Get XML file
Probe type: Probe
ECC queue topic: RemoteRunner
ECC queue name: WMI: Get XML file
Description: Retrieve an XML file for some application
Used by Discovery: true
Used by Runbook: false
Right click, Save
Under the "Probe Parameters" (related list), create a new entry with the following parameters:
Name: WMI_get_xml_file.js
Probe: (reference to the parent probe record: Windows - Get XML file)
Active: true
Value:
//// Sample probe to gather text from the file: D:\myapp\config\database.xml
//
var FOR_READING = 1;
var OUTPUT_FILE = "D:\\myapp\\config\\database.xml";
var res = getInfo();
WScript.Echo(res);
function getInfo() {
var output = readFile(OUTPUT_FILE);
return output;
}
function readFile(fileName) {
var fileObj = new ActiveXObject("Scripting.FileSystemObject");
var objTextFile = fileObj.OpenTextFile(fileName, FOR_READING);
var output = objTextFile.ReadAll();
objTextFile.close();
return output;
}
Value script: (empty)
Now comes the sensor. Normally, we would create a variable as an XMLdocument object from the result.output and use getNodeText() to tease out the parameters we want. That's not necessary here (never mind the fact that it doesn't work either). Discovery is smart enough to recognize that we retrieved an XML file and puts it directly inline with the XML payload on the ECC input record (look under the tag).
Here's the cool part I learned... To get to that information, simply dot-walk the XML structure to the answer. Given the following information in the
section of the XML payload...
<result>
<test>
<one>
<two att="xxx">abcd1234</two>
<three boo="yah" att="yyy">1234abcd</three>
<two>another</two>
</one>
<number>1234</number>
</test>
Instead of using XMLDocument and getNodeText() functions, use the much simpler statement:
var myNum = result.test.number;
To use this in a sensor, create a new sensor from Discovery Definition> Sensors, click New and fill in the form similar to the following:
Name: Windows - Get XML file
Reacts to probe: Windows - Get XML file
Sensor type: Sensor
Description: Tell me what's in the "number" tag of the retrieved XML file
Sensor type: Javascript
Active: true
Script:
//
// Name: My sensor
// Description: Parse the output retrieved by the probe of the same name
//
new DiscoverySensor({
/*
* Process the results of the probe
*
* @param: result - the value of the "result" tag of the XML payload on the input record
*/
process: function(result) {
var myNum = result.test.number; // Dot walk to the answer
gs.log('>>>DEBUG: My sensor: number=' + myNum);
},
type: "DiscoverySensor"
});
The power and simplicity of this is the statement "var myNum=result.test.number" to dot-walk to the answer. If you look in the system log, you should see an entry with the message:
>>>DEBUG: My sensor: number=1234
I had one challenge when I ran in to with XML tags that have dashes (-) in them. The JavaScript parser thinks of these as mathematical equations with subtraction. For example:
var myNum = result.data-sources.data-source.name; // Won't work !!!
is interpreted as "
" minus "
result.data
" minus "
sources.data
". I got an error stating "sources is an unknown reference". The workaround is to use array notation for the items with dashes. Rewriting the statement above would look like:
source.name
var myNum = result['data-sources']['data-source'].name;
If you have XML tags with attributes (as in "three" from the example above having attributes "boo" and "att"), you can get the values of these with a statement like the following:
var booVal = result.test.one.three['@boo'];
Will produce the answer "yah".
Happy XML file parsing!
Reference:
http://wiki.service-now.com/index.php?title=XMLDocument_Script_Object
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.