Page 1 of 1

Execute terminal command not working (notificatoin)

Posted: Fri Nov 09, 2012 6:56 pm
by tegryan
Hi all,

I'm trying to set up a notification command. I want to use notify-send, like I do from the command line or in Bash scripts. However, the below does not work:

define command {
command_name notify-service-send
command_line /usr/bin/notify-send "$NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}

var/log/messages, var/log/nagios/nagios.log both look like it worked, but I don't see the notification on my desktop. I thought maybe it was sending it to the nagios user's desktop, but I created a test script and executed it as root and it sent it to my user's desktop.

What am I doing wrong?

Re: Execute terminal command not working (notificatoin)

Posted: Mon Nov 12, 2012 12:19 pm
by tegryan
It's a bit ugly but here's the solution I settled on for now:

define command {
command_name notify-service-send
command_line sudo -u <user to send the message to> /usr/lib64/nagios/plugins/notify '$HOSTNAME$' '$SERVICESTATE$'
}


And my command script looks like:

Code: Select all

#!/bin/sh

init_notify() {
        pids=`pgrep -u <user to send the message to> nautilus`
        for pid in $pids; do
                # find DBUS session bus for this session
                DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=$
                # use it
                #export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS
        done
}

notify() {
	if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
                init_notify
        fi

	title=$1
        text=$2
        timeout=$3

        if [ -z "$title" ]; then
                return
        fi
	if [ -z "$text" ]; then
                text=$title
fi
	if [ -z "$timeout" ]; then
                timeout=5000
        fi
	#echo $text $timeout
        #notify-send "$title" "$text" -t $timeout
        DISPLAY=:0.0 /usr/bin/notify-send "$title" "$text" -t $timeout
}

notify $1 $2



Re: Execute terminal command not working (notificatoin)

Posted: Mon Nov 12, 2012 2:37 pm
by slansing
Thank you for sharing your temporary solution with the community!