0

我写了一个自定义事实,它返回一个逗号分隔的列表addr:port,如下所示:

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

和往常一样,我收到一个错误:

错误:无法从远程服务器检索目录:服务器上的错误 400:在节点 talend 上的 /etc/puppet/services/talend/talend/manifests/foo.pp:10 上使用 0 访问名称时,名称不是哈希或数组- jobserver-01.local.cloud.ft.com 警告:未在失败的目录上使用缓存错误:无法检索目录;跳过跑步

我做错了什么?据我所见,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"}]

任何进一步的想法?干杯!!

4

1 回答 1

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}": }
  }

运行 puppet agent 给了我这个:

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'
于 2013-03-15T01:46:43.863 回答