Skip to content

Commit

Permalink
Merge pull request #44 from athal7/add-result-count-check
Browse files Browse the repository at this point in the history
Add check for alerting on query result count
  • Loading branch information
eheydrick authored Mar 24, 2017
2 parents ad9e1d7 + 01c53b2 commit 6676a83
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]
- Added check-mysql-query-result-count.rb script

## [1.1.0] - 2017-01-15
### Added
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* bin/check-mysql-disk.rb
* bin/check-mysql-innodb-lock.rb
* bin/check-mysql-threads.rb
* bin/check-mysql-query-result-count.rb
* bin/metrics-mysql-graphite.rb
* bin/metrics-mysql-processes.rb
* bin/metrics-mysql-raw.rb
Expand Down Expand Up @@ -45,6 +46,11 @@
/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
```

**check-mysql-query-result-count** example
```bash
/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'
```

## Installation

[Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
Expand All @@ -53,7 +59,7 @@
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`

## Troubleshooting
When used in `chef`, if the dependencies are missing, an error may abort the chef-client run:
When used in `chef`, if the dependencies are missing, an error may abort the chef-client run:
```bash
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
Expand Down
102 changes: 102 additions & 0 deletions bin/check-mysql-query-result-count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env ruby
#
# MySQL Query Result Count Check
#
# Checks the length of a result set from a MySQL query.
#
# Copyright 2017 Andrew Thal <[email protected]>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'sensu-plugin/check/cli'
require 'mysql'
require 'inifile'

class MysqlQueryCountCheck < 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',
default: ''

option :ini,
short: '-i',
long: '--ini VALUE',
description: 'My.cnf ini file'

option :socket,
short: '-S SOCKET',
long: '--socket SOCKET',
description: 'MySQL Unix socket to connect to'

option :warn,
short: '-w COUNT',
long: '--warning COUNT',
description: 'COUNT warning threshold for number of items returned by the query',
proc: proc(&:to_i),
required: true

option :crit,
short: '-c COUNT',
long: '--critical COUNT',
description: 'COUNT critical threshold for number of items returned by the query',
proc: proc(&:to_i),
required: true

option :query,
short: '-q QUERY',
long: '--query QUERY',
description: 'Query to execute',
required: true

def run
if config[:ini]
ini = IniFile.load(config[:ini])
section = ini['client']
db_user = section['user']
db_pass = section['password']
else
db_user = config[:user]
db_pass = config[:password]
end
db = Mysql.real_connect(config[:hostname], db_user, db_pass, config[:database], config[:port].to_i, config[:socket])
length = db.query(config[:query]).count

if length >= config[:crit]
critical "Result count is above the CRITICAL limit: #{length} length / #{config[:crit]} limit"
elsif length >= config[:warn]
warning "Result count is above the WARNING limit: #{length} length / #{config[:warn]} limit"
else
ok 'Result count length is below 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 => e
critical e

ensure
db.close if db
end
end

0 comments on commit 6676a83

Please sign in to comment.