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

Toggle Monitoring State of a Host using App Function

MarwanC
Advisor

I have written this AppFunction to toggle the monitoring state of a host, and I have given it all the required

scope, but I am still getting this error below. Any idea? I am planning to develop this further to be able to use in a workflow.

Any feedback is much appreciated.

------------------------

Given this permission:

-----------------------

Scopes: app-settings:objects:read, app-settings:objects:write, settings:objects:read, settings:objects:write, settings:schemas:read, app-engine:functions:run, app-engine:apps:run

The Function:

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

export default async function () : Promise<{ statusCode: number; message: string }>
{
  const hostId = "HOST-F6DC785465402DD8"; // Replace with your actual host ID
 
  try {
    const response = await settingsObjectsClient.postSettingsObjects({
      body: [
        {
          schemaId: "builtin:host.monitoring",
          scope: hostId,
          value: {
            monitoringEnabled: false,
          },
        },
      ],
    });
    return { statusCode: 200, message: "{hostId} - Monitoring state updated successfully." };

  } catch (error: unknown) {
    let errorDetails = "";
 
    if (error instanceof Error) {
      errorDetails = `Error name: ${error.name}\n` +
                     `Error message: ${error.message}\n` +
                     (error.stack ? `Stack trace: ${error.stack}` : "No stack trace available.");
    } else {
      errorDetails = `Unknown error object: ${JSON.stringify(error, null, 2)}`;
    }
 
    return {
      statusCode: 500,
      message: `${hostId} - Failed to update monitoring state.\nDebug info:\n${errorDetails}`,
    };
  }
 
}
 
-----------------
I get this Error:
----------------
 
{"statusCode":500,"message":"HOST-F6DC785465402DD8 - Failed to update monitoring state.\nDebug info:\nError name: 403\nError message: OAuth token is missing required scope. Use one of: [settings:objects:write]\nStack trace: 403: OAuth token is missing required scope. Use one of: [settings:objects:write]\n    at se.postSettingsObjects (file:///invoker.js:5:19821)\n    at eventLoopTick (ext:core/01_core.js:177:7)\n    at async Ds (file:///invoker.js:5:47784)"}


 
4 REPLIES 4

MarwanC
Advisor

I also get the same error when running from a workflow

 

Message: HOST-F6DC785465402DD8 - Failed to update monitoring state.
Debug info:
Error name: 403
Error message: OAuth token is missing required scope. Use one of: [settings:objects:write]
Stack trace: 403: OAuth token is missing required scope. Use one of: [settings:objects:write]
at SettingsObjectsClient.postSettingsObjects (file:///opt/sdk_modules/@dynatrace-sdk/client-classic-environment-v2/esm/index.js:23556:19)
at eventLoopTick (ext:core/01_core.js:177:7)
at async default (file:///script.ts:8:22)

MarwanC
Advisor

I gift this function to the community, as it is fully working; Enjoy!

 

import { execution } from '@dynatrace-sdk/automation-utils';
import { settingsObjectsClient } from '@dynatrace-sdk/client-classic-environment-v2'; // Make sure this import is present

export default async function () {

// We need to obtain the list of Hosts and iterate through it
// The list can be an API call or an array in this function.

const hostId = "HOST-F6DC785465402DD8"; // Replace with actual host ID

try {
const response = await settingsObjectsClient.postSettingsObjects({
body: [
{
schemaId: "builtin:host.monitoring",
scope: hostId,
value: {
enabled: true,
},
},
],
});

const ex = await execution();
const message = `${hostId} - Monitoring state updated successfully.`;

console.log('Message:', message);
console.log('Automated script execution on behalf of', ex.trigger);

return { triggeredBy: ex.trigger };

} catch (error: unknown) {
let errorDetails = "";

if (error instanceof Error) {
errorDetails = `Error name: ${error.name}\n` +
`Error message: ${error.message}\n` +
(error.stack ? `Stack trace: ${error.stack}` : "No stack trace available.");
} else {
errorDetails = `Unknown error object: ${JSON.stringify(error, null, 2)}`;
}

const ex = await execution();
const message = `${hostId} - Failed to update monitoring state.\nDebug info:\n${errorDetails}`;

console.log('Message:', message);

return { triggeredBy: ex.trigger };
}
}

 

 

@MarwanC So, what was reason it was throwing the error before? Just interested to know and thanks for sharing the working code 👍

Phani Devulapalli

added settings:objects:write permission. Enjoy

Featured Posts