Search code examples
zabbix

Zabbix configuration - Action


The action which I'm configuring is used to sending email notification. The problem is that if I use the macros in action's name and message the email will not be send out, if using just plain text the email can be sent out succesfully. I'm using the media type of Script and the script do the job of sending email. The script as below:

#!/bin/sh
export smtpemailfrom=zabbix@yourdomain.com
export zabbixemailto=$1
export zabbixsubject=$2
export zabbixbody=$3
export smtpserver=yoursmtpserver.com
export smtplogin=smtpuser
export smtppass=smtppassword

/usr/bin/sendEmail -f $smtpemailfrom -t $zabbixemailto -u $zabbixsubject -m $zabbixbody -s $smtpserver:25 -xu $smtplogin -xp $smtppass

I was wondering if the reason is that the are some special chars in message, but even if I add quotation around the subject and body, it still not work.


Solution

  • It is advisable to put input variables in double quotes.

    For instance, an email subject is likely to consist of multiple words. The following does not work as might be intended:

    $ export subject=hello world
    $ echo $subject
    hello
    

    Similarly, curly braces that are used in Zabbix macros have a special meaning to the shell, see Bash subshell creation with curly braces question.

    Therefore, you should double quote variable expansion, at least for those variables where you are not sure about the content:

    #!/bin/sh
    export smtpemailfrom=zabbix@yourdomain.com
    export zabbixemailto="$1"
    export zabbixsubject="$2"
    export zabbixbody="$3"
    export smtpserver=yoursmtpserver.com
    export smtplogin=smtpuser
    export smtppass=smtppassword
    
    /usr/bin/sendEmail -f $smtpemailfrom -t "$zabbixemailto" -u "$zabbixsubject" \
                -m "$zabbixbody" -s $smtpserver:25 -xu $smtplogin -xp "$smtppass"