DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Puppet Nagios Module

06.23.2010
| 6307 views |
  • submit to reddit
        // automatically build a nagios server and nrpe clients with puppet


class nagios {

    package {
      "nagios":  ensure => present;
      "httpd":   ensure => present;
    }


    #### Setup nagios configuration files ###########
    
    #####  Populate base configuration files ###
    # create the /etc/nagios directory, copy files over from puppet server
    file { "nagioscfg":
        path => "/etc/nagios",
        checksum => "mtime",
        ensure  => directory, owner => nagios, group => nagios, mode => 644, 
        source  => "puppet://$server/nagios/etc-nagios",
        recurse => true,
        ignore  => [ ".svn", "nrpe.cfg" ],
        notify  => Service[nagios],
        require => Package[nagios],    
    }

 
    # Configure system iptables    
    file {"iptablescfg":
        path => "/etc/sysconfig/iptables",
        ensure => file, owner => root, group => root, mode => 600,
        source => [ "puppet://$server/nagios/files/iptables.txt",
                    "puppet://$server/nagios/iptables.txt" ],
        notify => Service[iptables],
    }
    

    # Setup Nagios httpd config file
    file {"nagios-http-cfg":
        path => "/etc/httpd/conf.d/nagios.conf",
        ensure => file, owner => root, group => root, mode => 644,
        source => [ "puppet://$server/nagios/files/httpd-nagios.conf",
                    "puppet://$server/nagios/httpd-nagios.conf" ],
        notify => Service[httpd],
    }

    # Clean out testing virtual machine from store configs
    resources { "nagios_service":
        purge => true
    }
    resources { "nagios_host":
        purge => true
    }
    resources { "nagios_hostgroup":
        purge => true
    }

    #### Define and start services and daemons #####
    
    service {"iptables":
        ensure  => running,
        hasstatus       => true,
        hasrestart      => true,
        subscribe => File["iptablescfg"],
    }    
    
    service {"nagios":
        enable => true,
        ensure  => running,
        hasstatus       => true,
        hasrestart      => true,
        require => Package[nagios],
        subscribe => File["/etc/nagios"],
    }
    
    service {"httpd":
        enable => true,
        ensure  => running,
        hasstatus       => true,
        hasrestart      => true,
        require => Package[nagios],
    }


    ### Puppet is unable to reload a service from autogenerated files
    ### Create cronjob to automatically reload the nagios configuration once per hour
    cron { "nagreload":
        command => "/etc/init.d/nagios reload",
        user    => "root",
        minute  => "54",
    }



        
    #### collect resources and populate /etc/nagios/nagios_*.cfg####
    #### Uses puppet built in nagios hooks to find information about client machines ###
    #### and automatically create nagios configurations for them ####
    
    Nagios_host <<||>>
    Nagios_service <<||>>
    Nagios_hostextinfo <<||>>

    class target {
        $nrpeservice = "nrpe"
        $nrpepattern = "nrpe"
        $nrpepackage = "nrpe"
        $nrpedir     = "/etc/nagios"
        $nagiosuser  = "nrpe"
        $nagiosgroup = "nrpe"
        $pluginsdir  = "/usr/lib/nagios/plugins"
        $sudopath    = "/usr/bin"
        
        # Install the correct packages based on OS   
        case $operatingsystem {
            default: {
                package {
                "nrpe":                 ensure => present;
                "nagios-plugins-all":   ensure => present;
                "nagios-plugins-nrpe":  ensure => present;
                }
            }
            ubuntu: {
                package {
                    "nagios-nrpe-server":   ensure => present;
                    "nagios-nrpe-plugin":   ensure => present;
                }
            }
        }

        # x86_64 Centos installes into /usr/lib64 by default, create a symlink
        # so that nagios can find it's plugins.
        case $architecture {
            x86_64: {               
                case $operatingsystem {
                    centos: {
                        file { "/usr/lib/nagios":
                            ensure => link,
                            target => "/usr/lib64/nagios",
                        }                    
                    }                
                }
            }    
        }
            
        service {"nrpe":
            name => $operatingsystem ? {
                ubuntu => "nagios-nrpe-server",
                default => "nrpe",
            },
            enable => true,
            hasrestart      => true,
            # require => Package[nrpe],
            subscribe => File["/etc/nagios/nrpe.cfg"],
            ensure  => running,
        }
            


        file { "/etc/nagios/nrpe.cfg":
            mode    => "644",
            owner => $operatingsystem ? {
                ubuntu => "root",
                default => "nrpe",            
            },            
            group => $operatingsystem ? {
                ubuntu => "root",
                default => "nrpe",            
            },            
            content => template("nagios/nrpe.cfg"),
            # require => Package[nrpe],
            notify => Service[nrpe],
        }



        ## Build the /etc/nagios/nagios_host.cfg file on the server    
        @@nagios_host { $fqdn:
            ensure => present,
            alias => $hostname,
            address => $ipaddress,
            use => "linux-server",
            check_command => "check-host-alive",
            max_check_attempts => 3,
            hostgroups => "linux-servers",
            contact_groups => "admins",
        }

        ## Build nagios_hostextinfo.cfg on server    
        @@nagios_hostextinfo { $fqdn:
           ensure => present,
           icon_image_alt => $operatingsystem,
           icon_image => "base/$operatingsystem.png",
           statusmap_image => "base/$operatingsystem.gd2",
        }

        # Build nagios_service.cfg on server    
        @@nagios_service { "check_ping_${hostname}":
            host_name => "$fqdn",
            use => "generic-service",
            service_description => "Ping",
            check_command => "check_ping!100.0,20%!500.0,60%",
        }
    
        @@nagios_service { "check_users_${hostname}":
            use => "generic-service",
            check_command => "check_nrpe!check_users",
            service_description => "Users",
            host_name => "$fqdn",
        }
    
        @@nagios_service { "check_load_${hostname}":
            use => "generic-service",    
            check_command => "check_nrpe!check_load",
            service_description => "Check Load",
            host_name => "$fqdn",
        }
    
        @@nagios_service { "check_zombie_procs_${hostname}":
            use => "generic-service",
            check_command => "check_nrpe!check_zombie_procs",
            service_description => "Zombie Processes",
            host_name => "$fqdn",
        }
    
        @@nagios_service { "check_total_procs_${hostname}":
            use => "generic-service",
            check_command => "check_nrpe!check_total_procs",
            service_description => "Total Processes",
            host_name => "$fqdn",
        }
    
        @@nagios_service { "check_swap_${hostname}":
            use => "generic-service",
            check_command => "check_nrpe!check_swap",
            service_description => "Swap File",
            host_name => "$fqdn",
        }
    
        @@nagios_service { "check_all_disks_${hostname}":
            use => "generic-service",
            check_command => "check_nrpe!check_disk !20%!10%!",
            service_description => "Disk Status",
            host_name => "$fqdn",
        }
    }
}