Execute terminal command not working (notificatoin)

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
tegryan
Posts: 2
Joined: Fri Nov 09, 2012 4:11 am

Execute terminal command not working (notificatoin)

Post 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?
tegryan
Posts: 2
Joined: Fri Nov 09, 2012 4:11 am

Re: Execute terminal command not working (notificatoin)

Post 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


slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Execute terminal command not working (notificatoin)

Post by slansing »

Thank you for sharing your temporary solution with the community!
Locked