我使用CloudWatch自定义指标监控了内存,免费磁盘等性能计数器。我可以使用CloudWatch监控服务吗?我已经检查了云监视器监视的功能,但没有发现任何与监视服务相关的功能。我只需要监视服务是否正在运行,并在服务状态发生变化时发送通知。
答案 0 :(得分:4)
是的,但您提到的EC2Config Windows Integration等开箱即用的解决方案并不适用于服务级别的自定义指标。
CloudWatch自定义指标允许您使用自己定义的指标和数据扩展CloudWatch,因此您可以合理地实施它们以监控自己的服务。您的服务可以将指标数据写入CloudWatch本身,或者您可以编写另一个监控服务的流程,并根据您的服务对CloudWatch的响应编写指标。
根据您的编辑,为任意一组Windows服务发布CloudWatch自定义指标将需要一些特定于Windows的PowerShell,因为我们不能假设该服务将具有要ping的Web端点。
您需要创建一个服务监视器,通过Get-Service
评估您的服务,然后将数据点发布到CloudWatch自定义指标(如果它们正在运行)。
以下是PowerShell中的一个示例实现,它将为每300秒名称匹配*YOURSERVICENAMESHERE*
的服务编写自定义指标。如果要为 EC2实例上的每个服务运行此操作,可以将其替换为通配符*
,但这在规模上可能很昂贵。如果包含太多服务,也可能需要进行一些调整,因为您只能通过Write-CwMetricData
一次发送这么多指标。有关详细信息,请参阅代码注释。
通过仅在成功时创建数据点,您可以建立“失败”条件(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工作。
其他资源: