How to create a list of Nagios group hosts from hosts using Notepad ++ / RegEx or another

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

I have several (several 1000 lines) Nagios / Icinga configurations that look 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 host groups containing all 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 "host_name" contains "AP", it should go to GroupB and everything else in GroupA (note that they must be separated by commas).

Does anyone have any idea how I can automate this?

Thank you for your time:)

+4
source share
2 answers

, , Nagios:

nagios.cfg:

use_regexp_matching=1

.

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

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

, lookbehind/lookahead, . , , , , .

, , ?

+3

( ;)).

LINQPad :

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;

 }

IP- , , "string members =...", txt . , . , .

. Nagios Icinga . , #Adrian Frühwirth. , .

, - :)

.

0

Source: https://habr.com/ru/post/1530239/


All Articles