summaryrefslogtreecommitdiffstats
path: root/ext/nagios/naggen
blob: 16dbe6ce44d3f2a96f24f61025c586d3b286e9c0 (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
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env ruby
#
# = Synopsis
#
# Generate Nagios configurations from Puppet Resources in an ActiveRecord database
#
# = Usage
#
#   naggen [-h|--help] [-d|--debug] [-v|--verbose] [--compare]
#
# = Description
#
# This executable is a means of short-circuiting the process of generating nagios
# configurations on your server using Puppet Exported Resources.  It skips any possible
# naming conflicts resulting from Puppet's resource uniqueness requirements, and it
# also skips the inefficiencies involved in converting and transporting large numbers
# of Puppet resources.
#
# At the least, the machine that runs this will need ActiveRecord (2.0.2) installed,
# along with any database libraries you use.
#
# = Options
#
# Note that any configuration parameter that's valid in the configuration file
# is also a valid long argument.  For example, 'ssldir' is a valid configuration
# parameter, so you can specify '--ssldir <directory>' as an argument.
#
# You can add naggen-specific settings to your puppet.conf in a '[naggen]' section,
# just like any other executable.
#
# See the configuration file documentation at
# http://reductivelabs.com/projects/puppet/reference/configref.html for
# the full list of acceptable parameters. A commented list of all
# configuration options can also be generated by running puppet with
# '--genconfig'.
#
# compare::
#   Compare new and old files and only backup and write if the files are different.
#   Potentially expensive computationally, but idempotent.  Will exit with 0 if
#   no changes were made and 1 if there were.
#
# debug::
#   Enable full debugging.
#
# detailed-exitcodes::
#   Provide transaction information via exit codes.  If this is enabled, an exit
#   code of '2' means there were changes, and an exit code of '4' means that there
#   were failures during the transaction.
#
# help::
#   Print this help message
#
# verbose::
#   Print extra information.
#
# = Example
#
#   naggen --storeconfigs --confdir /foo --compare
#
#
# = License
#   Copyright 2011 Luke Kanies
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

require 'puppet'
require 'puppet/rails'
require 'puppet/rails/resource'
require 'puppet/rails/param_value'
require 'puppet/network/client'
require 'puppet/parser/collector'
require 'puppet/provider/naginator'
require 'getoptlong'

# Monkey-patch the rails resources so we can
# easily convert them to nagios instances.
class Puppet::Rails::Resource
    def to_param_hash
        values = @params_hash || Puppet::Rails::ParamValue.find_all_params_from_resource(self)
        if values.size == 0
            return {}
        end
        values.inject({}) do |hash, value|
            hash[value['name']] ||= []
            hash[value['name']] << value["value"]
            hash
        end
    end

    def to_nagios
        unless nagios_type = Nagios::Base.type(restype.sub("Nagios_", '').to_sym)
            raise Puppet::DevError, "Could not find nagios type '%s'" % restype
        end

        result = nagios_type.new

        to_param_hash.each do |param, value|
            next unless nagios_type.parameter?(param)
            result[param] = value
        end

        result[:name] = self.title

        result
    end
end

class NagiosWriter
    class FakeScope
        def debug(string)
            Puppet.debug string
        end

        def host
            "this host doesn't exist"
        end
    end

    attr_accessor :nagios_type, :bucket

    def backup(target)
        return unless FileTest.exist?(target) and File.stat(target).size > 0

        Puppet.info "Backing up %s" % target
        bucket.backup(target)
    end

    def collector
        collector = Puppet::Parser::Collector.new(FakeScope.new, "nagios_" + @nagios_type.to_s, nil, nil, :exported)

        # We don't have a scope, so we're stubbing everything out that would interact
        # with the scope.
        class << collector
            def collect_virtual(*args)
                []
            end

            def exported_resource(res)
                res
            end
        end

        collector
    end

    def default_target
        "/etc/nagios/nagios_#{nagios_type.to_s}.cfg"
    end

    def evaluate
        return unless resources = rails_resources()

        resources_by_target = resources.inject({}) do |hash, resource|
            target = resource["target"] || default_target
            hash[target] ||= []
            hash[target] << resource
            hash
        end

        changed = false
        resources_by_target.each do |target, resources|
            begin
                result = write(target, resources)
            rescue => detail
                $stderr.puts detail.backtrace
                Puppet.err "Could not write to %s: %s" % [target, detail]
            end

            changed = true if result
        end

        changed
    end

    def initialize(nagios_type)
        @nagios_type = nagios_type

        @bucket = Puppet::FileBucket::Dipper.new(:Path => Puppet[:clientbucketdir])
    end

    def rails_resources
        collector.send(:collect_exported)
    end

    def write(target, resources)
        # Skip the nagios type when we have no resources and no existing
        # file.
        return if resources.empty? and ! FileTest.exist?(target)

        dir = File.dirname(target)
        unless FileTest.exist?(dir)
            FileUtils.mkdir_p(dir)
        end

        count = 0

        tempfile = target + ".tmp"
        File.open(tempfile, "w") do |file|
            resources.each do |resource|
                count += 1
                file.puts resource.to_nagios.to_s.gsub("_naginator_name", Puppet::Provider::Naginator::NAME_STRING)
            end
        end

        if $options[:compare]
            if FileTest.exist?(target) and File.read(tempfile) == File.read(target)
                return false
            end
        end

        backup(target)

        # Atomic rename
        File.rename(tempfile, target)

        Puppet.notice "Wrote %s resources to %s" % [count, target]

        return true
    ensure
        File.unlink(tempfile) if tempfile and FileTest.exist?(tempfile)
    end
end

arguments = [
    [ "--compare", "-c", GetoptLong::NO_ARGUMENT ],
    [ "--debug",   "-d", GetoptLong::NO_ARGUMENT ],
    [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ],
    [ "--help",    "-h", GetoptLong::NO_ARGUMENT ]
]

Puppet.settings.addargs(arguments)

result = GetoptLong.new(*arguments)

$options = {}
result.each { |opt,arg|
    case opt
        when "--help"
            begin
                require 'rdoc/usage'
                RDoc::usage && exit
            rescue LoadError
                docs = []
                File.readlines(__FILE__).each do |line|
                    next if line =~ /^#\!/
                    unless line =~ /^#/
                        next if docs.length == 0 # skip the first line or so
                        break # else, we've passed the docs, so just break
                    end
                    docs << line.sub(/^# ?/, '')
                end

                print docs
                exit
            end
        when "--compare"
            $options[:compare] = true
        when "--verbose"
            $options[:verbose] = true
        when "--debug"
            $options[:debug] = true
        when "--debug"
            $options[:debug] = true
    else
            Puppet.settings.handlearg(opt, arg)
    end
}

# Read in Puppet settings, so we know how Puppet's configured.
Puppet.parse_config

Puppet::Util::Log.newdestination(:console)

if $options[:debug]
    Puppet::Util::Log.level = :debug
elsif $options[:verbose]
    Puppet::Util::Log.level = :info
end

# See if Naginator is installed directly, else load Puppet's version.
begin
    require 'nagios'
rescue LoadError
    require 'puppet/external/nagios'
end

changed = false
Nagios::Base.eachtype do |name, type|
    writer = NagiosWriter.new(name)

    changed = true if writer.evaluate
end

if $options[:compare] and changed
    exit(1)
else
    exit(0)
end