0

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.

Joanne
  • 514
  • 3
  • 8
  • 30
  • `ssh root@10.20.30.40 "` - You can group messages and redirect it via ssh later with a pipe: `{ echo blablastring; echo another line; } | ssh root@10.20.30.40 'cat >> /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg'`. `is that it sometimes 6 lines` - you want [delete lines between patterns](https://stackoverflow.com/questions/6287755/using-sed-to-delete-all-lines-between-two-matching-patterns) and [google](https://www.google.com/search?q=remove+lines+between+patterns). If you have hard time understanding sed I really advise to just play with [regexcrosswords](https://regexcrossword.com/). – KamilCuk Oct 22 '19 at 12:32
  • But the issue is that IP's are always different and there are also some (minor) differences in hostgroups. So without checking the vz_nodes_hosts.cfg by hand or creating another seperate check for (e.g.) IP, it seems not that easy to achieve. That's why I was wondering if it would be possible to remove 6 or 7 lines just based on the host_name. – Joanne Oct 22 '19 at 12:39
  • Ach ok. Can you post some example file before and after the replacement is done? So for example having a file consisting of all 3 code snippets from your post, you want to remove the code snipped between `define host{` and `}` that has inside the brackets for example `host_name serverraid300`? Will there be any other `}` characters in the file? Can we split the input file into chunks by `}` character and parse each chunk separately? Ex. with `IFS='}' read -a array – KamilCuk Oct 22 '19 at 13:05
  • That's the problem; I have no way of replacing them (as mentioned in my post). I am currently only displaying a message saying the host is already in the file. Nothing else. Also there are no special chars other than { and }. What I showed in the examles is what is being displayed. Only minor changes to host_name, alias, address and hostgroups. Nothing else. Don't know if this provides any help at all? – Joanne Oct 22 '19 at 13:42

1 Answers1

1

Okay I managed to get it removed and hopefully it will help others.

I am now using the follwing piece of code to replace a match based on the hostname used:

awk '!/servernonraid200/' RS= ORS="\n\n" /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg > /tmp/vz_nodes_hosts.cfg && mv -f /tmp/vz_nodes_hosts.cfg /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg

Before command:

define host{
        use                     linux-server
        host_name               server100
        alias                   server100
        address                 33.33.44.44
        }

define host{
        use                     linux-server
        host_name               servernonraid200
        alias                   servernonraid200
        address                 55.55.66.66
        hostgroups              vz_nodes,vz_partion
        }

define host{
        use                     linux-server
        host_name               serverraid300
        alias                   serverraid300
        address                 77.77.88.88
        hostgroups              vz_nodes,vz_partion,raid_megacli
        }

After command:

define host{
        use                     linux-server
        host_name               server100
        alias                   server100
        address                 33.33.44.44
        }

define host{
        use                     linux-server
        host_name               serverraid300
        alias                   serverraid300
        address                 77.77.88.88
        hostgroups              vz_nodes,vz_partion,raid_megacli
        }

So that's a big succes for me...!

However

It seems awk does not play nice with variables, so running the command with a variable like this:

awk -v '!/$HOST/' RS= ORS="\n\n" /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg > /tmp/vz_nodes_hosts.cfg && mv -f /tmp/vz_nodes_hosts.cfg /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg

Does not. I tried using awk -v, but that didn't prove helpful.

I will try to find a solution and improve my answer, so it might help others. If someone else has an idea on how tix this in the meantime, please do share. I will update the answer as well...!

Update 2 - Fixed!

Okay, I think I managed to fix the previous issue by using single quotes (sometimes the answer seems so easy)

The full working code is:

 awk '!/'$HOST'/' RS= ORS="\n\n" /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg > /tmp/vz_nodes_hosts.cfg && mv -f /tmp/vz_nodes_hosts.cfg /usr/local/nagios/etc/objects/vz_nodes_hosts.cfg

This will remove the correct host from the .cfg file with using a variable!

I hope this helps someone. If anyone has a better approach, please advice. :-)

Joanne
  • 514
  • 3
  • 8
  • 30
  • 1
    Are you running this awk line within a bash script or from the command-line? The command-line has special meaning for ! which I don't think applies inside a script. In any case, you might decide `'!'/"$HOST"/` is slightly more readable. – Gem Taylor Oct 24 '19 at 10:27