Notification of a NEW host

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Notification of a NEW host

Post by abrist »

JohnFLi wrote: If you were able to include the name, that would be awesome.
Totally possible. You would need to generate an old hostlist and then check the new list against it.
Generate old hostlist once (and then save):

Code: Select all

grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'
| sort > <oldlist>
And then a new hostlist when it is run:

Code: Select all

grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'
| sort > <newlist>
And then compare:

Code: Select all

#!/bin/bash
for i in $(cat <newlist>); do
        if !(grep -q $i <oldlist>); then
                echo "$i";
        fi
done
Don't forget to overwrite the old hostlist with the new one once you are done comparing.
Tis' just a scratch, so you will have to shoehorn it into the shell script you are using.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Notification of a NEW host

Post by JohnFLi »

ok, I got them mushed together.

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
if [ ! -f /usr/local/nagios/libexec/oldlist ]; then
    echo "1" > "/usr/local/nagios/libexec/oldlist"
fi
if [ ! -f /usr/local/nagios/libexec/newlist ]; then
    echo "1" > "/usr/local/nagios/libexec/newlist"
fi
# Read old count file
old_count=$(head -n 1 /usr/local/nagios/libexec/hostcount)
# Compare counts
if [ "$old_count" -lt "$current_count" ];then
   
  grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > newlist
  for i in $(cat newlist);
	 do
        if !(grep -q $i oldlist);
	 then
                echo "$i";
        fi
  done  
  echo "CRITICAL - New host has been added!"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"

  exit 2
fi
if [ "$old_count" -gt "$current_count" ];then
    
  grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > newlist
  for i in $(cat newlist);
	 do
        if !(grep -q $i oldlist);
	 then
                echo "$i";
        fi
  done
  echo "CRITICAL - Host has been Nuked!"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"

  exit 2
fi
  echo "OK - No new hosts added"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"
  grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > oldlist
  exit 0


Now how would I get the name into the alert?

Code: Select all

echo "CRITICAL - New host has been added!"
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 »

In this bit of code here:

Code: Select all

# Compare counts
if [ "$old_count" -lt "$current_count" ];then
   new_hosts=''
  grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > newlist
  for i in $(cat newlist);
    do
        if !(grep -q $i oldlist);
    then
                echo "$i";
        fi
  done 
  echo "CRITICAL - New host has been added!"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"

  exit 2
fi
If I'm correct, this line is simply echoing the name of the host.

Code: Select all

echo "$i"
What you could do it add it to a variable so the exit message sends that variable. Something like:

Code: Select all

# Compare counts
if [ "$old_count" -lt "$current_count" ];then
   new_hosts=''
  grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > newlist
  for i in $(cat newlist);
    do
        if !(grep -q $i oldlist);
    then
                new_hosts+={"$i"};
        fi
  done 
  echo "CRITICAL - New host(s) have been added! = $new_hosts"
  echo $current_count > "/usr/local/nagios/libexec/hostcount"

  exit 2
fi
Which if there were three new hosts added the output would be like:

CRITICAL - New host(s) have been added! = {host1}{host2}{host3}
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 »

Here it is tested and completed:

Code: Select all

#!/bin/bash
#
# Check for new hosts
# by Jim Clark / banditbbs @ gmail . com
#
# Verify old list exists
if [ ! -f /usr/local/nagios/libexec/oldhostlist ]; then
        grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > /usr/local/nagios/libexec/oldhostlist
fi
# Get current list of hosts
grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > /usr/local/nagios/libexec/newhostlist
# Compare lists
hosts=""
for i in $(cat /usr/local/nagios/libexec/newhostlist); do
        if !(grep -q $i /usr/local/nagios/libexec/oldhostlist); then
                hosts="$hosts - $i"
        fi
done
if [ ! -z "$hosts" ]; then
        echo "WARNING - Hosts added!$hosts"
        grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > /usr/local/nagios/libexec/oldhostlist
        exit 1
fi
echo "OK - No new hosts added"
grep -h host_name /usr/local/nagios/etc/hosts/* | sed -e 's/host_name[ \t]*//g;;s/[ \t]*//g'| sort > /usr/local/nagios/libexec/oldhostlist
exit 0
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
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: Notification of a NEW host

Post by jdalrymple »

If everyone is satisfied I'll lock and mark solved?
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Notification of a NEW host

Post by JohnFLi »

Looks like its working for me.

Thank you very much for your help.

Guess I need to learn perl ;)
Everybody is somebody else’s weirdo
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Notification of a NEW host

Post by abrist »

JohnFLi wrote:Guess I need to learn perl ;)
No need to curse around here :P
I find shell stream-processing and regex to be more fun and approachable, albeit a bit more fragile.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: Notification of a NEW host

Post by JohnFLi »

lol

As long as I can learn how to be able to create my own scripts and what-not
Everybody is somebody else’s weirdo
Locked