Page 1 of 2

Changes to service notifications not invoked

Posted: Tue Apr 12, 2011 11:05 am
by hhlodge
Hi,

I modified the notify-service-by-email command to include a Comment field with the macro $NOTIFICATIONCOMMENT$. The change is accepted and even reflected in commands.cfg when I write out the config files, but the mail doesn't include the text I added. In fact, I don't even see the "Additional Info" field that's there before I made a change. Is email generated elsewhere? TIA.

define command {
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n\nComment: $NOTIFICATIONCOMMENT$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

Re: Changes to service notifications not invoked

Posted: Wed Apr 13, 2011 8:47 am
by hhlodge
Today I noted in nagios.log that the notification doesn't appear to be done by the normal notify-service-by-email script but by this xi_service_notification_handler.

Wed Apr 13 9:36:38 2011 SERVICE NOTIFICATION: admins;localhost;Check Badness;OK;xi_service_notification_handler;OK: All hunky dory.

Where on my current Nagios Core systems, it's clearly the script.

Wed Apr 13 9:22:59 2011 SERVICE NOTIFICATION: admins;bt-na01;check-netapp-vols;WARNING;notify-service-by-email;/vol/oracle (86% gt 85% warn threshold)

Can someone tell me if this is how notifications are done in XI and point out where the email body fields are constructed? I can see NOTIFICATIONCOMMENTS being passed to the /usr/local/nagiosxi/scripts/handle_nagioscore_notification.php script as an argument but don't know where those go from there as I don't know PHP. Am I going down the right path here? If so, when is the regular notify-service-by-email used by XI or is it at all?

Re: Changes to service notifications not invoked

Posted: Wed Apr 13, 2011 9:36 am
by tonyyarusso
That's correct - XI routes notifications through a separate handler, so that it can honor various other user settings made available in XI. If you were to delve into the Core Config Manager, you would see that contacts reference a template called "xi_contact_generic", which in turn is defined as using the commands "xi_host_notification_handler" and "xi_service_notification_handler", which are PHP scripts. These already include a $NOTIFICATIONCOMMENT$ macro.

The settings for formatting the text of e-mails are defined on a per-user basis. You can access your by clicking your username in the upper right corner of the XI interface, and then navigating to "Notification Messages" in the left sidebar.

Re: Changes to service notifications not invoked

Posted: Wed Apr 13, 2011 9:58 am
by hhlodge
Ah, I see - thanks. Is there any way to change the base per-user format from the initial roll-out or change it globally afterward? And while I can appreciate the per-user customization, I can also see someone ruining their settings.

Re: Changes to service notifications not invoked

Posted: Wed Apr 13, 2011 10:23 am
by tonyyarusso
hhlodge wrote:Ah, I see - thanks. Is there any way to change the base per-user format from the initial roll-out
Unfortunately I believe this lies within the encrypted code, so you wouldn't have access to it.
hhlodge wrote:or change it globally afterward?
Yes, although there is not a GUI for this currently. These settings are stored in PostgreSQL, in the 'nagiosxi' database, in the 'xi_usermeta' table.

Some raw background on what that looks like:

Code: Select all

nagiosxi=# \d xi_usermeta
                                        Table "public.xi_usermeta"
   Column    |          Type          |                             Modifiers
-------------+------------------------+-------------------------------------------------------------------
 usermeta_id | integer                | not null default nextval('xi_usermeta_usermeta_id_seq'::regclass)
 user_id     | integer                | not null
 keyname     | character varying(255) | not null
 keyvalue    | text                   |
 autoload    | smallint               | default (0)::smallint
Indexes:
    "xi_usermeta_pkey" PRIMARY KEY, btree (usermeta_id)
    "xi_usermeta_user_id_key" UNIQUE, btree (user_id, keyname)
    "xi_usermeta_autoload_idx" btree (autoload)

Code: Select all

nagiosxi=# select * from xi_usermeta where keyname='notification_messages';
 usermeta_id | user_id |        keyname        |                                                                          keyvalue                                                                          | autoload
-------------+---------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------+----------
         182 |      18 | notification_messages | a:2:{s:5:"email";a:2:{s:4:"host";a:2:{s:7:"subject";s:46:"%type% Host Alert - %hostalias% is %hoststate%";s:7:"message";s:188:"***** Nagios XI Alert *****

Notification Type: %type%
Host: %host%
State: %hoststate%
Address: %hostaddress%
Info: %hostoutput%
Date/Time: %datetime%

Nagios URL: %xiserverurl%
";}s:7:"service";a:2:{s:7:"subject";s:62:"%type% Service Alert - %hostalias%/%service% is %servicestate%";s:7:"message";s:222:"***** Nagios XI Alert *****

Notification Type: %type%

Service: %service%
Host: %hostalias%
Address: %hostaddress%
State: %servicestate%
Info:
%serviceoutput%
Date/Time: %datetime%

Nagios URL: %xiserverurl%
";}}s:10:"mobiletext";a:2:{s:4:"host";a:2:{s:7:"subject";s:17:"%type% Host Alert";s:7:"message";s:130:"Host: %host%
State: %hoststate%
Address: %hostaddress%
Info: %hostoutput%
Date/Time: %datetime%

Nagios URL: %xiserverurl%
";}s:7:"service";a:2:{s:7:"subject";s:20:"%type% Service Alert";s:7:"message";s:162:"Service: %service%
Host: %hostalias%
Address: %hostaddress%
State: %servicestate%
Info:
%serviceoutput%
Date/Time: %datetime%

Nagios URL: %xiserverurl%
";}}} |        1
(1 row)
So, to change it you would construct a SQL query, either directly on the psql shell or through the external tool / language of your choice. To change it only for a particular user, the 'user_id' field here matches the 'user_id' field in the xi_users table. For instance, since I only have one user copied in the output above, you could get that same output using:

Code: Select all

nagiosxi=# SELECT * FROM xi_usermeta LEFT JOIN xi_users USING (user_id) WHERE keyname='notification_messages' AND username='nagiosadmin';

Re: Changes to service notifications not invoked

Posted: Wed Apr 13, 2011 10:29 am
by tonyyarusso
Note: This seemed appropriate for a feature request to look at for the future: http://tracker.nagios.com/view.php?id=140

Re: Changes to service notifications not invoked

Posted: Thu Apr 14, 2011 8:44 am
by hhlodge
I now have my notifications customized as I'd like them. I see the macros within these are not the standard Nagios macros but ones that I just happen to notice were the --option=%MACRO% names to the PHP script. A regular user would never know or see this. I looked in the user guide and see no mention of this there either and there's no help button on the page, so there's a big gap here in assistance for the users. Are these new macros published anywhere? I couldn't find it. Thanks again.

Re: Changes to service notifications not invoked

Posted: Thu Apr 14, 2011 11:47 am
by tonyyarusso
Sadly, no. I starting looking into it a while back and got sidetracked with other things. I'll add you to my "contact when this is done" list.
http://tracker.nagios.com/view.php?id=45

Re: Changes to service notifications not invoked

Posted: Fri Apr 15, 2011 12:43 pm
by hhlodge
I'll put them here in case someone searches for it. In general, they're quite close. I just happen to pick ones that differed a lot.

Service notification macros:

%author% = "$NOTIFICATIONAUTHOR$"
%comments% = "$NOTIFICATIONCOMMENT$"
%contact% = "$CONTACTNAME$"
%contactemail% = "$CONTACTEMAIL$"
%currentattempt% = $SERVICEATTEMPT$
%datetime% = "$LONGDATETIME$
%escalated% = "$NOTIFICATIONISESCALATED$"
%hostaddress% = "$HOSTADDRESS$"
%hostalias% = "$HOSTALIAS$"
%host% = "$HOSTNAME$"
%hoststate% = $HOSTSTATE$
%hoststateid% = $HOSTSTATEID$
%lastservicestateid% = $LASTSERVICESTATEID$
%lastservicestate% = $LASTSERVICESTATE$
%longserviceoutput% = "$LONGSERVICEOUTPUT$"
%maxattempts% = $MAXSERVICEATTEMPTS$
%serviceeventid% = $SERVICEEVENTID$
%serviceoutput% = "$SERVICEOUTPUT$"
%serviceproblemid% = $SERVICEPROBLEMID$
%service% = "$SERVICEDESC$"
%servicestateid% = $SERVICESTATEID$
%servicestate% = $SERVICESTATE$
%servicestatetype% = $SERVICESTATETYPE$
%type% = $NOTIFICATIONTYPE$

Re: Changes to service notifications not invoked

Posted: Fri Apr 15, 2011 3:03 pm
by rdedon
Thank you for this, very useful! :-)