Introduction
-
Using scripts we can configure
Rule
>>Condition
,Operations
,Dashboard custom widget
data.List of supported API's
API's are added in script environment. You can access any supported API's with
mcApi
object.Note: Here I am listing only few API's. For complete set of API's please refer API's document.
Examples:
Java script
Take an example I want to display last seen of node details in dashboard.
var myImports = new JavaImporter(java.io, java.lang, java.util); // <1> with(myImports) { // <2> var options = new HashMap(); // <3> //Sort by lastSeen. options.put("orderBy", "lastSeen"); // <4> //Order by descending options.put("order", "desc"); // <5> //Page limit, only 5 result options.put("pageLimit", new Long(5)); // <6> //Get nodes data var nodes = mcApi.node().getAll(options); // <7> }
<1> Import required packages for our coding.
JavaImporter
is used to import packages.
You can import any numbe of packages with comma separated.
<2> Add our importswith
loop
<3> Create aHashMap
to add our query filter values.
<4> Adding a filterorderBy
aslastSeen
<5> Adding another filterorder
asdesc
<6> Adding another filterpageLimit
as5
<7> Store queried results in the variablenodes
, can be used in templates(refer templates section)
mcApi
already binded with script engine, you can use it to query/set values from/to MyController.Groovy script
I want to print serial gateways on MyController log file.
import org.mycontroller.standalone.gateway.GatewayUtils.GATEWAY_TYPE // <1> def filters = [name: 'Serial-gateway', pageLimit: 10L, type: GATEWAY_TYPE.SERIAL] // <2> def queryResponse = mcApi.gateway().getAll(filters) // <3> mcApi.logger().info("Quesry Response:{}", queryResponse) // <4>
<1> Import
GATEWAY_TYPE
enum, will be used to filter
<2> Create filter list with filter values, Filtering with gateway
name
containsSerial-gateway
(case sensitive),type
asGATEWAY_TYPE.SERIAL
and limiting count on request as 1010L
with the keypageLimit
<3> Calling MyController API(mcApi.gateway().getAll(filters)
) with our filter
<4> Printing result undermycontroller/log/mycontroller.log
asINFO
log.NOTE: When log level set at
ERROR
,INFO
logs will not be printed!Python script
In this script we are getting gateways detail and printing in log file.
from java.util import HashMap # <1> from java.lang import Long # <2> options = {'pageLimit': Long(20)} # <3> gateways = mcApi.gateway().getAll(HashMap(options)) # <4> mcApi.logger().info("Quesry Response:{}", gateways) # <5>
<1> Import
HashMap
should be used to send filter queries
<2> ImportLong
used to send javaLong
type
<3> Create filter withpageLimit
of20
<4> Query MyController withMcApi
and store result ingateways
<5> Print result inmycontroller/log/mycontroller.log
file.