-
Notifications
You must be signed in to change notification settings - Fork 61
Mysql select value check #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| #!/usr/bin/env ruby | ||
| # | ||
| # MySQL Select Count Check | ||
| # | ||
| # Checks the length of a result set from a MySQL query. | ||
| # | ||
| # Copyright 2017 Andrew Thal <athal7@me.com> to check-mysql-query-result-count.rb | ||
| # Modified by Mutsutoshi Yoshimoto <negachov@gmail.com> 2018 to select count(*) version | ||
| # Modified by Jan Kunzmann <jan-github@phobia.de> 2018 to select value version | ||
| # | ||
| # Released under the same terms as Sensu (the MIT license); see LICENSE | ||
| # for details. | ||
|
|
||
| require 'sensu-plugin/check/cli' | ||
| require 'mysql' | ||
| require 'inifile' | ||
|
|
||
| class MysqlSelectCountCheck < Sensu::Plugin::Check::CLI | ||
| option :host, | ||
| short: '-h HOST', | ||
| long: '--host HOST', | ||
| description: 'MySQL Host to connect to', | ||
| required: true | ||
|
|
||
| option :port, | ||
| short: '-P PORT', | ||
| long: '--port PORT', | ||
| description: 'MySQL Port to connect to', | ||
| proc: proc(&:to_i), | ||
| default: 3306 | ||
|
|
||
| option :username, | ||
| short: '-u USERNAME', | ||
| long: '--user USERNAME', | ||
| description: 'MySQL Username' | ||
|
|
||
| option :password, | ||
| short: '-p PASSWORD', | ||
| long: '--pass PASSWORD', | ||
| description: 'MySQL password' | ||
|
|
||
| option :database, | ||
| short: '-d DATABASE', | ||
| long: '--database DATABASE', | ||
| description: 'MySQL database', | ||
| required: true | ||
|
|
||
| option :ini, | ||
| short: '-i', | ||
| long: '--ini VALUE', | ||
| description: 'My.cnf ini file' | ||
|
|
||
| option :ini_section, | ||
| description: 'Section in my.cnf ini file', | ||
| long: '--ini-section VALUE', | ||
| default: 'client' | ||
|
|
||
| option :socket, | ||
| short: '-S SOCKET', | ||
| long: '--socket SOCKET', | ||
| description: 'MySQL Unix socket to connect to' | ||
|
|
||
| option :warn, | ||
| short: '-w VALUE', | ||
| long: '--warning VALUE', | ||
| description: 'Warning when query value exceeds threshold', | ||
| proc: proc(&:to_f), | ||
| required: true | ||
|
|
||
| option :crit, | ||
| short: '-c VALUE', | ||
| long: '--critical VALUE', | ||
| description: 'Critical when query value exceeds threshold', | ||
| proc: proc(&:to_f), | ||
| required: true | ||
|
|
||
| option :query, | ||
| short: '-q SELECT_VALUE_QUERY', | ||
| long: '--query SELECT_VALUE_QUERY', | ||
| description: 'Query to execute', | ||
| required: true | ||
|
|
||
|
|
||
| def run | ||
| if config[:ini] | ||
| ini = IniFile.load(config[:ini]) | ||
| section = ini[config[:ini_section]] | ||
| db_user = section['user'] | ||
| db_pass = section['password'] | ||
| else | ||
| db_user = config[:username] | ||
| db_pass = config[:password] | ||
| end | ||
|
|
||
| db = Mysql.real_connect(config[:host], db_user, db_pass, config[:database], config[:port], config[:socket]) | ||
|
|
||
| rs = db.query(config[:query]) | ||
| fields = rs.fetch_fields | ||
| col_name = fields[0].name.capitalize | ||
|
|
||
| value = rs.fetch_row[0].to_f | ||
|
|
||
| if config[:crit] > config[:warn] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I think this is a clever way to solve the problem I generally find it's not a good idea to do this kind of auto detection as you can't validate that its been configured correctly. Perhaps passing in a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Will change. |
||
| beyond = "above" | ||
| beneath = "below" | ||
| factor = 1.0 | ||
| else | ||
| beyond = "below" | ||
| beneath = "above" | ||
| factor = -1.0 | ||
| end | ||
|
|
||
| if value * factor >= config[:crit] * factor | ||
| critical "#{col_name} #{value} is #{beyond} the CRITICAL limit of #{config[:crit]}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having the tripped threshold in the message seems redundant as it will start with the severity. Also not sure we really need to change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would argue that most people would understand "value A exceeds value B" as "value A is above value B". Looking at a monitoring dashboard or a notification from a check which I might not even have configured myself, the description should tell me unambiguous what's going on. I'll redo the code with hashes, though.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough though we are sacrificing a bit of code readability for the sake of being grammatically correct. |
||
| elsif value * factor >= config[:warn] * factor | ||
| warning "#{col_name} #{value} is #{beyond} the WARNING limit of #{config[:warn]}" | ||
| else | ||
| ok "#{col_name} #{value} is #{beneath} thresholds" | ||
| end | ||
|
|
||
| rescue Mysql::Error => e | ||
| errstr = "Error code: #{e.errno} Error message: #{e.error}" | ||
| critical "#{errstr} SQLSTATE: #{e.sqlstate}" if e.respond_to?('sqlstate') | ||
|
|
||
| rescue StandardError => e | ||
| critical "Unhandled exception: #{e}" | ||
|
|
||
| ensure | ||
| db.close if db | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.