File check_delayed_queue of Package monitoring-plugins-delayed_queue

#!/usr/bin/perl -w

# based on check_mem, MIT licensed

# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all copies
# or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
# OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.


use warnings;
use strict;
use Getopt::Std;

# Predefined exit codes for Nagios
use vars qw($opt_c $opt_w $opt_v %exit_codes);
%exit_codes   = ('UNKNOWN' , 3,
                 'OK'      , 0,
                 'WARNING' , 1,
                 'CRITICAL', 2,
                 );
our $opt_a = '/srv/www/obs/api';

# Get our variables, do our checking:
init();

# Get the numbers:
my ($queue_length) = get_queue_length();
print "$queue_length entries in delayed queue\n" if ($opt_v);

# Tell Nagios what we came up with
tell_nagios($queue_length);

sub tell_nagios {
    my ($queue_length) = @_;

    my $perfdata = "|QUEUE=$queue_length;;;;";

    if ($queue_length < 0) {
        finish("UNKNOWN - could not get queue_length!$perfdata",$exit_codes{'UNKNOWN'});
    }
    elsif ($queue_length > $opt_c) {
        finish("CRITICAL - queue length over $queue_length (consider running Delayed::Job.where(\'handler like \"%ActiveRecord:Event::Repo%\"\').delete_all)!$perfdata",$exit_codes{'CRITICAL'});
    }
    elsif ($queue_length > $opt_w) {
        finish("WARNING - queue length over $queue_length!$perfdata",$exit_codes{'WARNING'});
    }
    else {
        finish("OK - queue length is $queue_length!$perfdata",$exit_codes{'OK'});
    }
}

# Show usage
sub usage() {
  print "\ncheck_delayed_queue v1.0 - Nagios Plugin\n\n";
  print "usage:\n";
  print " check_delayed_queue -w <warnlevel> -c <critlevel>\n\n";
  print "options:\n";
  print " -w NUM    warn level if queue longer than NUM\n";
  print " -c NUM    criticali level if queue longer than NUM\n";
  print " -a <path> path to the OBS api installation (default: $opt_a)\n";
  exit $exit_codes{'UNKNOWN'};
}

sub get_queue_length {
    open (OUT, "cd $opt_a ; sudo RAILS_ENV=production __RUBY_PATH__ r \"puts Delayed::Job.count\" |");
    my $retval = <OUT>;
    chomp($retval) if $retval;
    close(OUT);
    $retval = -1 unless $retval || $retval eq "0";
    return ($retval);
}

sub init {
    # Get the options
    if ($#ARGV le 0) {
      &usage;
    }
    else {
      getopts('c:vw:a:');
    }

    # Shortcircuit the switches
    if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0) {
      print "*** You must define WARN and CRITICAL levels!\n";
      &usage;
    }

    # Check if levels are sane
    if ($opt_w >= $opt_c) {
      print "*** WARN level must not be greater than CRITICAL!\n";
      &usage;
    }
	if (! -d "$opt_a"){
      finish("UNKNOWN - $opt_a is no directory or could not be accessed by check user",$exit_codes{'UNKNOWN'});
    }
}

sub finish {
    my ($msg,$state) = @_;
    print "$msg\n";
    exit $state;
}

openSUSE Build Service is sponsored by