# Configure nagios target.
#
# === Global variables
#
#   $nagios_target_group:
#       Host and service group name. Defaults to $domain.
#
class nagios::target {

    if $nagios_target_group {
        $group = $nagios_target_group
    } else {
        if $::domain {
            $group = $::domain
        } else {
            $group = "unknown"
        }
    }

    @@nagios::host { $::homename:
        group  => $group,
        osname => $::operatingsystem,
        osicon => $::osfamily ? {
            ""      => "NONE",
            default => inline_template("<%= osfamily.downcase %>")
        },
    }

    Nagios::Service {
        host  => $::homename,
        group => $group,
    }

}


# Configure ssh service target.
#
class nagios::target::ssh inherits nagios::target {

    @@nagios::service { "${::homename}_ssh":
        command     => "check_ssh",
        description => "SSH",
    }

}


# Configure http service target.
#
class nagios::target::http inherits nagios::target {

    @@nagios::service { "${::homename}_http":
        command     => "check_http",
        description => "HTTP",
    }

}


# Configure https service target.
#
class nagios::target::https inherits nagios::target {

    @@nagios::service { "${::homename}_https":
        command     => "check_http!--ssl",
        description => "HTTPS",
    }

}


# Configure smtp service target.
#
class nagios::target::smtp inherits nagios::target {

    @@nagios::service { "${::homename}_smtp":
        command     => "check_smtp",
        description => "SMTP",
    }

}


# Configure nagios nrpe target.
#
class nagios::target::nrpe inherits nagios::target {

    if !$nagios_allow {
        $nagios_allow = "127.0.0.1"
    }

    include nagios::common

    case $operatingsystem {
        "centos","redhat","fedora": {
            $service = "nrpe"
            $nrpedir = "/etc/nrpe.d"
            package { [ "nrpe",
                        "nagios-plugins-disk",
                        "nagios-plugins-load",
                        "nagios-plugins-procs",
                        "nagios-plugins-users", ]:
                ensure => installed,
                before => [ File["/etc/nrpe.d"], Service["nrpe"] ],
            }
        }
        "ubuntu","debian": {
            $service = "nagios-nrpe-server"
            $nrpedir = "/etc/nagios/nrpe.d"
            package { [ "nagios-nrpe-server",
                        "nagios-plugins-basic", ]:
                ensure => installed,
                before => [ File["/etc/nrpe.d"], Service["nrpe"] ],
            }
        }
    }

    file { "/etc/nrpe.d":
        ensure  => directory,
        mode    => "0644",
        owner   => "root",
        group   => "root",
        purge   => true,
        force   => true,
        recurse => true,
        source  => "puppet:///modules/custom/empty",
    }

    service { "nrpe":
        name   => $service,
        ensure => running,
        enable => true,
    }

    file { "${nrpedir}/allowed_hosts.cfg":
        ensure  => present,
        mode    => "0644",
        owner   => "root",
        group   => "root",
        content => "allowed_hosts=${nagios_allow}\n",
        require => File["/etc/nrpe.d"],
        notify  => Service["nrpe"],
    }

    file { "${nrpedir}/check_disk.cfg":
        ensure  => present,
        mode    => "0644",
        owner   => "root",
        group   => "root",
        content => "command[check_disk]=${nagios::common::libdir}/check_disk -w 20% -c 10% -p /\n",
        require => File["/etc/nrpe.d"],
        notify  => Service["nrpe"],
    }
    @@nagios::service { "${::homename}_disk":
        command     => "check_nrpe!check_disk",
        description => "Disk",
    }

    file { "${nrpedir}/check_load.cfg":
        ensure  => present,
        mode    => "0644",
        owner   => "root",
        group   => "root",
        content => "command[check_load]=${nagios::common::libdir}/check_load -r -w 3,2,1 -c 6,4,2\n",
        require => File["/etc/nrpe.d"],
        notify  => Service["nrpe"],
    }
    @@nagios::service { "${::homename}_load":
        command     => "check_nrpe!check_load",
        description => "Load",
    }

    file { "${nrpedir}/check_swap.cfg":
        ensure  => present,
        mode    => "0644",
        owner   => "root",
        group   => "root",
        content => "command[check_swap]=${nagios::common::libdir}/check_swap -w 75% -c 50%\n",
        require => File["/etc/nrpe.d"],
        notify  => Service["nrpe"],
    }
    @@nagios::service { "${::homename}_swap":
        command     => "check_nrpe!check_swap",
        description => "Swap",
    }

#    @@nagios::service { "${::homename}_users":
#        command     => "check_nrpe!check_users",
#        description => "Users",
#    }
#
#    @@nagios::service { "${::homename}_procs":
#        command     => "check_nrpe!check_total_procs",
#        description => "Processes",
#    }

}
