Newline not working in email alert

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
brianp89
Posts: 18
Joined: Thu May 08, 2014 10:53 am

Newline not working in email alert

Post by brianp89 »

Hello,

I've created a perl script that outputs the results with a newline, which works fine in Nagios:

Code: Select all

OK - S:\\ Total=50.00GB, Used=5.45GB (10.9%), Free=44.55GB (89.1%) 
OK - E:\\ Total=300.00GB, Used=183.35GB (61.1%), Free=116.64GB (38.9%) 
CRITICAL - [Triggered by _Free%<10] - D:\\ Total=600.00GB, Used=541.18GB (90.2%), Free=58.82GB (9.8%) 
OK - G:\\ Total=399.99GB, Used=22.56GB (5.6%), Free=377.44GB (94.4%)  
However, when the alert is sent via email, the newlines don't show up properly, and instead all I see is "\n" where there should be a newline:

Code: Select all

OK - C:\ Total=40.00GB, Used=20.83GB (52.1%), Free=19.17GB (47.9%) \nOK - S:\ Total=50.00GB, Used=5.45GB (10.9%), Free=44.55GB (89.1%) \nOK - E:\ Total=300.00GB, Used=183.35GB (61.1%), Free=116.64GB (38.9%) \nCRITICAL - [Triggered by _Free%10] - D:\ Total=600.00GB, Used=541.18GB (90.2%), Free=58.82GB (9.8%) \nOK - G:\ Total=399.99GB, Used=22.56GB (5.6%), Free=377.44GB (94.4%)
How do I format the output in my perl script so that the newlines work in the email? Or is there a setting in Nagios that will parse the output as intended?

Thank you,
Brian
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Newline not working in email alert

Post by tmcdonald »

It's really hard to say without knowing more details. Can you post the script? How do you have it set up to run in Nagios? Just a notification command? Is your email client able to understand what \n means or is it displaying in HTML and needs <br /> instead?
Former Nagios employee
brianp89
Posts: 18
Joined: Thu May 08, 2014 10:53 am

Re: Newline not working in email alert

Post by brianp89 »

tmcdonald wrote:It's really hard to say without knowing more details. Can you post the script? How do you have it set up to run in Nagios? Just a notification command? Is your email client able to understand what \n means or is it displaying in HTML and needs <br /> instead?
It's the check_wmi_plus.pl script and I've edited the "checkvolsize" output code with:

Code: Select all

if ($results_text) {
   # show the results
   # remove the last ", "
   $results_text=~s/, +$//;
   # adds newline before each OK, WARNING and CRITICAL
   $results_text=~s/OK/\nOK/g;
   $results_text=~s/WARNING/\nWARNING/g;
   $results_text=~s/CRITICAL/\nCRITICAL/g;
   # removes first newline
   $results_text=~s/\n//;
   # correctly combine the results and perfdata
   my $combined_string=combine_display_and_perfdata($results_text,$performance_data);
   print $combined_string;
The email is sent as plain text so it won't understand <br />. And here's the service notification email format I'm using in NagiosXI:

Code: Select all

***** Nagios XI Alert *****

%alertsummary%

Notification Type: %type%

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


This alert has been sent to %recipients%.
Any suggestions would be appreciated! Thanks
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Newline not working in email alert

Post by rkennedy »

Can you post the mail headers for the message that you received looking like this?

Code: Select all

OK - C:\ Total=40.00GB, Used=20.83GB (52.1%), Free=19.17GB (47.9%) \nOK - S:\ Total=50.00GB, Used=5.45GB (10.9%), Free=44.55GB (89.1%) \nOK - E:\ Total=300.00GB, Used=183.35GB (61.1%), Free=116.64GB (38.9%) \nCRITICAL - [Triggered by _Free%10] - D:\ Total=600.00GB, Used=541.18GB (90.2%), Free=58.82GB (9.8%) \nOK - G:\ Total=399.99GB, Used=22.56GB (5.6%), Free=377.44GB (94.4%)
Former Nagios Employee
brianp89
Posts: 18
Joined: Thu May 08, 2014 10:53 am

Re: Newline not working in email alert

Post by brianp89 »

Here is the mail header:

Code: Select all

Received: from exch-cas3.company.com (2002:a88d:c9bf::a88d:c9bf) by
 exch-cas2.company.com (2002:a88d:c9a5::a88d:c9a5) with Microsoft SMTP Server
 (TLS) id 14.3.248.2; Fri, 8 Jan 2016 15:12:11 -0500
Received: from localhost.localdomain (168.141.208.19) by exch-cas3
 (168.141.201.192) with Microsoft SMTP Server id 14.3.248.2; Fri, 8 Jan 2016
 15:12:11 -0500
Date: Fri, 8 Jan 2016 15:12:11 -0500
Return-Path: [email protected]
To: <[email protected]>
From: <[email protected]>
Reply-To: <[email protected]>
Subject: PROBLEM Service Alert - internal-de1/Disk Space check - internal-de1 - ALL w10 c5 is CRITICAL
Message-ID: <[email protected]>
X-Priority: 2
X-Mailer: PHPMailer 5.1 (phpmailer.sourceforge.net)
X-MSMail-Priority: Medium
Importance: Medium
MIME-Version: 1.0
Content-Type: multipart/alternative;
	boundary="b1_e546282187662a224af3e2c5fc2ae149"
X-MS-Exchange-Organization-AuthSource: exch-cas3.company.com
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 10
X-MS-Exchange-Organization-AVStamp-Mailbox: MSFTFF;1;0;0 0 0
Now that I'm looking more closely at the email settings, I believe it can handle HTML tags. However, when I change the perl script's output to "<br>" instead of "\n" all I see is "br" in the email without a newline (even though <br> shows up in the Nagios check's output). It looks like the Nagios PHP mail sender is stripping out the "<>" characters. Do you know how this can be fixed?
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Newline not working in email alert

Post by rkennedy »

Can you try putting the <br> in double quotes? "" and see if that has an affect?
Former Nagios Employee
brianp89
Posts: 18
Joined: Thu May 08, 2014 10:53 am

Re: Newline not working in email alert

Post by brianp89 »

rkennedy wrote:Can you try putting the <br> in double quotes? "" and see if that has an affect?
Tried that, still no luck.

However, a coworker found a solution. He edited the "xi_service_notification_handler" in the Commands config section to include:

Code: Select all

--longserviceoutput="$$(echo "$LONGSERVICEOUTPUT$" | sed 's/\\n/\n/g' )"
Email alerts now display the newline correctly instead of "\n".
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Newline not working in email alert

Post by rkennedy »

Ah! Sed for the win!

Thanks for contributing how you solved this. Am I close this thread out?
Former Nagios Employee
brianp89
Posts: 18
Joined: Thu May 08, 2014 10:53 am

Re: Newline not working in email alert

Post by brianp89 »

Yep, you may close this thread. Thanks
bwallace
Posts: 1145
Joined: Tue Nov 17, 2015 1:57 pm

Re: Newline not working in email alert

Post by bwallace »

Thanks, we'll lock this thread now but feel free to open another should you require assistance with anything else.
Be sure to check out the Knowledgebase for helpful articles and solutions!
Locked