mysql-sapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "bundler", ">= 1.0.0"
5
+ gem "jeweler", ">= 1.8.4"
6
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Scott Tadman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = mysql-sapper
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to mysql-sapper
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Scott Tadman. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+
14
+ require 'rake'
15
+ require 'jeweler'
16
+
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+ gem.name = "mysql-sapper"
20
+ gem.homepage = "http://github.com/twg/mysql-sapper"
21
+ gem.license = "MIT"
22
+ gem.summary = %Q{MySQL Connection Sapper}
23
+ gem.description = %Q{Randomly kills MySQL processes for fun and profit.}
24
+ gem.email = "github@tadman.ca"
25
+ gem.authors = [ "Scott Tadman" ]
26
+ # dependencies defined in Gemfile
27
+ end
28
+
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+
33
+ Rake::TestTask.new(:test) do |test|
34
+ test.libs << 'lib' << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'mysql2'
5
+
6
+ DEFAULT_OPTIONS = {
7
+ :username => 'root',
8
+ :password => ''
9
+ }
10
+
11
+ mysql_options = {
12
+ }
13
+
14
+ config_path = "config"
15
+ config_file = "database.yml"
16
+ interval = 10
17
+ verbose = false
18
+ idle = nil
19
+
20
+ opts = OptionParser.new do |parser|
21
+ parser.on('-f', '--config=s', "Use configuration file (default #{config_path.inspect})") do |s|
22
+ config_path = s
23
+ end
24
+ parser.on('-u', '--username=s', "Authenticate with MySQL username (default #{mysql_options[:username].inspect})") do |s|
25
+ mysql_options[:username] = s
26
+ end
27
+ parser.on('-p', '--password=s', "Authenticate with MySQL password (default #{mysql_options[:password].inspect})") do |s|
28
+ mysql_options[:password] = s
29
+ end
30
+ parser.on('-o', '--port=s', "Connect on MySQL port") do |s|
31
+ mysql_options[:port] = s.to_i
32
+ end
33
+ parser.on('-s', '--socket=s', "Connect on MySQL socket") do |s|
34
+ mysql_options[:socket] = s
35
+ end
36
+ parser.on('-i', '--interval=s', "Kill connections every N seconds (default #{interval})") do |s|
37
+ interval = s.to_i
38
+ end
39
+ parser.on('-v', '--verbose', "Verbose messages on activity") do
40
+ verbose = true
41
+ end
42
+ parser.on('-d', '--idle', "Kill only idle connections") do
43
+ idle = true
44
+ end
45
+ parser.on('-h', '--help', "Show this help") do
46
+ puts parser
47
+ exit(0)
48
+ end
49
+ end
50
+
51
+ opts.parse(*ARGV)
52
+
53
+ if (config_path and config_file)
54
+ database_env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
55
+
56
+ last_path = nil
57
+ path = File.expand_path(File.join(config_path, config_file), Dir.pwd)
58
+
59
+ loop do
60
+ if (File.exist?(path))
61
+ config = YAML.read(File.open(path))
62
+
63
+ if (config)
64
+ if (env_config = config[database_env])
65
+ mysql_options = env_config.merge(mysql_options)
66
+ end
67
+ end
68
+
69
+ break
70
+ end
71
+
72
+ last_path = path
73
+
74
+ path = File.expand_path(File.join('..', config_path, config_file), File.dirname(path))
75
+
76
+ break if (last_path == path)
77
+ end
78
+ end
79
+
80
+ client = Mysql2::Client.new(DEFAULT_OPTIONS.merge(mysql_options))
81
+
82
+ begin
83
+ while (true)
84
+ connections = [ ]
85
+
86
+ client.query("SHOW PROCESSLIST").each do |row|
87
+ next if (row['Info'] == "SHOW PROCESSLIST")
88
+
89
+ next if (idle and row['Info'] != "")
90
+
91
+ connections << row['Id']
92
+ end
93
+
94
+ if (id = connections.sample)
95
+ if (verbose)
96
+ puts "Killing connection #{id}"
97
+ end
98
+
99
+ client.query("KILL %d" % id)
100
+ end
101
+
102
+ sleep(interval)
103
+ end
104
+ rescue Interrupt
105
+ exit(0)
106
+ end
107
+
@@ -0,0 +1,2 @@
1
+ module MysqlSapper
2
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'mysql-sapper'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestMysqlSapper < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql-sapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott Tadman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.4
46
+ description: Randomly kills MySQL processes for fun and profit.
47
+ email: github@tadman.ca
48
+ executables:
49
+ - mysql-sapper
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - LICENSE.txt
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.rdoc
59
+ - Rakefile
60
+ - bin/mysql-sapper
61
+ - lib/mysql-sapper.rb
62
+ - test/helper.rb
63
+ - test/test_mysql-sapper.rb
64
+ homepage: http://github.com/twg/mysql-sapper
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: MySQL Connection Sapper
89
+ test_files: []