function Get-IsAlertNotSameasConfigured {
param(
[PSCustomObject]$existingRule,
[PSCustomObject]$configuredAlertDataSource
)
if(($existingRule.DataSource.MetricName -eq $configuredAlertDataSource.metric) -and ($existingRule.Operator -eq $configuredAlertDataSource.operator) -and ($existingRule.Threshold -eq $configuredAlertDataSource.threshold) -and ($existingRule.TimeAggregation -eq $configuredAlertDataSource.timeAggregationOperator) -and ($existingRule.WindowsSize -eq $configuredAlertDataSource.windowSize))
{
return $true
}
else
{
return $false
}
}
$resoureName = "Testing123girish"
$resourceGroupName = "Testing123girish"
$resource = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceName $resoureName
$existingRules = Get-AzureRmAlertRule -ResourceGroup $resourceGroupName -TargetResourceId $resource.ResourceId -DetailedOutput
$configuredAlerts = (Get-Content -Raw -Path C:\Users\v-gigoud\Desktop\xslt\alert.json | ConvertFrom-Json).alerts
if($existingRules -eq $null)
{
$metricDefintions = Get-AzureRmMetricDefinition -ResourceId $resource.ResourceId
foreach($configuredAlert in $configuredAlerts)
{
$metricPresent= $false
foreach($metricDefintion in $metricDefintions)
{
if($metricDefintion.Name.LocalizedValue -contains $configuredAlert.metric)
{
$metricPresent= $true
}
}
if(!$metricPresent)
{
write-host "Metrics $($configuredAlert.metric) Not found for this resource, Skipping adding the alert rule"
}
else
{
write-host "Metrics $($configuredAlert.metric) found for this resource, Adding the alert rule"
$actionEmail = New-AzureRmAlertRuleEmail -CustomEmail myname@company.com
Add-AzureRmMetricAlertRule -Name ($configuredAlert.metric + $configuredAlert.operator + $configuredAlert.threshold) `
-ResourceGroup $resourceGroupName `
-Location $resource.Location `
-TargetResourceId $resource.ResourceId `
-MetricName $configuredAlert.metric`
-Operator $configuredAlert.operator `
-Threshold $configuredAlert.threshold `
-WindowSize $configuredAlert.windowSize `
-TimeAggregationOperator $configuredAlert.timeAggregationOperator `
-Description ($configuredAlert.metric) `
-Actions $actionEmail
}
}
}
else
{
write-host "The existing rules will be checked with the configuration specified"
foreach($existingRule in $existingRules)
{
write-host "The configuration alerts will be checked for existing rule $($existingRule.Name)"
$metricFound = $false
foreach($configuredAlert in $configuredAlerts)
{
if(Get-IsAlertNotSameasConfigured $existingRule.Properties.Condition $configuredAlert)
{
$metricFound = $true
break
}
else
{
if($metricFound -eq $true)
{
write-host "The alerts are already present $($existingRule.Name)"
}
else
{
$metricDefintions = Get-AzureRmMetricDefinition -ResourceId $resource.ResourceId
foreach($configuredAlert in $configuredAlerts)
{
$metricPresent= $false
foreach($metricDefintion in $metricDefintions)
{
if($metricDefintion.Name.LocalizedValue -contains $configuredAlert.metric)
{
$metricPresent= $true
}
}
if(!$metricPresent)
{
write-host "Metrics $($configuredAlert.metric) Not found for this resource, Skipping adding the alert rule"
}
else
{
write-host "Metrics $($configuredAlert.metric) found for this resource, Adding the alert rule"
$actionEmail = New-AzureRmAlertRuleEmail -CustomEmail myname@company.com
Add-AzureRmMetricAlertRule -Name ($configuredAlert.metric + $configuredAlert.operator + $configuredAlert.threshold) `
-ResourceGroup $resourceGroupName `
-Location $resource.Location `
-TargetResourceId $resource.ResourceId `
-MetricName $configuredAlert.metric`
-Operator $configuredAlert.operator `
-Threshold $configuredAlert.threshold `
-WindowSize $configuredAlert.windowSize `
-TimeAggregationOperator $configuredAlert.timeAggregationOperator `
-Description ($configuredAlert.metric) `
-Actions $actionEmail
}
}
}
}
}
}
}
param(
[PSCustomObject]$existingRule,
[PSCustomObject]$configuredAlertDataSource
)
if(($existingRule.DataSource.MetricName -eq $configuredAlertDataSource.metric) -and ($existingRule.Operator -eq $configuredAlertDataSource.operator) -and ($existingRule.Threshold -eq $configuredAlertDataSource.threshold) -and ($existingRule.TimeAggregation -eq $configuredAlertDataSource.timeAggregationOperator) -and ($existingRule.WindowsSize -eq $configuredAlertDataSource.windowSize))
{
return $true
}
else
{
return $false
}
}
$resoureName = "Testing123girish"
$resourceGroupName = "Testing123girish"
$resource = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceName $resoureName
$existingRules = Get-AzureRmAlertRule -ResourceGroup $resourceGroupName -TargetResourceId $resource.ResourceId -DetailedOutput
$configuredAlerts = (Get-Content -Raw -Path C:\Users\v-gigoud\Desktop\xslt\alert.json | ConvertFrom-Json).alerts
if($existingRules -eq $null)
{
$metricDefintions = Get-AzureRmMetricDefinition -ResourceId $resource.ResourceId
foreach($configuredAlert in $configuredAlerts)
{
$metricPresent= $false
foreach($metricDefintion in $metricDefintions)
{
if($metricDefintion.Name.LocalizedValue -contains $configuredAlert.metric)
{
$metricPresent= $true
}
}
if(!$metricPresent)
{
write-host "Metrics $($configuredAlert.metric) Not found for this resource, Skipping adding the alert rule"
}
else
{
write-host "Metrics $($configuredAlert.metric) found for this resource, Adding the alert rule"
$actionEmail = New-AzureRmAlertRuleEmail -CustomEmail myname@company.com
Add-AzureRmMetricAlertRule -Name ($configuredAlert.metric + $configuredAlert.operator + $configuredAlert.threshold) `
-ResourceGroup $resourceGroupName `
-Location $resource.Location `
-TargetResourceId $resource.ResourceId `
-MetricName $configuredAlert.metric`
-Operator $configuredAlert.operator `
-Threshold $configuredAlert.threshold `
-WindowSize $configuredAlert.windowSize `
-TimeAggregationOperator $configuredAlert.timeAggregationOperator `
-Description ($configuredAlert.metric) `
-Actions $actionEmail
}
}
}
else
{
write-host "The existing rules will be checked with the configuration specified"
foreach($existingRule in $existingRules)
{
write-host "The configuration alerts will be checked for existing rule $($existingRule.Name)"
$metricFound = $false
foreach($configuredAlert in $configuredAlerts)
{
if(Get-IsAlertNotSameasConfigured $existingRule.Properties.Condition $configuredAlert)
{
$metricFound = $true
break
}
else
{
if($metricFound -eq $true)
{
write-host "The alerts are already present $($existingRule.Name)"
}
else
{
$metricDefintions = Get-AzureRmMetricDefinition -ResourceId $resource.ResourceId
foreach($configuredAlert in $configuredAlerts)
{
$metricPresent= $false
foreach($metricDefintion in $metricDefintions)
{
if($metricDefintion.Name.LocalizedValue -contains $configuredAlert.metric)
{
$metricPresent= $true
}
}
if(!$metricPresent)
{
write-host "Metrics $($configuredAlert.metric) Not found for this resource, Skipping adding the alert rule"
}
else
{
write-host "Metrics $($configuredAlert.metric) found for this resource, Adding the alert rule"
$actionEmail = New-AzureRmAlertRuleEmail -CustomEmail myname@company.com
Add-AzureRmMetricAlertRule -Name ($configuredAlert.metric + $configuredAlert.operator + $configuredAlert.threshold) `
-ResourceGroup $resourceGroupName `
-Location $resource.Location `
-TargetResourceId $resource.ResourceId `
-MetricName $configuredAlert.metric`
-Operator $configuredAlert.operator `
-Threshold $configuredAlert.threshold `
-WindowSize $configuredAlert.windowSize `
-TimeAggregationOperator $configuredAlert.timeAggregationOperator `
-Description ($configuredAlert.metric) `
-Actions $actionEmail
}
}
}
}
}
}
}
No comments:
Post a Comment