Created
November 8, 2013 14:58
-
-
Save vitaliel/7372177 to your computer and use it in GitHub Desktop.
Nagios plugin to monitor delayed job active record queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# | |
# Nagios plugin to monitor delayed job active record queue | |
# | |
# will use mysql2 gem | |
# --dsn mysql2://root@localhost/portal_development | |
# | |
# Nagios plugin exit codes | |
# 0 - ok | |
# 1 - warn | |
# 2 - critical | |
# 3 - unknown | |
require 'optparse' | |
require 'optparse/time' | |
require 'ostruct' | |
require 'logger' | |
require 'rubygems' | |
require 'sequel' | |
class CliArguments | |
attr_reader :opts | |
# | |
# Return a structure describing the options. | |
# | |
def parse(args) | |
# The options specified on the command line will be collected in *options*. | |
# We set default values here. | |
options = OpenStruct.new | |
options.warning = 10 | |
options.critical = 20 | |
options.verbose = false | |
@opts = opts = OptionParser.new do |opts| | |
opts.banner = "Usage: #{__FILE__} [options]" | |
opts.separator "" | |
opts.separator "Specific options:" | |
opts.on('-w', '--warning N', 'Number of jobs for warning code') { |n| options.warning = n.to_i } | |
opts.on('-c', '--critical N', 'Number of jobs for critical code') { |n| options.critical = n.to_i } | |
opts.on('-d', '--dsn DSN', 'Sequel datasource name: mysql://user:pass@host:port/database_name') { |dsn| options.dsn = dsn } | |
# No argument, shows at tail. This will print an options summary. | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
end | |
opts.parse!(args) | |
options | |
end # parse() | |
end # class CliArguments | |
# | |
if __FILE__ == $0 | |
options = (parser = CliArguments.new).parse(ARGV) | |
#p options | |
if options.dsn.nil? | |
puts parser.opts | |
exit 1 | |
end | |
begin | |
db = Sequel.connect(options.dsn, encoding: 'utf8') | |
row = db['select count(*) as count from delayed_jobs'].first | |
puts row[:count] | |
if row[:count] >= options.critical | |
exit 2 | |
elsif row[:count] >= options.warning | |
exit 1 | |
end | |
rescue => e | |
puts e.message | |
exit 3 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment