Page 1 of 1

Using real hostname vs "on-Nagios" hostname

Posted: Wed Aug 12, 2015 4:48 pm
by pamplifier
Sorry if this is a dumb question, but does anyone know how I can reference a server's "real" hostname rather than what is defined on the Nagios host definition?

Lets say I have multiple machines defining themselves as "localhost" in their own hosts.cfg in their own Nagios directories.

And let's say there are all notifying me by email of whatever state changes that happened. I'm going to get a lot of emails from machines calling themselves "localhost", which isn't very helpful in problem solving.

I would like to use their "real" hostnames, as they are defined by their systems, that is returned when I run echo $(hostname) on the command line. To have what is printed to the emails rely on what the system defines, not just what Nagios has defined.

I just wanted to avoid changing all their host definition hostnames because on the off chance I need to rename them on the system (say from myBox1 to myBox1A), I will have to rename them on Nagios as well to keep the notifications accurate. If there is a way to avoid this, I'd love to hear it. Thanks!

Re: Using real hostname vs "on-Nagios" hostname

Posted: Wed Aug 12, 2015 8:01 pm
by Box293
Let's say your notification command is:

Code: Select all

# 'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
        }
You could echo the localhost by adding the command in the command line using backticks, like:

Code: Select all

`$(hostname)`
Maybe add it at the beginning:

Code: Select all

/usr/bin/printf "%b" "***** Nagios *****\n\nSending host is `$(hostname)`\n\nNotification Type: $NOTIFICATIONTYPE$\n
I haven't tested this but I'm sure it's where you need to implement it.

Re: Using real hostname vs "on-Nagios" hostname

Posted: Fri Aug 14, 2015 11:03 am
by pamplifier
This got me on the right track, though it was slightly off. This is what works:

Code: Select all

`hostname`
So this

Code: Select all

    /usr/bin/printf "%b" "***** Nagios *****\n\nSending host is `hostname`\n\nNotification Type: $NOTIFICATIONTYPE$\n
returned what I was looking for.

I would've never thought of trying the backticks, thank you! I'm wondering why $(hostname) doesn't work, yet `hostname` does :?

Re: Using real hostname vs "on-Nagios" hostname

Posted: Fri Aug 14, 2015 12:11 pm
by jolson
I'm glad this worked out for you! As for the differences between backticking and $(), functionally they are very similar - I'm not sure why backticks worked here and $() did not. Are we good to close this thread up?

Re: Using real hostname vs "on-Nagios" hostname

Posted: Fri Aug 14, 2015 1:25 pm
by pamplifier
Yes, the original question has been answered so this can close. Hopefully I can find the answer to that secondary question somewhere though, haha.