summaryrefslogtreecommitdiff
path: root/lib/chef/event_loggers
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2014-10-16 15:43:58 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2014-10-22 13:03:30 -0700
commit7412b9721d853bc7afb78c5dc1f3d74441596dd1 (patch)
tree8c6bf6248cb6657f4486e556c57ae3cd2f3a9bf3 /lib/chef/event_loggers
parent7993d05ac6251b06093f282becf75be7a8a49bd0 (diff)
downloadchef-7412b9721d853bc7afb78c5dc1f3d74441596dd1.tar.gz
Use windows evt log by default on windows
Diffstat (limited to 'lib/chef/event_loggers')
-rw-r--r--lib/chef/event_loggers/base.rb62
-rw-r--r--lib/chef/event_loggers/windows_eventlog.rb104
2 files changed, 166 insertions, 0 deletions
diff --git a/lib/chef/event_loggers/base.rb b/lib/chef/event_loggers/base.rb
new file mode 100644
index 0000000000..1f676dd516
--- /dev/null
+++ b/lib/chef/event_loggers/base.rb
@@ -0,0 +1,62 @@
+#
+# Author:: Jay Mundrawala (<jdm@getchef.com>)
+#
+# Copyright:: 2014, 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 'chef/event_dispatch/base'
+
+class Chef
+ module EventLoggers
+ class UnknownEventLogger < StandardError; end
+ class UnavailableEventLogger < StandardError; end
+
+ def self.event_loggers_by_name
+ @event_loggers_by_name ||= {}
+ end
+
+ def self.register(name, logger)
+ event_loggers_by_name[name.to_s] = logger
+ end
+
+ def self.by_name(name)
+ event_loggers_by_name[name]
+ end
+
+ def self.available_event_loggers
+ event_loggers_by_name.select do |key, val|
+ val.available?
+ end.keys
+ end
+
+ def self.new(name)
+ event_logger_class = by_name(name.to_s) or
+ raise UnknownEventLogger, "No event logger found for #{name} (available: #{available_event_loggers.join(', ')})"
+ raise UnavailableEventLogger unless available_event_loggers.include? name.to_s
+ event_logger_class.new
+ end
+
+ class Base < EventDispatch::Base
+ def self.short_name(name)
+ Chef::EventLoggers.register(name, self)
+ end
+
+ # Returns true if this implementation of EventLoggers can be used
+ def self.available?
+ false
+ end
+ end
+ end
+end
diff --git a/lib/chef/event_loggers/windows_eventlog.rb b/lib/chef/event_loggers/windows_eventlog.rb
new file mode 100644
index 0000000000..e3bbbfa1e6
--- /dev/null
+++ b/lib/chef/event_loggers/windows_eventlog.rb
@@ -0,0 +1,104 @@
+#
+# Author:: Jay Mundrawala (<jdm@getchef.com>)
+#
+# Copyright:: 2014, 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 'chef/event_loggers/base'
+require 'chef/platform/query_helpers'
+
+if Chef::Platform::windows?
+ [:INFINITE, :WAIT_FAILED, :FORMAT_MESSAGE_IGNORE_INSERTS, :ERROR_INSUFFICIENT_BUFFER].each do |c|
+ # These are redefined in 'win32/eventlog'
+ Windows::Constants.send(:remove_const, c)
+ end
+
+ require 'win32/eventlog'
+ include Win32
+end
+
+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'
+
+ def self.available?
+ return Chef::Platform::windows?
+ end
+
+ def initialize
+ @eventlog = EventLog::open('Application')
+ end
+
+ def run_start(version)
+ @eventlog.report_event(
+ :event_type => 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 => 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 => 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)
+ @eventlog.report_event(
+ :event_type => EventLog::ERROR_TYPE,
+ :source => SOURCE,
+ :event_id => RUN_FAILED_EVENT_ID,
+ :data => [@run_status.run_id,
+ @run_status.elapsed_time.to_s,
+ e.class.name,
+ e.message,
+ e.backtrace.join("\n")]
+ )
+ end
+
+ end
+ end
+end