papertrail-cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ papertrail-cli (0.1.0)
5
+ faraday
6
+ json
7
+ typhoeus
8
+ yajl-ruby
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ addressable (2.2.4)
14
+ faraday (0.5.5)
15
+ addressable (~> 2.2.4)
16
+ multipart-post (~> 1.1.0)
17
+ rack (>= 1.1.0, < 2)
18
+ json (1.5.1)
19
+ multipart-post (1.1.0)
20
+ rack (1.2.1)
21
+ typhoeus (0.2.0)
22
+ yajl-ruby (0.7.8)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ faraday
29
+ json
30
+ papertrail-cli!
31
+ typhoeus
32
+ yajl-ruby
@@ -0,0 +1,66 @@
1
+ # papertrail Command-line tail & search client for Papertrail log management service
2
+
3
+ Ask [Papertrail] for recent log messages, optionally with a search query (the output
4
+ can also be piped through grep). Optionally poll for new events, like tail -f.
5
+
6
+ Papertrail::SearchClient class can also be used to perform one-off API searches
7
+ or follow (tail) events matching a given query (interface may change).
8
+
9
+
10
+ ## Installation
11
+
12
+ Install the gem, which includes a binary called "papertrail":
13
+
14
+ gem install papertrail-cli
15
+
16
+
17
+ ## Configuration
18
+
19
+ Create ~/.papertrail.yml containing your credentials, or specify the path to
20
+ that file with -c. Example (from examples/papertrail.yml.example):
21
+ username: your@account.com
22
+ password: yourpassword
23
+
24
+ You may want to alias "trail" to "papertrail", like:
25
+ echo "alias trail=papertrail" >> ~/.bashrc
26
+
27
+
28
+ ## Usage & Examples
29
+
30
+ $ papertrail -h
31
+ papertrail - command-line tail and search for Papertrail log management service
32
+ -h, --help Show usage
33
+ -f, --follow Continue running and print new events (off)
34
+ -d, --delay SECONDS Delay between refresh (30)
35
+ -c, --configfile PATH Path to config (~/.papertrail.yml)
36
+
37
+ Usage: papertrail [-f] [-d seconds] [-c /path/to/papertrail.yml] [query]
38
+
39
+ Examples:
40
+ papertrail -f
41
+ papertrail something
42
+ papertrail 1.2.3 Failure
43
+ papertrail -f "(www OR db) (nginx OR pgsql) -accepted"
44
+ papertrail -f -d 10 "ns1 OR 'connection refused'"
45
+
46
+ More: http://papertrailapp.com/
47
+
48
+
49
+ ## Contribute
50
+
51
+ Bug report:
52
+
53
+ 1. See whether the issue has already been reported:
54
+ http://github.com/papertrail/papertrail-cli/issues/
55
+ 2. If you don't find one, create an issue with a repro case.
56
+
57
+ Enhancement or fix:
58
+
59
+ 1. Fork the project:
60
+ http://github.com/papertrail/papertrail-cli
61
+ 2. Make your changes with tests.
62
+ 3. Commit the changes without changing the Rakefile or other files unrelated
63
+ to your enhancement.
64
+ 4. Send a pull request.
65
+
66
+ [Papertrail]: http://papertrailapp.com/
@@ -0,0 +1,154 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def module_name
16
+ name.split('-').first
17
+ end
18
+
19
+ def version
20
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
21
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
22
+ end
23
+
24
+ def date
25
+ Date.today.to_s
26
+ end
27
+
28
+ def rubyforge_project
29
+ name
30
+ end
31
+
32
+ def gemspec_file
33
+ "#{name}.gemspec"
34
+ end
35
+
36
+ def gem_file
37
+ "#{name}-#{version}.gem"
38
+ end
39
+
40
+ def replace_header(head, header_name)
41
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
42
+ end
43
+
44
+ #############################################################################
45
+ #
46
+ # Standard tasks
47
+ #
48
+ #############################################################################
49
+
50
+ task :default => :test
51
+
52
+ require 'rake/testtask'
53
+ Rake::TestTask.new(:test) do |test|
54
+ test.libs << 'lib' << 'test'
55
+ test.pattern = 'test/**/test_*.rb'
56
+ test.verbose = true
57
+ end
58
+
59
+ desc "Generate RCov test coverage and open in your browser"
60
+ task :coverage do
61
+ require 'rcov'
62
+ sh "rm -fr coverage"
63
+ sh "rcov test/test_*.rb"
64
+ sh "open coverage/index.html"
65
+ end
66
+
67
+ require 'rake/rdoctask'
68
+ Rake::RDocTask.new do |rdoc|
69
+ rdoc.rdoc_dir = 'rdoc'
70
+ rdoc.title = "#{name} #{version}"
71
+ rdoc.rdoc_files.include('README*')
72
+ rdoc.rdoc_files.include('lib/**/*.rb')
73
+ end
74
+
75
+ desc "Open an irb session preloaded with this library"
76
+ task :console do
77
+ sh "irb -rubygems -r ./lib/#{name}.rb"
78
+ end
79
+
80
+ #############################################################################
81
+ #
82
+ # Custom tasks (add your own tasks here)
83
+ #
84
+ #############################################################################
85
+
86
+
87
+
88
+ #############################################################################
89
+ #
90
+ # Packaging tasks
91
+ #
92
+ #############################################################################
93
+
94
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
95
+ task :release => :build do
96
+ unless `git branch` =~ /^\* master$/
97
+ puts "You must be on the master branch to release!"
98
+ exit!
99
+ end
100
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
101
+ sh "git tag v#{version}"
102
+ sh "git push origin master"
103
+ sh "git push origin v#{version}"
104
+ sh "gem push pkg/#{name}-#{version}.gem"
105
+ end
106
+
107
+ desc "Build #{gem_file} into the pkg directory"
108
+ task :build => :gemspec do
109
+ sh "mkdir -p pkg"
110
+ sh "gem build #{gemspec_file}"
111
+ sh "mv #{gem_file} pkg"
112
+ end
113
+
114
+ desc "Generate #{gemspec_file}"
115
+ task :gemspec => :validate do
116
+ # read spec file and split out manifest section
117
+ spec = File.read(gemspec_file)
118
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
119
+
120
+ # replace name version and date
121
+ replace_header(head, :name)
122
+ replace_header(head, :version)
123
+ replace_header(head, :date)
124
+ #comment this out if your rubyforge_project has a different name
125
+ replace_header(head, :rubyforge_project)
126
+
127
+ # determine file list from git ls-files
128
+ files = `git ls-files`.
129
+ split("\n").
130
+ sort.
131
+ reject { |file| file =~ /^\./ }.
132
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
133
+ map { |file| " #{file}" }.
134
+ join("\n")
135
+
136
+ # piece file back together and write
137
+ manifest = " s.files = %w[\n#{files}\n ]\n"
138
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
139
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
140
+ puts "Updated #{gemspec_file}"
141
+ end
142
+
143
+ desc "Validate #{gemspec_file}"
144
+ task :validate do
145
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{module_name}"]
146
+ unless libfiles.empty?
147
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{module_name}` dir."
148
+ exit!
149
+ end
150
+ unless Dir['VERSION*'].empty?
151
+ puts "A `VERSION` file at root level violates Gem best practices."
152
+ exit!
153
+ end
154
+ end
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'yaml'
5
+ require 'time'
6
+
7
+ require 'faraday'
8
+
9
+ require 'papertrail-cli'
10
+
11
+ class PapertrailSearch
12
+ def run
13
+ options = {
14
+ :configfile => File.expand_path('~/.papertrail.yml'),
15
+ :delay => 30,
16
+ :follow => false
17
+ }
18
+
19
+ OptionParser.new do |opts|
20
+ opts.banner = "papertrail - command-line tail and search for Papertrail log management service"
21
+
22
+ opts.on("-h", "--help", "Show usage") do |v|
23
+ puts opts
24
+ exit
25
+ end
26
+ opts.on("-f", "--follow", "Continue running and print new events (off)") do |v|
27
+ options[:follow] = true
28
+ end
29
+ opts.on("-d", "--delay SECONDS", "Delay between refresh (30)") do |v|
30
+ options[:delay] = v.to_i
31
+ end
32
+ opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v|
33
+ options[:configfile] = File.expand_path(v)
34
+ end
35
+
36
+ opts.separator usage
37
+ end.parse!
38
+
39
+ credentials = open(options[:configfile]) do |f|
40
+ YAML.load(f)
41
+ end
42
+
43
+ client = Papertrail::SearchClient.new(credentials['username'], credentials['password'])
44
+
45
+ search_and_print(client)
46
+
47
+ if options[:follow]
48
+ loop do
49
+ sleep options[:delay]
50
+ search_and_print(client)
51
+ end
52
+ end
53
+ end
54
+
55
+ def search_and_print(client)
56
+ events = client.search(ARGV[0])
57
+ Papertrail::SearchClient.format_events(events) { |e| puts e } if events
58
+ end
59
+
60
+ def usage
61
+ <<EOF
62
+
63
+ Usage: papertrail [-f] [-d seconds] [-c /path/to/papertrail.yml] [query]
64
+
65
+ Examples:
66
+ papertrail -f
67
+ papertrail something
68
+ papertrail 1.2.3 Failure
69
+ papertrail -f "(www OR db) (nginx OR pgsql) -accepted"
70
+ papertrail -f -d 10 "ns1 OR 'connection refused'"
71
+
72
+ More: http://papertrailapp.com/
73
+
74
+ EOF
75
+ end
76
+ end
77
+
78
+ PapertrailSearch.new.run
@@ -0,0 +1,2 @@
1
+ username: your@account.com
2
+ password: yourpassword
@@ -0,0 +1,5 @@
1
+ module PapertrailCli
2
+ VERSION = "0.1.0"
3
+ end
4
+
5
+ require 'papertrail/search_client'
@@ -0,0 +1,46 @@
1
+ module Papertrail
2
+ class SearchClient
3
+ attr_accessor :username, :password, :conn
4
+
5
+ def initialize(username, password)
6
+ @username = username
7
+ @password = password
8
+
9
+ @conn = Faraday::Connection.new(:url => 'https://papertrailapp.com', :ssl => { :verify => true }) do |builder|
10
+ builder.basic_auth(@username, @password)
11
+
12
+ builder.adapter :typhoeus
13
+ builder.response :yajl
14
+ end
15
+
16
+ @max_id_seen = {}
17
+ end
18
+
19
+ # search for all events or a specific query, defaulting to all events since
20
+ # last result set (call with since=0 for all).
21
+ def search(q = nil, since = nil)
22
+ response = @conn.get('/api/v1/events/search.json') do |r|
23
+ r.params = params_for_query(q, since)
24
+ end
25
+
26
+ if response.body
27
+ @max_id_seen[q] = response.body['max_id']
28
+ response.body['events']
29
+ end
30
+ end
31
+
32
+ def params_for_query(q = nil, since = nil)
33
+ params = {}
34
+ params[:q] = q if q
35
+ params[:min_id] = @max_id_seen[q] if @max_id_seen[q]
36
+ params[:min_id] = since if since
37
+ params
38
+ end
39
+
40
+ def self.format_events(events, &block)
41
+ events.each do |event|
42
+ yield "#{Time.parse(event['received_at']).strftime('%b %e %X')} #{event['hostname']} #{event['program']} #{event['message']}"
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,81 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'papertrail-cli'
16
+ s.version = '0.1.0'
17
+ s.date = '2011-02-08'
18
+ s.rubyforge_project = 'papertrail-cli'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "Command-line client for Papertrail hosted log management service."
23
+ s.description = "Command-line client for Papertrail hosted log management service. Tails and searches system & app logs."
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ['Papertrail']
29
+ s.email = 'troy@sevenscale.com'
30
+ s.homepage = 'http://github.com/papertrail/papertrail'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ ## This sections is only necessary if you have C extensions.
37
+ # s.require_paths << 'ext'
38
+ # s.extensions = %w[ext/extconf.rb]
39
+
40
+ ## If your gem includes any executables, list them here.
41
+ s.executables = ["papertrail"]
42
+ s.default_executable = 'papertrail'
43
+
44
+ ## Specify any RDoc options here. You'll want to add your README and
45
+ ## LICENSE files to the extra_rdoc_files list.
46
+ s.rdoc_options = ["--charset=UTF-8"]
47
+ # s.extra_rdoc_files = %w[README LICENSE]
48
+ s.extra_rdoc_files = []
49
+
50
+ ## List your runtime dependencies here. Runtime dependencies are those
51
+ ## that are needed for an end user to actually USE your code.
52
+ s.add_dependency('typhoeus')
53
+ s.add_dependency('yajl-ruby')
54
+ s.add_dependency('faraday')
55
+ s.add_dependency('json')
56
+
57
+ ## List your development dependencies here. Development dependencies are
58
+ ## those that are only needed during development
59
+ # s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
60
+
61
+ ## Leave this section as-is. It will be automatically generated from the
62
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
63
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
64
+ # = MANIFEST =
65
+ s.files = %w[
66
+ Gemfile
67
+ Gemfile.lock
68
+ README.md
69
+ Rakefile
70
+ bin/papertrail
71
+ examples/papertrail.yml.example
72
+ lib/papertrail-cli.rb
73
+ lib/papertrail/search_client.rb
74
+ papertrail-cli.gemspec
75
+ ]
76
+ # = MANIFEST =
77
+
78
+ ## Test files will be grabbed from the file list. Make sure the path glob
79
+ ## matches what you actually use.
80
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
81
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: papertrail-cli
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Papertrail
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-08 00:00:00 -08:00
19
+ default_executable: papertrail
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: typhoeus
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: yajl-ruby
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: faraday
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: json
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ description: Command-line client for Papertrail hosted log management service. Tails and searches system & app logs.
78
+ email: troy@sevenscale.com
79
+ executables:
80
+ - papertrail
81
+ extensions: []
82
+
83
+ extra_rdoc_files: []
84
+
85
+ files:
86
+ - Gemfile
87
+ - Gemfile.lock
88
+ - README.md
89
+ - Rakefile
90
+ - bin/papertrail
91
+ - examples/papertrail.yml.example
92
+ - lib/papertrail-cli.rb
93
+ - lib/papertrail/search_client.rb
94
+ - papertrail-cli.gemspec
95
+ has_rdoc: true
96
+ homepage: http://github.com/papertrail/papertrail
97
+ licenses: []
98
+
99
+ post_install_message:
100
+ rdoc_options:
101
+ - --charset=UTF-8
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project: papertrail-cli
125
+ rubygems_version: 1.3.7
126
+ signing_key:
127
+ specification_version: 2
128
+ summary: Command-line client for Papertrail hosted log management service.
129
+ test_files: []
130
+