我已经使用CloudWatch自定义度量来监视性能计数器,例如内存、空闲磁盘等。我可以使用CloudWatch监视服务吗?我已经检查过云表监视器的功能,但没有发现与监视服务有关的任何功能。我只需要监视服务是否正在运行,并在服务状态发生变化时发送通知。
发布于 2017-01-24 20:43:28
是的,但是像您提到的EC2Config Windows集成这样的开箱即用的解决方案并不能很容易地用于服务级别的自定义度量。
CloudWatch自定义度量允许您使用自己定义的度量和数据扩展CloudWatch,因此您可以自己合理地实现它们以监视您自己的服务。您的服务可以将度量数据写入CloudWatch本身,也可以编写另一个过程来监视您的服务,并根据从您的服务到CloudWatch的响应来编写度量。
根据您的编辑,要发布任意一组windows服务的CloudWatch自定义度量标准,将需要一些特定于windows的powershell,因为我们不能假设该服务将有一个指向ping的web端点。
您将希望创建一个服务监视器,该监视器通过Get-Service评估您的服务,然后如果它们正在运行,则将数据点发布到CloudWatch自定义度量标准。
下面是PowerShell中的一个示例实现,它将每300秒钟为具有匹配*YOURSERVICENAMESHERE*名称的服务编写自定义度量。如果您想要对EC2实例上的每个服务运行它,可以用通配符*替换它,但是这在规模上可能很昂贵。如果盒子上有太多的服务,也可能需要进行一些调整,因为一次只能通过Write-CwMetricData发送这么多的度量。有关详细信息,请参阅代码注释。
仅在成功时创建一个数据点,就可以建立一个“失败”条件(INSUFFICIENT_DATA为X秒),用于创建满足通知约束的CloudWatch警报。
必须在安装和配置了EC2的Windows 用于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“循环。
附加资源:
https://stackoverflow.com/questions/41833050
复制相似问题