-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail-host-notification.rb
executable file
·193 lines (173 loc) · 6.13 KB
/
mail-host-notification.rb
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
188
189
190
191
192
193
#!/usr/bin/env ruby
=begin
**********************************************************************
*
* Author : Lasse Wackers
*
* Date created : 20180927
*
* Purpose : This script sends host-notifications via E-Mail to the specified users.
* This script use Environment variables with information about the host which are filled by icinga2.
* You have to use erb files to change the content of your mails.
*
* License : GPL
*
**********************************************************************
=end
require 'optparse'
require 'logger'
require 'erb'
require 'net/smtp'
# Logger Options
## Loading logging API
LOG_PATH = "/var/log/icinga2/icinga2_notifications.log"
@logger = Logger.new(LOG_PATH)
@logger.level = Logger::INFO
begin
# Mailing Options
mailserver = "localhost"
# @param mailserver String
# @param smtp SMTP Object
# @param from String
# @param to String or Array of Strings
# @param cc String or Array of Strings or NilClass
# @param mail_message String
# @return NilClass
def send_mail(mailserver, smtp, from, to, cc=nil, mail_message)
@from = from
@to = to
@cc = cc
@mail_message = mail_message
smtp.start mailserver
smtp.mailfrom @from
@to = [@to] if @to.class == String
@cc = [] if !defined?(@cc) or @cc.class == NilClass
@cc = [@cc] if @cc.class == String
(@to+@cc).map do |addr|
smtp.rcptto addr
end
smtp.data @mail_message
smtp.finish
end
## Get all options from Environment Variables
options = Hash.new
options["date"] = ENV["LONGDATETIME"]
options["hostname"] = ENV["HOSTNAME"]
options["hostdisplayname"] = ENV["HOSTDISPLAYNAME"]
options["hostoutput"] = ENV["HOSTOUTPUT"]
options["usermail"] = ENV["USEREMAIL"]
options["hoststate"] = ENV["HOSTSTATE"]
options["notificationtype"] = ENV["NOTIFICATIONTYPE"]
options["hostaddress"] = ENV["HOSTADDRESS"]
options["hostaddress6"] = ENV["HOSTADDRESS6"]
options["notificationauthorname"] = ENV["NOTIFICATIONAUTHORNAME"]
options["notificationcomment"] = ENV["NOTIFICATIONCOMMENT"]
options["icingaweb2url"] = ENV["ICINGAWEB2URL"]
options["mailfrom"] = ENV["MAILFROM"]
options["extrainformation"] = Array.new
@logger.debug(ARGV)
## Overwrite options with argument options
optparser = OptionParser.new do |option|
option.banner = "Usage: #{__FILE__}"
option.separator 'Required parameters:
-d LONGDATETIME (\$icinga.long_date_time\$)
-l HOSTNAME (\$host.name\$)
-n HOSTDISPLAYNAME (\$host.display_name\$)
-o HOSTOUTPUT (\$host.output\$)
-r USEREMAIL (\$user.email\$)
-s HOSTSTATE (\$host.state\$)
-t NOTIFICATIONTYPE (\$notification.type\$)
-T Template File
Optional parameters:
-4 HOSTADDRESS (\$address\$)
-6 HOSTADDRESS6 (\$address6\$)
-b NOTIFICATIONAUTHORNAME (\$notification.author\$)
-c NOTIFICATIONCOMMENT (\$notification.comment\$)
-i ICINGAWEB2URL (\$notification_icingaweb2url\$, Default: unset)
-f MAILFROM (\$notification_mailfrom\$, requires GNU mailutils (Debian/Ubuntu) or mailx (RHEL/SUSE))
-E Extra Information
-v (\$notification_sendtosyslog\$, Default: false)'
option.on('-d', '--date=DATE') do |date|
options["date"] = date
end
option.on('-l', '--hostname=HOSTNAME') do |hostname|
options["hostname"] = hostname
end
option.on('-n', '--hostdisplayname=HOSTDISPLAYNAME') do |hostdisplayname|
options["hostdisplayname"] = hostdisplayname
end
option.on('-o', '--hostoutput=HOSTOUTPUT') do |hostoutput|
options["hostoutput"] = hostoutput
end
option.on('-r', '--usermail=USEREMAIL') do |usermail|
options["usermail"] = usermail
end
option.on('-s', '--hoststate=HOSTSTATE') do |hoststate|
options["hoststate"] = hoststate
end
option.on('-t', '--notificationtype=NOTIFICATIONTYPE') do |notificationtype|
options["notificationtype"] = notificationtype
end
option.on('-f', '--mailfrom=MAILFROM') do |mailfrom|
options["mailfrom"] = mailfrom
end
option.on('-4', '--address=ADDRESS') do |address|
options["address"] = address
end
option.on('-6', '--address6=ADDRESS') do |address6|
options["address6"] = address6
end
option.on('-b', '--author=AUTHOR') do |author|
options["author"] = author
end
option.on('-c', '--comment=COMMENT') do |comment|
options["comment"] = comment
end
option.on('-i', '--icingaweb2url=ICINGAWEB2URL') do |icingaweb2url|
options["icingaweb2url"] = icingaweb2url
end
option.on('-E', '--extrainformation=EXTRAINFORMATION') do |extrainformation|
options["extrainformation"] << extrainformation.strip.chomp
end
option.on('-v', '--log=LOG') do |log|
options["log"] = log
end
option.on('-T', '--templatefile=TEMPLATEFILE') do |templatefile|
options["templatefile"] = templatefile
unless File.exist?(templatefile)
msg = "Templatefile #{templatefile} does not exist"
@logger.fatal(msg) unless defined?(options["log"])
abort(msg)
end
end
end.parse!
@logger.debug(options)
## Check required options
required_options = ["date","hostname","hostdisplayname","hostoutput","usermail","hoststate","notificationtype","templatefile"]
required_options.each do |option|
if options[option].class == NilClass
msg = "Require option #{option}"
@logger.fatal(msg) unless defined?(options["log"])
abort(msg)
end
end
## Create summary for logging
summary = Array.new
options.each_pair do |key,value|
summary << "#{key}: #{value}"
end
## Create mail informations
to = options["usermail"]
from = options["mailfrom"]
## Load Template File
template = ERB.new(File.read(options["templatefile"]), safe_eval=nil, trim_mode=nil, outvar='_erbout')
## Render Template
mail_message = template.result( binding )
## Sending E-Mail
smtp = Net::SMTP.new mailserver, 25
send_mail(mailserver, smtp, from, to, cc=nil, mail_message)
@logger.info("Mail sent: #{summary.join(";")}")
rescue => err
@logger.debug(err)
@logger.fatal("Error in script. The variables or options were: #{summary.join(";")}")
end