-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlerter.rb
More file actions
executable file
·54 lines (36 loc) · 1.76 KB
/
Alerter.rb
File metadata and controls
executable file
·54 lines (36 loc) · 1.76 KB
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
# this class is used to send alerts to registered email addresses and what not
class Alerter
def initialize(emails, maxPings=-1, maxHTTPs=-1, send=true)
@emailAddresses, @maxPingsBeforeAlert, @maxHTTPBeforeAlert, @sendAlerts = emails, maxPings, maxHTTPs, send
end
def readEmailTemplate(templateLocation)
# load the alert template from the file in the specified location
@alertTemplatelocation = templateLocation
puts "Reading template..." + @alertTemplatelocation.to_s
@alertTemplate = File.read(@alertTemplatelocation.to_s)
end
def sendEmailAlert(op, dest, fails, sEpoch, sDate)
# first finish generating the email from the template
@emailMessage = @alertTemplate.dup # otherwise the copy would be by reference
@emailSubject = "ALERT(#{op.to_s}): #{dest.to_s} - #{sDate.to_s}"
emailMessage.sub!("<op>", op.to_s)
emailMessage.sub!("<dest>", dest.to_s)
emailMessage.sub!("<fails>", fails.to_s)
emailMessage.sub!("<sEpoch>", sEpoch.to_s)
emailMessage.sub!("<sDate>", sDate.to_s)
#puts "\t" + "#{emailMessage}"
# use either of the commands below to send the email
# echo "<emailMessage>" | mail -s "<emailSubject>" emailAddress
# mail -s "<subject>" <email_address> < "<file>"
# TODO: the mail command fails silently, replace with something else?
cmdTest1 = "echo \"#{emailMessage.to_s}\" | mail -s \"#{emailSubject.to_s}\" #{emailAddresses.to_s}"
#puts cmdTest1
output = `#{cmdTest1}`
#cmdTest2 = "mail -s \"#{emailSubject.to_s}\" #{emailAddresses.to_s} < \"#{emailMessage.to_s}\""
#puts cmdTest2
#output = `#{cmdTest2}`
return true
end
attr_accessor :emailAddresses, :alertTemplateLocation, :maxPingsBeforeAlert, :maxHTTPBeforeAlert, :sendAlerts
attr_accessor :alertTemplateLocation, :emailMessage, :emailSubject
end