Puppet检查是否存在nagios资源

时间:2016-02-25 11:48:17

标签: puppet

我有一个puppet 3.8.5环境,其中包含一个以相同方式在3台服务器上安装应用程序的模块。

在模块中,我有以下课程

class app::monitoring {

include nrpe
include nagios::export

@@nagios_contactgroup:{ 'APP':
  ensure                        =>  present,
  alias                         => 'APP Developer',
  members                       => 'user1, user2',
  target                        => '/etc/nagios/conf.d/contacts.cfg',
  }

@@nagios_contact {'user1':
  ensure                        => present,
  alias                         => 'user1',
  email                         => 'user1@somewhere.com',
  service_notification_period   => 'workhours',
  host_notification_period      => 'workhours',
  service_notification_commands => 'notify-service-by-email',
  host_notification_commands    => 'notify-service-by-email',
  target                        => '/etc/nagios/conf.d/contacts.cfg',
  }

@@nagios_service { 'check_app_http_${fqdn}':
  ensure                        => present,
  use                           => "local-service',
  host_name                     => $fqdn,
  service_description           => 'Check App - port 8000',
  check_command                 => 'check_http_app!8000',
  notifications_enabled         => '1',
  target                        => '/etc/nagios/conf.d/service.cfg',
  }

  @@nagios_command {"check_http_app":
    ensure                      => present,
    command_line                => '/usr/lib64/nagios/plugins/check_http -H $HOSTADDRESS$ -p $ARG1$',
    target                      => '/etc/nagios/conf.d/commands.cfg',
    }
  }

正如预期的那样,当puppet在每台服务器上运行时,所有工作都能正常工作,但是当在Nagios服务器上运行puppet时,会因重复的条目错误而失败。 有没有办法更改代码,以便在后续服务器上运行时,如果/何时没有出现重复的资源错误?

目前我手动创建

  • nagios_contactgroup,
  • nagios_contact和
  • nagios_command
/ etc / nagios / objects中的

条目,因此它们有效地硬编码到Nagios中。 我更愿意能够在没有人为干预的情况下完全重建nagios。

2 个答案:

答案 0 :(得分:1)

我们遇到了同样的问题,并提出了一种解决方法-nagios_command和其他函数的包装:

define ournagios::nagios_command (
  $nagios_title, 
  ... # all other built-in parameters
){
  # some custom code for changing parameters
  # Note that any resource creation here must be
  # wrapped with `if ! defined()`

  if ! defined(Nagios_command["$nagios_title"]) {
    nagios_command { $nagios_title:
      command_name => $command_name,
      ensure       => $ensure,
      command_line => $command_line,
      group        => $group,
      mode         => $mode,
      owner        => $owner,
      poller_tag   => $poller_tag,
      provider     => $provider,
      target       => $_target,
      use          => $use,
    }
  }
}

我们这样使用它:

@@ournagios::nagios_command { "check_dns-${::hostname}":
  nagios_title => "check_dns",
  command_line => '$USER1$/check_dns -s \'$HOSTADDRESS$\' -H \'$ARG1$\' -a \'$ARG2$\' -w \'$ARG3$\' -c \'$ARG4$\'',
}

创建了多个ournagios::nagios_check资源,但是所有资源都有不同的名称,而实际的nagios_command仅创建了一次。

答案 1 :(得分:0)

您必须确保使用@@导出的资源具有唯一的名称,以便在Nagios服务器上收集资源时,您不会获得重复的资源。

请参阅the manual

相关问题