Script Error in mail-host-notifications.sh

I’m trying to use the pushbullet notification plugin which calls mail-host plugins and for some reason Icinga2 is complaining about errors in mail-host-notification.sh

I’m running Icinga2 on a RaspberryPi 4, and everything else works fine, I can execute the scripts manually as root and my user, and it doesn’t give me any errors, it just displays the help text since I didn’t supply any of the mandatory arguments.

root@raspberrypi:/etc/icinga2/scripts # bash -x mail-host-notification.sh 
++ basename /etc/icinga2/scripts/mail-host-notification.sh
+ PROG=mail-host-notification.sh
++ hostname
+ ICINGA2HOST=pi.mydomain.us
+ MAILBIN=mail
++ command -v mail
+ '[' -z /usr/bin/mail ']'
+ getopts 4:6::b:c:d:f:hi:l:n:o:r:s:t:v: opt
+ shift 0
+ for P in LONGDATETIME HOSTNAME HOSTDISPLAYNAME HOSTOUTPUT HOSTSTATE USEREMAIL NOTIFICATIONTYPE
+ eval 'PAR=$LONGDATETIME'
++ PAR=
+ '[' '!' '' ']'
+ Error 'Required parameter '\''LONGDATETIME'\'' is missing.'
+ '[' 'Required parameter '\''LONGDATETIME'\'' is missing.' ']'
+ echo 'Required parameter '\''LONGDATETIME'\'' is missing.'
Required parameter 'LONGDATETIME' is missing.
+ Usage
+ cat

Required parameters:
  -d LONGDATETIME ($icinga.long_date_time$)
  -l HOSTNAME ($host.name$)
  -n HOSTDISPLAYNAME ($host.display_name$)
  -o HOSTOUTPUT ($host.output$)
  -r USEREMAIL ($user.email$)
  -s HOSTSTATE ($host.state$)
  -t NOTIFICATIONTYPE ($notification.type$)

Optional parameters:
  -4 HOSTADDRESS ($address$)
  -6 HOSTADDRESS6 ($address6$)
  -b NOTIFICATIONAUTHORNAME ($notification.author$)
  -c NOTIFICATIONCOMMENT ($notification.comment$)
  -i ICINGAWEB2URL ($notification_icingaweb2url$, Default: unset)
  -f MAILFROM ($notification_mailfrom$, requires GNU mailutils (Debian/Ubuntu) or mailx (RHEL/SUSE))
  -v ($notification_sendtosyslog$, Default: false)

+ exit 1

When I followed the directions for the pushbullet plugin and then deploy the changes via Director it complains of the following errors, which the Icinga daemon also complains about as well when I execute icinga2 daemon -C

[2019-11-03 21:41:57 -0500] information/cli: Icinga application loader (version: r2.11.2-1)
[2019-11-03 21:41:57 -0500] information/cli: Loading configuration file(s).
[2019-11-03 21:41:57 -0500] critical/config: Error: syntax error, unexpected $undefined, expecting $end
Location: in /etc/icinga2/scripts/mail-host-notification.sh: 5:19-5:19
/etc/icinga2/scripts/mail-host-notification.sh(3): # Except of function urlencode which is Copyright (C) by Brian White (brian@aljex.com) used under MIT license
/etc/icinga2/scripts/mail-host-notification.sh(4): 
/etc/icinga2/scripts/mail-host-notification.sh(5): PROG="$(basename "$0")"
                                                                     ^
/etc/icinga2/scripts/mail-host-notification.sh(6): ICINGA2HOST="$(hostname)"
/etc/icinga2/scripts/mail-host-notification.sh(7): MAILBIN="mail"

[2019-11-03 21:41:57 -0500] critical/cli: Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config.

When I comment out that variable assignment I then get this error

[2019-11-03 21:43:32 -0500] information/cli: Icinga application loader (version: r2.11.2-1)
[2019-11-03 21:43:32 -0500] information/cli: Loading configuration file(s).
[2019-11-03 21:43:32 -0500] critical/config: Error: syntax error, unexpected '[', expecting '('
Location: in /etc/icinga2/scripts/mail-host-notification.sh: 9:4-9:4
/etc/icinga2/scripts/mail-host-notification.sh(7): MAILBIN="mail"
/etc/icinga2/scripts/mail-host-notification.sh(8): 
/etc/icinga2/scripts/mail-host-notification.sh(9): if [ -z "$(command -v $MAILBIN)" ] ; then
                                                      ^
/etc/icinga2/scripts/mail-host-notification.sh(10):   echo "$MAILBIN not found in \$PATH. Consider installing it."
/etc/icinga2/scripts/mail-host-notification.sh(11):   exit 1

