Skip to content

Instantly share code, notes, and snippets.

@luhaoming
Created January 18, 2018 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luhaoming/4cfc131e783723bb0e59ca9f488ab57b to your computer and use it in GitHub Desktop.
Save luhaoming/4cfc131e783723bb0e59ca9f488ab57b to your computer and use it in GitHub Desktop.
cpualert.vbs
'==== summary =====
' 每10分鐘測試CPU使用率, 若連續測試超過10次 則透過SMTP寄送警訊給管理者
' 如有需要, 修改以下內容
' objMessage.Subject 警訊內容
' objMessage.From 寄件人
' objMessage.To    收件人
' objMessage.TextBody 警訊內容
' 提供寄信的SMTP主機
' objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver
'==== summary =====
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
intThresholdViolations = 0
objRefresher.Refresh
Do
For each intProcessorUse in objProcessor
If intProcessorUse.PercentProcessorTime > 90 Then
intThresholdViolations = intThresholdViolations + 1
If intThresholdViolations = 10 Then
intThresholdViolations = 0
'Wscript.Echo "Processor usage threshold exceeded."
SendAlert
End If
Else
intThresholdViolations = 0
End If
Next
Wscript.Sleep 600000
objRefresher.Refresh
Loop
Sub SendAlert
'== 寄送email ==
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "ISUIR CPU Alert"
objMessage.From = """ISUIR_Alert"" <haoming@microsoft.com>"
objMessage.To = "haoming@microsoft.com"
objMessage.TextBody = "ISUIR CPU overloading .." & vbCRLF & "Please pay attention on the machine ."
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.microsoft.com"
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment