parselogs 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTQ1MDc5ZjI5YjBhYjg5ODk0NTUyNmU1NDJkNmU1NTQyMzAzMWY3Nw==
5
+ data.tar.gz: !binary |-
6
+ ZTYwYjA3ZTc4Yzc3NGNjNmVmNTQzZTdjZTg1ZWVhZGEwYWY3NDllZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZWRjMWMyZDYyMjdhYTA1NjM3M2VjYTNkZDk4NWVhODFkMzY1ZmJkNjVjOWYy
10
+ YTJiODEwZGY1ZTcxNWM5MTk5ZWI0Y2FkNTk5YjA1ZmRiN2NiZGI4NDgxOGE1
11
+ N2VjYTEyMjQ3MmJlNzc3M2ZhOTI0YmMxNWM4ZTZhOGU3OTQ3ODU=
12
+ data.tar.gz: !binary |-
13
+ YWM3OTM2ZDc5MGFmYjIzMTcxM2FmNzg0ZGIzOTI5N2UzYmFlMDIwOTNiODI5
14
+ YTVjNzhjYjhhZGIyMTMzY2I5NDUyMGI4MzBiMmE1NzNjYjU5ZjgyODFiZmFi
15
+ YzJjNGI3YzM4YmQ1ZTYxYzU1ZjRmN2ZjMWU5N2QwYjUwY2M1YTY=
@@ -0,0 +1,42 @@
1
+ The Parselogs cli tool
2
+ ====================
3
+ A simple log parser that searches a given string over a period of days and handles compressed files
4
+
5
+
6
+ Installation
7
+ ------------
8
+ gem install parselogs
9
+
10
+
11
+ Help output
12
+ -----------
13
+
14
+ SYNOPSIS
15
+
16
+ parselogs [global options] command [command options] [files...]
17
+
18
+ GLOBAL OPTIONS
19
+
20
+ -d, --days=days - number of days back to search (default: 1)
21
+ --help - Show this message
22
+ -s, --search=string - the string you are searching for (default: none)
23
+ --version
24
+
25
+ COMMANDS
26
+
27
+ all - Search all logs from locations provided
28
+ help - Shows a list of commands or help for one command
29
+
30
+
31
+ Sample Usage
32
+ ------------
33
+
34
+ parselogs --days 30 --search ctsimpson all /var/log/syslog*
35
+
36
+ Mulitple File Locations
37
+ -----------------------
38
+
39
+ Just separate out multiple folder locations as arguments at the end of the command
40
+
41
+ parselogs -d 60 -s ctsimpson all /var/log/*.log /tmp/*.log
42
+
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'gli'
4
+ begin
5
+ require 'parselogs'
6
+ rescue LoadError
7
+ STDERR.puts "Seems to be some problem loading all the libraries"
8
+ exit 64
9
+ end
10
+
11
+ include GLI::App
12
+
13
+ # Usage example :
14
+ # parselogs -s ctsimpso -d 30 /var/log /var/log/archive
15
+
16
+ program_desc 'Extract user data from logs for a given period of time'
17
+
18
+ version Parselogs::VERSION
19
+
20
+ desc 'the string you are searching for'
21
+ arg_name 'string'
22
+ flag [:s, :search]
23
+
24
+ desc 'number of days back to search'
25
+ arg_name 'days'
26
+ flag [:d, :days], :default_value => 1
27
+
28
+ desc 'Search all logs from locations provided'
29
+ arg_name ''
30
+ command :all do |c|
31
+ c.action do |global_options,options,args|
32
+ if args.length < 1
33
+ raise "You need to provide some folder locations"
34
+ end
35
+
36
+ search = ''
37
+ if global_options[:search].nil?
38
+ raise "You must provide search criteria"
39
+ else
40
+ search = global_options[:search]
41
+ end
42
+
43
+ puts "Searching #{global_options[:days]} day(s) back."
44
+ args.each do |folder|
45
+ track = Parselogs::Parse.new(global_options)
46
+ track.search(folder)
47
+ end
48
+
49
+ #args.each { |folder| puts "Completed searching #{folder}"} unless args.empty?
50
+
51
+ # If you have any errors, just raise them
52
+ # raise "that command made no sense"
53
+
54
+ puts "Search ran successful"
55
+ end
56
+ end
57
+
58
+ pre do |global,command,options,args|
59
+ # Pre logic here
60
+ # Return true to proceed; false to abort and not call the
61
+ # chosen command
62
+ # Use skips_pre before a command to skip this block
63
+ # on that command only
64
+ true
65
+ end
66
+
67
+ post do |global,command,options,args|
68
+ # Post logic here
69
+ # Use skips_post before a command to skip this
70
+ # block on that command only
71
+ end
72
+
73
+ on_error do |exception|
74
+ # Error logic here
75
+ # return false to skip default error handling
76
+ true
77
+ end
78
+
79
+ exit run(ARGV)
@@ -0,0 +1,8 @@
1
+ require 'parselogs/version.rb'
2
+ require 'parselogs/track.rb'
3
+ require 'parselogs/file_ops.rb'
4
+
5
+ # Add requires for other fixtures you add to your project here, so
6
+ # you just need to require this one file in your bin file
7
+
8
+
@@ -0,0 +1,64 @@
1
+ require 'date'
2
+
3
+ module Parselogs
4
+ class FileOps
5
+
6
+
7
+ def get_file_with_date(file)
8
+ file_date = File.stat(file).mtime.to_s
9
+ {:name => file, :date => file_date}
10
+ end
11
+
12
+
13
+ def file_in_range?(file_date,days_back)
14
+ compare_date = Date.today - days_back
15
+ file_date = Date.parse(file_date)
16
+ file_date > compare_date
17
+ end
18
+
19
+ def parse_file(file,search)
20
+ if logfile?(file)
21
+ File.open(file) do |open_file|
22
+ open_file.each do |line|
23
+ parse_line(line,search)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def parse_compressed_file(file,search)
30
+ File.open(file) do |f|
31
+ gz = Zlib::GzipReader.new(f)
32
+ gz.each_line do |line|
33
+ parse_line(line,search)
34
+ end
35
+ end
36
+ end
37
+
38
+ def logfile?(file)
39
+ if file.match(/.log$|.log.1$/)
40
+ true
41
+ else
42
+ false
43
+ end
44
+ end
45
+
46
+ def compressed?(file)
47
+ File.extname(file) == '.gz'
48
+ end
49
+
50
+ def parse_line(line,search)
51
+ match_data = []
52
+ line = encoder(line)
53
+ if line.match(search)
54
+ puts line
55
+ end
56
+ end
57
+
58
+ def encoder(input)
59
+ input.force_encoding('UTF-8').encode('UTF-16', :invalid => :replace, :replace => '').encode('UTF-8')
60
+ end
61
+
62
+
63
+ end
64
+ end
@@ -0,0 +1,28 @@
1
+ require 'date'
2
+ require 'zlib'
3
+
4
+ module Parselogs
5
+ class Parse
6
+
7
+ def initialize(options)
8
+ @options = options
9
+ end
10
+
11
+ def search(folder)
12
+ file_ops = Parselogs::FileOps.new()
13
+ files = Dir.glob(folder)
14
+ files.each do |file|
15
+ file = file_ops.get_file_with_date(file)
16
+ if file_ops.file_in_range?(file[:date], @options[:days].to_i)
17
+ puts "Parsing : #{file[:name]}"
18
+ if file_ops.logfile?(file[:name])
19
+ file_ops.parse_file(file[:name],@options[:search])
20
+ elsif file_ops.compressed?(file[:name])
21
+ file_ops.parse_compressed_file(file[:name],@options[:search])
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ module Parselogs
2
+ VERSION = '0.0.8'
3
+ end
@@ -0,0 +1,5 @@
1
+ = parselogs
2
+
3
+ Generate this with
4
+ parselogs rdoc
5
+ After you have described your command line interface
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parselogs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Chris Simpson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aruba
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gli
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.5.4
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.5.4
69
+ description:
70
+ email: ctsimpson@gmail.com
71
+ executables:
72
+ - parselogs
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - README.rdoc
76
+ - parselogs.rdoc
77
+ files:
78
+ - bin/parselogs
79
+ - lib/parselogs/version.rb
80
+ - lib/parselogs/track.rb
81
+ - lib/parselogs/file_ops.rb
82
+ - lib/parselogs.rb
83
+ - README.rdoc
84
+ - parselogs.rdoc
85
+ homepage: http://ctsimpson.com
86
+ licenses: []
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.0.3
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Simple Log parser that handles compressed files
109
+ test_files: []