· 9 years ago · Feb 02, 2017, 08:20 PM
1<#
2.SYNOPSIS
3This is a PowerShell Script to generate email alerts from Dell OpenManage Server Administrator Alerts
4
5.DESCRIPTION
6This Script Is used to send SMTP alerts from Servers running Dell Open Manage Server Administrator. It can automatically configure itself for most common OpenManage Alerts using the -Setup Parameter. It can also send test alerts using -Test.
7
8.PARAMETER Setup
9Runs omconfig commands to set the script as action on alert generation.
10
11.PARAMETER Test
12Sends a test alert.
13
14.PARAMETER eventType
15The Event Class to generate an Alert for.
16
17.EXAMPLE
18./OMSANotify.ps1 -Test
19
20
21.LINK
22https://bitbucket.org/ncouraud/omsa-notify
23#>
24
25# Setup Our Parameters
26[CmdletBinding()]
27Param(
28 # The Event Type that we need to respond to.
29 [Parameter(Mandatory=$False,Position=1)]
30 [string]$eventType,
31
32 # Run the Setup Commands
33 [switch]$Setup,
34
35 # Send a Test Alert
36 [switch]$Test
37)
38
39# Define the List of Alerts that we Respond to. Desired Alerts May be Added/Removed.
40$Alerts = @{}
41$Alerts.Add("powersupply",'Power Supply Failure')
42$Alerts.Add("powersupplywarn",'Power Supply Warning')
43$Alerts.Add("tempwarn",'Temperature Warning')
44$Alerts.Add("tempfail",'Temperature Failure')
45$Alerts.Add("fanwarn",'Fan Speed Warning')
46$Alerts.Add("fanfail",'Fan Speed Failure')
47$Alerts.Add("voltwarn",'Voltage warning')
48$Alerts.Add("voltfail",'Voltage Failure')
49$Alerts.Add("Intrusion",'Chassis Intrusion')
50$Alerts.Add("redundegrad",'Redundancy Degraded')
51$Alerts.Add("redunlost",'Redundancy Lost')
52$Alerts.Add("memprefail",'Memory Pre-Failure')
53$Alerts.Add("memfail",'Memory Failure')
54$Alerts.Add("hardwarelogwarn",'Hardware Log Warning')
55$Alerts.Add("hardwarelogfull",'Hardware Log Full')
56$Alerts.Add("processorwarn",'Processor Warning')
57$Alerts.Add("processorfail",'Processor Failure')
58$Alerts.Add("watchdogasr",'Watchdog ASR')
59$Alerts.Add("batterywarn",'Battery Warning')
60$Alerts.Add("batteryfail",'Battery Failure')
61$Alerts.Add("systempowerwarn",'System Power Warning')
62$Alerts.Add("systempowerfail",'System Power Failure')
63$Alerts.Add("storagesyswarn",'Storage System Warning')
64$Alerts.Add("storagesysfail",'Storage System Failure')
65$Alerts.Add("storagectrlwarn",'Storage Controller Warning')
66$Alerts.Add("storagectrlfail",'Storage Controller Failure')
67$Alerts.Add("pdiskwarn",'Physical Disk Warning')
68$Alerts.Add("pdiskfail",'Physical Disk Failure')
69$Alerts.Add("vdiskwarn",'Virtual Disk Warning')
70$Alerts.Add("vdiskfail",'Virtual Disk Failure')
71$Alerts.Add("enclosurewarn",'Enclosure Warning')
72$Alerts.Add("enclosurefail",'Enclosure Failure')
73$Alerts.Add("storagectrlbatterywarn",'Storage Controller Battery Warning')
74$Alerts.Add("storagectrlbatteryfail",'Storage Controller Battery Failure')
75$Alerts.Add("test",'Test Alert')
76
77# Sends our Alert Mail
78function sendMail($AlertType, $body) {
79 #SMTP server name
80 $smtpServer = "smtp.gmail.com"
81 $SMTPPort = "587"
82 $Username = "@gmail.com"
83 $Password = ""
84
85 #Creating a Mail object
86 $msg = New-Object System.Net.Mail.MailMessage
87
88 #Creating SMTP server object
89 $smtp = New-Object System.Net.Mail.SmtpClient($smtpServer,$SMTPPort)om
90 $smtp.EnableSSL = $true
91 $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
92
93 #Email structure
94 $msg.From = "$env:COMPUTERNAME@domain.com"
95 $msg.ReplyTo = "No-Reply@domain.com"
96 $msg.To.Add("ADMIN@domain.com")
97 $msg.Subject = "Dell OMSA $AlertType Alert on $env:COMPUTERNAME"
98 $msg.body = $body
99
100 #Sending email
101 $smtp.Send($msg)
102
103}
104
105# Kicks Off OM Alert Config Commands for all Warnings/Failures
106Function Setup(){
107 # Define our command String
108 $ScriptPath = (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Definition
109 $command = "powershell "+$ScriptPath+" -eventType"
110
111 # Set Up OpenManage Alert handlers
112 SetOMAlert "powersupply" $command;
113 SetOMAlert "powersupplywarn" $command;
114 SetOMAlert "tempwarn" $command;
115 SetOMAlert "tempfail" $command;
116 SetOMAlert "fanwarn" $command;
117 SetOMAlert "fanfail" $command;
118 SetOMAlert "voltwarn" $command;
119 SetOMAlert "voltfail" $command;
120 SetOMAlert "Intrusion" $command;
121 SetOMAlert "redundegrad" $command;
122 SetOMAlert "redunlost" $command;
123 SetOMAlert "memprefail" $command;
124 SetOMAlert "memfail" $command;
125 SetOMAlert "hardwarelogwarn" $command;
126 SetOMAlert "hardwarelogfull" $command;
127 SetOMAlert "processorwarn" $command;
128 SetOMAlert "processorfail" $command;
129 SetOMAlert "watchdogasr" $command;
130 SetOMAlert "batterywarn" $command;
131 SetOMAlert "batteryfail" $command;
132 SetOMAlert "systempowerwarn" $command;
133 SetOMAlert "systempowerfail" $command;
134 SetOMAlert "storagesyswarn" $command;
135 SetOMAlert "storagesysfail" $command;
136 SetOMAlert "storagectrlwarn" $command;
137 SetOMAlert "storagectrlfail" $command;
138 SetOMAlert "pdiskwarn" $command;
139 SetOMAlert "pdiskfail" $command;
140 SetOMAlert "vdiskwarn" $command;
141 SetOMAlert "vdiskfail" $command;
142 SetOMAlert "enclosurewarn" $command;
143 SetOMAlert "enclosurefail" $command;
144 SetOMAlert "storagectrlbatterywarn" $command;
145 SetOMAlert "storagectrlbatteryfail" $command;
146
147 # Register Our Event Log Source
148 if ([System.Diagnostics.EventLog]::SourceExists("OMSANotify") -eq $false) {
149 [System.Diagnostics.EventLog]::CreateEventSource("OMSANotify", "System")
150 }
151}
152
153# OMCONFIG Runner for individual Alert config
154Function SetOMAlert($event, $cmdString){
155 invoke-command -scriptblock {omconfig system alertaction event=$Event execappath="$cmdString $event"}
156}
157
158# Lets Generate A Test case Email, so we can be sure it works
159Function Test(){
160 ProcessAlert "test";
161 }
162
163# Logs OMSA Event and Email in Windows Event Log
164Function logEvent($event)
165{
166 $EventMsg = "OMSA Notify Processed Dell Open Manage Event $event"
167 Write-EventLog -Logname System -Source OMSANotify -eventId 1 -entryType Warning -message $EventMsg
168}
169
170# Handles All Alert Processing.
171Function ProcessAlert($alert) {
172 $AlertMessageString = ""
173
174 # Check if it's a known OMSA Alert
175 If($Alerts.containsKey($alert)){
176 $AlertMessageString = $Alerts.Get_Item($alert) + " was reported on $Env:COMPUTERNAME. Please log in ASAP and check OMSA for further details."
177 }
178 Else {
179 "Unknown Alert - $alert was reported at $Date on $Env:COMPUTERNAME. Please log in ASAP and check OMSA for further details."
180 }
181
182 sendMail $alert $AlertMessageString;
183
184 #Register our event in Windows Event Log.
185 logEvent $alert;
186}
187
188
189if($eventType) {
190 ProcessAlert $event;
191}
192else {
193 if($Setup) {
194 Setup;
195 }
196 if($Test) {
197 Test;
198 }
199}