Scripting Probes and Sensor

tim2222
Tera Expert

Out of curiosity I've been playing with the SncProbe API for triggering a probe. I've found quite a few community resources covering how to trigger a probe e.g. Re: How to start a mid server script file from run script activity?

What I haven't found was how to capture (and action) the output from the probe from script. As far as I can tell there is no mechanism to specify a calback or other function via SncProbe.

Having investigated how Discovery works it appears it's triggered by business rules on the ECC queue. By adding a new business rule I've managed to create an approach that allows the developer to specify a parameter to SncProbe of a script to execute on the output of the probe. I'm not using the following in production but may be a helpful pointer to others.

Business Rule

PropertyValue
NameSncProbe - Sensors
TableQueue [ecc_queue]

When

before
Insertticked
Advancedticked
Conditionnew SncProbeSensor(current).isSncProbeSensor()
Scriptnew SncProbeSensor(current).process();

Script Include

PropertyValue
NameSncProbeSensor
Description

The SncProbe API provides no facility to process the output of the probe.

This script include replicates Discovery and other sensor processors by picking up an "output_script" parameter that is used to process the output from the probe.

Script

var SncProbeSensor = Class.create();

SncProbeSensor.prototype = {

      initialize: function(eccGr) {

              this.eccGr = eccGr;

      },

     

      isSncProbeSensor: function() {

              var eccGr = this.eccGr;

             

              if (eccGr.state != 'ready' || eccGr.queue != 'input' || new GlideXMLParameters(eccGr.payload).get('output_script') == null) {

                      return false;

              }

              return true;

      },

      process: function() {

              var eccGr = this.eccGr;

     

              var parameters = new GlideXMLParameters(eccGr.payload);

              var output = parameters.getElementValueByTagName('output');

              var script = parameters.get('output_script');

             

              // wrap the eval in a call() to avoid any script breaking into our context

              (function(script){

                      var output = this.output;

                      return eval(script + ';');

              }).call({output: output},script);

              eccGr.state = 'processed';

              eccGr.processed = (new GlideDateTime()).getDisplayValue();

      },

      type: 'SncProbeSensor'

};

Example Code

Query a *nix server for its Kernel version - assumes you have credentials etc. set up. The kernel version will be sent to the log as a warning.

var probe = SncProbe.get("SSHCommand");

probe.setName("uname -a");

probe.setSource("10.10.10.10");

probe.addParameter("skip_sensor", true);

// Note you can specify a post_processor_script to convert raw input to Javascript

//probe.addParameter("post_processor_script", "new ProbePostProcessor({ process: function() { current.output = output; }, type: 'ProbePostProcessor'});");

// output_script is the script to execute. "output" is the variable made available containing the probe output

probe.addParameter("output_script", "gs.warn(output)");

probe.create("MIDSERVERNAME");

3 REPLIES 3

Mike Moody
Kilo Guru

Hi Tim,



I just wanted to say thanks for this post, it pointed me in the right direction to addressing a performance issue for a high velocity integration scenario via the ECC queue.   I was leveraging the OOB RESTMessageV2 object to send Integration calls via our MID Server(s) to internal systems. Despite the fact that I was using the executeAsync() function on that object, I was still experiencing unacceptable delays in processing large numbers of outbound calls. This was primarily due to the fact that I needed to capture the error or success response for each transaction in order to record it in ServiceNow and parse/use the response results. It would seem that the function name executeAsync is a little misleading, because it's only asynchronous within the context of that script's lifespan. The documentation even guides you to use the waitForResponse() function within the context of the same script you are using the executeAsync function in, which essentially forces your asynchronous transaction back to a synchronous transaction. https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_RMV2-executeAsync



Since the RESTMessageV2   object essentially generates a RESTProbe in the ECC queue, I was able to leverage your approach to pass custom parameters along with the probe and leverage those to process the response via a Business Rule directly on the ECC queue that watches for the Input response to the RESTProbe. Then I modified the calling script to simply executeAsync with no waitForResponse call.



Thanks!


Sri37
Tera Contributor

Hi @Mike Moody ,

Its been some time you have put in this post. Can you please help me add your RESTProbe/ HTTPProbe example here, if you still have?

I am looking for passing some custom parameters along with probe to call REST API Endpoint using a specific MID. And finally, process the REST API Response

Aditya Telideva
ServiceNow Employee
ServiceNow Employee

Thanks for this Tim....