Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewaite-intouch committed Jul 6, 2015
0 parents commit 7a3a1ed
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.gem
Gemfile.lock
.bundle
vendor
coverage/
Empty file added CHANGELOG.md
Empty file.
20 changes: 20 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The following is a list of people who have contributed ideas, code, bug
reports, or in general have helped logstash along its way.

Contributors:
* Aaron Mildenstein (untergeek)
* Adam Tucker (adamjt)
* John Pariseau (ururk)
* Jordan Sissel (jordansissel)
* Mathieu Guillaume (mguillaume)
* Pier-Hugues Pellerin (ph)
* Richard Pijnenburg (electrical)
* Suyog Rao (suyograo)
* Ted Timmons (tedder)
* Ryan O'Keeffe (danielredoak)
* Luke Waite (lukewaite)

Note: If you've sent us patches, bug reports, or otherwise contributed to
Logstash, and you aren't on the list above and want to be, please let us know
and we'll make sure you're here. Contributions from folks like you are what make
open source awesome.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gemspec
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>

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.
5 changes: 5 additions & 0 deletions NOTICE.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Elasticsearch
Copyright 2012-2015 Elasticsearch

This product includes software developed by The Apache Software
Foundation (http://www.apache.org/).
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Logstash Plugin

This is a plugin for [Logstash](https://github.com/elasticsearch/logstash).

It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.

## Documentation

Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elasticsearch.org/guide/en/logstash/current/).

- For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
- For more asciidoc formatting tips, see the excellent reference here https://github.com/elasticsearch/docs#asciidoc-guide

## Need Help?

Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.

## Developing

### 1. Plugin Developement and Testing

#### Code
- To get started, you'll need JRuby with the Bundler gem installed.

- Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).

- Install dependencies
```sh
bundle install
```

#### Test

- Update your dependencies

```sh
bundle install
```

- Run tests

```sh
bundle exec rspec
```

### 2. Running your unpublished Plugin in Logstash

#### 2.1 Run in a local Logstash clone

- Edit Logstash `Gemfile` and add the local plugin path, for example:
```ruby
gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
```
- Install plugin
```sh
bin/plugin install --no-verify
```
- Run Logstash with your plugin
```sh
bin/logstash -e 'filter {awesome {}}'
```
At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.

#### 2.2 Run in an installed Logstash

You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:

- Build your plugin gem
```sh
gem build logstash-filter-awesome.gemspec
```
- Install the plugin from the Logstash home
```sh
bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
```
- Start Logstash and proceed to test the plugin

## Contributing

All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.

Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.

It is more important to the community that you are able to contribute.

