summaryrefslogtreecommitdiff
path: root/manifests/init.pp
blob: 416ac0d9ea14caa4d0c0be97105f8efeaa32f0b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# nagios.pp - everything nagios related
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.


# the directory containing all nagios configs:
$nagios_cfgdir = "/var/lib/puppet/modules/nagios"
modules_dir{ nagios: }

class nagios {
    case $operatingsystem {
        debian: { include nagios::debian }
        centos: { include nagios::centos }
        default: { include nagios::base }
    }
}

class nagios::debian inherits nagios::base {
    Package [nagios]{
            name => "nagios2",
    }
    package {
        "nagios-plugins-standard":
            ensure => installed,
    }
	Service[nagios] {
			# Current Debian/etch pattern
			pattern => "/usr/sbin/nagios2 -d /etc/nagios2/nagios.cfg",
			subscribe => File [ $nagios_cfgdir ]
	}
    File["$etc_nagios_path/htpasswd.users"]{
        group => www-data,
    }

    file {
        [ "/etc/nagios2/conf.d/localhost_nagios2.cfg",
          "/etc/nagios2/conf.d/extinfo_nagios2.cfg",
          "/etc/nagios2/conf.d/services_nagios2.cfg" ]:
            ensure => absent,
            notify => Service[nagios2];
    }
	# permit external commands from the CGI
    file {
       "/var/lib/nagios2":
            ensure => directory, mode => 751,
            owner => nagios, group => nagios,
            notify => Service[nagios2];
    }
    file{
        "/var/lib/nagios2/rw":
            ensure => directory, mode => 2710,
            owner => nagios, group => www-data,
            notify => Service[nagios2];

    }
	
	# TODO: these are not very robust!
	replace {
		# Debian installs a default check for the localhost. Since VServers
		# usually have no localhost IP, this fixes the definition to check the
		# real IP
		fix_default_config:
			file => "/etc/nagios2/conf.d/localhost_nagios2.cfg",
			pattern => "address *127.0.0.1",
			replacement => "address $ipaddress",
			notify => Service[nagios2];
		# enable external commands from the CGI
		enable_extcommands:
			file => "/etc/nagios2/nagios.cfg",
			pattern => "check_external_commands=0",
			replacement => "check_external_commands=1",
			notify => Service[nagios2];
		# put a cap on service checks
		cap_service_checks:
			file => "/etc/nagios2/nagios.cfg",
			pattern => "max_concurrent_checks=0",
			replacement => "max_concurrent_checks=30",
			notify => Service[nagios2];
	}
    
}
# end nagios::debian

class nagios::centos inherits nagios::base {
    package { [ 'nagios-plugins-smtp','nagios-plugins-http', 'nagios-plugins-ssh', 'nagios-plugins-udp', 'nagios-plugins-tcp', 'nagios-plugins-dig', 'nagios-plugins-nrpe', 'nagios-plugins-load', 'nagios-plugins-dns', 'nagios-plugins-ping', 'nagios-plugins-procs', 'nagios-plugins-users', 'nagios-plugins-ldap', 'nagios-plugins-disk', 'nagios-devel', 'nagios-plugins-swap', 'nagios-plugins-nagios', 'nagios-plugins-perl' ]:
        ensure => 'present',
    }
    Service[nagios]{
        hasstatus => true,
    }
    
}

class nagios::vars {
    case $operatingsystem {
        debian: {
            $etc_nagios_path =  "/etc/nagios2"
            }
        default: {
            $etc_nagios_path =  "/etc/nagios"
        }
    }
}


class nagios::base {

    package { nagios:
        ensure => present,   
    }

    service{nagios:
        ensure => running,
        enable => true,
        #hasstatus => true, #fixme!
        require => Package[nagios],
    }

    include nagios::vars
	
	# import the various definitions
	File <<| tag == 'nagios' |>>

    file {
		"$etc_nagios_path/htpasswd.users":
            source => [
                "puppet://$servername/files/nagios/htpasswd.users",
                "puppet://$servername/nagios/htpasswd.users"
            ],
            mode => 0640, owner => root, group => apache;
    }
    
