2013-03-14 49 views
0

我寫了一個自定義的事實,它返回的addr:port一個逗號分隔的列表,像這樣:如何迭代字符串以構造nagios_service作爲導出資源檢查?

sb_intl_conn => sbcms-t:22,sbsql05-wvuk-inst5:1434,sborc07-uk-t:1533,..,..,.. 

字符串中元素的個數從節點到不同的節點。我需要對他們每個人做一次Nagios tcp-port-check。我認爲sb_intl_conn.split(",")將把這個字符串轉換成一個數組,然後我怎麼能遍歷它來做這樣的事情?

@@nagios_service { "check_stat_${::fqdn}_${addr}_${port}": 
    use     => 'generic-service', 
    check_command  => "remote-nrpe-tcp-check!${addr}!${port}", 
    service_description => "V2::CON: ${addr} [Palms]", 
    display_name  => "Connection check: ${addr}:${port}", 
    servicegroups  => 'batch-worker', 
    hostgroup_name  => 'batch-job', 
} 

任何幫助將不勝感激。乾杯!!


更新:1

我綁到仿真iamauser的建議,但沒有能夠讓我的頭圍繞它。這是我做的:在我的foo.pp:

class test::foo { 

    define bar { 
     $var1 = inline_template("<%= scope.lookupvar($name).split(':').first.to_s.chomp %>") 
     $var2 = inline_template("<%= scope.lookupvar($name).split(':').last.to_s.chomp %>") 
     notify {"${var1}_${var2}": } 
    } 
} 

,然後在我的node.pp:

$ifs = ['abc.com:80','xyz.co.uk:1512'] 
test::foo::bar {$ifs:} 

會拋出這些錯誤的節點上:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse inline template: Could not find value for 'abc' in 65 at /etc/puppet/services/test/manifests/foo.pp:4 on node jobserver-01.local.cloud.uk 
warning: Not using cache on failed catalog 
err: Could not retrieve catalog; skipping run 

不明白我做錯了什麼。爲什麼是Could not find value for 'abc';不是abc.com?任何想法??乾杯!!


更新:2

我結束了使用heira,決定給一個嘗試的想法原「的哈希陣列」和有一些問題實施:

這是我在heira:

hiera -d -c /etc/puppet/hiera.yaml nag_chk m_env=talend s_env=local 
[ ... ] 
DEBUG: Thu Mar 21 12:28:02 +0000 2013: Got answer for key nagi_chk, final answer 
DEBUG: Thu Mar 21 12:28:02 +0000 2013: Answer after outer loop = archimedes-db-02.svc.ft.com:1521 ftftp01-uvln-uk-p:22 www.google.com:80 ftaps01-lvpr-uk-local:8080 
archimedes-db-02:1521 ftftp01-uvln-uk-p:22 google.com:80 

然後,在我的foo.pp

class test::foo { 

    define bar2() { 
     $var1 = $name['addr'] 
     $var2 = $name['port'] 
     notify {"*** ${var1}_${var2} *********": } 
    } 
} 

和我node.pp:

$array_chk = hiera('nag_chk') 
$urls = inline_template("<%= '$array_chk'.split(' ').map{|kv| v1,v2 = kv.split(':'); {'addr' => v1, 'port' => v2}} -%>") 
test::foo::bar2 {$urls:} 

和往常一樣,我得到一個錯誤:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: name is not an hash or array when accessing it with 0 at /etc/puppet/services/talend/talend/manifests/foo.pp:10 on node talend-jobserver-01.local.cloud.ft.com warning: Not using cache on failed catalog err: Could not retrieve catalog; skipping run

什麼是我做錯了什麼?據我所看到的,在正確的格式在irb控制檯「湊陣」:

irb(main):001:0> STRING = "archimedes-db-02:1521 ftftp01-uvln-uk-p:22 google.com:80" 
=> "archimedes-db-02:1521 ftftp01-uvln-uk-p:22 google.com:80" 
irb(main):003:0> 
irb(main):002:0> STRING.split(' ').map{|kv| v1,v2 = kv.split(':'); {'addr' => v1, 'port' => v2}} 
=> [{"addr"=>"archimedes-db-02", "port"=>"1521"}, {"addr"=>"ftftp01-uvln-uk-p", "port"=>"22"}, {"addr"=>"google.com", "port"=>"80"}] 

任何進一步的思考(S)?乾杯!!

回答

2

這個例子可能有助於解決你的特殊情況。

$foo = [{"addr" => "bar", "port" => "1"},          
      {"addr" => "bat", "port" => "2"}]          
    testmod::bar {$foo:}               
    define testmod::bar() {              
    $var1 = $name["addr"]              
    $var2 = $name["port"]              
    notify {"${var1}_${var2}": }             
    }  

將nagios類型放入定義類型中。您可能必須將csv更改爲散列。

更新:在@MacUsers更新後添加。對我來說,以下工作:

$foo = ["abc.com:80","xyz.co.uk:1512"] 
    testmod::bar {$foo:} 
    define testmod::bar() { 
    $var1 = inline_template("<%= '$name'.split(':').first.to_s.chomp %>") 
    $var2 = inline_template("<%= '$name'.split(':').last.to_s.chomp %>") 
    notify {"${var1}_${var2}": } 
    } 

運行木偶代理給了我這樣的:

Notice:  /Stage[main]/Testmodule/Testmodule::Testmod::Bar[abc.com:80]/Notify[abc.com_80]/message: defined 'message' as 'abc.com_80' 
Notice: xyz.co.uk_1512 
Notice: /Stage[main]/Testmodule/Testmodule::Testmod::Bar[xyz.co.uk:1512]/Notify[xyz.co.uk_1512]/message: defined 'message' as 'xyz.co.uk_1512' 
+0

感謝擡起頭。我今晚會試一試並報告。乾杯!! – MacUsers 2013-03-15 16:20:03

+0

只是想知道,如果我使用Ruby DSL而不是Puppet DSL,在這種情況下會更容易嗎?乾杯!! – MacUsers 2013-03-15 17:03:47

+0

我沒有用過很多的Ruby-DSL,所以不能評論太多。你可以使用Ruby-DSL創建一個哈希,然後將其傳遞給Puppet define type ...試試看:) – iamauser 2013-03-15 17:08:14