如何通过某种警报检查Azure VM是否在特定时间运行?

时间:2017-05-16 00:51:10

标签: azure azure-virtual-machine

我想知道如何为Azure VM创建警报,告诉我服务器是否在特定时间运行。

场景: Azure网络的服务器需要在早上7:30开始为用户做好准备,因为他们每天晚上7:30关闭以节省$$。 今天,azure自动化脚本找不到资源组的任何vms!这意味着服务器没有启动。我想创建一个警报,它只会告诉我服务器是否在早上7:45没有运行。所以我可以启动它们。 (现在运行脚本现在确实找到了所有服务器,但之前由于某种原因没有...可能Azure正在移动资源组中的vms?)

我看过: - Microsoft Operations Management Suit>日志搜索>添加警报规则。 - 资源管理器>虚拟机>监控>警报规则>添加metic alert&添加活动日志警报。 但我无法确定在特定时间只运行警报的位置。

更新/编辑: 使用的脚本:

param ( 
    [Parameter(Mandatory=$false)]  
    [String]$AzureCredentialAssetName = 'AzureCred', 

    [Parameter(Mandatory=$false)]  
    [String]$AzureSubscriptionIDAssetName = 'AzureSubscriptionId'
) 

# Setting error and warning action preferences 
$ErrorActionPreference = "SilentlyContinue" 
$WarningPreference = "SilentlyContinue" 

# Connecting to Azure 
$Cred = Get-AutomationPSCredential -Name $AzureCredentialAssetName -ErrorAction Stop 
$null = Add-AzureAccount -Credential $Cred -ErrorAction Stop -ErrorVariable err 
$null = Add-AzureRmAccount -Credential $Cred -ErrorAction Stop -ErrorVariable err 

# Selecting the subscription to work against 
$SubID = Get-AutomationVariable -Name $AzureSubscriptionIDAssetName 
Select-AzureRmSubscription -SubscriptionId $SubID 

# Getting all resource groups 
$ResourceGroup = "Servers"

# Getting all virtual machines 
$RmVMs = (Get-AzureRmVM -ResourceGroupName $ResourceGroup -ErrorAction $ErrorActionPreference -WarningAction $WarningPreference).Name 

# Managing virtual machines deployed with the Resource Manager deployment model
"Loop through all VMs in resource group $ResourceGroup."
if ($RmVMs) 
{ 
    foreach ($RmVM in $RmVMs) 
    { 
        "`t$RmVM found ..." 
        $RmPState = (Get-AzureRmVM -ResourceGroupName $ResourceGroup -Name $RmVM -Status -ErrorAction $ErrorActionPreference -WarningAction $WarningPreference).Statuses.Code[1]
        if ($RmPState -eq 'PowerState/deallocated') 
        { 
            "`t$RmVM is starting up ..." 
            $RmSState = (Start-AzureRmVM -ResourceGroupName $ResourceGroup -Name $RmVM -ErrorAction $ErrorActionPreference -WarningAction $WarningPreference).IsSuccessStatusCode 

            if ($RmSState -eq 'True') 
            { 
                "`t$RmVM has been started." 
            } 
            else 
            { 
                "`t$RmVM failed to start." 
            } 
        }               
    } 
}     
else
{
    "No VMs for $ResourceGroup deployed with the Resource Manager deployment model."  
}
"Runbook Completed."

我只想要一个故障保险,知道服务器是否应该运行。

预期产出:

Loop through all VMs in resource group Servers.

    DOMAINCONTROLLER found ...

    SQLSERVER found ...

    GATEWAY found ...

    APPLICATIONHOST found ...

Runbook Completed.

而不是:

Loop through all VMs in resource group Servers.

No VMs for Servers deployed with the Resource Manager deployment model.

Runbook Completed.

即。手动重新运行相同的脚本给出了预期的结果。

1 个答案:

答案 0 :(得分:0)

据我所知,当您的VM在特定时间启动或停止时,Azure指标警报无法发送邮件。

根据您的描述,Start/Stop VMs during off-hours [Preview] solution in Automation是省钱的好方法。

非工作时间内的启动/停止虚拟机[预览]解决方案根据用户定义的计划启动和停止Azure资源管理器虚拟机,并深入了解使用OMS启动和停止虚拟机的自动化作业的成功Log Analytics。

它可以在启动和停止VM Runbook完成时发送电子邮件通知。

您还可以使用Azure自动化在特定时间启动或停止VM,有关详细信息,请参阅此link

更新

你的脚本适合我。我检查你的脚本,$RmVMs = (Get-AzureRmVM -ResourceGroupName $ResourceGroup -ErrorAction $ErrorActionPreference -WarningAction $WarningPreference).Name返回null。你有多个订阅吗?您的订阅ID似乎是错误的。请确保您的订阅ID正确无误。您可以在本地PowerShell上获取订阅ID。

Get-AzureRmSubscription

UPDATE2:

您可以保存订阅自动连接。您可以使用以下脚本:

param ( 
    [Parameter(Mandatory=$false)]  
    [String]$AzureCredentialAssetName 

) 

# Authenticate to Azure with certificate
Write-Verbose "Get connection asset: $ConnectionAssetName" -Verbose
$connectionName = Get-AutomationConnection -Name $AzureCredentialAssetName
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $AzureCredentialAssetName        

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

$ResourceGroup = "shui2"
.....

enter image description here