2016-04-29 28 views
1

我有nagios/icinga目標文件具有下面類似的內容,但很多。我如何使用bash或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 
} 

目標是從文件中提取特定主機或服務對象,例如,

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

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

+0

這種試圖用sed -n「/ ^定義主機{/ ,/ \} $/p'bgp.cfg雖然有效,但我認爲@ ritesht93提供的答案更好 –

回答

1

如果我理解正確的提供,你不需要servicegroup ..right?如果你想要的內容

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

僅在大括號您可以使用此:

您可以使用此命令sed

$ 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

好的答案!但是,獲取服務和組的大括號內容不是問題嗎? – Inian

+0

@Inian OP表示他只需要'主機對象'和'服務對象',所以我認爲這意味着不需要任何組,即沒有'hostgroup'和'servicegroup' – ritesht93

+0

第一個像charm sed -nr'/一樣工作。 *(\ bhost \ b | \ bservice \ b)。* \ {/,/ \}/p'數據 –

相關問題