summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/log/destinations.rb
blob: 9fe61d484d9a5ba7c09a3b80574ec40af9cd7971 (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
Puppet::Util::Log.newdesttype :syslog do
    def close
        Syslog.close
    end

    def initialize
        if Syslog.opened?
            Syslog.close
        end
        name = Puppet[:name]
        name = "puppet-#{name}" unless name =~ /puppet/

        options = Syslog::LOG_PID | Syslog::LOG_NDELAY

        # XXX This should really be configurable.
        str = Puppet[:syslogfacility]
        begin
            facility = Syslog.const_get("LOG_#{str.upcase}")
        rescue NameError
            raise Puppet::Error, "Invalid syslog facility %s" % str
        end

        @syslog = Syslog.open(name, options, facility)
    end

    def handle(msg)
        # XXX Syslog currently has a bug that makes it so you
        # cannot log a message with a '%' in it.  So, we get rid
        # of them.
        if msg.source == "Puppet"
            @syslog.send(msg.level, msg.to_s.gsub("%", '%%'))
        else
            @syslog.send(msg.level, "(%s) %s" % [msg.source.to_s.gsub("%", ""),
                    msg.to_s.gsub("%", '%%')
                ]
            )
        end
    end
end

Puppet::Util::Log.newdesttype :file do
    match(/^\//)

    def close
        if defined?(@file)
            @file.close
            @file = nil
        end
    end

    def flush
        if defined?(@file)
            @file.flush
        end
    end

    def initialize(path)
        @name = path
        # first make sure the directory exists
        # We can't just use 'Config.use' here, because they've
        # specified a "special" destination.
        unless FileTest.exist?(File.dirname(path))
            Puppet.recmkdir(File.dirname(path))
            Puppet.info "Creating log directory %s" % File.dirname(path)
        end

        # create the log file, if it doesn't already exist
        file = File.open(path, File::WRONLY|File::CREAT|File::APPEND)

        @file = file

        @autoflush = Puppet[:autoflush]
    end

    def handle(msg)
        @file.puts("%s %s (%s): %s" % [msg.time, msg.source, msg.level, msg.to_s])

        @file.flush if @autoflush
    end
end

Puppet::Util::Log.newdesttype :console do


    RED     = {:console => "", :html => "FFA0A0"}
    GREEN   = {:console => "", :html => "00CD00"}
    YELLOW  = {:console => "", :html => "FFFF60"}
    BLUE    = {:console => "", :html => "80A0FF"}
    PURPLE  = {:console => "", :html => "FFA500"}
    CYAN    = {:console => "", :html => "40FFFF"}
    WHITE   = {:console => "", :html => "FFFFFF"}
    HRED    = {:console => "", :html => "FFA0A0"}
    HGREEN  = {:console => "", :html => "00CD00"}
    HYELLOW = {:console => "", :html => "FFFF60"}
    HBLUE   = {:console => "", :html => "80A0FF"}
    HPURPLE = {:console => "", :html => "FFA500"}
    HCYAN   = {:console => "", :html => "40FFFF"}
    HWHITE  = {:console => "", :html => "FFFFFF"}
    RESET   = {:console => "",    :html => ""      }

    @@colormap = {
        :debug => WHITE,
        :info => GREEN,
        :notice => CYAN,
        :warning => YELLOW,
        :err => HPURPLE,
        :alert => RED,
        :emerg => HRED,
        :crit => HRED
    }

    def colorize(level, str)
        case Puppet[:color]
        when true, :ansi, "ansi", "yes"; console_color(level, str)
        when :html, "html"; html_color(level, str)
        else
            str
        end
    end

    def console_color(level, str)
        @@colormap[level][:console] + str + RESET[:console]
    end

    def html_color(level, str)
        %{<span style="color: %s">%s</span>} % [@@colormap[level][:html], str]
    end

    def initialize
        # Flush output immediately.
        $stdout.sync = true
    end

    def handle(msg)
        if msg.source == "Puppet"
            puts colorize(msg.level, "%s: %s" % [msg.level, msg.to_s])
        else
            puts colorize(msg.level, "%s: %s: %s" % [msg.level, msg.source, msg.to_s])
        end
    end
end

Puppet::Util::Log.newdesttype :host do
    def initialize(host)
        Puppet.info "Treating %s as a hostname" % host
        args = {}
        if host =~ /:(\d+)/
            args[:Port] = $1
            args[:Server] = host.sub(/:\d+/, '')
        else
            args[:Server] = host
        end

        @name = host

        @driver = Puppet::Network::Client::LogClient.new(args)
    end

    def handle(msg)
        unless msg.is_a?(String) or msg.remote
            unless defined?(@hostname)
                @hostname = Facter["hostname"].value
            end
            unless defined?(@domain)
                @domain = Facter["domain"].value
                if @domain
                    @hostname += "." + @domain
                end
            end
            if msg.source =~ /^\//
                msg.source = @hostname + ":" + msg.source
            elsif msg.source == "Puppet"
                msg.source = @hostname + " " + msg.source
            else
                msg.source = @hostname + " " + msg.source
            end
            begin
                #puts "would have sent %s" % msg
                #puts "would have sent %s" %
                #    CGI.escape(YAML.dump(msg))
                begin
                    tmp = CGI.escape(YAML.dump(msg))
                rescue => detail
                    puts "Could not dump: %s" % detail.to_s
                    return
                end
                # Add the hostname to the source
                @driver.addlog(tmp)
            rescue => detail
                if Puppet[:trace]
                    puts detail.backtrace
                end
                Puppet.err detail
                Puppet::Util::Log.close(self)
            end
        end
    end
end

# Log to a transaction report.
Puppet::Util::Log.newdesttype :report do
    attr_reader :report

    match "Puppet::Transaction::Report"

    def initialize(report)
        @report = report
    end

    def handle(msg)
        @report << msg
    end
end

# Log to an array, just for testing.
Puppet::Util::Log.newdesttype :array do
    match "Array"

    def initialize(messages)
        @messages = messages
    end

    def handle(msg)
        @messages << msg
    end
end