Configuring a script to swarm elements
Tip
See also: Swarming
Script with fixed info
As a first step, create a short Automation script that will swarm a fixed element to a fixed destination:
Collect the following information:
[element-key]
: DMA ID/element ID pair for the element you want to swarm. You can find this information in the element properties window in DataMiner Cube.[target-agent-id]
: ID of the target DMA. You can find this on the System Center > Agents page.
Add a C# code block with the contents below, replacing the placeholders with the values from the previous step.
using System; using System.Linq; using Skyline.DataMiner.Automation; using Skyline.DataMiner.Net; using Skyline.DataMiner.Net.Swarming.Helper; public class Script { public void Run(Engine engine) { ElementID element = ElementID.FromString("[element-key]"); int targetAgentId = [target-agent-id]; var swarmingResults = SwarmingHelper.Create(engine.GetUserConnection()) .SwarmElement(element) .ToAgent(targetAgentId); var swarmingResultForElement = swarmingResults.First(); if (!swarmingResultForElement.Success) { engine.ExitFail($"Swarming failed: {swarmingResultForElement?.Message}"); } } }
Execute the script to launch a swarming action for the specified element to the specified target host.
Code parts explained
Below you can find some more information about specific parts of the code in the example script above.
using Skyline.DataMiner.Net.Swarming.Helper;
This line pulls in the appropriate namespace for the SwarmingHelper type, which is used further down.
var swarmingResults = SwarmingHelper.Create(engine.GetUserConnection())
.SwarmElement(element)
.ToAgent(targetAgentId);
The lines above communicate with SLNet to request a swarming action for a given element. As part of this action, the element will first be unloaded from the Agent where it was previously hosted and then loaded onto the new host.
var swarmingResultForElement = swarmingResults.First();
if (!swarmingResultForElement.Success)
{
engine.ExitFail($"Swarming failed: {swarmingResultForElement?.Message}");
}
The lines above deal with failures, if any.
Script with input parameters
In the example below, the script above is updated to use input variables instead of a fixed element and target Agent. This will make the script reusable for all element swarming actions.
Add the following two script input parameters to the Automation script mentioned above:
Element Key
Target Agent ID
Find the following code lines in the script:
ElementID element = ElementID.FromString("[element-key]"); int targetAgentId = [target-agent-id];
Replace these lines with the following lines:
var element = ElementID.FromString(engine.GetScriptParam("Element Key").Value); int targetAgentId = Int32.Parse(engine.GetScriptParam("Target Agent ID").Value);
You now have a script that can be invoked from anywhere, specifying an element and target DataMiner Agent.
The full script C# code should now look like this:
using System;
using System.Linq;
using Skyline.DataMiner.Automation;
using Skyline.DataMiner.Net;
using Skyline.DataMiner.Net.Swarming.Helper;
public class Script
{
public void Run(Engine engine)
{
var element = ElementID.FromString(engine.GetScriptParam("Element Key").Value);
int targetAgentId = Int32.Parse(engine.GetScriptParam("Target Agent ID").Value);
var swarmingResults = SwarmingHelper.Create(engine.GetUserConnection())
.SwarmElement(element)
.ToAgent(targetAgentId);
var swarmingResultForElement = swarmingResults.First();
if (!swarmingResultForElement.Success)
{
engine.ExitFail($"Swarming failed: {swarmingResultForElement?.Message}");
}
}
}