23 Jan 2024 09:27 AM - last edited on 23 Jan 2024 02:50 PM by Michal_Gebacki
Hi Community,
I'd like to output a list of Hosts that are not in a ManagementZone.
The EntitySelector does not allow us to do this.
Do you have an idea?
Solved! Go to Solution.
23 Jan 2024 09:44 AM - edited 23 Jan 2024 09:45 AM
Hi,
I would say using entities API:
Second one is providing much more information than mz, but you can see if that host has a MZ or not:
Best regards
24 Jan 2024 03:54 PM
Isn't there an easier way to get the information?
24 Jan 2024 04:29 PM
This may help, part of a dashboard - not my code, from a DT SME.
import { monitoredEntitiesClient } from '@dynatrace-sdk/client-classic-environment-v2';
export default async function() {
// Check if that tile should be activated based on selected indicators and scope
if($Indicators[0] === "All" || $Indicators.includes("hostGroupCoverage")) {
let scope;
if($Scope[0] === "Tenant") {
scope = 'Tenant';
} else {
let mzList = "";
for(let i=0; i<$Scope.length; i++) {
mzList += i === 0 ? `"${$Scope[i]}"` : `,"${$Scope[i]}"`;
}
scope = mzList;
}
// Define entity selector based on the selected scope
let entitySelector;
if(scope === "Tenant") {
entitySelector = 'type("HOST"),isMonitoringCandidate(false)';
} else {
entitySelector = `type("HOST"),isMonitoringCandidate(false),mzName(${scope})`;
}
let config = { from: `now-1d`, entitySelector: entitySelector};
// Get all monitored hosts
const monitoredHosts = await monitoredEntitiesClient.getEntities(config);
// Define entity selector based on the selected scope
if(scope === "Tenant") {
entitySelector = 'type("HOST"),isMonitoringCandidate(false),fromRelationships.isInstanceOf(type("HOST_GROUP"))';
} else {
entitySelector = `type("HOST"),isMonitoringCandidate(false),fromRelationships.isInstanceOf(type("HOST_GROUP")),mzName(${scope})`;
}
config = { from: `now-1d`, entitySelector: entitySelector};
// Get all monitored hosts that are assigned with a host group
const hostsWithHostGroup = await monitoredEntitiesClient.getEntities(config);
// Compute host group coverage
const hostGroupCoverage = parseInt(hostsWithHostGroup.totalCount/monitoredHosts.totalCount*100);
// Compute local score based on the host group coverage
const score = hostGroupCoverage === 100 ? 1 : (hostGroupCoverage >= 80 ? 0.5 : 0);
let result;
switch(true) {
case score === 1:
result = `✅ ${hostGroupCoverage}%`;
break;
case score === 0.5:
result = `💡 ${hostGroupCoverage}%`;
break;
case score === 0:
result = `⚠️ ${hostGroupCoverage}%`;
break;
}
// Return the emojied result
return result;
} else {
return "N/A";
}
}
24 Jan 2024 07:51 PM
hi @PraWij,
Thanks for this code.
OK, but what are the $Indicators and $Scope variables?
25 Jan 2024 05:19 PM - edited 25 Jan 2024 05:22 PM
In the full dashboard, scope also includes the mgmt zones.
Indicators example:
24 Jan 2024 05:13 PM - edited 24 Jan 2024 05:14 PM
Hi,
Maybe easier can be using Grail, if it is enabled in your tenant. I think you know more Grail than mine but something as that:
fetch dt.entity.host
| expand managementZones
| filter managementZones == ""
Best regards
24 Jan 2024 07:52 PM
@AntonPineiro
This doesn't work. I'll try with the sdk
27 Jan 2024 05:42 PM
Hi,
You can try this one, looks like is working:
fetch dt.entity.host
| fieldsAdd managementZones
| filter isNull(managementZones)
Best regards
30 Jan 2024 11:01 AM
You are welcome!
29 Jan 2024 12:11 PM
This looks interesting,
fetch dt.entity.host | fieldsAdd managementZones | filter isNull(managementZones)
What do I need to add to search by a particular management zone? contains or equal? need the DQL.
29 Jan 2024 01:23 PM
Hi,
Maybe more than one option but looks like this:
fetch dt.entity.host
| fieldsAdd managementZones
| filter matchesValue(managementZones, "XXXXXX")
Best regards