08 May 2025 09:49 PM
Struggling to extract hostname via JS, can someone look at this - I keep getting Constratins Violated
try {
// Use entitySelector that just gets all HOSTS
console.log(`Searching for all hosts first...`);
const allHostsResponse = await monitoredEntitiesClient.getEntities({
entitySelector: `type("HOST")`,
fields: '+properties.monitoringState'
});
console.log(`Found ${allHostsResponse.totalCount} hosts in total`);
// Manually filter for the hostname
const matchedHosts = allHostsResponse.entities.filter(entity =>
entity.displayName === hostname ||
entity.entityId.includes(hostname.toUpperCase())
);
if (matchedHosts.length === 0) {
console.warn(`Host ${hostname} not found in Dynatrace environment`);
results.push({
hostname,
status: "error",
message: "Host not found in Dynatrace environment"
});
continue;
}
Snippet of Code
Solved! Go to Solution.
09 May 2025 08:22 AM
Hi @Dyno193, If the requirement is to get the hostnames from the environment, then u can get it from the notebook, with the query,
fetch dt.entity.host
| fields entity.name
Or if any automation then u can use this code to get the entity name and add ur condition on top of it.
export default async function ({ executionId }) {
try {
const query = `
fetch dt.entity.host
| fields entity.name`;
const queryResponse = await queryExecutionClient.queryExecute({ body: { query } });
if (queryResponse.requestToken) {
const pollQuery = async (requestToken) => {
let pollResponse;
do {
pollResponse = await queryExecutionClient.queryPoll({ requestToken });
if (pollResponse.state === 'RUNNING') {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
} while (pollResponse.state === 'RUNNING');
return pollResponse;
};
const finalResponse = await pollQuery(queryResponse.requestToken);
console.log(finalResponse.result.records);
}
} catch (error) {
console.error(`Dynatrace API Error:`, error);
return { error: error.message };
}
}
Thanks,