![](/img/trans.png)
[英]Monitoring services of EC2 Windows Service status running/stopped using AWS CloudWatch
[英]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.