Page 1 of 2

Notification of a NEW host

Posted: Tue Apr 21, 2015 10:17 am
by JohnFLi
Is it possible to have it set so that when a new host is added, that a notification gets sent to a person (or people)?

The reason for this is that I have 4 other Admins that also add systems to Nagios and some of them always mess something up.
If I can get notified when a new system is added, I can go in and make sure its done right.

Re: Notification of a NEW host

Posted: Tue Apr 21, 2015 12:15 pm
by jdalrymple
Use this and count the number of *cfg files in /usr/local/nagios/etc/hosts/ maybe?

Re: Notification of a NEW host

Posted: Tue Apr 21, 2015 6:08 pm
by JohnFLi
that might work, put i get an error when trying to run it

Code: Select all

]# ./check_files.pl
Can't locate Date/Parse.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at ./check_files.pl line 159.
BEGIN failed--compilation aborted at ./check_files.pl line 159.

Re: Notification of a NEW host

Posted: Tue Apr 21, 2015 6:19 pm
by Box293

Code: Select all

yum -y install perl-TimeDate

Re: Notification of a NEW host

Posted: Tue Apr 21, 2015 9:59 pm
by BanditBBS
Or use this quick script I just wrote since I loved your idea and suffer same thing here!!!!

Code: Select all

#!/bin/bash
#
# Check for new hosts
# by Jim Clark / banditbbs @ gmail . com
#
# Get current count of host files
current_count=$(ls /usr/local/nagios/etc/hosts | wc -l)
# Check for this script running before and if not create dummy file
if [ ! -f /usr/local/nagios/libexec/hostcount ]; then
    echo "1" > "/usr/local/nagios/libexec/hostcount"
fi
# Read old count file
old_count=$(head -n 1 /usr/local/nagios/libexec/hostcount)
# Compare counts
if [ "$old_count" -lt "$current_count" ];then
  echo "Warning - New host has been added!"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"
  exit 1
fi
  echo "OK - No new hosts added"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"
  exit 0
I'm not in the office to test this, but pretty darn sure it is bug free 8-)

EDIT #1: Wait, dont use it yet, have to update it to write the new count to the file
EDIT #2: Ok, fixed. You'll have to set max checks to 1 as when it detects and exits with a warning, it'll return to OK on the very next check.
EDIT #3: Connected to work and fixed the bug I had in it, lol
EDIT #4: Added code correction in following post.

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 12:25 am
by rseiwert
Need to account for the case when hosts are deleted. I'm fairly certain XI deletes the old host files when hosts are deleted. If you were to delete 5 hosts you will not get an alert until a 6th new host is added.
if you change

Code: Select all

  
  echo "OK - No new hosts added"
  exit 0

Code: Select all

  echo "OK - No new hosts added"
  echo $current_count > "/usr/local/nagios/libexec/hostcount" 
  exit 0
it would solve this issue. My initial though was to query the database directly for a count of hosts and trigger off that but not knowing the table structure or MySQL or how to access this in a script this way was probably out of reach for me.

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 6:54 am
by BanditBBS
Yeah good catch on that. I'll modify my original post once I get to work. The only time this won't catch one is if somebody deletes and adds all within the same apply config!

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 10:26 am
by JohnFLi

Code: Select all

yum -y install perl-TimeDate
that worked, thank you.


BanditBBS;
Your script works great as well. Thank you.
I will try to get it to check for deleted hosts as well.

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 10:33 am
by BanditBBS
Also, even a better idea would be to write a plugin to search the audit log for "New host inserted:" and display any outputs. it would have the new hostname after the : as well. I have no clue which DB the audit log is kept in. if someone in nagios wants to tell me the DB and table I'll attempt to write the plugin.

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 10:39 am
by JohnFLi
ok, i added some lines to notify when a host has been deleted

If you were able to include the name, that would be awesome.

Code: Select all

#!/bin/bash
#
# Check for new hosts
# by Jim Clark / banditbbs @ gmail . com
#
# Get current count of host files
current_count=$(ls /usr/local/nagios/etc/hosts | wc -l)
# Check for this script running before and if not create dummy file
if [ ! -f /usr/local/nagios/libexec/hostcount ]; then
    echo "1" > "/usr/local/nagios/libexec/hostcount"
fi
# Read old count file
old_count=$(head -n 1 /usr/local/nagios/libexec/hostcount)
# Compare counts
if [ "$old_count" -lt "$current_count" ];then
  echo "Warning - New host has been added!"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"
  exit 1
fi
if [ "$old_count" -gt "$current_count" ];then
  echo "Warning - Host has been Nuked!"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"
  exit 1
fi
  echo "OK - No new hosts added"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"
  exit 0
The part I added is

Code: Select all

if [ "$old_count" -gt "$current_count" ];then
  echo "Warning - Host has been Nuked!"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"
  exit 1
fi

Dont forget to set permissions on the hostcount file so that Nagios can write to it ;)