For more information about contributing, see the [CONTRIBUTING](https://github.com/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@files=[]

task :default do
system("rake -T")
end

require "logstash/devutils/rake"
198 changes: 198 additions & 0 deletions lib/logstash/inputs/cloudwatch_logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# encoding: utf-8
require "logstash/inputs/base"
require "logstash/namespace"
require "logstash/plugin_mixins/aws_config"
require "logstash/timestamp"
require "time"
require "tmpdir"
require "stud/interval"
require "stud/temporary"

# Stream events from ClougWatch Logs streams.
class LogStash::Inputs::CloudWatch_Logs < LogStash::Inputs::Base
include LogStash::PluginMixins::AwsConfig::V2

config_name "cloudwatch_logs"

default :codec, "plain"

config :log_group, :validate => :string, :required => true

# Where to write the since database (keeps track of the date
# the last handled file was added to S3). The default will write
# sincedb files to some path matching "$HOME/.sincedb*"
# Should be a path with filename not just a directory.
config :sincedb_path, :validate => :string, :default => nil

# Interval to wait between to check the file list again after a run is finished.
# Value is in seconds.
config :interval, :validate => :number, :default => 60

# def register
public
def register
require "digest/md5"
require "aws-sdk"

@logger.info("Registering cloudwatch-logs input", :log_group => @log_group)

Aws::ConfigService::Client.new(aws_options_hash)

@cloudwatch = Aws::CloudWatchLogs::Client.new(aws_options_hash)
end #def register

# def run
public
def run(queue)
Stud.interval(@interval) do
process_group(queue)
end
end # def run

# def list_new_streams
public
def list_new_streams(token = nil, objects = [])
params = {
:log_group_name => @log_group,
:order_by => "LastEventTime",
:descending => false
}

if token != nil
params[:next_token] = token
end

streams = @cloudwatch.describe_log_streams(params)

objects.push(*streams.log_streams)
if streams.next_token == nil
@logger.debug("CloudWatch Logs hit end of tokens for streams")
objects
else
@logger.debug("CloudWatch Logs calling list_new_streams again on token", :token => streams.next_token)
list_new_streams(streams.next_token, objects)
end

end # def list_new_streams

# def process_log
private
def process_log(queue, log, stream)

@codec.decode(log.message.to_str) do |event|
event[LogStash::Event::TIMESTAMP] = parse_time(log.timestamp)
event["[cloudwatch][ingestion_time]"] = parse_time(log.ingestion_time)
event["[cloudwatch][log_group]"] = @log_group
event["[cloudwatch][log_stream]"] = stream.log_stream_name
decorate(event)

queue << event
end
end
# def process_log

# def parse_time
private
def parse_time(data)
LogStash::Timestamp.at(data.to_i / 1000, (data.to_i % 1000) * 1000)
end # def parse_time

# def process_group
public
def process_group(queue)
objects = list_new_streams

last_read = sincedb.read
current_window = DateTime.now.strftime('%Q')

if last_read < 0
last_read = 1
end

objects.each do |stream|
if stream.last_ingestion_time && stream.last_ingestion_time > last_read
process_log_stream(queue, stream, last_read, current_window)
end
end

sincedb.write(current_window)
end # def process_group

# def process_log_stream
private
def process_log_stream(queue, stream, last_read, current_window, token = nil)
@logger.debug("CloudWatch Logs processing stream",
:log_stream => stream.log_stream_name,
:log_group => @log_group,
:lastRead => last_read,
:currentWindow => current_window,
:token => token
)

params = {
:log_group_name => @log_group,
:log_stream_name => stream.log_stream_name,
:start_from_head => true
}

if token != nil
params[:next_token] = token
end

logs = @cloudwatch.get_log_events(params)

logs.events.each do |log|
if log.ingestion_time > last_read
process_log(queue, log, stream)
end
end

# if there are more pages, continue
if logs.events.count != 0 && logs.next_forward_token != nil
process_log_stream(queue, stream, last_read, current_window, logs.next_forward_token)
end
end # def process_log_stream

private
def sincedb
@sincedb ||= if @sincedb_path.nil?
@logger.info("Using default generated file for the sincedb", :filename => sincedb_file)
SinceDB::File.new(sincedb_file)
else
@logger.info("Using the provided sincedb_path",
:sincedb_path => @sincedb_path)
SinceDB::File.new(@sincedb_path)
end
end

private
def sincedb_file
File.join(ENV["HOME"], ".sincedb_" + Digest::MD5.hexdigest("#{@log_group}"))
end

module SinceDB
class File
def initialize(file)
@sincedb_path = file
end

def newer?(date)
date > read
end

def read
if ::File.exists?(@sincedb_path)
since = ::File.read(@sincedb_path).chomp.strip.to_i
else
since = 1
end
return since
end

def write(since = nil)
since = DateTime.now.strftime('%Q') if since.nil?
::File.open(@sincedb_path, 'w') { |file| file.write(since.to_s) }
end
end
end
end # class LogStash::Inputs::CloudWatch_Logs
33 changes: 33 additions & 0 deletions logstash-input-cloudwatch-logs.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Gem::Specification.new do |s|

s.name = 'logstash-input-cloudwatch-logs'
s.version = '0.0.1'
s.licenses = ['Apache License (2.0)']
s.summary = 'Stream events from CloudWatch Logs.'
s.description = 'This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program'
s.authors = ['Luke Waite']
s.email = 'lwaite@gmail.com'
s.homepage = ''
s.require_paths = ['lib']

# Files
s.files = `git ls-files`.split($\)+::Dir.glob('vendor/*')

# Tests
s.test_files = s.files.grep(%r{^(test|spec|features)/})

# Special flag to let us know this is actually a logstash plugin
s.metadata = { 'logstash_plugin' => 'true', 'logstash_group' => 'input' }

# Gem dependencies
s.add_runtime_dependency 'logstash-core', '>= 1.4.0', '< 2.0.0'
s.add_runtime_dependency 'logstash-mixin-aws'
s.add_runtime_dependency 'stud', '~> 0.0.18'
s.add_runtime_dependency 'aws-sdk', '~> 2.0'

s.add_development_dependency 'logstash-devutils'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'coveralls'
s.add_development_dependency 'logstash-codec-json'
end

2 changes: 2 additions & 0 deletions spec/inputs/cloudwatch_logs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"

0 comments on commit 7a3a1ed

Please sign in to comment.