summaryrefslogtreecommitdiff
path: root/lib/chef/event_loggers/windows_eventlog.rb
blob: eecf0f033bec707c114a7dde2198519cfd5ddeae (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
#
# Author:: Jay Mundrawala (<jdm@chef.io>)
#
# Copyright:: Copyright 2014-2019, Chef Software Inc.
#
# 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_relative "base"
require_relative "../platform/query_helpers"
require_relative "../win32/eventlog"
require_relative "../dist"

class Chef
  module EventLoggers
    class WindowsEventLogger < EventLoggers::Base
      short_name(:win_evt)

      # These must match those that are defined in the manifest file
      RUN_START_EVENT_ID = 10000
      RUN_STARTED_EVENT_ID = 10001
      RUN_COMPLETED_EVENT_ID = 10002
      RUN_FAILED_EVENT_ID = 10003

      EVENT_CATEGORY_ID = 11000
      LOG_CATEGORY_ID = 11001

      # Since we must install the event logger, this is not really configurable
      SOURCE = Chef::Dist::SHORT.freeze

      def self.available?
        ChefUtils.windows?
      end

      def initialize
        @eventlog = ::Win32::EventLog.open("Application")
      end

      def run_start(version, run_status)
        @eventlog.report_event(
          event_type: ::Win32::EventLog::INFO_TYPE,
          source: SOURCE,
          event_id: RUN_START_EVENT_ID,
          data: [version]
        )
      end

      def run_started(run_status)
        @run_status = run_status
        @eventlog.report_event(
          event_type: ::Win32::EventLog::INFO_TYPE,
          source: SOURCE,
          event_id: RUN_STARTED_EVENT_ID,
          data: [run_status.run_id]
        )
      end

      def run_completed(node)
        @eventlog.report_event(
          event_type: ::Win32::EventLog::INFO_TYPE,
          source: SOURCE,
          event_id: RUN_COMPLETED_EVENT_ID,
          data: [@run_status.run_id, @run_status.elapsed_time.to_s]
        )
      end

      # Failed chef-client run %1 in %2 seconds.
      # Exception type: %3
      # Exception message: %4
      # Exception backtrace: %5
      def run_failed(e)
        data =
          if @run_status
            [@run_status.run_id,
             @run_status.elapsed_time.to_s]
          else
            %w{UNKNOWN UNKNOWN}
          end

        @eventlog.report_event(
          event_type: ::Win32::EventLog::ERROR_TYPE,
          source: SOURCE,
          event_id: RUN_FAILED_EVENT_ID,
          data: data + [e.class.name,
                           e.message,
                           e.backtrace.join("\n")]
        )
      end

    end
  end
end