    file {
        "$nagios_cfgdir/hosts.d":
            ensure => directory,
            owner => root,
            group => root,
            mode => 0755,
    }

	define command($command_line) {
		file { "$nagios_cfgdir/hosts.d/${name}_command.cfg":
				ensure => present, content => template( "nagios/command.erb" ),
				mode => 644, owner => root, group => root,
				notify => Service[nagios2],
		}
	}

	nagios2::command {
		# from ssh.pp
		ssh_port:
			command_line => '/usr/lib/nagios/plugins/check_ssh -p $ARG1$ $HOSTADDRESS$';
		# from apache2.pp
		http_port:
			command_line => '/usr/lib/nagios/plugins/check_http -p $ARG1$ -H $HOSTADDRESS$ -I $HOSTADDRESS$';
		# from bind.pp
		nameserver: command_line => '/usr/lib/nagios/plugins/check_dns -H www.edv-bus.at -s $HOSTADDRESS$';
		# TODO: debug this, produces copious false positives:
		# check_dig2: command_line => '/usr/lib/nagios/plugins/check_dig -H $HOSTADDRESS$ -l $ARG1$ --record_type=$ARG2$ --expected_address=$ARG3$ --warning=2.0 --critical=4.0';
		check_dig2: command_line => '/usr/lib/nagios/plugins/check_dig -H $HOSTADDRESS$ -l $ARG1$ --record_type=$ARG2$'
	}
    
	define host($ip = $fqdn, $short_alias = $fqdn) {
		@@file {
			"$nagios_cfgdir/${name}_host.cfg":
				ensure => present, content => template( "nagios/host.erb" ),
				mode => 644, owner => root, group => root,
				tag => 'nagios'
		}
	}

	define service($check_command = '', 
		$nagios2_host_name = $fqdn, $nagios2_description = '')
	{
		# this is required to pass nagios' internal checks:
		# every service needs to have a defined host
		include nagios2::target
		$real_check_command = $check_command ? {
			'' => $name,
			default => $check_command
		}
		$real_nagios2_description = $nagios2_description ? {
			'' => $name,
			default => $nagios2_description
		}
		@@file {
			"$nagios_cfgdir/${nagios2_host_name}_${name}_service.cfg":
				ensure => present, content => template( "nagios/service.erb" ),
				mode => 644, owner => root, group => root,
				tag => 'nagios'
		}
	}

	define extra_host($ip = $fqdn, $short_alias = $fqdn, $parent = "none") {
		$nagios_parent = $parent
		file {
			"$nagios_cfgdir/${name}_host.cfg":
				ensure => present, content => template( "nagios/host.erb" ),
				mode => 644, owner => root, group => root,
				notify => Service[nagios2],
		}
	}
	#
	# include this class in every host that should be monitored by nagios
	class target {
		nagios2::host { $fqdn: }
		debug ( "$fqdn has $nagios_parent as parent" )
	}
} # end nagios::base

#####################################################################################################
## The main nagios monitor class
#class nagios2 {
#
#	file {
#		"/etc/nagios2/conf.d/hostgroups_nagios2.cfg":
#			source => "puppet://$servername/nagios/hostgroups_nagios2.cfg",
#			mode => 0644, owner => root, group => www-data,
#			notify => Service[nagios2];
#	}
#
##	line { include_cfgdir:
##		file => "/etc/nagios2/nagios.cfg",
##		line => "cfg_dir=$nagios_cfgdir",
##		notify => Service[nagios2],
##	}
#
#	munin::plugin {
#		nagios_hosts: script_path => "/usr/local/bin";
#		nagios_svc: script_path => "/usr/local/bin";
#		nagios_perf_hosts: ensure => nagios_perf_, script_path => "/usr/local/bin";
#		nagios_perf_svc: ensure => nagios_perf_, script_path => "/usr/local/bin";
#	}
#
#	file { "/etc/munin/plugin-conf.d/nagios":
#		content => "[nagios_*]\nuser root\n",
#		mode => 0655, owner => root, group => root,
#		notify => Service[munin-node]
#	}
#
#}