4

I'm using Notepad++ and I would like to ask for help to achieve the following.

I have some(several 1000 lines) Nagios/Icinga config that looks like this:

define host {
    use         generic-host
    host_name   FakeNameA-748-SomeNameA
    alias       FakeNameA-748-SomeNameA
    address     10.1.1.97
    }
define host {
    use         generic-host
    host_name   H548-AP02
    alias       H548-AP02
    address     172.19.115.190
    }
define host {
    use         generic-host
    host_name   FakeNameB-302-SomeNameB
    alias       FakeNameB-302-SomeNameB
    address     192.168.149.1
    }
define host {
    use         generic-host
    host_name   FakeNameC-902-Acronym
    alias       FakeNameC-902-Acronym
    address     192.168.48.1
    }
define host {
    use         generic-host
    host_name   H902-AP01
    alias       H902-AP01
    address     192.168.48.190
    }

I need to create some hostgroups that contain all the host names, but in two groups.

Like this:

define hostgroup {
    hostgroup_name    GroupA
    alias    GroupA
    members FakeNameA-748-SomeNameA,FakeNameB-302-SomeNameB,FakeNameC-902-Acronym
    }

define hostgroup {
    hostgroup_name    GroupB
    alias    GroupB
    members H548-AP02,H902-AP01
    }

As you can see, if the "host_name" contains "AP" it should go into GroupB and everything else into GroupA(note that they should be comma separated).

Does anyone have some idea of how i could automate this?

Thank you for your time :)

Brandur
  • 41
  • 1
  • 3

2 Answers2

3

If your hostnames match the schema in your example you can do this dynamically with regular expression matching that comes with Nagios:

nagios.cfg:

use_regexp_matching=1

.

define hostgroup {
    hostgroup_name  GroupA
    members         ^[^-]+-[0-9]+-[^-]$ 
}

define hostgroup {
    hostgroup_name  GroupB
    members         ^[^-]+-AP[0-9]+$
}

Unfortunately the regular expression engine used does not support lookbehind/lookahead, otherwise this could be achieved more easily and reliably. As it is, you have to find expressions that match one group but not the other for both groups in question.

On a side note, why do you bother setting aliases that are the same as the hostnames?

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
  • Thank you for answering. I inherited this setup. So I have not set this setup :) I could live with just a onetime generated list, because it is maintained manually. It will be updated sometime in the future. But not at the moment. – Brandur Mar 05 '14 at 12:56
0

I solved this(with some help ;) ).

I used the program called LINQPad with the following code:

void Main()

 {
    List<string> lines= GetLines();
    List<string> hostNames = new List<string>();
    string members = "10.0.1.191,10.0.1.193,10.0.107.190";
    foreach(string member in members.Split(','))
    {
        var index = lines.FindIndex((s) =>            s.Contains(member));
        string aliasLine = lines[index + 1];
        int count = aliasLine.IndexOf("\t\t");
        string hostName = aliasLine.Remove(0, count + 2);
        hostNames.Add(hostName);
    }

    string.Join(",",hostNames).Dump();

 }

List<string> GetLines()
 {
    List<string> lines = new List<string>();
    using (var sr = new StreamReader("c:\\temp\\Nagios_Host_List.txt")) {
        while (!sr.EndOfStream)
        {
            lines.Add( sr.ReadLine());
        }
    }
    return lines;

 }

Because I already had a members list with all the IP addresses in a single comma seperated line, I just pasted this besides the "string members = ..." and the txt file contained all the host entries. The result was a single line with comma separated host names. Just what i needed.

It solved my specific issue at the moment. But when we will be upgrading from Nagios to Icinga in the future. I will be taking into account what #Adrian Frühwirth posted. This way we could automate it more.

Hopefully someone can use this information to there advantage :)

Happy day.

Brandur
  • 41
  • 1
  • 3