redis_scanner 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: edfb7559c6d635bfd8cd409776cd54334c8be449
4
+ data.tar.gz: 6a488cfff92de0b0f5b2e10c71e2d8586fd738d6
5
+ SHA512:
6
+ metadata.gz: e3b0ab5826f98726db115056a91e8e23dc1df22d171a41906d1057567114f2b4259bd4aa7dabf035123083725b18ac68c4129b5c761bc8f423274e16cfa9839a
7
+ data.tar.gz: 48c2aca4dda664b4ec2da7ddd6831b96ef5e34a93ca44eae01641c45261c91399eb9b0b8a16303d37b691eca3b5abf1626399843118c1740720a92db77bb51aa
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at vincent@boohee.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_scanner.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # RedisScanner
2
+
3
+ RedisScanner is a tiny tool for scanning all redis keys and creating statistic result. The features are the followings.
4
+
5
+ * Scans keys using *scan* replacing *keys* command.
6
+ * Creates statistic result by key's pattern.
7
+
8
+ ## Installation
9
+
10
+ Install redis_scanner in shell.
11
+
12
+ ```shell
13
+ gem install redis_scanner
14
+ ```
15
+
16
+ *Installation prerequirement*
17
+
18
+ You should install [ruby](https://www.ruby-lang.org/) first.
19
+
20
+ ## Usage
21
+
22
+ * Use default options
23
+
24
+ ```shell
25
+ redis_scanner
26
+ ```
27
+
28
+ * Scan keys with some pattern
29
+
30
+ ```shell
31
+ redis_scanner -m u:*
32
+ ```
33
+
34
+ * Output statistic result to a file
35
+
36
+ ```shell
37
+ redis_scanner -f keys_stat.txt
38
+ ```
39
+
40
+ * Redis options. The options are same of redis-cli.
41
+
42
+ ```shell
43
+ redis_scanner -h <host> -p <port> ...
44
+ ```
45
+
46
+ ## Development
47
+
48
+ You are free to modify it for your need.
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xiewenwei/redis_scanner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
53
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/bin/redis_scanner ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'optparse'
6
+ require 'redis_scanner'
7
+
8
+ options = {}
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: redis_scanner [options]"
11
+
12
+ opts.on("-f FILE", "--file FILE", "Output file") do |v|
13
+ options[:file] = v
14
+ end
15
+
16
+ opts.on("-m MATCH", "--match MATCH", "Scan pattern") do |v|
17
+ options[:match] = v
18
+ end
19
+
20
+ # redis client options
21
+ # -h <hostname> Server hostname (default: 127.0.0.1).
22
+ # -p <port> Server port (default: 6379).
23
+ # -s <socket> Server socket (overrides hostname and port).
24
+ # -a <password> Password to use when connecting to the server.
25
+ # -n <db> Database number.
26
+ opts.on("-h HOST", "--host HOST", "Server hostname (default: 127.0.0.1)") do |v|
27
+ options[:host] = v
28
+ end
29
+
30
+ opts.on("-p PORT", "--port PORT", "Server port (default: 6379)") do |v|
31
+ options[:port] = v
32
+ end
33
+
34
+ opts.on("-s SOCKET", "--socket SOCKET", "Server socket (overrides hostname and port)") do |v|
35
+ options[:socket] = v
36
+ end
37
+
38
+ opts.on("-a PASSWORD", "--password PASSWORD", "Password to use when connecting to the server.") do |v|
39
+ options[:password] = v
40
+ end
41
+
42
+ opts.on("-n DB", "--db DB", "Database number") do |v|
43
+ options[:db] = v
44
+ end
45
+
46
+ end.parse!
47
+
48
+ # puts "options is #{options.inspect}"
49
+
50
+ RedisScanner.scan options
@@ -0,0 +1,58 @@
1
+ module RedisScanner
2
+ class Engine
3
+ def initialize(redis, options)
4
+ @redis = redis
5
+ @options = options
6
+ end
7
+
8
+ def run
9
+ convert_to_sorted_array scan
10
+ end
11
+
12
+ private
13
+
14
+ def convert_to_sorted_array(stat)
15
+ stat.to_a.sort do |x, y|
16
+ if x[1] == y[1]
17
+ x[0] <=> y[0]
18
+ else
19
+ y[1] <=> x[1]
20
+ end
21
+ end
22
+ end
23
+
24
+ def scan
25
+ cursor = 0
26
+ stat = Hash.new(0)
27
+ while true
28
+ if @options[:match]
29
+ cursor, result = @redis.scan cursor, match: @options[:match]
30
+ else
31
+ cursor, result = @redis.scan cursor
32
+ end
33
+ result.each do |key|
34
+ pattern = resolve_pattern(key)
35
+ stat[pattern] += 1
36
+ end
37
+ cursor = cursor.to_i
38
+ break if cursor == 0
39
+ end
40
+ stat
41
+ end
42
+
43
+ PATTERNS = [
44
+ [/.*(:\d+:).*/, ":<id>:"],
45
+ [/.*(:\w+-\w+-\w+-\w+-\w+:).*/, ":<user_key>:"]
46
+ ]
47
+
48
+ def resolve_pattern(key)
49
+ PATTERNS.each do |pattern, replacer|
50
+ if m = pattern.match(key)
51
+ key = key.sub(m[1], replacer)
52
+ break
53
+ end
54
+ end
55
+ key
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module RedisScanner
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ require "redis_scanner/version"
2
+ require "redis_scanner/engine"
3
+ require "redis"
4
+
5
+ module RedisScanner
6
+ def self.scan(options)
7
+ redis = Redis.new extract_redis_options(options)
8
+ engine = Engine.new redis, options
9
+ result = engine.run
10
+ output_result(result, options)
11
+ end
12
+
13
+ def self.output_result(result, options)
14
+ if options[:file]
15
+ File.open(options[:file], "w") do |file|
16
+ result.each { |key, count| file.puts "#{key} #{count}" }
17
+ end
18
+ else
19
+ puts "=========result is========="
20
+ result.each { |key, count| puts "#{key} #{count}" }
21
+ puts "==========================="
22
+ end
23
+ end
24
+
25
+ def self.extract_redis_options(options)
26
+ result = {}
27
+ [:host, :port, :socket, :password, :db].each do |key|
28
+ result[key] = options[key]
29
+ end
30
+ result
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_scanner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_scanner"
8
+ spec.version = RedisScanner::VERSION
9
+ spec.authors = ["Vincent Xie"]
10
+ spec.email = ["xiewenwei@gmail.com"]
11
+
12
+ spec.summary = %q{RedisScanner is a tiny tool for scanning redis's keys and creating statistic information.}
13
+ spec.description = %q{RedisScanner is a tiny tool for scanning redis's keys and creating statistic information. It scans keys using redis scan command and computing statistic result by key's pattern.}
14
+ spec.homepage = "http://github.com/xiewenwei/redis_scanner.git"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "redis"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_scanner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vincent Xie
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: RedisScanner is a tiny tool for scanning redis's keys and creating statistic
70
+ information. It scans keys using redis scan command and computing statistic result
71
+ by key's pattern.
72
+ email:
73
+ - xiewenwei@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - bin/redis_scanner
85
+ - lib/redis_scanner.rb
86
+ - lib/redis_scanner/engine.rb
87
+ - lib/redis_scanner/version.rb
88
+ - redis_scanner.gemspec
89
+ homepage: http://github.com/xiewenwei/redis_scanner.git
90
+ licenses: []
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: RedisScanner is a tiny tool for scanning redis's keys and creating statistic
112
+ information.
113
+ test_files: []