1

I have nagios/icinga object file with similar contents below but lots of it. How do i extract the similar objects for example only the "host objects" or "service objects" using bash or python.

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
}

Target is to extract specific host, or service objects from the file eg.

    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;
    } 

Ans: sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' datafile as provided by @ritesht93

sherpaurgen
  • 3,028
  • 6
  • 32
  • 45
  • tried with this sed -n '/^define host{/,/\}$/p' bgp.cfg although works but i think the answer provided by @ritesht93 is better – sherpaurgen Apr 29 '16 at 06:39

1 Answers1

1

If I understand correctly, you don't need servicegroup ..right?

You can use this sed command:

$ 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
}
$

If you want the contents only within braces you can use this:

$ 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


$
riteshtch
  • 8,629
  • 4
  • 25
  • 38
  • Good answer! But wasn't the question to get the content within the braces for service and group? – Inian Apr 29 '16 at 06:09
  • @Inian OP said he needs only `host objects` and `service objects`, so i think it means no groups are needed i.e no `hostgroup` and no `servicegroup` – riteshtch Apr 29 '16 at 06:14
  • the first one worked like charm sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' data – sherpaurgen Apr 29 '16 at 06:17