This repository has been archived by the owner on Jun 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
OMSANotify.ps1
187 lines (155 loc) · 6.09 KB
/
OMSANotify.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<#
.SYNOPSIS
This is a PowerShell Script to generate email alerts from Dell OpenManage Server Administrator Alerts
.DESCRIPTION
This 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.
.PARAMETER Setup
Runs omconfig commands to set the script as action on alert generation.
.PARAMETER Test
Sends a test alert.
.PARAMETER EventType
The Event Class to generate an Alert for.
.EXAMPLE
./OMSANotify.ps1 -Test
.LINK
https://github.com/ncouraud/omsa-notify
#>
# Setup Our Parameters
[CmdletBinding()]
Param(
# The Event Type that we need to respond to.
[Parameter(Mandatory=$False,Position=1)]
[string]$EventType,
# Run the Setup Commands
[switch]$Setup,
# Send a Test Alert
[switch]$Test
)
# Setup our Variables
$EmailSmtpServer = "smtp.domain.local" #Your SMTP server
$EmailDomainSender = "domain.local" #The domain portion of your email address
$EmailTo = "recipient@domain.local" #Email to send to
$EmailReplyTo = "no-reply@domain.local" #Email to send as
# Todays date for logging
$Date = Get-Date
# Define the List of Alerts that we Respond to. Desired Alerts May be Added/Removed.
$Alerts = @{}
$Alerts.Add("test",'Test Alert')
$Alerts.Add("powersupply",'Power Supply Failure')
$Alerts.Add("powersupplywarn",'Power Supply Warning')
$Alerts.Add("tempwarn",'Temperature Warning')
$Alerts.Add("tempfail",'Temperature Failure')
$Alerts.Add("fanwarn",'Fan Speed Warning')
$Alerts.Add("fanfail",'Fan Speed Failure')
$Alerts.Add("voltwarn",'Voltage warning')
$Alerts.Add("voltfail",'Voltage Failure')
$Alerts.Add("Intrusion",'Chassis Intrusion')
$Alerts.Add("redundegrad",'Redundancy Degraded')
$Alerts.Add("redunlost",'Redundancy Lost')
$Alerts.Add("memprefail",'Memory Pre-Failure')
$Alerts.Add("memfail",'Memory Failure')
$Alerts.Add("hardwarelogwarn",'Hardware Log Warning')
$Alerts.Add("hardwarelogfull",'Hardware Log Full')
$Alerts.Add("processorwarn",'Processor Warning')
$Alerts.Add("processorfail",'Processor Failure')
$Alerts.Add("watchdogasr",'Watchdog ASR')
$Alerts.Add("batterywarn",'Battery Warning')
$Alerts.Add("batteryfail",'Battery Failure')
$Alerts.Add("systempowerwarn",'System Power Warning')
$Alerts.Add("systempowerfail",'System Power Failure')
$Alerts.Add("storagesyswarn",'Storage System Warning')
$Alerts.Add("storagesysfail",'Storage System Failure')
$Alerts.Add("storagectrlwarn",'Storage Controller Warning')
$Alerts.Add("storagectrlfail",'Storage Controller Failure')
$Alerts.Add("pdiskwarn",'Physical Disk Warning')
$Alerts.Add("pdiskfail",'Physical Disk Failure')
$Alerts.Add("vdiskwarn",'Virtual Disk Warning')
$Alerts.Add("vdiskfail",'Virtual Disk Failure')
$Alerts.Add("enclosurewarn",'Enclosure Warning')
$Alerts.Add("enclosurefail",'Enclosure Failure')
$Alerts.Add("storagectrlbatterywarn",'Storage Controller Battery Warning')
$Alerts.Add("storagectrlbatteryfail",'Storage Controller Battery Failure')
$Alerts.Add("systempeakpower",'System Peak Power')
$Alerts.Add("removableflashmediapresent",'Removable Flash Media Present')
$Alerts.Add("removableflashmediaremoved",'Removable Flash Media Removed')
$Alerts.Add("removableflashmediafail",'Removable Flash Media Failure')
# Sends our Alert Mail
Function sendMail($AlertType, $Body) {
#Creating a Mail object
$Msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$Smtp = new-object Net.Mail.SmtpClient($EmailSmtpServer)
#Email structure
$Msg.From = "$env:COMPUTERNAME@$EmailDomainSender"
$Msg.ReplyTo = $EmailReplyTo
$Msg.To.Add($EmailTo)
$Msg.Subject = "Dell OMSA - $AlertType Alert on $($Env:COMPUTERNAME)"
$Msg.body = $Body
#Sending email
$Smtp.Send($Msg)
}
# Kicks Off OM Alert Config Commands for all Warnings/Failures
Function Setup() {
# Define our command String
$ScriptPath = (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Definition
$command = "powershell "+$ScriptPath+" -EventType"
# Set Up OpenManage Alert handlers
Foreach ($Alert in $Alerts.Keys) {
If ( $Alert -NotLike "test" ) {
SetOMAlert $Alert $command
}
}
# Register Our Event Log Source
If ([System.Diagnostics.EventLog]::SourceExists("OMSANotify") -eq $false) {
[System.Diagnostics.EventLog]::CreateEventSource("OMSANotify", "System")
}
}
# OMCONFIG Runner for individual Alert config
Function SetOMAlert($Event, $cmdString) {
Invoke-Command -Scriptblock {omconfig system alertaction event=$Event execappath="$cmdString $Event"}
}
# Lets Generate A Test case Email, so we can be sure it works
Function Test() {
ProcessAlert "test"
}
# Logs OMSA Event and Email in Windows Event Log
Function logEvent($Event) {
# Write event to Windows System log
Write-EventLog -Logname System -Source OMSANotify -EventId 1 -EntryType Warning -Message $Event
# Write event to local file log
$ScriptFolder = Split-Path $script:MyInvocation.MyCommand.Path
$Event | Out-File $ScriptFolder"\OMSANotify-event.log" -Append
}
# Handles All Alert Processing.
Function ProcessAlert($Alert) {
$AlertMessageString = ""
# Check if it's a known OMSA Alert
If ( $Alerts.containsKey($Alert) ) {
$AlertProcessed = "$($Alerts.Get_Item($Alert))"
}
Else {
$AlertProcessed = "Unknown Alert - $Alert"
}
$AlertMessageString = "$AlertProcessed was reported at $Date on $($Env:COMPUTERNAME). Check OMSA for further details - https://$($Env:COMPUTERNAME):1311"
Try {
sendMail $AlertProcessed $AlertMessageString
logEvent $AlertMessageString
}
Catch {
# Write errors to log file
$ScriptFolder = Split-Path $script:MyInvocation.MyCommand.Path
write-output "$Date - OMSANotify Error" | Out-File $ScriptFolder"\OMSANotify-error.log" -Append
$_ | Format-List * -Force | Out-File $ScriptFolder"\OMSANotify-error.log" -Append
}
}
If ($EventType) {
ProcessAlert $EventType
}
Else {
If ($Setup) {
Setup
}
If ($Test) {
Test
}
}