I am working on a complete kickstart script for setting up servers and adding them finally to my Nagios configuration. Adding the newly created / kickstarted servers is not an issue, however how do I remove old ones (if they previously existed)?
Current situation
This is what I am currently using:
checknagios=$(ssh root@10.20.30.40 "cat /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg | grep $HOST" 2>&1)
It currently only adds new / kickstarted servers to the vz_nodes_hosts.cfg file. If the hosts exists it will simply output a message (so I have to remove and add it manually). I want this procedure automated so if the hosts already exists in vz_nodes_hosts.cfg I want it removed.
I have three examples at the moment of a previously added host:
Example 1
define host{
use linux-server
host_name server100
alias server100
address 33.33.44.44
}
(actually the above server has an incorrect setup, but I want to make sure these entries also get removed completely)
Example 2
define host{
use linux-server
host_name servernonraid200
alias servernonraid200
address 55.55.66.66
hostgroups vz_nodes,vz_partion
}
Example 3
define host{
use linux-server
host_name serverraid300
alias serverraid300
address 77.77.88.88
hostgroups vz_nodes,vz_partion,raid_megacli
}
Now what would be the best way, if the hostname matches
This is what I currently have (it only reports a message, instead of removing an old entry):
if [[ "$checknagios" == *"$HOST"* ]]; then
echo -e "This hardware node already exists in Nagios? Please check manually."
else
ssh root@10.20.30.40 "echo 'define host{' >> /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg"
ssh root@10.20.30.40 "echo ' use linux-server' >> /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg"
ssh root@10.20.30.40 "echo ' host_name $HOST' >> /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg"
ssh root@10.20.30.40 "echo ' alias $HOST' >> /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg"
ssh root@10.20.30.40 "echo ' address $NODEIP' >> /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg"
(and here after it checks if a RAID controller is present and adds it to the hostgroups of nagios)
So what do I want to achieve?
When the hosts already exists in vz_nodes_hosts.cfg file, it should remove it (everything related to it). I am guessing I have to use sed somehow, however I have no clue on how to remove all those related lines. Also another issue is, is that it sometimes 6 lines (see example 1) and sometimes 7 lines (see examples 2 & 3).
The only method of checking is to check if the host_name (or alias) is already in vz_nodes_hosts.cfg. If it is there, I want to remove everything related to it (6 or 7 lines).
Is this possible? If it's difficult or (maybe) even impossible it should remove the 7 lines (as seen in examples 2 and 3). I doubt the 6 lines will happen a lot and only happens when something is missing, but in time this will get obviously less and less as I am working all the time on this script and tweaking it.
I hope I explained it correcly and hoping someone can help or shed some light on this issue. Thanks in advance.