Notification of a NEW host

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Notification of a NEW host

Post 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.
Everybody is somebody else’s weirdo
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: Notification of a NEW host

Post by jdalrymple »

Use this and count the number of *cfg files in /usr/local/nagios/etc/hosts/ maybe?
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Notification of a NEW host

Post 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.
Everybody is somebody else’s weirdo
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Notification of a NEW host

Post by Box293 »

Code: Select all

yum -y install perl-TimeDate
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

Re: Notification of a NEW host

Post 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.
Last edited by BanditBBS on Wed Apr 22, 2015 7:58 am, edited 1 time in total.
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
User avatar
rseiwert
Posts: 196
Joined: Wed Jun 22, 2011 10:33 pm
Location: Somewhere between Here and Now

Re: Notification of a NEW host

Post 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.
Grumpy Olde IT Guy
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

Re: Notification of a NEW host

Post 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!
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Notification of a NEW host

Post 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.
Everybody is somebody else’s weirdo
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

Re: Notification of a NEW host

Post 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.
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Notification of a NEW host

Post 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 ;)
Everybody is somebody else’s weirdo
Locked