Skip to content

Commit

Permalink
Add CloudWatch Logs integration cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Geng committed Aug 18, 2015
1 parent dd4e422 commit 5437013
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
9 changes: 9 additions & 0 deletions opsworks_cloudwatchlogs/attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if node["platform"] == "amazon"
default["cloudwatchlogs"]["config_file"] = "/etc/awslogs/awslogs.conf" # Configures the logs for the agent to ship
default["cloudwatchlogs"]["home_dir"] = "/etc/awslogs" # Contains configuration files
default["cloudwatchlogs"]["state_file"] = "/var/lib/awslogs/agent-state" # See http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/QuickStartChef.html
else
default["cloudwatchlogs"]["config_file"] = "/opt/aws/cloudwatch/cwlogs.cfg" # Configures the logs to ship, used by installation script
default["cloudwatchlogs"]["home_dir"] = "/var/awslogs" # Contains the awslogs package
default["cloudwatchlogs"]["state_file"] = "/var/awslogs/state/agent-state" # See http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/QuickStartChef.html
end
9 changes: 9 additions & 0 deletions opsworks_cloudwatchlogs/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name "opsworks_cloudwatchlogs"
description "CloudWatch Logs Integration"
maintainer "AWS OpsWorks"
license "Apache 2.0"
version "1.0.0"

recipe "opsworks_cloudwatchlogs::default", "Uses install or uninstall recipe"
recipe "opsworks_cloudwatchlogs::install", "Install CloudWatch Logs agent."
recipe "opsworks_cloudwatchlogs::uninstall", "Remove CloudWatch Logs agent and config files."
5 changes: 5 additions & 0 deletions opsworks_cloudwatchlogs/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if node["opsworks"]["cloud_watch_logs_configurations"].any?
include_recipe "opsworks_cloudwatchlogs::install"
else
include_recipe "opsworks_cloudwatchlogs::uninstall"
end
46 changes: 46 additions & 0 deletions opsworks_cloudwatchlogs/recipes/install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
directory node["cloudwatchlogs"]["home_dir"] do
recursive true
end

template node["cloudwatchlogs"]["config_file"] do
source "awslogs.conf.erb"
variables({
:state_file => node["cloudwatchlogs"]["state_file"],
:cloudwatchlogs => node["opsworks"]["cloud_watch_logs_configurations"],
:hostname => node["opsworks"]["instance"]["hostname"]
})
owner "root"
group "root"
mode 0644
end

if platform?("amazon")
package "awslogs"

template "#{node['cloudwatchlogs']['home_dir']}/awscli.conf" do
source "awscli.conf.erb"
variables :region => node["opsworks"]["instance"]["region"]
end
else
directory "/opt/aws/cloudwatch" do
recursive true
end

remote_file "/opt/aws/cloudwatch/awslogs-agent-setup.py" do
source "https://aws-cloudwatch.s3.amazonaws.com/downloads/latest/awslogs-agent-setup.py"
mode 0700
retries 3
end

package "python"

execute "Install CloudWatch Logs agent" do
command "/opt/aws/cloudwatch/awslogs-agent-setup.py -n -r '#{node['opsworks']['instance']['region']}' -c '#{node['cloudwatchlogs']['config_file']}'"
not_if { File.exists?(node["cloudwatchlogs"]["state_file"]) }
end
end

service "awslogs" do
supports :status => true, :restart => true
action [:enable, :start]
end
23 changes: 23 additions & 0 deletions opsworks_cloudwatchlogs/recipes/uninstall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
service "awslogs" do
action :stop
end

directory node["cloudwatchlogs"]["home_dir"] do
action :delete
recursive true
end

if platform?("amazon")
package "awslogs" do
action :remove
end
else
directory "/opt/aws/cloudwatch" do
action :delete
recursive true
end

file "/etc/init.d/awslogs" do
action :delete
end
end
4 changes: 4 additions & 0 deletions opsworks_cloudwatchlogs/templates/default/awscli.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[plugins]
cwlogs = cwlogs
[default]
region = <%= @region %>
14 changes: 14 additions & 0 deletions opsworks_cloudwatchlogs/templates/default/awslogs.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[general]
state_file = <%= @state_file %>

<% @cloudwatchlogs.each do |layer_name, layer_logs| %>
<% layer_logs.each do |log| %>
[<%= layer_name %> <%= log["file"] %>]
datetime_format = [%Y-%m-%d %H:%M:%S]
log_group_name = <%= layer_name %>
log_stream_name = <%= @hostname %> - <%= log["file"] %>
initial_position = start_of_file
file = <%= log["file"] %>

<% end %>
<% end %>

0 comments on commit 5437013

Please sign in to comment.