summaryrefslogtreecommitdiff
path: root/manifests/init.pp
blob: 35b4a3adb0380ab934b7033b06be867fe136417b (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# nagios.pp - everything nagios related
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# adapted and improved by admin(at)immerda.ch
# adapted by Puzzle ITC - haerry+puppet(at)puzzle.ch


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

class nagios {
    case $operatingsystem {
        centos: { include nagios::centos }
        default: { fail("No such operatingsystem: $operatingsystem yet defined") }
    }
}

class nagios::base {

    # needs apache to work
    include apache

    package { nagios:
        ensure => present,   
    }

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

    # manage nagios cfg files
    file {nagios_cfg_dir:
        path => "/etc/nagios/",
        source => "puppet://$server/nagios/empty",
        ensure => directory,
        recurse => true,
        purge => true,
        notify => Service[nagios],
        mode => 0755, owner => root, group => root;
    }
    # this file should contain all the nagios_puppet-paths:
    file {nagios_main_cfg: 
            path => "/etc/nagios/nagios.cfg",
			source => [ "puppet://$server/files/nagios/configs/${fqdn}/nagios.cfg",
                        "puppet://$server/files/nagios/configs/${operatingsystem}/nagios.cfg",
                        "puppet://$server/files/nagios/configs/nagios.cfg",
                        "puppet://$server/nagios/configs/${operatingsystem}/nagios.cfg",
                        "puppet://$server/nagios/configs/nagios.cfg" ],
            notify => Service[nagios],
            mode => 0644, owner => root, group => root;
    }    
    file { nagios_cgi_cfg:
        path => "/etc/nagios/cgi.cfg",
        source => [ "puppet://$server/files/nagios/configs/${fqdn}/cgi.cfg",
                    "puppet://$server/files/nagios/configs/${operatingsystem}/cgi.cfg",
                    "puppet://$server/files/nagios/configs/cgi.cfg",
                    "puppet://$server/nagios/configs/${operatingsystem}/cgi.cfg",
                    "puppet://$server/nagios/configs/cgi.cfg" ],
        owner => 'root',
        group => 0,
        mode => '0644',
        notify => Service['apache'],
    }
    
	file {"/etc/nagios/htpasswd.users":
            source => [
                "puppet://$server/files/nagios/htpasswd.users",
                "puppet://$server/nagios/htpasswd.users" ],
            mode => 0640, owner => root, group => apache;
    }

    file{[ "/etc/nagios/nagios_command.cfg", 
           "/etc/nagios/nagios_contact.cfg", 
           "/etc/nagios/nagios_contactgroup.cfg",
           "/etc/nagios/nagios_host.cfg",
           "/etc/nagios/nagios_hostextinfo.cfg",
           "/etc/nagios/nagios_hostgroup.cfg",
           "/etc/nagios/nagios_hostgroupescalation.cfg",
           "/etc/nagios/nagios_service.cfg",
           "/etc/nagios/nagios_servicedependency.cfg",
           "/etc/nagios/nagios_serviceescalation.cfg",
           "/etc/nagios/nagios_serviceextinfo.cfg",
           "/etc/nagios/nagios_timeperdiod.cfg" ]:
        ensure => file,
        replace => false,
        notify => Service[nagios],
        mode => 0644, owner => root, group => 0;
    }

    # old way of commands to not break the current config
    # TODO: integrate these commands into native nagios types
    file{ "/etc/nagios/legacy/":
        source => "puppet://$server/files/nagios/legacy",
        ensure => directory,
        recurse => true,
        purge => true,
        notify => Service[nagios],
        mode => 0755, owner => root, group => 0;
    }
	
    nagios_command{
        ssh_port:
			command_line => '$USER1/check_ssh -p $ARG1$ $HOSTADDRESS$';
		# from apache2.pp
		http_port:
			command_line => '$USER1/check_http -p $ARG1$ -H $HOSTADDRESS$ -I $HOSTADDRESS$';
		# from bind.pp
		check_dig2: 
            command_line => '$USER1/check_dig -H $HOSTADDRESS$ -l $ARG1$ --record_type=$ARG2$';
        check_ntp:
            command_line => '$USER1/check_ntp -H $HOSTADDRESS$ -w 0.5 -c 1 -j -1:100 -k -1:200';
	}

    Nagios_command <<||>>
    Nagios_contact <<||>>
    Nagios_contactgroup <<||>>
    Nagios_host <<||>>
    Nagios_hostextinfo <<||>>
    Nagios_hostgroup <<||>>
    Nagios_hostgroupescalation <<||>>
    Nagios_service <<||>>
    Nagios_servicedependency <<||>>
    Nagios_serviceescalation <<||>>
    Nagios_serviceextinfo <<||>>
    Nagios_timeperiod <<||>>

    if defined(Class["munin::client"]) {
        include munin::plugins::nagios
    }
} # end nagios::base

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', 'nagios-plugins-ntp', 'nagios-plugins-snmp' ]:
        ensure => 'present',
        before => Service[nagios],
    }

    Service[nagios]{
        hasstatus => true,
    }
    # default cmd file from rpm
    # don't forget it to add to the puppet paths
    file { nagios_commands_cfg:
        path => "/etc/nagios/commands.cfg",
        source => [ "puppet://$server/files/nagios/configs/${fqdn}/commands.cfg",
                    "puppet://$server/files/nagios/configs/${operatingsystem}/commands.cfg",
                    "puppet://$server/nagios/configs/${operatingsystem}/commands.cfg" ],
        owner => 'root',
        group => 0,
        mode => '0644',
        notify => Service['apache'],
    }
    # default file from rpm
    file { nagios_localhost_cfg:
        path => "/etc/nagios/localhost.cfg",
        source => [ "puppet://$server/files/nagios/configs/${fqdn}/localhost.cfg",
                    "puppet://$server/files/nagios/configs/${operatingsystem}/localhost.cfg",
                    "puppet://$server/nagios/configs/${operatingsystem}/localhost.cfg" ],
        owner => 'root',
        group => 0,
        mode => '0644',
        notify => Service['apache'],
    }
    file{"/etc/nagios/private/":
        source => "puppet://$server/nagios/empty",
        ensure => directory,
        purge => true,
        recurse => true,
        mode => '0750', owner => root, group => nagios;
    }
    file{"/etc/nagios/private/resource.cfg":
        source => "puppet://$server/nagios/configs/${operatingsystem}/private/resource.cfg.${architecture}",
        owner => root, group => nagios, mode => '0640';
    }
}

