简体   繁体   English

使用AWS CloudWatch监视EC2 Windows实例的服务

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

I have monitored performance counters such as memory, free disk etc using CloudWatch custom metrics. 我使用CloudWatch自定义指标监控了性能计数器,例如内存,可用磁盘等。 Can I monitor the services using CloudWatch? 我可以使用CloudWatch监视服务吗? I have checked the features which cloud watch monitors but found nothing related to monitoring services. 我检查了云监视所监视的功能,但未发现与监视服务相关的内容。 I just need to monitor whether the service is running or not and send a notification when the state of the service changes. 我只需要监视服务是否正在运行,并在服务状态更改时发送通知即可。

Yes, but out-of-the-box solutions like the EC2Config Windows Integration you alluded to aren't as readily available for service-level custom metrics. 是的,但是您提到的现成的解决方案(例如EC2Config Windows集成)不适用于服务级别的自定义指标。

CloudWatch Custom Metrics allow you to extend CloudWatch with your own defined metrics and data, so you can reasonable implement them yourselves to monitor your own services. CloudWatch自定义指标允许您使用自己定义的指标和数据扩展CloudWatch,因此您可以合理地实现它们以监控自己的服务。 Your service can write metrics data to CloudWatch itself, or you can write another process that monitors your service and writes metrics based on responses from your service to CloudWatch. 您的服务可以将指标数据写入CloudWatch本身,也可以编写另一个流程来监视您的服务,并根据服务对CloudWatch的响应写入指标。


Per your edits, to publish CloudWatch custom metrics for an arbitrary set of windows services will require some windows-specific powershell, because we can't assume that the service will have a web endpoint to ping. 根据您的编辑,要发布任意一组Windows服务的CloudWatch自定义指标,将需要一些特定于Windows的Powershell,因为我们无法假定该服务将具有可ping的Web终结点。

You'll want to create a service monitor that evaluates your services via Get-Service , and then publishes a data point to a CloudWatch custom metrics if they are running. 您将要创建一个服务监视器,该监视器通过Get-Service评估您Get-Service ,然后将数据点发布到CloudWatch自定义指标(如果它们正在运行)。

Here is an example implementation in PowerShell that will write custom metrics for services with a name matching *YOURSERVICENAMESHERE* every 300 seconds. 这是PowerShell中的示例实现,它将每300秒为名称匹配*YOURSERVICENAMESHERE*服务编写自定义指标。 If you want to run this for every service on the EC2 instance , you can replace this with the wildcard * , but this may be expensive at scale. 如果要对EC2实例上的每个服务运行此命令,则可以将其替换为通配符* ,但是这样做可能会花费很大。 It may also require some tweaking if too many services are on the box, because you can only send so many metrics at a time via Write-CwMetricData . 如果包装盒上有太多服务,则可能还需要进行一些调整,因为您一次只能通过Write-CwMetricData发送这么多度量标准。 See code comments for details on that. 有关详细信息,请参见代码注释。

By only creating a data point on success, you establish a 'failure' condition (INSUFFICIENT_DATA for X seconds) that you can use to create CloudWatch Alarms that satisfy your notification constraint. 通过仅在成功时创建数据点,即可建立“失败”条件(X秒钟为INSUFFICIENT_DATA),可用于创建满足通知约束的CloudWatch警报。

This script MUST be run on a Windows EC2 instance with AWS Tools for PowerShell installed and configured: 此脚本必须在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
}

Save this to a file and you can run it from the command line to begin writing metrics. 将其保存到文件中,然后可以从命令行运行它以开始编写指标。 Once you're comfortable with it, feel free to ditch the "while true" loop for a scheduled task or powershell job. 一旦您适应了它,就可以随意放弃计划任务或Powershell作业的“ while true”循环。

Additional Resources: 其他资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM