Module: check_mk
Branch: master
Commit: 1eef4bf3e98e6134096a303bb5eb9cd321f57aba
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=1eef4bf3e98e61…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Jan 20 11:17:51 2014 +0100
Added mail notificaton method to custom user notification dialog
The user notification dialog (currently accessible via WATO > Users > Notify Users)
has been extended by adding a mail notification mechanism.
---
.werks/665 | 9 ++++++
ChangeLog | 3 +-
web/htdocs/notify.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++---
3 files changed, 94 insertions(+), 5 deletions(-)
diff --git a/.werks/665 b/.werks/665
new file mode 100644
index 0000000..4ff353e
--- /dev/null
+++ b/.werks/665
@@ -0,0 +1,9 @@
+Title: Added mail notificaton method to custom user notification dialog
+Level: 1
+Component: multisite
+Version: 1.2.5i1
+Date: 1390213013
+Class: feature
+
+The user notification dialog (currently accessible via WATO > Users > Notify
Users)
+has been extended by adding a mail notification mechanism.
diff --git a/ChangeLog b/ChangeLog
index 1cae374..0c55a9c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -46,8 +46,8 @@
* 0568 f5_big_ip_conns: check now supports predictive monitoring and both connections
types are merged in one check
* 0257 windows_agent: now reports extended process information (obsoletes psperf.bat
plugin)...
* 0457 hitachi_hnas_volume: New check for Usage and Status of Volumes in Hitachi HNAS
storage systems
- * 0458 hitachi_hnas_fc_if: New check for FibreChannel Interfaces in Hitachi HNAS
storage systems
* 0450 mem.used: Add information about shared memory (on Linux hosts)
+ * 0458 hitachi_hnas_fc_if: New check for FibreChannel Interfaces in Hitachi HNAS
storage systems
* 0103 FIX: services: Fixed bug with service inventory defined in main.mk...
* 0299 FIX: borcade_mlx_fan: Prettified output, handling "other" state now
* 0300 FIX: cisco_fru_power: Trying not to inventorize not plugged in FRUs...
@@ -122,6 +122,7 @@
* 0429 Implemented role permissions for dashboards...
* 0430 It is now possible to define custom time ranges in PNP graph search...
* 0449 Show all custom variables of hosts and services in the detail views...
+ * 0665 Added mail notificaton method to custom user notification dialog...
* 0302 FIX: Fixed highlight of choosen elements in foldertee/views snapin in
Chrome/IE
* 0239 FIX: Fixed incorrect html formatting when displaying host or service
comments...
* 0307 FIX: Increased performance of multisite GUI with a large userbase...
diff --git a/web/htdocs/notify.py b/web/htdocs/notify.py
index 061c483..ce85ab0 100644
--- a/web/htdocs/notify.py
+++ b/web/htdocs/notify.py
@@ -84,6 +84,10 @@ def load_plugins():
'title': _('Send hint to message inbox (bottom of
sidebar)'),
'handler': notify_gui_msg,
},
+ 'mail': {
+ 'title': _('Send an E-Mail'),
+ 'handler': notify_mail,
+ },
}
dest_choices = [
@@ -197,14 +201,21 @@ def page_notify():
recipients = msg['dest'][1]
num_recipients = len(recipients)
+ num_success = 0
+ num_failed = 0
# Now loop all notitification methods to send the notifications
for user_id in recipients:
for method in msg['methods']:
- handler = notify_methods[method]['handler']
- handler(user_id, msg)
-
- msg = _('The notification has been sent to %d recipients.') %
num_recipients
+ try:
+ handler = notify_methods[method]['handler']
+ handler(user_id, msg)
+ num_success += 1
+ except MKInternalError, e:
+ num_failed += 1
+ html.show_error(_('Failed to send %s notification to %s: %s')
% (method, user_id, e))
+
+ msg = _('The notification has been sent to %d of %d recipients.') %
(num_success, num_recipients)
msg += ' <a href="%s">%s</a>' % (html.makeuri([]),
_('Back to previous page'))
msg += '<p>Sent notification to: %s</p>' % ',
'.join(recipients)
@@ -227,3 +238,71 @@ def notify_gui_msg(user_id, msg):
if msg not in messages:
messages.append(msg)
save_gui_messages(messages, user_id)
+ return True
+
+
+def notify_mail(user_id, msg):
+ import subprocess, time
+ users = userdb.load_users(lock = False)
+ user = users.get(user_id)
+
+ if not user:
+ raise MKInternalError(_('This user does not exist.'))
+
+ if not user.get('email'):
+ raise MKInternalError(_('This user has no mail address configured.'))
+
+ recipient_name = user.get('alias')
+ if not recipient_name:
+ recipient_name = user_id
+
+ sender_name = users[config.user_id].get('alias')
+ if not sender_name:
+ sender_name = user_id
+
+ # Code mostly taken from notify_via_email() from notify.py module
+ subject = _('Check_MK: Notification')
+ body = _('''Greetings %s,
+
+%s sent you a notification:
+
+---
+%s
+---
+
+''') % (recipient_name, sender_name, msg['text'])
+
+ if msg['valid_till']:
+ body += _('This notification has been created at %s and is valid till
%s.') % (
+ time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(msg['time'])),
+ time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(msg['valid_till']))
+ )
+
+ # FIXME: Maybe use the configured mail command for Check_MK-Notify one day
+ command = u"mail -s '$SUBJECT$' '$CONTACTEMAIL$'"
+ command_utf8 = command.replace('$SUBJECT$',
subject).replace('$CONTACTEMAIL$',
user['email']).encode("utf-8")
+
+ # Make sure that mail(x) is using UTF-8. Otherwise we cannot send notifications
+ # with non-ASCII characters. Unfortunately we do not know whether C.UTF-8 is
+ # available. If e.g. nail detects a non-Ascii character in the mail body and
+ # the specified encoding is not available, it will silently not send the mail!
+ # Our resultion in future: use /usr/sbin/sendmail directly.
+ # Our resultion in the present: look with locale -a for an existing UTF encoding
+ # and use that.
+ for encoding in os.popen("locale -a 2>/dev/null"):
+ l = encoding.lower()
+ if "utf8" in l or "utf-8" in l or "utf.8" in l:
+ encoding = encoding.strip()
+ os.putenv("LANG", encoding)
+ break
+ else:
+ raise MKInternalError(_('No UTF-8 encoding found in your locale -a! Please
provide C.UTF-8 encoding.'))
+
+ p = subprocess.Popen(command_utf8, shell=True, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, stdin=subprocess.PIPE)
+ stdout_txt, stderr_txt = p.communicate(body.encode("utf-8"))
+ exitcode = p.returncode
+ if exitcode != 0:
+ raise MKInternalError(_('Mail could not be delivered. Exit code of command is
%r') % exitcode)
+ else:
+ return True