Module: REC::Notify

Defined in:
lib/rec/notify.rb

Overview

Provides the capability to send alerts

mail

The simplest approach is to use the native mail program (no credentials required) Notify.mail(alert)

email and jabber

You can also send emails and instant messages via servers, but you’ll need to provide credentials to do that. Notify.smtp_credentials(user, password, domain, server, port) Notify.jabber_credentials(user, password, server)

Then you can send messages: Notify.email(alert) Notify.jabber(alert) or send messages to another recipient, with another subject Notify.email(alert, [email protected], “Serious problem”) Notify.jabber(alert, [email protected])

Sleeping

If you want to avoid being sent instant messages during sleeping hours, you can specify a range of working hours during which urgent alerts may be sent by jabber and outside those working hours the alert will be sent by email instead Notify.workHours(9,18) # IMs only between 9am and 6pm Notify.urgent(alert) # sent as instant message if during work hours, else by email Notify.jabber(alert) # sent as instant message regardless of the time Notify.normal(alert) # sent by email

Securing credentials

In order to keep email/messaging credentials secure, they can be provided in a file that is only readable by the user executing the rules so the rules script need not contain passwords. load(“/home/rec/alert.conf”) Loads the credentials into the rules. The file can contain something like: Notify.email_credentials(“[email protected]”, “tricky”, “mydomain.com”) Notify.jabber_credentials(“[email protected]”, “tricky”) /home/rec/alert.conf should be readable only by the otherwise unprivileged user (sec) running the script.

While we’re on the topic of security, just a reminder that Notify::mail requires no credentials.

Constant Summary collapse

@@workHours =
7...21
@@defaultSubject =
"Alert"

Class Method Summary collapse

Class Method Details

.default_subject(subject) ⇒ Object

Sets the default subject for alerts, overriding the system default of “Alert”



58
59
60
# File 'lib/rec/notify.rb', line 58

def self.default_subject(subject)
	@@defaultSubject = subject
end

.email(alert, recipient = @@emailTo, subject = @@defaultSubject) ⇒ Object

Send the alert via an email server



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rec/notify.rb', line 94

def self.email(alert, recipient=@@emailTo, subject=@@defaultSubject)
	smtp = Net::SMTP.new(@@emailServer, @@emailPort)
	smtp.enable_starttls()
	smtp.start(@@emailDomain, @@emailUser, @@emailPassword, :plain)
	message = []
	message << "To: #{recipient}\n"
	message << "Subject: #{subject}\n\n"
	message << alert
	smtp.send_message(message, @@emailUser, recipient)
	smtp.finish()
end

.emailTo=(address) ⇒ Object

sets the email address to receive alerts



79
80
81
# File 'lib/rec/notify.rb', line 79

def self.emailTo=(address)
	@@emailTo = address
end

.jabber(alert, recipient = @@jabberTo, subject = @@defaultSubject) ⇒ Object

Send the alert via google talk (or any other XMPP service)



107
108
109
110
111
112
113
# File 'lib/rec/notify.rb', line 107

def self.jabber(alert, recipient=@@jabberTo, subject=@@defaultSubject)
	client = Jabber::Client::new(Jabber::JID.new(@@jabberUser))
	client.connect(@@jabberServer)
	client.auth(@@jabberPassword)
	message = Jabber::Message::new(recipient, alert).set_type(:normal).set_id('1').set_subject(subject)
	client.send(message)
end

.jabber_credentials(user, password, server = "talk.google.com") ⇒ Object

provides the credentials needed for sending instant messages



72
73
74
75
76
# File 'lib/rec/notify.rb', line 72

def self.jabber_credentials(user, password, server="talk.google.com")
	@@jabberUser = user
	@@jabberPassword = password
	@@jabberServer = server
end

.jabberTo=(address) ⇒ Object

sets the jabber address to receive alerts



84
85
86
# File 'lib/rec/notify.rb', line 84

def self.jabberTo=(address)
	@@jabberTo = address
end

.mail(alert, recipient = @@emailTo, subject = @@defaultSubject) ⇒ Object

Send the alert via local mailer



89
90
91
# File 'lib/rec/notify.rb', line 89

def self.mail(alert, recipient=@@emailTo, subject=@@defaultSubject)
	`echo "#{alert}" | mail -s #{subject.gsub(/\s/,'\ ')} #{recipient} 1<&2`
end

.normal(alert) ⇒ Object

Alias for Notify::email



125
126
127
# File 'lib/rec/notify.rb', line 125

def self.normal(alert)
	self.email(alert)
end

.smtp_credentials(user, password, domain, server = "smtp.gmail.com", port = 587) ⇒ Object

provides the credentials needed for sending email



63
64
65
66
67
68
69
# File 'lib/rec/notify.rb', line 63

def self.smtp_credentials(user, password, domain, server="smtp.gmail.com", port=587)
	@@emailUser = user
	@@emailPassword = password
	@@emailDomain = domain
	@@emailServer = server
	@@emailPort = port
end

.urgent(alert) ⇒ Object

Send an instant message during work hours, else send an email



116
117
118
119
120
121
122
# File 'lib/rec/notify.rb', line 116

def self.urgent(alert)
	if @@workHours.include?(Time.now.hour)
		self.jabber(alert)
	else
		self.email(alert)
	end
end

.work_hours(start, finish) ⇒ Object

define the working hours during which instant messages are allowed Note that Notify.work_hours(7,21) means “7am-9pm” as you would assume, so from 07:00 to 20:59



53
54
55
# File 'lib/rec/notify.rb', line 53

def self.work_hours(start, finish)
	@@workHours = start..finish
end