cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I want to add a loop task to enable/disable host monitoring using workflows

Smalhotra89
Frequent Guest

Hi team,

I want to use loop task for a list of hosts to enable or disable monitoring in Dynatrace. I would like to have multiple hosts to be executed with single workflow for http_request.

 

[
{
"externalId": "string",
"schemaId": "builtin:host.monitoring",
"schemaVersion":"1.4",
"scope": "HOST-7AA30C2D50D93E12",
"value": {
"enabled": false
}
}
]

 

 

7 REPLIES 7

sinisa_zubic
Dynatrace Champion
Dynatrace Champion

Hi @Smalhotra89 

 

Yes this is possible with the loop task. Before you start you need to generate an "Access Token" with the scopes "Read settings" and "Write settings". 

Then you can start creating the workflow. First you need to fetch all settings objects for the list of hosts you want to disable or enable with a http request task. The list of hosts is put in the URL and is comma separated. In the header section you add the Autorization details, please have a look at following screenshot

sinisa_zubic_0-1718814871834.png

in the subsequent http request task you need to configure the loop task like this

sinisa_zubic_1-1718814949557.png

The input for the tasks should be that

sinisa_zubic_2-1718815087103.png

 

For this approach you have to create the token and manage it yourself. 

 

But you could also do the same thing with a javascript task with custom javascript code, the benefit there would be that you don't need to create a token for changing the settings. Since the used sdks underneath managed this for you.

this is the snippet for the javascript task

// optional import of sdk modules
import { execution } from '@dynatrace-sdk/automation-utils';
import { actionExecution } from "@dynatrace-sdk/automation-utils";

import { SettingsObject, settingsObjectsClient } from "@dynatrace-sdk/client-classic-environment-v2";

export default async function ({ execution_id, action_execution_id }) {
  // your code goes here
  // e.g. get the current execution
  const ex = await execution(execution_id);
  const actionEx = await actionExecution(action_execution_id);
  const scopes = actionEx.loopItem["host"];
  //console.log(scopes);
  const settingsObjects = await settingsObjectsClient.getSettingsObjects({
      schemaIds: "builtin:host.monitoring",
      scopes,
      fields: "objectId,value,summary,searchSummary,created,modifid,createdBy,modifiedBy,author,updateToken,scope,modificationInfo,schemaId,schemaVersion,externalId"
    });
  //console.log(settingsObjects);
  if (settingsObjects.items.length > 0){
    const settingsObject = (settingsObjects.items[0]);
    const settingsObjectResponse = await settingsObjectsClient.putSettingsObjectByObjectId({
        objectId: settingsObject.objectId,
        body: {
          updateToken: settingsObject.updateToken,
          value: {
            "enabled": false
          }
        }
      });
    return { settingsObjectResponse };
  }
}

 

 

sinisa_zubic_3-1718815258779.png

 

 

Best,
Sini

 

 

Smalhotra89
Frequent Guest

Thanks Sini, javascript task works well.

I was unable to use the http request with loop task as it was unable to read allobjects.json.item in loop. I am not sure if I did something wrong but the first task to get all objects task ran through fine.

vs185250
Observer

How do I iterate through all values from a fetch query using javascript?

 

vs185250_0-1725068819983.pngvs185250_1-1725068883642.pngvs185250_2-1725068924616.png

 

 

vs185250
Observer

here is the loop item

vs185250_3-1725068975651.png

 

vs185250
Observer

nvm. figured it out

vs185250_4-1725069193027.png

 

vs185250
Observer

this worked for me and disabled monitoring on all hosts

vs185250_5-1725070526855.png

 

Smalhotra89
Frequent Guest

Thank You, I made it work for all tagged hosts. 

Featured Posts