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

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

Class Method Summary collapse

Class Method Details

.default_subject(subject) ⇒ Object

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



49
50
51
# File 'lib/rec/notify.rb', line 49

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

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

Send the alert via an email server



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

def self.email(alert, recipient=@@emailTo, subject=@@defaultSubject)
	smtp = Net::SMTP.new(@@emailServer, @@emailPort)
	smtp.enable_starttls()
	smtp.start(@@emailDomain, @@emailUsername, @@emailPassword, :plain)
	smtp.send_message(alert, @@emailUsername, recipient)
end

.emailTo=(address) ⇒ Object

sets the email address to receive alerts



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

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)



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

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



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

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



77
78
79
# File 'lib/rec/notify.rb', line 77

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

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

Send the alert via local mailer



82
83
84
# File 'lib/rec/notify.rb', line 82

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



120
121
122
# File 'lib/rec/notify.rb', line 120

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



56
57
58
59
60
61
62
# File 'lib/rec/notify.rb', line 56

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



111
112
113
114
115
116
117
# File 'lib/rec/notify.rb', line 111

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



105
106
107
# File 'lib/rec/notify.rb', line 105

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