Page 1 of 1

printf "%b" in notification command definition

Posted: Tue Aug 06, 2019 6:25 am
by roddie
Can someone please help explain the %b portion of the printf command in the default notification command definitions? Is it just to allow \n to produce new lines in the construction of an email? I want to build an additional notification command that is simplified for SMS messaging (so will all be on a single line) and wondering if I need to keep the %b, replace it with something else or just drop it?

Code: Select all

define command{
        command_name    notify-host-by-sms
        command_line    /usr/bin/printf "%b" "$NOTIFICATIONTYPE$ ALERT: $HOSTALIAS$ is $HOSTSTATE$ ($SHORTDATETIME$)" | /usr/bin/sendemail -f $USER9$ -t $CONTACTEMAIL$ -s "mail.server.com:587" -o tls=yes -xu $USER9$ -xp $USER10$ -l /var/log/sendemail.log
        }

Re: printf "%b" in notification command definition

Posted: Tue Aug 06, 2019 7:42 am
by eloyd
From the man page for printf:

Code: Select all

       %b     ARGUMENT as a string with '\' escapes interpreted, except that octal escapes are of the form \0 or \0NNN
One could argue that this is so you can pass in arbitrary "blobs" (hence, "b") of data that don't get interpreted as further \-codes. So, basically, you're half right. This is so the macros that Nagios passes in, which may have weird stuff in them, get printed out properly instead of interpreted by the printf itself.

Re: printf "%b" in notification command definition

Posted: Tue Aug 06, 2019 7:48 am
by roddie
Okay, brilliant; thanks for your help :P

Re: printf "%b" in notification command definition

Posted: Tue Aug 06, 2019 9:49 am
by mbellerue
Glad to hear it's working! Also, thanks, Eric!