PowerShell性能监控告警

时间:2022-09-28 08:50:31

实际上,我们可以通过PowerShell来获取性能计数器的值,比如当计算机可用内存小于500MB时,则自动发送邮件告警。

在PowerShell中,并没有专门的命令来获取性能计数器的值,也无法直接发送邮件,都需要先创建.Net Framework类的实例。

1)获取性能计数器代码如下:

#新建性能计数器对象

$counter = New-Object Diagnostics.PerformanceCounter

#添加计数器,此处监视内存性能

$counter.CategoryName = "Memory"

#添加计数器属性值,此处监视可用内存

$counter.CounterName = "Available MBytes"

#获取当前计数器的值

$value = $counter.NextValue()

2)发送邮件代码如下:

#定义SMTP服务器地址,发件人账户及其密码

$smtpServer = "smtp.163.com"

$smtpUser = "sender@163.com"

$smtpPassword = "mypasswd"

 

$mail = New-Object System.Net.Mail.MailMessage

#定义发件人邮箱地址、收件人邮箱地址

$MailAddress="sender@163.com"

$MailtoAddress="receiver@126.com"

$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)

$mail.To.Add($MailtoAddress)

 

#定义邮件标题、优先级和正文

$mail.Subject = "警报";

$mail.Priority  = "High"

$mail.Body = "PowerShell发送邮件测试!"

 

完整的代码如下:

#添加性能计数器

$counter = New-Object Diagnostics.PerformanceCounter

$counter.CategoryName = "Memory"

$counter.CounterName = "Available MBytes"

$value = $counter.NextValue()

#设置预设阀值

$prevalue = 500

 

#定义SMTP服务器地址,发件人账户及其密码

$smtpServer = "smtp.163.com"

$smtpUser = "sender@163.com"

$smtpPassword = "mypasswd"

 

$mail = New-Object System.Net.Mail.MailMessage

#定义发件人邮箱地址、收件人邮箱地址

$MailAddress="sender@163.com"

$MailtoAddress="receiver@qq.com"

$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)

$mail.To.Add($MailtoAddress)

 

#定义邮件标题、优先级和正文

$mail.Subject = "警报";

$mail.Priority  = "High"

$mail.Body = "XX值已达到$value,已超出预设阀值$prevalue!"

 

#判断是否超过预设阀值,如超出则发邮件报警

If ( $value -gt $prevalue)

{

 $smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer

 $smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword

 $smtp.Send($mail)

}

 

 

===============================================

#查看当前CPU利用率

 1 $counter = New-Object Diagnostics.PerformanceCounter
 2 
 3 $counter.CategoryName = "Processor"
 4 
 5 $counter.CounterName = "% Processor Time"
 6 
 7 $counter.InstanceName = "_Total"
 8 
 9 while ($true)
10 
11 {
12 
13 $value = $counter.NextValue()
14 
15 Write-Host "CPU: $value"
16 
17 sleep 1
18 
19 }

 

#查看进程的CPU利用率:

1 #取CPU核心数量,如果计算机为多核心的话,Process/% Processor Time的结果为每个核心的CPU利用率之和,会大于100%
2 $cpus = (gwmi win32_computersystem).NumberOfLogicalProcessors
3  
4 $counter = New-Object Diagnostics.PerformanceCounter
5 $counter.CategoryName = "Process"
6 $counter.CounterName = "% Processor Time"
7 $counter.InstanceName = "winrar" #定义需要监视的进程
8 #获取当前计数器的值
9 $counter.NextValue()/$cpus

 

NumberOfCores:表示CPU核心数
NumberOfLogicalProcessors:表示CPU线程数

 

#查看进程的内存使用情况:

1 $TotalMemory = (Get-WmiObject -Class Win32_PhysicalMemory |measure capacity -sum).Sum   #(gwmi win32_computersystem).TotalPhysicalMemory
2 
3 $counter = New-Object Diagnostics.PerformanceCounter
4 $counter.CategoryName = "Process"
5 $counter.CounterName = "Working Set - Private"
6 $counter.InstanceName = "ZQErpService" #定义需要监视的进程
7 #获取当前计数器的值
8 
9 $counter.NextValue()/$TotalMemory