【问题标题】:Monitoring services of EC2 Windows instance using AWS CloudWatch使用 AWS CloudWatch 监控 EC2 Windows 实例的服务
【发布时间】:2017-01-24 16:14:58
【问题描述】:

我使用 CloudWatch 自定义指标监控了内存、可用磁盘等性能计数器。我可以使用 CloudWatch 监控服务吗?我检查了云手表监控的功能,但没有发现与监控服务相关的内容。我只需要监控服务是否正在运行,并在服务状态发生变化时发送通知。

【问题讨论】:

    标签: amazon-web-services amazon-ec2 monitoring amazon-cloudwatch


    【解决方案1】:

    是的,但是像您提到的 EC2Config Windows Integration 这样的开箱即用解决方案并不容易用于服务级别自定义指标。

    CloudWatch 自定义指标允许您使用自己定义的指标和数据扩展 CloudWatch,因此您可以自己合理地实施它们来监控自己的服务。您的服务可以将指标数据写入 CloudWatch 本身,或者您可以编写另一个进程来监控您的服务并根据您的服务对 CloudWatch 的响应写入指标。


    根据您的编辑,为任意一组 Windows 服务发布 CloudWatch 自定义指标将需要一些特定于 Windows 的 powershell,因为我们不能假设该服务将具有要 ping 的 Web 端点。

    您需要创建一个服务监视器,通过 Get-Service 评估您的服务,然后将数据点发布到 CloudWatch 自定义指标(如果它们正在运行)。

    这是 PowerShell 中的示例实现,它将每 300 秒为名称匹配 *YOURSERVICENAMESHERE* 的服务编写自定义指标。如果您想为EC2 实例上的每项服务运行它,您可以用通配符* 替换它,但这在规模上可能会很昂贵。如果盒子上有太多服务,可能还需要进行一些调整,因为您一次只能通过Write-CwMetricData 发送这么多指标。有关详细信息,请参见代码 cmets。

    通过仅在成功时创建数据点,您可以建立“失败”条件(INSUFFICIENT_DATA 持续 X 秒),您可以使用该条件创建满足通知约束的 CloudWatch 警报。

    此脚本必须在安装和配置AWS Tools for PowerShell 的 Windows EC2 实例上运行:

    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
    }
    

    将此保存到文件中,您可以从命令行运行它以开始编写指标。一旦您对它感到满意,请随意放弃“while true”循环以执行计划任务或 powershell 作业。

    其他资源:

    【讨论】:

    • 我的要求不太复杂。它必须只监视服务是否正在运行。这样,它必须监控 ec2 实例的 Services 列下的所有服务。而且,还有一件事,我想为 Windows 实例执行此操作。
    • 我已经修改了我的问题。请检查一下。
    • 确保您的实例正在运行最新版本的适用于 PowerShell 的 AWS 工具(以便您拥有所有正确的类定义)并确保您为其提供了适当的凭证,以便它可以写入Cloudwatch。
    • 关于凭据,请查看此文档了解详细信息:docs.aws.amazon.com/powershell/latest/userguide/…
    • @prudhvi 太棒了!听起来你几乎得到了它。这个脚本的设计方式,应该只为运行服务添加一个数据点,这样的行为是预期的。这确实改变了图表——缺少新数据点本身就是数据。使用 CloudWatch 警报,您可以通过对 CloudWatch 中称为“INSUFFICIENT_DATA”的状态发出警报来封装它,这将让您知道您的服务是否停止运行,或者服务监视器本身是否停止。例如,对于您的服务,您可以为此指标创建警报,其中在某个定义的时间间隔内没有可用数据。
    猜你喜欢
    • 2017-05-10
    • 2018-10-23
    • 2016-09-23
    • 2016-11-22
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    相关资源
    最近更新 更多