[2019-11-03 21:43:32 -0500] critical/cli: Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config.

I’ve run it though shellcheck and it updated the script to use current features (?) of bash (like $() instead of backticks) and replacing which with command -v, I also had to change the interpreter from sh to bash.

Even after all that it still gives the same errors and I have zero idea why Icinga2 doesn’t like it. I’m pretty proficient with bash, I’ve been using Linux for over a decade, but this boggles the mind.

Shell escaping is a PITA.
It is good to use shellcheck but sometimes it takes things too far. I am also suspecting that icinga user or whatever user is icinga daemon running under Pi is not using the same shell as the users you are checking your scripts against.

Try some of the following and see if it helps:
PROG="$(basename \"$0\")" or
PROG="$(basename $0)"

Cheers,
George

You aren’t kidding! I only found out about using $() a few months ago and I’ve been writing shell scripts for years, it definitely makes things easier to read.

This is correct, Icinga is running as the nagios user and the shell is set to /usr/sbin/nologin, but AFAIK that shouldn’t matter since I am explicitly telling it to execute with bash via the shebang being set to /bin/bash

Just for the hell of it I changed the nagios user’s shell to /bin/bash and attempted to restart Icinga2 but it failed, citing the above mentioned issue with the conditional statement

I logged in as the nagios user after setting the shell and executed it manually and it has no issues

nagios@pi:/etc/icinga2/conf.d$ bash /tmp/mail-host-notification.sh
Required parameter ‘LONGDATETIME’ is missing.

That does fix the initial error, but I still have the second error mentioned above with the conditional statement.

Did you merge the templates as it describes?

In older versions of Icinga, it was defined in commands.conf :
LONGDATETIME = "$icinga.long_date_time$"
but in newer versions it is:
notification_date = "$icinga.long_date_time$"

We all learn something new every day and that makes it exciting!

Cheers,
George

If you are using bash you should try to use bash commands and not calling external Programms.

This is from the pure bash bible

basename() {
    # Usage: basename "path"
    : "${1%/}"
    printf '%s\n' "${_##*/}"
}
1 Like

That’s not the issue though, it’s only giving that because I’m not supplying the necessary arguments on the CLI. The issue is that Icinga complains about the conditional statement.

Thanks for the insight, but that doesn’t solve the issue Icinga has with the conditional statement. Also I didn’t write this script, this is directly from the Icinga Github repository and is installed by default, so it should work perfectly considering the devs wrote it.

The error is below.

critical/config: Error: syntax error, unexpected '[', expecting '('
Location: in mail-host-notification.sh: 9:4-9:4
mail-host-notification.sh(7): MAILBIN="mail"
mail-host-notification.sh(9): if [ -z "$(command -v $MAILBIN)" ] ; then
                                 ^

Its complaining about the first square bracket, even though that’s the proper syntax. I even switched it to [] and it still complains about it.

Edit: This is definitely an issue with Icinga2 and not the Pi/ARM because I just tried to do the same thing on my server, which runs the x86_64 version of Arch Linux and is using the unmodified version which is installed by default, and it gives the exact same error.

Hi,

I’m coming from here:

In the issue you said you included the script folder:

Include the scripts directory by adding that path into icinga.conf (include "scripts/*.sh")

This is not neccasary, you define a NotificationCommand and in the definition you set the path to the script. An example can be found in the default configuration in conf.d/commands.conf or in the documentation:
https://icinga.com/docs/icinga2/latest/doc/09-object-types/#notificationcommand

Removing the include statement for the script folder should solve the problem with the config compiler (daemon -C).

Best regards
Michael

1 Like

The pushbullet script you are trying to install is from 3 years ago. Since then a lot of things have changed in Icinga including notifications parameters in commands.conf. In my opinion that is where the problem is but I could be wrong since I dont use pushbullet to test this script.

Cheers,
George

Thanks Micheal, this is the answer I was looking for.

I’m using the Director module and the documentation for it is seriously lacking, practically the only thing available is “here’s how to set it up” and I’m pretty much left to figure everything else out on my own. This is extremely confusing considering all the documentation is for how to do stuff manually via config files, and from what I understand, if you choose to use the Director, you shouldn’t be doing your configurations manually since the Director can’t manage them.

Edit: I removed the import line and it complained about missing templates for mail-host-notification and mail-service-notification so I added them via the Director and restarted Icinga, but I don’t see the two pushbullet scripts available in the dropdown box like I do for the other two, shouldn’t they show up as well?