2016-04-29 9 views
1

extrahieren Ich habe Nagios/Icinga-Objekt-Datei mit ähnlichen Inhalten unten, aber viele davon. Wie extrahiere ich die ähnlichen Objekte zum Beispiel nur die "Host-Objekte" oder "Service-Objekte" mit Bash oder Python.wie Zeichenfolge zwischen geschweiften Klammern aus Datei

define host{   ## extract including the "define host {....}" 
    use    generic-switch 
    host_name  bras ; 
    alias   bras-gw.example.com; 
    address   20.94.66.88 
    hostgroups  bgp; 
} 

define host{ ## extract including the "define host {....} define host {....} " 
    use    generic-switch 
    host_name  ar1 ; 
    alias   ar1.example.com; 
    address   22.98.66.244 
    hostgroups  bgp; 
} 

define servicegroup { 
    servicegroup_name Premium 
    alias Premium-BGP 
} 
define service { 
    host_name    ar0 
    service_description  Get-Speed- BGP-INTL dsdf34 
    check_command   check_bgp!secreat!10.10.40.44 
    check_interval   1 
    use      generic-service 
    notification_interval 0 ; set > 0 if you want to be re-notified 
} 

define service { 
    host_name    ar10 
    service_description  Get-Speed- BGP-INTL rrdf34 
    check_command   check_bgp!secreat!10.10.40.77 
    check_interval   1 
    use      generic-service 
    notification_interval 0 ; set > 0 if you want to be re-notified 
    check_period       24x7 
      notification_period     24x7 
      contact_groups      p2p,l2,system2,admins 
      use         generic-service 
      max_check_attempts  3 
      notification_options c,r 
} 

Ziel ist bestimmte Host oder Service-Objekte aus der Datei zB zu extrahieren.

define host{ 
    use    generic-switch 
    host_name  ar0 ; 
    alias   id6.example.net; 
    address   20.24.6.22 
    hostgroups  bgp; 
} 
define host{ 
    use    generic-switch 
    host_name  bras ; 
    alias   bras-gw.abc.com.dp; 
    address   202.33.66.254 
    hostgroups  bgp; 
} 
define host{ 
    use    generic-switch 
    host_name  ar1 ; 
    alias   ar1.abc.com; 
    address   20.94.66.44 
    hostgroups  bgp; 
    } 

Am: sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' datafile als ritesht93

von @ bereitgestellt
+0

mit dieser versucht, sed -n ‚/^define host {/ ,/\} $/p 'bgp.cfg obwohl funktioniert, aber ich denke, die Antwort von @ ritesht93 ist besser –

Antwort

1

Wenn ich richtig verstehe, Sie brauchen nicht servicegroup ..right?

können Sie verwenden diese sed Befehl:

$ sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' data 
define host{   ## extract including the "define host {....}" 
    use    generic-switch 
    host_name  bras ; 
    alias   bras-gw.example.com; 
    address   20.94.66.88 
    hostgroups  bgp; 
} 
define host{ ## extract including the "define host {....} define host {....} " 
    use    generic-switch 
    host_name  ar1 ; 
    alias   ar1.example.com; 
    address   22.98.66.244 
    hostgroups  bgp; 
} 
define service { 
    host_name    ar0 
    service_description  Get-Speed- BGP-INTL dsdf34 
    check_command   check_bgp!secreat!10.10.40.44 
    check_interval   1 
    use      generic-service 
    notification_interval 0 ; set > 0 if you want to be re-notified 
} 
define service { 
    host_name    ar10 
    service_description  Get-Speed- BGP-INTL rrdf34 
    check_command   check_bgp!secreat!10.10.40.77 
    check_interval   1 
    use      generic-service 
    notification_interval 0 ; set > 0 if you want to be re-notified 
    check_period       24x7 
      notification_period     24x7 
      contact_groups      p2p,l2,system2,admins 
      use         generic-service 
      max_check_attempts  3 
      notification_options c,r 
} 
$ 

Wenn Sie den Inhalt in Klammern nur wollen, können Sie verwenden diese:

$ sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' data | sed -r '/^\s*define.*\{/d;s/\}/\n/g' 
    use    generic-switch 
    host_name  bras ; 
    alias   bras-gw.example.com; 
    address   20.94.66.88 
    hostgroups  bgp; 


    use    generic-switch 
    host_name  ar1 ; 
    alias   ar1.example.com; 
    address   22.98.66.244 
    hostgroups  bgp; 


    host_name    ar0 
    service_description  Get-Speed- BGP-INTL dsdf34 
    check_command   check_bgp!secreat!10.10.40.44 
    check_interval   1 
    use      generic-service 
    notification_interval 0 ; set > 0 if you want to be re-notified 


    host_name    ar10 
    service_description  Get-Speed- BGP-INTL rrdf34 
    check_command   check_bgp!secreat!10.10.40.77 
    check_interval   1 
    use      generic-service 
    notification_interval 0 ; set > 0 if you want to be re-notified 
    check_period       24x7 
      notification_period     24x7 
      contact_groups      p2p,l2,system2,admins 
      use         generic-service 
      max_check_attempts  3 
      notification_options c,r 


$ 
+0

Gute Antwort! Aber war nicht die Frage, um den Inhalt innerhalb der Klammern für Service und Gruppe zu bekommen? – Inian

+0

@Inian OP sagte, er brauche nur 'host objects' und' service objects', also denke ich, dass es keine Gruppen benötigt, dh keine 'hostgroup' und keine' servicegroup' – ritesht93

+0

die erste funktionierte wie charme sed -nr '/. * (\ bhost \ b | \ bservice \ b). * \ {/,/\}/p 'Daten –