sensu-plugins-mysql-boutetnico 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 146f44e13b444f45ee61db8098eb3b3062dbef74
4
+ data.tar.gz: 8516156a74fc97731a1ad5c2d04c1663f9d75c64
5
+ SHA512:
6
+ metadata.gz: 15df20ea87d2e4dc6ac85f2a93ef8de7eebf00d4c733684f8c5a10bf0b3950ee7f7ea195dca3485edf4db81170187f2d5b2f2c9e01f89887ac42c0c5ee63b0a7
7
+ data.tar.gz: 1b63f91ac45984154d8a4c6873ff7d4b0d2e5409a9547daf9fbe6a85d0b7b5998fc74dce1af1613b813bcf803a54cc0d38fb18636583b97990db44a81f208c80
@@ -0,0 +1 @@
1
+ Can be found at [https://github.com/boutetnico/sensu-plugins-mysql/releases](https://github.com/boutetnico/sensu-plugins-mysql/releases).
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sensu-Plugins
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,161 @@
1
+ ## Sensu-Plugins-mysql
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/sensu-plugins-mysql-boutetnico.svg)](https://badge.fury.io/rb/sensu-plugins-mysql-boutetnico.svg)
4
+ [![Sensu Bonsai Asset](https://img.shields.io/badge/Bonsai-Download%20Me-brightgreen.svg?colorB=89C967&logo=sensu)](https://bonsai.sensu.io/assets/boutetnico/sensu-plugins-mysql)
5
+
6
+ ## This is an unofficial fork
7
+
8
+ This fork is automatically tested, built and published to [RubyGems](https://rubygems.org/gems/sensu-plugins-mysql-boutetnico/) and [Bonsai](https://bonsai.sensu.io/assets/boutetnico/sensu-plugins-mysql).
9
+
10
+ ## Files
11
+ * bin/check-cloudwatch-mysql-sensu.rb
12
+ * bin/check-mysql-alive.rb
13
+ * bin/check-mysql-status.rb
14
+ * bin/check-mysql-connections.rb
15
+ * bin/check-mysql-disk.rb
16
+ * bin/check-mysql-innodb-lock.rb
17
+ * bin/check-mysql-threads.rb
18
+ * bin/check-mysql-query-result-count.rb
19
+ * bin/check-mysql-select-count.rb
20
+ * bin/check-mysql-msr-replication-status.rb
21
+ * bin/metrics-mysql-graphite.rb
22
+ * bin/metrics-mysql-multiple-select-count.rb
23
+ * bin/metrics-mysql-processes.rb
24
+ * bin/metrics-mysql-raw.rb
25
+ * bin/metrics-mysql.rb
26
+ * bin/metrics-mysql-query-result-count.rb
27
+ * bin/metrics-mysql-select-count.rb
28
+ * bin/mysql-metrics.sql
29
+
30
+ ## Usage
31
+ **metrics-mysql**
32
+ ```
33
+ {
34
+ "mysql":{
35
+ "hostname": "localhost",
36
+ "username": "sensu_user",
37
+ "password": "sensu_user_pass"
38
+ }
39
+ }
40
+ ```
41
+
42
+ **metrics-mysql-graphite** example:
43
+ ```bash
44
+ /opt/sensu/embedded/bin$ /opt/sensu/embedded/bin/ruby metrics-mysql-graphite.rb --host=localhost --port=3306 --user=collectd --pass=tflypass --socket=/data/mysql.sock
45
+ ```
46
+
47
+ **check-mysql-connections** example
48
+ ```bash
49
+ /opt/sensu/embedded/bin$ /opt/sensu/embedded/bin/ruby check-mysql-connections.rb --host=localhost --port=3306 --user=collectd --pass=tflypass --socket=/data/mysql.sock
50
+ ```
51
+
52
+ **check-mysql-connections** example with ini file to hide credentials
53
+ ```bash
54
+ $ /opt/sensu/embedded/bin/check-mysql-connections.rb --host=<DBHOST> --ini=/etc/sensu/my.ini
55
+ ```
56
+
57
+ **check-mysql-alive** example
58
+ ```bash
59
+ $ /opt/sensu/embedded/bin/check-mysql-alive.rb --host=<DBHOST> --ini=/etc/sensu/my.ini --ini-section=customsection
60
+ ```
61
+
62
+ **check-mysql-disk** example (note: you must supply max disk size)
63
+ ```bash
64
+ $ /opt/sensu/embedded/bin/check-mysql-disk.rb --host=<DBHOST> --ini=/etc/sensu/my.ini --size <MAX_DB_SIZE>
65
+ ```
66
+
67
+ **check-mysql-innodb-lock** example
68
+ ```bash
69
+ $ /opt/sensu/embedded/bin/check-mysql-innodb-lock.rb --host=<DBHOST> --ini=/etc/sensu/my.ini
70
+ ```
71
+
72
+ **check-mysql-threads** example
73
+ ```bash
74
+ $ /opt/sensu/embedded/bin/check-mysql-threads.rb --host=<DBHOST> --ini=/etc/sensu/my.ini --warnlow 0
75
+ ```
76
+
77
+ **check-mysql-replication-status** example
78
+ ```bash
79
+ $ /opt/sensu/embedded/bin/check-mysql-replication-status.rb --host=<SLAVE> --ini=/etc/sensu/my.ini
80
+ ```
81
+
82
+ **check-mysql-msr-replication-status** example
83
+ ```bash
84
+ $ /opt/sensu/embedded/bin/check-mysql-replication-status.rb --host=<SLAVE> --ini=/etc/sensu/my.ini
85
+ ```
86
+
87
+ **check-mysql-query-result-count** example
88
+ ```bash
89
+ /opt/sensu/embedded/bin$ /opt/sensu/embedded/bin/ruby check-mysql-query-result-count.rb --host=localhost --port=3306 --user=collectd --pass=tflypass --socket=/data/mysql.sock --warning 1 --critical 10 --query 'SELECT DISTINCT(t.id) FROM table t where t.failed = true'
90
+ ```
91
+
92
+ **check-mysql-select-count** example
93
+ ```bash
94
+ /opt/sensu/embedded/bin$ /opt/sensu/embedded/bin/ruby check-mysql-select-count.rb --host=localhost --port=3306 --user=collectd --pass=tflypass --socket=/data/mysql.sock --warning 30000 --critical 50000 --query 'SELECT count(*) FROM table t'
95
+ ```
96
+
97
+ **metrics-mysql-query-result-count** example
98
+ ```bash
99
+ /opt/sensu/embedded/bin$ /opt/sensu/embedded/bin/ruby metrics-mysql-query-result-count.rb --host=localhost --port=3306 --user=collectd --pass=tflypass --socket=/data/mysql.sock --query 'SELECT DISTINCT(t.id) FROM table t where t.failed = true'
100
+ ```
101
+
102
+ **metrics-mysql-select-count** example
103
+ ```bash
104
+ /opt/sensu/embedded/bin$ /opt/sensu/embedded/bin/ruby metrics-mysql-select-count.rb --host=localhost --port=3306 --user=collectd --pass=tflypass --socket=/data/mysql.sock --query 'SELECT COUNT(*) FROM table t'
105
+ ```
106
+
107
+ **metrics-mysql-multiple-select-count** example
108
+ ```bash
109
+ /opt/sensu/embedded/bin$ /opt/sensu/embedded/bin/metrics-mysql-multiple-select-count.rb --host=localhost --port=3306 --user=collectd --pass=tflypass --socket=/data/mysql.sock --query '{"t1_count":"SELECT COUNT(*) FROM table t1", "t2_count":"SELECT COUNT(*) FROM table t2"}'
110
+ ```
111
+ ### Security
112
+
113
+ In keeping with the principle of least privilege you should create a new user with the minimum required permissions. See the table below for minimum permissions for each check.
114
+
115
+ | Check | Permissions |
116
+ |:---------------------------------------|:----------------------------------------------------------|
117
+ | check-mysql-alive.rb | `SELECT` |
118
+ | check-mysql-connections.rb | `SELECT` |
119
+ | check-mysql-disk.rb | `SELECT` |
120
+ | check-mysql-innodb-lock.rb | `PROCESS` |
121
+ | check-mysql-query-result-count.rb | depends on query |
122
+ | check-mysql-select-count.rb | `SELECT` |
123
+ | check-mysql-replication-status.rb | `SUPER` OR `REPLICATION_CLIENT` (the latter is preferable)|
124
+ | check-mysql-msr-replication-status.rb | `SELECT` |
125
+ | check-mysql-status.rb | `SELECT` |
126
+ | check-mysql-threads.rb | `SELECT` |
127
+ | metrics-mysql-graphite.rb | `SELECT` |
128
+ | metrics-mysql-processes.rb | `SELECT` |
129
+ | metrics-mysql-query-result-count.rb | depends on query |
130
+ | metrics-mysql-select-count.rb   | depends on query                                         |
131
+ | metrics-mysql-multiple-select-count   | depends on query                                         |
132
+ | metrics-mysql-raw.rb | `SELECT` |
133
+ | metrics-mysql.rb | `INSERT` into `sensumetrics.sensu_historic_metrics` |
134
+
135
+ I would recommend using different users when you need to have more than RO access (`REPLICATION_CLIENT` or using the metrics handler) to limit the potential ramifications of that user being compromised.
136
+
137
+ ## Installation
138
+
139
+ [Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
140
+
141
+ ## Notes
142
+ The ruby executables are install in path similar to `/opt/sensu/embedded/lib/ruby/gems/2.0.0/gems/sensu-plugins-mysql-0.0.4/bin`
143
+
144
+ ## Troubleshooting
145
+ When used in `chef`, if the dependencies are missing, an error may abort the chef-client run:
146
+ ```bash
147
+ *** extconf.rb failed ***
148
+ Could not create Makefile due to some reason, probably lack of
149
+ necessary libraries and/or headers. Check the mkmf.log file for more
150
+ details. You may need configuration options.
151
+ ```
152
+ This may be fixed by installing the mysql client library before the plugin:
153
+ ```ruby
154
+ # http://serverfault.com/questions/415392/install-mysql-gem-for-use-in-chef-client
155
+ package "libmysqlclient-dev" do
156
+ action :install
157
+ end
158
+
159
+ sensu_gem 'sensu-plugins-mysql' do version '0.0.4' end
160
+
161
+ ```
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # MySQL Alive Plugin
4
+ # ===
5
+ #
6
+ # This plugin attempts to login to mysql with provided credentials.
7
+ #
8
+ # Copyright 2011 Joe Crim <josephcrim@gmail.com>
9
+ # Updated by Lewis Preson 2012 to accept a database parameter
10
+ # Updated by Oluwaseun Obajobi 2014 to accept ini argument
11
+ #
12
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
13
+ # for details.
14
+ #
15
+ # USING INI ARGUMENT
16
+ # This was implemented to load mysql credentials without parsing the username/password.
17
+ # The ini file should be readable by the sensu user/group.
18
+ # Ref: http://eric.lubow.org/2009/ruby/parsing-ini-files-with-ruby/
19
+ #
20
+ # EXAMPLE
21
+ # mysql-alive.rb -h db01 --ini '/etc/sensu/my.cnf'
22
+ # mysql-alive.rb -h db01 --ini '/etc/sensu/my.cnf' --ini-section customsection
23
+ #
24
+ # MY.CNF INI FORMAT
25
+ # [client]
26
+ # user=sensu
27
+ # password="abcd1234"
28
+ #
29
+ # [customsection]
30
+ # user=user
31
+ # password="password"
32
+ #
33
+
34
+ require 'sensu-plugin/check/cli'
35
+ require 'mysql'
36
+ require 'inifile'
37
+
38
+ class CheckMySQL < Sensu::Plugin::Check::CLI
39
+ option :user,
40
+ description: 'MySQL User',
41
+ short: '-u USER',
42
+ long: '--user USER'
43
+
44
+ option :password,
45
+ description: 'MySQL Password',
46
+ short: '-p PASS',
47
+ long: '--password PASS'
48
+
49
+ option :ini,
50
+ description: 'My.cnf ini file',
51
+ short: '-i VALUE',
52
+ long: '--ini VALUE'
53
+
54
+ option :ini_section,
55
+ description: 'Section in my.cnf ini file',
56
+ long: '--ini-section VALUE',
57
+ default: 'client'
58
+
59
+ option :hostname,
60
+ description: 'Hostname to login to',
61
+ short: '-h HOST',
62
+ long: '--hostname HOST'
63
+
64
+ option :database,
65
+ description: 'Database schema to connect to',
66
+ short: '-d DATABASE',
67
+ long: '--database DATABASE',
68
+ default: 'test'
69
+
70
+ option :port,
71
+ description: 'Port to connect to',
72
+ short: '-P PORT',
73
+ long: '--port PORT',
74
+ default: '3306'
75
+
76
+ option :socket,
77
+ description: 'Socket to use',
78
+ short: '-s SOCKET',
79
+ long: '--socket SOCKET'
80
+
81
+ def run
82
+ if config[:ini]
83
+ ini = IniFile.load(config[:ini])
84
+ section = ini[config[:ini_section]]
85
+ db_user = section['user']
86
+ db_pass = section['password']
87
+ else
88
+ db_user = config[:user]
89
+ db_pass = config[:password]
90
+ end
91
+
92
+ begin
93
+ db = Mysql.real_connect(config[:hostname], db_user, db_pass, config[:database], config[:port].to_i, config[:socket])
94
+ info = db.get_server_info
95
+ ok "Server version: #{info}"
96
+ rescue Mysql::Error => e
97
+ critical "Error message: #{e.error}"
98
+ ensure
99
+ db.close if db
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # MySQL Health Plugin
4
+ # ===
5
+ #
6
+ # This plugin counts the maximum connections your MySQL has reached and warns you according to specified limits
7
+ #
8
+ # Copyright 2012 Panagiotis Papadomitsos <pj@ezgr.net>
9
+ #
10
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
11
+ # for details.
12
+
13
+ require 'sensu-plugin/check/cli'
14
+ require 'mysql'
15
+ require 'inifile'
16
+
17
+ class CheckMySQLHealth < Sensu::Plugin::Check::CLI
18
+ option :user,
19
+ description: 'MySQL User',
20
+ short: '-u USER',
21
+ long: '--user USER',
22
+ default: 'root'
23
+
24
+ option :password,
25
+ description: 'MySQL Password',
26
+ short: '-p PASS',
27
+ long: '--password PASS'
28
+
29
+ option :ini,
30
+ description: 'My.cnf ini file',
31
+ short: '-i',
32
+ long: '--ini VALUE'
33
+
34
+ option :ini_section,
35
+ description: 'Section in my.cnf ini file',
36
+ long: '--ini-section VALUE',
37
+ default: 'client'
38
+
39
+ option :hostname,
40
+ description: 'Hostname to login to',
41
+ short: '-h HOST',
42
+ long: '--hostname HOST',
43
+ default: 'localhost'
44
+
45
+ option :port,
46
+ description: 'Port to connect to',
47
+ short: '-P PORT',
48
+ long: '--port PORT',
49
+ default: '3306'
50
+
51
+ option :socket,
52
+ description: 'Socket to use',
53
+ short: '-s SOCKET',
54
+ long: '--socket SOCKET'
55
+
56
+ option :maxwarn,
57
+ description: "Number of connections upon which we'll issue a warning",
58
+ short: '-w NUMBER',
59
+ long: '--warnnum NUMBER',
60
+ default: 100
61
+
62
+ option :maxcrit,
63
+ description: "Number of connections upon which we'll issue an alert",
64
+ short: '-c NUMBER',
65
+ long: '--critnum NUMBER',
66
+ default: 128
67
+
68
+ option :usepc,
69
+ description: 'Use percentage of defined max connections instead of absolute number',
70
+ short: '-a',
71
+ long: '--percentage',
72
+ default: false
73
+
74
+ def run
75
+ if config[:ini]
76
+ ini = IniFile.load(config[:ini])
77
+ section = ini[config[:ini_section]]
78
+ db_user = section['user']
79
+ db_pass = section['password']
80
+ else
81
+ db_user = config[:user]
82
+ db_pass = config[:password]
83
+ end
84
+ db = Mysql.real_connect(config[:hostname], db_user, db_pass, config[:database], config[:port].to_i, config[:socket])
85
+ max_con = db
86
+ .query("SHOW VARIABLES LIKE 'max_connections'")
87
+ .fetch_hash
88
+ .fetch('Value')
89
+ .to_i
90
+ used_con = db
91
+ .query("SHOW GLOBAL STATUS LIKE 'Threads_connected'")
92
+ .fetch_hash
93
+ .fetch('Value')
94
+ .to_i
95
+ if config[:usepc]
96
+ pc = used_con.fdiv(max_con) * 100
97
+ critical "Max connections reached in MySQL: #{used_con} out of #{max_con}" if pc >= config[:maxcrit].to_i
98
+ warning "Max connections reached in MySQL: #{used_con} out of #{max_con}" if pc >= config[:maxwarn].to_i
99
+ ok "Max connections is under limit in MySQL: #{used_con} out of #{max_con}" # rubocop:disable Style/IdenticalConditionalBranches
100
+ else
101
+ critical "Max connections reached in MySQL: #{used_con} out of #{max_con}" if used_con >= config[:maxcrit].to_i
102
+ warning "Max connections reached in MySQL: #{used_con} out of #{max_con}" if used_con >= config[:maxwarn].to_i
103
+ ok "Max connections is under limit in MySQL: #{used_con} out of #{max_con}" # rubocop:disable Style/IdenticalConditionalBranches
104
+ end
105
+ rescue Mysql::Error => e
106
+ critical "MySQL check failed: #{e.error}"
107
+ ensure
108
+ db.close if db
109
+ end
110
+ end
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # MySQL Disk Usage Check
4
+ # ===
5
+ #
6
+ # Copyright 2011 Sonian, Inc <chefs@sonian.net>
7
+ #
8
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
9
+ # for details.
10
+ #
11
+ # Check the size of the database and compare to crit and warn thresholds
12
+
13
+ require 'sensu-plugin/check/cli'
14
+ require 'mysql'
15
+ require 'inifile'
16
+
17
+ class CheckMysqlDisk < Sensu::Plugin::Check::CLI
18
+ option :host,
19
+ short: '-h',
20
+ long: '--host=VALUE',
21
+ description: 'Database host'
22
+
23
+ option :user,
24
+ short: '-u',
25
+ long: '--username=VALUE',
26
+ description: 'Database username'
27
+
28
+ option :pass,
29
+ short: '-p',
30
+ long: '--password=VALUE',
31
+ description: 'Database password'
32
+
33
+ option :ini,
34
+ description: 'My.cnf ini file',
35
+ short: '-i',
36
+ long: '--ini VALUE'
37
+
38
+ option :ini_section,
39
+ description: 'Section in my.cnf ini file',
40
+ long: '--ini-section VALUE',
41
+ default: 'client'
42
+
43
+ option :size,
44
+ short: '-s',
45
+ long: '--size=VALUE',
46
+ description: 'Database size',
47
+ proc: proc(&:to_f),
48
+ required: true
49
+
50
+ option :warn,
51
+ short: '-w',
52
+ long: '--warning=VALUE',
53
+ description: 'Warning threshold',
54
+ proc: proc(&:to_f),
55
+ default: 85
56
+
57
+ option :crit,
58
+ short: '-c',
59
+ long: '--critical=VALUE',
60
+ description: 'Critical threshold',
61
+ proc: proc(&:to_f),
62
+ default: 95
63
+
64
+ option :port,
65
+ description: 'Port to connect to',
66
+ short: '-P PORT',
67
+ long: '--port PORT',
68
+ proc: proc(&:to_i),
69
+ default: 3306
70
+
71
+ option :socket,
72
+ description: 'Socket to use',
73
+ short: '-S SOCKET',
74
+ long: '--socket SOCKET',
75
+ default: nil
76
+
77
+ def run
78
+ if config[:ini]
79
+ ini = IniFile.load(config[:ini])
80
+ section = ini[config[:ini_section]]
81
+ db_user = section['user']
82
+ db_pass = section['password']
83
+ else
84
+ db_user = config[:user]
85
+ db_pass = config[:pass]
86
+ end
87
+ db_host = config[:host]
88
+ disk_size = config[:size]
89
+ critical_usage = config[:crit]
90
+ warning_usage = config[:warn]
91
+
92
+ if [db_host, db_user, db_pass, disk_size].any?(&:nil?)
93
+ unknown 'Must specify host, user, password and size'
94
+ end
95
+
96
+ begin
97
+ total_size = 0.0
98
+ db = Mysql.real_connect(config[:host], db_user, db_pass, nil, config[:port], config[:socket])
99
+
100
+ results = db.query <<-SQL
101
+ SELECT table_schema,
102
+ count(*) TABLES,
103
+ concat(round(sum(table_rows)/1000000,2),'M') rows,
104
+ round(sum(data_length)/(1024*1024*1024),2) DATA,
105
+ round(sum(index_length)/(1024*1024*1024),2) idx,
106
+ round(sum(data_length+index_length)/(1024*1024*1024),2) total_size,
107
+ round(sum(index_length)/sum(data_length),2) idxfrac
108
+ FROM information_schema.TABLES group by table_schema
109
+ SQL
110
+
111
+ unless results.nil?
112
+ results.each_hash do |row|
113
+ # #YELLOW
114
+ total_size = total_size + row['total_size'].to_f # rubocop:disable Style/SelfAssignment
115
+ end
116
+ end
117
+
118
+ disk_use_percentage = total_size / disk_size * 100
119
+ diskstr = "DB size: #{total_size}, disk use: #{disk_use_percentage}%"
120
+
121
+ if disk_use_percentage > critical_usage
122
+ critical "Database size exceeds critical threshold: #{diskstr}"
123
+ elsif disk_use_percentage > warning_usage
124
+ warning "Database size exceeds warning threshold: #{diskstr}"
125
+ else
126
+ ok diskstr
127
+ end
128
+ rescue Mysql::Error => e
129
+ errstr = "Error code: #{e.errno} Error message: #{e.error}"
130
+ critical "#{errstr} SQLSTATE: #{e.sqlstate}" if e.respond_to?('sqlstate')
131
+ rescue StandardError => e
132
+ critical e
133
+ ensure
134
+ db.close if db
135
+ end
136
+ end
137
+ end