簡體   English   中英

使用AWS CloudWatch監視EC2 Windows實例的服務

[英]Monitoring services of EC2 Windows instance using AWS CloudWatch

我使用CloudWatch自定義指標監控了性能計數器,例如內存,可用磁盤等。 我可以使用CloudWatch監視服務嗎? 我檢查了雲監視所監視的功能,但未發現與監視服務相關的內容。 我只需要監視服務是否正在運行,並在服務狀態更改時發送通知即可。

是的,但是您提到的現成的解決方案(例如EC2Config Windows集成)不適用於服務級別的自定義指標。

CloudWatch自定義指標允許您使用自己定義的指標和數據擴展CloudWatch,因此您可以合理地實現它們以監控自己的服務。 您的服務可以將指標數據寫入CloudWatch本身,也可以編寫另一個流程來監視您的服務,並根據服務對CloudWatch的響應寫入指標。


根據您的編輯,要發布任意一組Windows服務的CloudWatch自定義指標,將需要一些特定於Windows的Powershell,因為我們無法假定該服務將具有可ping的Web終結點。

您將要創建一個服務監視器,該監視器通過Get-Service評估您Get-Service ,然后將數據點發布到CloudWatch自定義指標(如果它們正在運行)。

這是PowerShell中的示例實現,它將每300秒為名稱匹配*YOURSERVICENAMESHERE*服務編寫自定義指標。 如果要對EC2實例上的每個服務運行此命令,則可以將其替換為通配符* ,但是這樣做可能會花費很大。 如果包裝盒上有太多服務,則可能還需要進行一些調整,因為您一次只能通過Write-CwMetricData發送這么多度量標准。 有關詳細信息,請參見代碼注釋。

通過僅在成功時創建數據點,即可建立“失敗”條件(X秒鍾為INSUFFICIENT_DATA),可用於創建滿足通知約束的CloudWatch警報。

此腳本必須在Windows EC2實例上運行,其中安裝並配置了適用於PowerShell的AWS工具

Param
(
    [string]$Period = 300,
    [string]$Namespace = 'service-monitor'
)

# Use the EC2 metadata service to get the host EC2 instance's ID
$instanceId = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")

# Associate current EC2 instance with your custom cloudwatch metric
$instanceDimension = New-Object -TypeName Amazon.CloudWatch.Model.Dimension;
$instanceDimension.Name = "instanceid";
$instanceDimension.Value = $instanceId;

# "Job" loop; write to CloudWatch and then sleep for the interval defined by the period variable above, in seconds.
while($true)
{
    $metrics = @();

    $runningServices = Get-Service -Name *YOURSERVICENAMESHERE* | ? { $_.Status -eq 'Running' }

    # For each running service, add a metric to metrics collection that adds a data point to a CloudWatch Metric named 'Status' with dimensions: instanceid, servicename
    $runningServices | % { 
        $dimensions = @();

        $serviceDimension = New-Object -TypeName Amazon.CloudWatch.Model.Dimension;
        $serviceDimension.Name = "service"
        $serviceDimension.Value = $_.Name;

        $dimensions += $instanceDimension;
        $dimensions += $serviceDimension;

        $metric = New-Object -TypeName Amazon.CloudWatch.Model.MetricDatum;
        $metric.Timestamp = [DateTime]::UtcNow;
        $metric.MetricName = 'Status';
        $metric.Value = 1;
        $metric.Dimensions = $dimensions;

        $metrics += $metric;       

        Write-Host "Checking status for: $($_.Name)"        
    }

    # Write all of the metrics for this run of the job at once, to save on costs for calling the CloudWatch API.
    # This will fail if there are too many services in metrics collection; if this happens, just reduce the amount of
    # services monitored, or edit this line into the above foreach loop and write each metric directly.
    Write-CWMetricData -Namespace $Namespace -MetricData $metrics

    Write-Host "Sleeping for $Period seconds."

    Start-Sleep -s $Period
}

將其保存到文件中,然后可以從命令行運行它以開始編寫指標。 一旦您適應了它,就可以隨意放棄計划任務或Powershell作業的“ while true”循環。

其他資源:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM