· 9 years ago · Feb 02, 2017, 08:14 PM
1# Sends our Alert Mail
2function sendMail($AlertType, $body) {
3 #SMTP server name
4 $smtpServer = "smtp.gmail.com"
5 $SMTPPort = "587"
6 $Username = "@gmail.com"
7 $Password = ""
8
9 #Creating a Mail object
10 $msg = New-Object System.Net.Mail.MailMessage
11
12 #Creating SMTP server object
13 $smtp = New-Object System.Net.Mail.SmtpClient($smtpServer,$SMTPPort)om
14 $smtp.EnableSSL = $true
15 $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
16
17 #Email structure
18 $msg.From = "$env:COMPUTERNAME@domain.com"
19 $msg.ReplyTo = "No-Reply@domain.com"
20 $msg.To.Add("ADMIN@domain.com")
21 $msg.Subject = "Dell OMSA $AlertType Alert on $env:COMPUTERNAME"
22 $msg.body = $body
23
24 #Sending email
25 $smtp.Send($msg)
26
27}
28
29# Kicks Off OM Alert Config Commands for all Warnings/Failures
30Function Setup(){
31 # Define our command String
32 $ScriptPath = (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Definition
33 $command = "powershell "+$ScriptPath+" -eventType"
34
35 # Set Up OpenManage Alert handlers
36 SetOMAlert "powersupply" $command;
37 SetOMAlert "powersupplywarn" $command;
38 SetOMAlert "tempwarn" $command;
39 SetOMAlert "tempfail" $command;
40 SetOMAlert "fanwarn" $command;
41 SetOMAlert "fanfail" $command;
42 SetOMAlert "voltwarn" $command;
43 SetOMAlert "voltfail" $command;
44 SetOMAlert "Intrusion" $command;
45 SetOMAlert "redundegrad" $command;
46 SetOMAlert "redunlost" $command;
47 SetOMAlert "memprefail" $command;
48 SetOMAlert "memfail" $command;
49 SetOMAlert "hardwarelogwarn" $command;
50 SetOMAlert "hardwarelogfull" $command;
51 SetOMAlert "processorwarn" $command;
52 SetOMAlert "processorfail" $command;
53 SetOMAlert "watchdogasr" $command;
54 SetOMAlert "batterywarn" $command;
55 SetOMAlert "batteryfail" $command;
56 SetOMAlert "systempowerwarn" $command;
57 SetOMAlert "systempowerfail" $command;
58 SetOMAlert "storagesyswarn" $command;
59 SetOMAlert "storagesysfail" $command;
60 SetOMAlert "storagectrlwarn" $command;
61 SetOMAlert "storagectrlfail" $command;
62 SetOMAlert "pdiskwarn" $command;
63 SetOMAlert "pdiskfail" $command;
64 SetOMAlert "vdiskwarn" $command;
65 SetOMAlert "vdiskfail" $command;
66 SetOMAlert "enclosurewarn" $command;
67 SetOMAlert "enclosurefail" $command;
68 SetOMAlert "storagectrlbatterywarn" $command;
69 SetOMAlert "storagectrlbatteryfail" $command;
70
71 # Register Our Event Log Source
72 if ([System.Diagnostics.EventLog]::SourceExists("OMSANotify") -eq $false) {
73 [System.Diagnostics.EventLog]::CreateEventSource("OMSANotify", "System")
74 }
75}
76
77# OMCONFIG Runner for individual Alert config
78Function SetOMAlert($event, $cmdString){
79 invoke-command -scriptblock {omconfig system alertaction event=$Event execappath="$cmdString $event"}
80}
81
82# Lets Generate A Test case Email, so we can be sure it works
83Function Test(){
84 ProcessAlert "test";
85 }
86
87# Logs OMSA Event and Email in Windows Event Log
88Function logEvent($event)
89{
90 $EventMsg = "OMSA Notify Processed Dell Open Manage Event $event"
91 Write-EventLog -Logname System -Source OMSANotify -eventId 1 -entryType Warning -message $EventMsg
92}
93
94# Handles All Alert Processing.
95Function ProcessAlert($alert) {
96 $AlertMessageString = ""
97
98 # Check if it's a known OMSA Alert
99 If($Alerts.containsKey($alert)){
100 $AlertMessageString = $Alerts.Get_Item($alert) + " was reported on $Env:COMPUTERNAME. Please log in ASAP and check OMSA for further details."
101 }
102 Else {
103 "Unknown Alert - $alert was reported at $Date on $Env:COMPUTERNAME. Please log in ASAP and check OMSA for further details."
104 }
105
106 sendMail $alert $AlertMessageString;
107
108 #Register our event in Windows Event Log.
109 logEvent $alert;
110}
111
112
113if($eventType) {
114 ProcessAlert $event;
115}
116else {
117 if($Setup) {
118 Setup;
119 }
120 if($Test) {
121 Test;
122 }
123}