Page 2 of 2

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 5:02 pm
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.

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 6:30 pm
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!"

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 7:04 pm
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}

Re: Notification of a NEW host

Posted: Wed Apr 22, 2015 7:14 pm
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

Re: Notification of a NEW host

Posted: Thu Apr 23, 2015 9:34 am
by jdalrymple
If everyone is satisfied I'll lock and mark solved?

Re: Notification of a NEW host

Posted: Thu Apr 23, 2015 10:17 am
by JohnFLi
Looks like its working for me.

Thank you very much for your help.

Guess I need to learn perl ;)

Re: Notification of a NEW host

Posted: Thu Apr 23, 2015 10:44 am
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.

Re: Notification of a NEW host

Posted: Thu Apr 23, 2015 11:06 am
by JohnFLi
lol

As long as I can learn how to be able to create my own scripts and what-not