如何迭代字符串以构造nagios_service检查作为导出资源?

时间:2013-03-14 23:28:21

标签: ruby erb puppet nagios

我写了一个自定义事实,它返回逗号分隔的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开启   SERVER:当访问0时,name不是哈希或数组   节点上的/etc/puppet/services/talend/talend/manifests/foo.pp:10   talend-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"}]

还有什么进一步的想法?干杯!!

1 个答案:

答案 0 :(得分: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类型放在define类型中。您可能必须将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'