Fn AWS Source Availability Contract
Query
// REVIEW ONLY - Reusable Workspace Function Spec: fn_AWS_SourceAvailabilityContract
// Purpose: Implements the workbook & operational availability contract so panels explicitly surface
// NOT_CONNECTED / CONNECTOR_ERROR / NO_DATA / DELAYED / HEALTHY instead of rendering empty silently.
let ExpectedSources = _GetWatchlist('AWS_SourceSlaConfig')
| where tolower(tostring(column_ifexists("Enabled", ""))) == "true"
| project SourceTable=tostring(column_ifexists("SourceTable", "")), ConnectorNamePattern=tostring(column_ifexists("ConnectorNamePattern", "")),
ExpectedFreshnessMinutes=todouble(column_ifexists("ExpectedFreshnessMinutes", "")), ZeroVolumeAlertMinutes=todouble(column_ifexists("ZeroVolumeAlertMinutes", ""));
let ObservedTelemetry = union isfuzzy=true withsource=SourceTable
(AWSCloudTrail | where TimeGenerated > ago(7d) | project TimeGenerated, IngestTime=ingestion_time()),
(AWSGuardDuty | where TimeGenerated > ago(7d) | project TimeGenerated, IngestTime=ingestion_time()),
(AWSVPCFlow | where TimeGenerated > ago(7d) | project TimeGenerated, IngestTime=ingestion_time()),
(AWSRoute53Resolver | where TimeGenerated > ago(7d) | project TimeGenerated, IngestTime=ingestion_time()),
(AWSWAF | where TimeGenerated > ago(7d) | project TimeGenerated, IngestTime=ingestion_time()),
(AWSNetworkFirewallAlert | where TimeGenerated > ago(7d) | project TimeGenerated, IngestTime=ingestion_time()),
(AWSCloudWatch | where TimeGenerated > ago(7d) | project TimeGenerated, IngestTime=ingestion_time())
| summarize Events7d=count(), Events24h=countif(TimeGenerated > ago(24h)),
LastEventUtc=max(TimeGenerated), LastIngestUtc=max(IngestTime),
P95IngestLagMin=round(percentile(datetime_diff('second', IngestTime, TimeGenerated), 95) / 60.0, 1)
by SourceTable;
let ConnectorFailures = SentinelHealth
| where TimeGenerated > ago(24h) and SentinelResourceType == "DataConnector"
| where Status in ("Failure", "Warning", "Partial Success")
| summarize HealthErrors24h=count(), LastErrorUtc=max(TimeGenerated), Reasons=make_set(Reason, 10)
by SentinelResourceName, SentinelResourceKind;
ExpectedSources
| join kind=leftouter ObservedTelemetry on SourceTable
| extend FreshnessMin = iff(isnull(LastEventUtc), 999999.0, round(datetime_diff('second', now(), LastEventUtc) / 60.0, 1)),
DummyKey = 1
| join kind=leftouter (
ConnectorFailures
| summarize TotalAwsConnectorErrors24h=sum(HealthErrors24h), ConnectorErrorReasons=make_set(Reasons, 10)
by DummyKey=1
) on DummyKey
| extend AvailabilityState = case(
isnull(Events7d) or Events7d == 0, iff(coalesce(TotalAwsConnectorErrors24h, 0) > 0, "CONNECTOR_ERROR", "NOT_CONNECTED"),
FreshnessMin > ZeroVolumeAlertMinutes, iff(coalesce(TotalAwsConnectorErrors24h, 0) > 0, "CONNECTOR_ERROR", "NO_DATA"),
FreshnessMin > ExpectedFreshnessMinutes or P95IngestLagMin > ExpectedFreshnessMinutes, "DELAYED",
"HEALTHY")
| project SourceTable, AvailabilityState, Events24h=coalesce(Events24h, 0), LastEventUtc, LastIngestUtc,
FreshnessMin, P95IngestLagMin, ExpectedFreshnessMinutes, ZeroVolumeAlertMinutes,
TotalAwsConnectorErrors24h=coalesce(TotalAwsConnectorErrors24h, 0), ConnectorErrorReasons
| take 100Explanation
This KQL query is designed to monitor the availability and health of AWS data sources connected to a system. Here's a simplified breakdown of what the query does:
-
Define Expected Sources: It retrieves a list of expected AWS data sources from a watchlist, filtering for those that are enabled. It gathers details like the source table name, connector name pattern, expected data freshness, and alert thresholds for zero data volume.
-
Collect Observed Telemetry: It gathers telemetry data from various AWS services (like CloudTrail, GuardDuty, VPC Flow, etc.) over the past 7 days. It calculates statistics such as the number of events in the last 7 days and 24 hours, the time of the last event and ingestion, and the 95th percentile of ingestion lag time.
-
Identify Connector Failures: It checks for any failures or warnings in the data connectors over the past 24 hours, summarizing the number of errors and the reasons for these errors.
-
Join and Analyze Data: It combines the expected sources with the observed telemetry and connector failures. It calculates the freshness of the data and determines the availability state of each source based on several conditions:
- If no events are observed or there are connector errors, it marks the source as "NOT_CONNECTED" or "CONNECTOR_ERROR".
- If data is stale or missing, it marks it as "NO_DATA" or "DELAYED".
- If everything is functioning as expected, it marks the source as "HEALTHY".
-
Output: It projects relevant fields like the source table, availability state, event counts, timestamps, freshness metrics, and error details, and limits the output to 100 records.
Overall, this query helps in assessing the operational status of AWS data sources, ensuring they are connected and providing timely data.