powershell 通过Powershell进行AWS Cloudwatch指标
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 通过Powershell进行AWS Cloudwatch指标相关的知识,希望对你有一定的参考价值。
Import-Module AWSPowerShell
Function PushMetrics() {
try
{
Write-CWMetricData -Namespace $Namespace -MetricData $metrics -Verbose
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Output $ErrorMessage | Out-File -FilePath $logpath -Append
}
$metrics = @();
}
#Feel free to change this to match any existing namespaces you might have.
$Namespace = 'Custom-Services'
#Update the log path, use this for troubleshooting.
$logpath = "C:\AWS Cloudwatch\Log.txt"
# Associate current EC2 instance with your custom cloudwatch metric
$instanceDimension = New-Object -TypeName Amazon.CloudWatch.Model.Dimension;
$instanceDimension.Name = "Host";
$instanceDimension.Value = $env:computername;
Write-Output "$([DateTime]::Now)" | Out-File -FilePath $logpath -Append
$metrics = @();
# Windows Services
$services = Get-Service -Name *custom*
$services | Out-File -FilePath $logpath -Append
# For each service, add a metric to metrics collection that adds a data point to a CloudWatch Metric named 'Status' with dimensions: instanceid, servicename
$services | % {
$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 = if ($_.Status -eq 'Running') {1} ELSE {0}
$metric.Dimensions = $dimensions;
$metrics += $metric;
PushMetrics;
}
# Scheduled Tasks
$schtask = schtasks.exe /query /V /FO CSV | ConvertFrom-Csv
if ($schtask)
{
foreach ($sch in $schtask)
{
if (($sch.TaskName).ToLower() -like "*custom*")
{
$dte = Get-Date;
$cutoff = [DateTime]$dte.AddHours(-3);
$dimensions = @();
$serviceDimension = New-Object -TypeName Amazon.CloudWatch.Model.Dimension;
$serviceDimension.Name = "Service"
$serviceDimension.Value = $sch.TaskName;
$dimensions += $instanceDimension;
$dimensions += $serviceDimension;
$metric = New-Object -TypeName Amazon.CloudWatch.Model.MetricDatum;
$metric.Timestamp = [DateTime]::UtcNow;
$metric.MetricName = 'Status';
$status = 1;
if ([DateTime]$sch."Last Run Time" -lt $cutoff)
{
$status = 0;
}
$metric.Value = $status;
$metric.Dimensions = $dimensions;
$metrics += $metric;
PushMetrics;
}
}
}
以上是关于powershell 通过Powershell进行AWS Cloudwatch指标的主要内容,如果未能解决你的问题,请参考以下文章
powershell 通过不断增长的阵列进行迭代
powershell 通过不断增长的阵列进行迭代
通过 Powershell 对多个 AD 对象属性进行排序
Powershell通过变量数组批量添加保留地址
-print 通过 npm 打印出字符串,但实际上通过 powershell 进行评估
PowerShell初级篇●Powershell管道