# Creating an automation scenario
This section of the documentation is devoted to the general principles of creating scenarios for SM auto-building.
Before creating your first scenario, it is recommended that you familiarize yourself with the general concepts of the Automation functionality:
Basics of the Automation functionality
In this section, we will consider the general principles for creating and building automation scenarios.
# Adding a scenario
Go through the main menu to the section Actions→Automation to open the scenario management screen.
To add a new scenario to the system, click the ➕ New Scenario button in the upper right corner of the screen.
Fill in the main parameters of the new scenario:
- Scenario Owner - The Workgroup that will own the scenario.
- Name - logically understandable name of the scenario.
- Type - CMDBAutoDiscovery.
- Description (optional).
- Import script (optional).
Click the Create button - the scenario will be created and the scenario constructor will open.
Each scenario starts with the Launch Event block -
OnLogEvent
- automatically added when creating each scenario.- You cannot delete or modify the
OnLogEvent
block. - The outgoing pin (variable)
Value
contains data of a primary event passed to the collector via:- Public API
- Integration
- Acure Agent
- You cannot delete or modify the
# Event filtering
The next step is to filter events going into the Automation scenario.
To do this, add a local function block to the scenario:
In the Object Manager, click ➕ in the Local Functions category and a
CodeFunction
local function will be added.From the action menu above the
CodeFunction
, select Add to Canvas</>.Double click on the added local function block to open the Object Inspector.
Change the Name of the local function, for example
FilterEvents
.Add Input pin
InValue
with typeMonq.Cl.CollectorLogEvent
(this system type describes the structure of Acure primary events).Configure Output pins as follows:
ok
, typeExec
failed
, typeExec
Switch to the edit mode of the current local function
FilterEvents
, click on the Editor tab while in the Object Inspector.Bring the function code to the following form:
private async ValueTask<FuncResult> FilterEvents(Monq.Cl.CollectorLogEvent InValue) { if (InValue._stream.id == 248) { return new FuncResult("ok"); } return new FuncResult("failed"); }
In this example, the primary event field
_stream.id
(ID of the "Data Stream") is checked with the predefined value (248). If the primary event came via Data Stream with the identifierid == 248
the scenario will go further along theok
Output pin, otherwise along thefailed
Output pin.Connect the corresponding pins by dragging the link between them:
Block
OnLogEvent
pinOut
➞ BlockFilterEvents
pinIn
- connection for defining scenario execution sequence.Block
OnLogEvent
pinValue
➞ BlockFilterEvents
pinInValue
- connection for transfering variable values.
# Event handling
An example of creating a Configuration Item (CI) by an event from Zabbix (the host
field) will be considered.
# An example of a primary event from *Zabbix* monitoring system
"id": "50133",
"clock": 1646785676,
"acknowledged": 0,
"name": "Disk space usage",
"value": 1,
"severity": 4,
"trigger": {
"id": "20170",
"revealedDescription": "Disk space usage",
"description": "Disk space usage",
"revealedComments": "",
"comments": "",
"expression": "{25124}>90",
"lastChangeTime": 0,
"priority": 4,
"state": 0,
"status": 0,
"url": "",
"value": 1
},
"group": {
"id": "24",
"name": "d12-apps"
},
"host": {
"id": "10452",
"hostValue": "d12 Host",
"name": "d12 Host"
},
"itemsIds": [
37676
],
"tags": [],
"zabbixVersion": 5.4
}
# Declaring structures
First, you need to decompose the structure of the primary event into properties. To do this, you need the global BreakStruct
function.
Add the
BreakStruct
global function to the script using the right mouse button.Connect the incoming
Object
pin of theBreakStruct
function to the outgoingValue
pin of theOnLogEvent
trigger event. The structure of the primary event will be automatically "mapped out" into the corresponding properties in the form of outgoing pins of theBreakStruct
function.We are interested in the outgoing
source
pin with theDynamic
type, which contains the source event with data from Zabbix. Namely, thehost
object, according to which we will create Configuration Items:"host": { "id": "10452", "hostValue": "d12 Host", "name": "d12 Host" }
To work with the values contained in the fragment, it is necessary to describe the structure of this object:
Create a new structure in the Object Manager - click ➕ in the Structures category - a local
Struct
structure will be added.Click the left mouse button on the newly created structure to open the Object Inspector for this structure.
Set the Name of the structure, for example
ZabbixHostObject
Add structure properties according to the JSON:
id
, typeString
hostValue
, typeString
name
, typeString
Structure declaration is completed at this stage.
# Access to event fields
Next, to access the host
object and its properties, you need the global function BreakDynamic
Add the
BreakDynamic
global function to the scenario using the right mouse button.Connect the incoming
Object
pin of theBreakDynamic
function to the outgoingsource
pin of the previous globalBreakStruct
function.Open the Object Inspector of the
BreakDynamic
function and add an outputhosts
pin with typeZabbixHostObject
(previously declared structure).Next, you need to "decompose" the structure of the
host
object again by adding another new global functionBreakStruct
and connecting the pins:- Outgoing
host
pin ofBreakDynamic
function ➞ IncomingObject
pin ofBreakStruct
function
- Outgoing
At this stage, we have access to Zabbix event variables
source.host.id
,source.host.name
, andsource.host.hostValue
as outgoing pins of theBreakStruct
function.The next step is to prepare a request to create a Configuration Item using the Acure public API.
# Creating CI
An example of creating a CI with a default type and assigning as owner the current Workgroup (scenario owner) will be considered.
To create a CI, a request with the following minimum content is used:
{
# CI identifier:
"id": 0
# CI name
"name": "",
# Description of CI
"description": "",
# ID of the Workgroup - owner of the CI:
"ownerWorkGroup": {
"id": 0
}
}
According to the request body presented above, it is necessary to create 2 structures:
createCIQuery
- a structure describing the entire JSON,ownerWorkGroup
- structure describing object"ownerWorkGroup": {"id": 0 }
Create a new structure in the Object Manager - click ➕ in the Structures category - a local
Struct
structure will be added.Left-click on the newly created structure to open the Object Inspector for this structure.
Set the Name of the structure, for example
ownerWorkGroup
Add structure properties according to the JSON:
id
, typeInteger64
Repeat steps 2-3 to create another structure.
Set the Name of the structure, for example
createCIQuery
Add structure properties according to the JSON:
id
, typeString
name
, typeString
description
, typeString
ownerWorkGroup
, typeownerWorkGroup
- created structure.
# Preparing the request body
Next, you need to generate a request to create a CI, where:
- CI name =
source.host.name
- CI Description =
source.host.id
- CI Owner = Scenario Owner
Use the global function SetMembersInStruct
to assign values to the properties of the created structures.
# Assign the ID of the Workgroup that owns the scenario to the id
variable in the ownerWorkGroup
structure:
Add the
SetMembersInStruct
global function to the script.Create a new local variable in the Object Manager.
In the Object Inspector, specify the name of the created variable, for example
ownerWorkGroupVar
. Set the variable type toownerWorkGroup
- the previously created structure.Add the
ownerWorkGroupVar
variable to the scenario from the Object Manager using the context menu item Add to Canvas Get.Connect the
ownerWorkGroupVar
variable pin to the incomingObject
pin of theSetMembersInStruct
block and open the block in the Object Inspector.Check the incoming pin
Id
in the object manager - it will be added as an Input pin of the current block.Add the
OwnerWorkGroupId
system variable to the scenario and connect it to the incomingId
pin of theSetMembersInStruct
block.In order for this function to be executed when the scenario is run, connect the outgoing
ok
pin of the localFilterEvents
function to the incomingIn
pin of theSetMembersInStruct
function.
# Set the "CI Name" and "CI Description" values in the createCIQuery
structure:
Add another global function
SetMembersInStruct
to the scenario.Create a new local variable in the Object Manager
In the Object Inspector specify the name of the created variable, for example
CreateCIQueryVar
. Variable type =CreateCIQuery
- the previously created structure.Add the
CreateCIQueryVar
variable to the script from the Object Manager.Connect the
CreateCIQueryVar
variable pin to the incomingObject
pin of theSetMembersInStruct
block and open the block's Object Inspector.Select the pins you want to set values to, they will appear as incoming pins of the
SetMembersInStruct
function block.Perform the following link setup:
- Incoming pin
name
of the functionSetMembersInStruct
➞ Outgoing pinname
of the functionBreakStruct
(passing the value ofsource.host.name
to the body of the request to create CI). - Incoming
description
pin of theSetMembersInStruct
function ➞ Outgoingid
pin of theBreakStruct
function (passing the value ofsource.host.id
to the body of the request to create CI). - Incoming
ownerWorkGroup
pin of theSetMembersInStruct
function ➞ OutgoingResult
pin of theSetMembersInStruct
function (passing the value of the scenario owner's Workgroup ID to the body of the request to create CI). - Incoming pin
In
of the functionSetMembersInStruct
➞ Outgoing pinOut
of the functionSetMembersInStruct
(indicate the order of execution of functions in the scenario).
- Incoming pin
Set the value of the incoming pin
id
of theSetMemberInStruct
function to0
to automatically generate the Configuration Item identifier.
At this point, your scenario should look like this:
Next, you need to send an HTTP POST request to the Acure public API with the prepared parameters. This requires the MonqHTTPRequest
global function.
Add the
MonqHTTPRequest
global function to the scenario using the context menu.For authorization in the public API, a special token
BearerToken
is used. To authorize the request, you need theRobotToken
system variable. Add it from the Object Manager.Also for the HTTP request, you must specify the userspace identifier. The current value is stored in the
UserspaceId
system variable, add it to the scenario as well.Perform the following link setup:
- Incoming
UserspaceId
pin of theMonqHTTPRequest
function ➞ Outgoing pin of theUserspaceId
variable (pass the value of the space identifier in the header of the request to create CI). - Incoming pin
BearerToken
of theMonqHTTPRequest
function ➞ Outgoing pin of theRobotToken
variable (passing the value of the authorization token in the header of the request to create CI). - Incoming
Body
pin of theMonqHTTPRequest
function ➞ OutgoingResult
pin of theSetMembersInStruct
function (passing the body of the HTTP request to create CI). - Incoming
In
pin of theMonqHTTPRequest
function ➞ OutgoingOut
pin of theSetMembersInStruct
function (indicate the order of execution of functions in the scenario).
- Incoming
Set the incoming
Url
andType
pins of theMonqHTTPRequest
function by specifying the appropriate values:Url
=https://<monq-global-domain>/api/public/sm/v2/rsm/config-items?makeNameUnique=false
Type
=POST
Replace
<monq-global-domain>
with the domain name of your Acure instance.Click the Compile button on the top bar of the scenario constructor.
Activate the scenario by using the Active/Inactive switch on the top panel of the scenario constructor.
The finished scenario looks like this:
Now, when receiving an event from the Zabbix monitoring system about a problem or a recovery event, a Configuration Item will be created.
The functionality of the Automation module is not limited to this. This scenario can be extended with various conditions and checks, as well as making other public API requests.
If you have any difficulties, you can import the finished scenario into your system using the Import/Export functionality:
- Copy the code of the finished scenario to the clipboard: [zabbix-host.txt]
- Go to the Automation scenario management screen.
- Create a new scenario, set Owner and Name of the scenario.
- Paste the copied code into the import line.
- Click Create.
- Change the
_stream.id
parameter in the localFilterEvents
function to the ID of the Data Stream whose events are to be processed by the automation scenario.