Last active
August 29, 2015 13:57
-
-
Save pataiji/9889936 to your computer and use it in GitHub Desktop.
ログをS3に送るコマンド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/rvm/rubies/ruby-2.1.1/bin/ruby | |
class Send2s3 | |
require 'aws-sdk-core' | |
REGION = 'ap-northeast-1' | |
BUCKET = 'bucket-name' | |
LOG_DIR = 'staging/' | |
def initialize | |
@s3 = Aws::S3.new(region: REGION) | |
end | |
def run(name, body, content_type = nil) | |
if @s3.put_object({ bucket: BUCKET, key: "#{LOG_DIR}#{name}", body: body, content_type: content_type }) | |
puts "sended\n" | |
else | |
puts "can't send\n" | |
end | |
end | |
end | |
class LogFile | |
LOG_DIRS = %w(/var/log/httpd/) | |
# LOG_DIRS = %w(/var/log/httpd/ /home/webapp/shared/log/) | |
SYSLOG = %w( | |
/var/log/cron | |
/var/log/maillog | |
/var/log/messages | |
/var/log/secure | |
/var/log/spooler | |
) | |
def initialize | |
@files = [] | |
get_files_all | |
end | |
def files | |
@files | |
end | |
def get_files_all | |
LOG_DIRS.each do |log_dir| | |
get_files(log_dir) | |
end | |
end | |
def get_files(log_dir) | |
Dir::foreach(log_dir) do |obj| | |
next if obj.to_s =~ /\A\.|\.\.\z/ | |
log_dir = log_dir.sub(/\/\z/, '') | |
file_path = "#{log_dir}/#{obj}" | |
if File.directory?(file_path) | |
get_files_from_dir(file_path) | |
else | |
next unless obj.to_s =~ /-\d{8}\.gz\z/ | |
file = File.open(file_path) | |
next if file.size == 0 | |
ts = obj.to_s.scan(/-(\d{8})\.gz\z/).first.first | |
date = "#{ts[0..3]}-#{ts[4..5]}-#{ts[6..7]}" | |
file_dir = "#{log_dir.sub(/\A\//, '').gsub('/', '-')}-#{obj.to_s.sub(/-\d{8}\.gz\z/, '')}" | |
@files << { name: "#{file_dir}/#{date}/#{`hostname`.strip}.gz", body: file, content_type: 'application/x-gzip' } | |
end | |
end | |
end | |
def get_rotated_files(log) | |
Dir::glob("#{log}-*") do |log_file| | |
file = File.open(log_file) | |
next if file.size == 0 | |
file_name, ts = log_file.to_s.scan(/(\w+)-(\d{8})\z/).first | |
date = "#{ts[0..3]}-#{ts[4..5]}-#{ts[6..7]}" | |
file_dir = log.sub(/\A\//, '').gsub('/', '-') | |
@files << { name: "#{file_dir}/#{date}/#{`hostname`.strip}", body: file, content_type: 'text/plain' } | |
end | |
end | |
end | |
send2s3 = Send2s3.new | |
log_file = LogFile.new | |
log_file.files.each do |file| | |
send2s3.run(file[:name], file[:body], file[:content_type]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment