diff --git a/doc/source/06_medium/html-and-detailled-notifications.rst b/doc/source/06_medium/html-and-detailled-notifications.rst index 17b063877a..a0a34b6abe 100644 --- a/doc/source/06_medium/html-and-detailled-notifications.rst +++ b/doc/source/06_medium/html-and-detailled-notifications.rst @@ -7,9 +7,9 @@ Tired of brut text email notification, want to add more details to them and even .. image:: /_static/images/shinken-html-notification.png -Here it is the command you can use to have a basic html notification. You have to specify usual shinken macros in option. If not specified then it will try to get them from environment variable if you have set option ``enable_environment_macros`` in ``shinken.cfg`` if you want to used them. It isn't recommanded to use them for large environment. You better use option ``-c``, ``-o`` and ``-s`` or ``-h`` depend on which object you'll notify. Each macros separated by double comma. +Here it is the command you can use to have a basic html notification. You have to specify usual shinken macros in option. If not specified then it will try to get them from environment variable if you have set option ``enable_environment_macros`` in ``shinken.cfg``. It isn't recommended to use them for large environment. You better use option ``-c``, ``-o`` and ``-s`` or ``-h`` depending upon which object you'll notify. Each macros is separated by double comma. -Here is commands examples ready to use with it : +Here is commands examples ready to use: :: @@ -24,11 +24,41 @@ Here is commands examples ready to use with it : command_line $PLUGINSDIR$/notify_by_email.py -n service -S localhost -r $CONTACTEMAIL$ -f html -c "$NOTIFICATIONTYPE$,,$HOSTNAME$,,$HOSTADDRESS$,,$LONGDATETIME$" -o "$SERVICEDESC$,,$SERVICESTATE$,,$SERVICEOUTPUT$,,$SERVICEDURATION$" } +Text mail +~~~~~~~~~~~~~~~~~~~~ + +Despite the nice HTML formatting, you still prefer to use plain old text mail, use the `` -f text`` parameter instead of ``if html``. + +Mail sender +~~~~~~~~~~~~~~~~~~~~ + +Default mail sender is built automatically with current server name and current Shinken user. + +If you want to specif the mail sender, use the ``-s (--sender)``. For example: ``-s me@myserver.com``. + + Add an header logo ~~~~~~~~~~~~~~~~~~~~ -Let's say that you want to deliver mail to a customer and integrate its logo into, you only have to get a logo, named it ``customer_logo.png`` and paste it into ``/var/lib/shinken/share/images/``. +Let's say that you want to deliver mail to a customer and integrate host logo into the mail, you only have to get a logo, name it ``customer_logo.png`` and paste it into ``/var/lib/shinken/share/images/``. + +If WebUI2 is installed, then the script will try to find the company logo that can be defined in the WebUI2. The WebUI2 company logo is PNG file located in ``/var/lib/shinken/share/photos/`` and which name is defined in ``/etc/shinken/modules/webui2.cfg``, property ``company_logo``. + + +Add a WebUI link +~~~~~~~~~~~~~~~~~~~~ + +The host or service Web UI link can be included in the mail body. Simply use ``-w (--webui)`` option and you will get a direct link to the host or service page in the Web UI. + +The link is built using the current server name and the port defined in the Web UI configuration file. +If WebUI2 is installed, the script will try to find the port number defined in ``/etc/shinken/modules/webui2.cfg``, property ``port``. +If WebUI1 is installed, the script will try to find the port number defined in ``/etc/shinken/modules/webui.cfg``, property ``port``. +If both of them are installed, priority is WebUI2. + +If neither WebUI2 nor WebUI are installed on your server, there will be no link in the mail even if this option is used. + +If you want to define the root part of the URL, use option ``-u (--url)``. For example: ``-u http://webui.myserver:8080/shinken`` will be used as root part instead of ``http://shinken:7767``. Detailled Notifications -------------------------- @@ -39,7 +69,7 @@ Detailled notifications is a way to customize and add useful informations in ema - **_IMPACT** : Specify what will be the impact - **_FIXACTIONS** : And what is the recommended actions to do when there is an alert about it. -Example +Example ~~~~~~~~ For example, you can have the below configuration: @@ -47,7 +77,7 @@ our service : :: - + define service { service_description Cpu use 20min_long,linux-service @@ -92,6 +122,6 @@ And now define our notification ways: host_notification_options d,u,r,f,s service_notification_commands detailled-service-by-email ; send service notifications via email host_notification_commands detailled-host-by-email ; send host notifications via email - } + } Then you'll receive a nice html mail giving all your details in a table. diff --git a/etc/commands/notify-host-by-email.cfg b/etc/commands/notify-host-by-email.cfg index fdb0ce7168..9e93d37ab3 100644 --- a/etc/commands/notify-host-by-email.cfg +++ b/etc/commands/notify-host-by-email.cfg @@ -1,5 +1,5 @@ ## Notify Host by Email define command { command_name notify-host-by-email - command_line $PLUGINSDIR$/notify_by_email.py -n host -S localhost -r $CONTACTEMAIL$ -f html -c "$NOTIFICATIONTYPE$,,$HOSTNAME$,,$HOSTADDRESS$,,$LONGDATETIME$" -o "$HOSTALIAS$,,$HOSTSTATE$,,$HOSTDURATION$" + command_line $PLUGINSDIR$/notify_by_email.py -n host -S localhost -r $CONTACTEMAIL$ -f html -c "$NOTIFICATIONTYPE$,,$HOSTNAME$,,$HOSTADDRESS$,,$LONGDATETIME$" -o "$HOSTSTATE$,,$HOSTDURATION$" } diff --git a/libexec/notify_by_email.py b/libexec/notify_by_email.py index 295eafbcbb..a65358e5fd 100755 --- a/libexec/notify_by_email.py +++ b/libexec/notify_by_email.py @@ -36,6 +36,8 @@ shinken_customer_logo = 'customer_logo.jpg' webui_config_file = '/etc/shinken/modules/webui.cfg' +webui2_config_file = '/etc/shinken/modules/webui2.cfg' +webui2_image_dir = '/var/lib/shinken/share/photos' # Set up root logging def setup_logging(): @@ -50,39 +52,95 @@ def setup_logging(): def overload_test_variable(): shinken_notification_object_var = { 'service': { - 'Service description': 'CPU load', - 'Service state': 'PROBLEM', + 'Service description': 'Test_Service', + 'Service state': 'TEST', 'Service output': 'Houston, we got a problem here! Oh, wait. No. It\'s just a test.', - 'Service duration': '00h 00min 10s' + 'Service state duration': '00h 00min 10s' }, 'host': { - 'Hostname': 'Shinken monitoring solution: Test host', - 'Host state': 'TEST PROBLEM', - 'Host duration': '00h 00h 20s' + 'Hostname': 'Test_Host', + 'Host state': 'TEST', + 'Host state duration': '00h 00h 20s' } } shinken_var = { - 'Hostname': 'shinken.domain.tld', + 'Hostname': 'shinken', 'Host address': '127.0.0.1', 'Notification type': 'TEST', 'Date': 'Now, test' } return (shinken_notification_object_var, shinken_var) +def get_webui_logo(): + company_logo='' + + try: + webui_config_fh = open(webui2_config_file) + except IOError: + # WebUI2 not installed ... + full_logo_path = os.path.join(shinken_image_dir, shinken_customer_logo) + if os.path.isfile(full_logo_path): + return full_logo_path + + if opts.webui: + # WebUI2 installed + logging.debug('Webui2 is installed') + webui_config = webui_config_fh.readlines() + for line in webui_config: + if 'company_logo' in line: + company_logo = line.rsplit('company_logo')[1].strip() + company_logo += '.png' + logging.debug('Found company logo property: %s', company_logo) + if company_logo: + full_logo_path = os.path.join(webui2_image_dir, company_logo) + if os.path.isfile(full_logo_path): + logging.debug('Found company logo file: %s', full_logo_path) + return full_logo_path + else: + logging.debug('File %s does not exist!', full_logo_path) + return '' + + return company_logo + def get_webui_port(): - webui_config_fh = open(webui_config_file) + port='' + + try: + webui_config_fh = open(webui2_config_file) + except IOError: + # WebUI2 not installed, try WebUI1 + try: + webui_config_fh = open(webui_config_file) + except IOError: + # No WebUI + return '' + else: + # WebUI1 installed + logging.debug('Webui1 is installed') + else: + # WebUI2 installed + logging.debug('Webui2 is installed') + + logging.debug('Webui file handler: %s' % (webui_config_fh)) webui_config = webui_config_fh.readlines() + logging.debug('Webui config: %s' % (webui_config)) for line in webui_config: if 'port' in line: - port = line.rsplit('port')[1] - return port.strip() + port = line.rsplit('port')[1].strip() + return port def get_shinken_url(): if opts.webui: hostname = socket.gethostname() webui_port = get_webui_port() - url = 'http://%s:%s/%s/%s' % (hostname, webui_port, opts.notification_object, urllib.quote(shinken_var['Hostname'])) + if not webui_port: + return + + if opts.webui_url: + url = '%s/%s/%s' % (opts.webui_url, opts.notification_object, urllib.quote(shinken_var['Hostname'])) + else: + url = 'http://%s:%s/%s/%s' % (hostname, webui_port, opts.notification_object, urllib.quote(shinken_var['Hostname'])) # Append service if we notify a service object if opts.notification_object == 'service': @@ -93,10 +151,10 @@ def get_shinken_url(): # Get current process user that will be the mail sender def get_user(): if opts.sender: - return opts.sender + return opts.sender else: - return '@'.join((getpass.getuser(), socket.gethostname())) - + return '@'.join((getpass.getuser(), socket.gethostname())) + ############################################################################# # Common mail functions and var @@ -106,7 +164,19 @@ def get_user(): # Construct mail subject field based on which object we notify def get_mail_subject(object): - mail_subject = { 'host': 'Host %s alert for %s since %s' % (shinken_notification_object_var['host']['Host state'], shinken_var['Hostname'], shinken_notification_object_var['host']['Host duration']), 'service': '%s on Host: %s about service %s since %s' % (shinken_notification_object_var['service']['Service state'], shinken_var['Hostname'], shinken_notification_object_var['service']['Service description'], shinken_notification_object_var['service']['Service duration'])} + mail_subject = { + 'host': 'Host %s alert for %s since %s' % ( + shinken_notification_object_var['host']['Host state'], + shinken_var['Hostname'], + shinken_notification_object_var['host']['Host state duration'] + ), + 'service': '%s on Host: %s about service %s since %s' % ( + shinken_notification_object_var['service']['Service state'], + shinken_var['Hostname'], + shinken_notification_object_var['service']['Service description'], + shinken_notification_object_var['service']['Service state duration'] + ) + } return mail_subject[object] @@ -135,7 +205,7 @@ def create_mail(format): msg['To'] = opts.receivers logging.debug('Subject: %s' % (opts.prefix + get_mail_subject(opts.notification_object))) msg['Subject'] = opts.prefix + get_mail_subject(opts.notification_object) - + return msg ############################################################################# @@ -148,7 +218,7 @@ def create_txt_message(msg): for k,v in sorted(shinken_var.iteritems()): txt_content.append(k + ': ' + v) - + # Add url at the end url = get_shinken_url() if url != None: @@ -160,7 +230,7 @@ def create_txt_message(msg): msg.attach(msgText) return msg - + ############################################################################# # Html creation lair ############################################################################# @@ -184,8 +254,12 @@ def create_html_message(msg): logging.debug('Grabbed Shinken URL : %s' % url) # Header part - html_content = ['''
\r -\r'''] - - full_logo_path = os.path.join(shinken_image_dir, shinken_customer_logo) - if os.path.isfile(full_logo_path): +th.customer {width: 600px; background-color: #004488; color: #ffffff;}\r +\r +\r +\r''' + ] + + full_logo_path = get_webui_logo() + if full_logo_path: msg = add_image2mail(full_logo_path, msg) html_content.append('') html_content.append('%s |
---|