使用 AWS CloudWatch 监控 EC2 Windows 实例的服务 [英] Monitoring services of EC2 Windows instance using AWS CloudWatch

查看:187
本文介绍了使用 AWS CloudWatch 监控 EC2 Windows 实例的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have monitored performance counters such as memory, free disk etc using CloudWatch custom metrics. Can I monitor the services using 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.

推荐答案

是的,但开箱即用的解决方案,例如 EC2Config Windows 集成对于服务级别的自定义指标并不那么容易获得.

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.

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

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. 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.

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

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.

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

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.

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

Here is an example implementation in PowerShell that will write custom metrics for services with a name matching *YOURSERVICENAMESHERE* every 300 seconds. 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. 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. See code comments for details on that.

仅在成功时创建数据点,即可建立失败"条件(X 秒的 INSUFFICIENT_DATA),可用于创建满足通知约束的 CloudWatch 警报.

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.

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

This script MUST be run on a Windows EC2 instance with AWS Tools for PowerShell installed and configured:

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"循环.

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.

其他资源:

  • AWS Documentation - Scenario: Publish Metrics to CloudWatch - A tutorial/walkthrough for publishing custom metrics for a hypothetical application. This would be a good place to get started learning how to publish your own custom metrics.
  • AWS Documentation - Publish Custom Metrics
  • MSDN - Get-Service Cmdlet Reference
  • AWS Tools For PowerShell Documentation - Write-CWMetricData

这篇关于使用 AWS CloudWatch 监控 EC2 Windows 实例的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