#!/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.
#!/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
# 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.
# 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.
#!/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
No need to curse around here
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.