class nagios::target {
    include nagios::target::host
    nagios::service::ping{$fqdn:}
}

class nagios::target::host {
    nagios::host { $fqdn: parents => $nagios_parent }
}

# defines
define nagios::host(
    $ip = $fqdn, 
    $nagios_alias = $hostname, 
    $max_check_attempts = 4,
    $notification_interval = 120,
    $use = 'generic-host', 
    $nagios_contact_groups_in = $nagios_contact_groups,
    $parents = 'localhost' ) 
{
    $real_nagios_contact_groups = $nagios_contact_groups_in ? {
        '' => 'admins',
        default => $nagios_contact_groups_in
    }
    $real_nagios_parents = $parents ? {
        '' => 'localhost',
        default => $parents
    }
    
    @@nagios_host { $name:
        ensure => present,
        address => $ip,
        alias => $nagios_alias,
        max_check_attempts => $max_check_attempts,
        notification_interval => $notification_interval,
        parents => $real_nagios_parents,
        contact_groups => $real_nagios_contact_groups,
        use => $use,
    }
}

# this will define a host which isn't managed by puppet. 
# a ping serivce is automatically added
# please note:
# - you can use it only on the nagios master (no exported resources)
# - you can not use this host for any other services!
define nagios::extra_host($ip, $nagios_alias, $host_use = 'generic-host', $parents = 'localhost' ) {
    nagios::host{$name:
        ip => $ip, 
        nagios_alias => $nagios_alias, 
        use => $use, 
        parents => $parents 
    }
    nagios_service { "check_ping_${name}":
        check_command => "check_ping!100.0,20%!500.0,60%",
        use => "generic-service",
        host_name => $ip,
        notification_period => "24x7",
        service_description => "${alias}_check_ping"
   }
}

define nagios::service(
    $check_command, 
	$host_name = $fqdn, 
    $use = 'generic-service',
    $notification_period = "24x7",
    $max_check_attempts = 4,
    $retry_check_interval = 1,
    $notification_interval = 960,
    $normal_check_interval = 5,
    $check_period = "24x7",
    $nagios_contact_groups_in = $nagios_contact_groups,
    $service_description = ''){


    # this is required to pass nagios' internal checks:
    # every service needs to have a defined host
    include nagios::target::host

    $real_nagios_contact_groups = $nagios_contact_groups_in ? {
        '' => 'admins',
        default => $nagios_contact_groups_in
    }

    @@nagios_service {$name:
        check_command => $check_command,
        use => $use,
        host_name => $host_name,
        notification_period => $notification_period,
        max_check_attempts => $max_check_attempts,
        retry_check_interval => $retry_check_interval,
        notification_interval => $notification_interval,
        normal_check_interval => $normal_check_interval,
        contact_groups => $real_nagios_contact_groups,
        check_period => $check_period,
    }
    # if no service_description is set it is a namevar
    case $service_description {
        '': {}
        default: {
            Nagios_service[$name]{
                service_description => $service_description,
            }
        }
    }
}

define nagios::service::ping(){
    nagios::service{ "check_ping_${hostname}":
        check_command => "check_ping!100.0,20%!500.0,60%",
    }
}

class nagios::service::ntp {
    nagios::service{ "check_ntp_${hostname}":
        check_command => "check_ntp",
        host_name => $fqdn,
    }
}