Using real hostname vs "on-Nagios" hostname

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
pamplifier
Posts: 21
Joined: Wed May 13, 2015 7:31 am

Using real hostname vs "on-Nagios" hostname

Post 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!
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

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

Post 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.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
pamplifier
Posts: 21
Joined: Wed May 13, 2015 7:31 am

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

Post 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 :?
jolson
Attack Rabbit
Posts: 2560
Joined: Thu Feb 12, 2015 12:40 pm

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

Post 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?
Twits Blog
Show me a man who lives alone and has a perpetually clean kitchen, and 8 times out of 9 I'll show you a man with detestable spiritual qualities.
pamplifier
Posts: 21
Joined: Wed May 13, 2015 7:31 am

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

Post 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.
Locked