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!
Using real hostname vs "on-Nagios" hostname
-
- Posts: 21
- Joined: Wed May 13, 2015 7:31 am
- 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
Let's say your notification command is:
You could echo the localhost by adding the command in the command line using backticks, like:
Maybe add it at the beginning:
I haven't tested this but I'm sure it's where you need to implement it.
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$
}
Code: Select all
`$(hostname)`
Code: Select all
/usr/bin/printf "%b" "***** Nagios *****\n\nSending host is `$(hostname)`\n\nNotification Type: $NOTIFICATIONTYPE$\n
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
-
- Posts: 21
- Joined: Wed May 13, 2015 7:31 am
Re: Using real hostname vs "on-Nagios" hostname
This got me on the right track, though it was slightly off. This is what works:
So this
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
Code: Select all
`hostname`
Code: Select all
/usr/bin/printf "%b" "***** Nagios *****\n\nSending host is `hostname`\n\nNotification Type: $NOTIFICATIONTYPE$\n
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
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?
-
- Posts: 21
- Joined: Wed May 13, 2015 7:31 am
Re: Using real hostname vs "on-Nagios" hostname
Yes, the original question has been answered so this can close. Hopefully I can find the answer to that secondary question somewhere though, haha.