· 9 years ago · Feb 02, 2017, 10:00 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 $From = "@gmail.com"
81 $To = "@.com"
82 $Cc = "@.com"
83 $Subject = "Dell OMSA $AlertType Alert on $env:COMPUTERNAME"
84 $Body = "$body"
85 $SMTPServer = "smtp.gmail.com"
86 $SMTPPort = "587"
87
88
89 #Sending email
90 Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
91 -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
92 -Credential (Get-Credential)
93
94}
95
96# Kicks Off OM Alert Config Commands for all Warnings/Failures
97Function Setup(){
98 # Define our command String
99 $ScriptPath = (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Definition
100 $command = "powershell "+$ScriptPath+" -eventType"
101
102 # Set Up OpenManage Alert handlers
103 SetOMAlert "powersupply" $command;
104 SetOMAlert "powersupplywarn" $command;
105 SetOMAlert "tempwarn" $command;
106 SetOMAlert "tempfail" $command;
107 SetOMAlert "fanwarn" $command;
108 SetOMAlert "fanfail" $command;
109 SetOMAlert "voltwarn" $command;
110 SetOMAlert "voltfail" $command;
111 SetOMAlert "Intrusion" $command;
112 SetOMAlert "redundegrad" $command;
113 SetOMAlert "redunlost" $command;
114 SetOMAlert "memprefail" $command;
115 SetOMAlert "memfail" $command;
116 SetOMAlert "hardwarelogwarn" $command;
117 SetOMAlert "hardwarelogfull" $command;
118 SetOMAlert "processorwarn" $command;
119 SetOMAlert "processorfail" $command;
120 SetOMAlert "watchdogasr" $command;
121 SetOMAlert "batterywarn" $command;
122 SetOMAlert "batteryfail" $command;
123 SetOMAlert "systempowerwarn" $command;
124 SetOMAlert "systempowerfail" $command;
125 SetOMAlert "storagesyswarn" $command;
126 SetOMAlert "storagesysfail" $command;
127 SetOMAlert "storagectrlwarn" $command;
128 SetOMAlert "storagectrlfail" $command;
129 SetOMAlert "pdiskwarn" $command;
130 SetOMAlert "pdiskfail" $command;
131 SetOMAlert "vdiskwarn" $command;
132 SetOMAlert "vdiskfail" $command;
133 SetOMAlert "enclosurewarn" $command;
134 SetOMAlert "enclosurefail" $command;
135 SetOMAlert "storagectrlbatterywarn" $command;
136 SetOMAlert "storagectrlbatteryfail" $command;
137
138 # Register Our Event Log Source
139 if ([System.Diagnostics.EventLog]::SourceExists("OMSANotify") -eq $false) {
140 [System.Diagnostics.EventLog]::CreateEventSource("OMSANotify", "System")
141 }
142}
143
144# OMCONFIG Runner for individual Alert config
145Function SetOMAlert($event, $cmdString){
146 invoke-command -scriptblock {omconfig system alertaction event=$Event execappath="$cmdString $event"}
147}
148
149# Lets Generate A Test case Email, so we can be sure it works
150Function Test(){
151 ProcessAlert "test";
152 }
153
154# Logs OMSA Event and Email in Windows Event Log
155Function logEvent($event)
156{
157 $EventMsg = "OMSA Notify Processed Dell Open Manage Event $event"
158 Write-EventLog -Logname System -Source OMSANotify -eventId 1 -entryType Warning -message $EventMsg
159}
160
161# Handles All Alert Processing.
162Function ProcessAlert($alert) {
163 $AlertMessageString = ""
164
165 # Check if it's a known OMSA Alert
166 If($Alerts.containsKey($alert)){
167 $AlertMessageString = $Alerts.Get_Item($alert) + " was reported on $Env:COMPUTERNAME. Please log in ASAP and check OMSA for further details."
168 }
169 Else {
170 "Unknown Alert - $alert was reported at $Date on $Env:COMPUTERNAME. Please log in ASAP and check OMSA for further details."
171 }
172
173 sendMail $alert $AlertMessageString;
174
175 #Register our event in Windows Event Log.
176 logEvent $alert;
177}
178
179
180if($eventType) {
181 ProcessAlert $event;
182}
183else {
184 if($Setup) {
185 Setup;
186 }
187 if($Test) {
188 Test;
189 }
190}