#!/usr/bin/perl

## REMOTE PROGRAM CHECK FOR MONITORING NOTIFICATION SUBSYSTEM

use strict;
use NOCpulse::Config;

  my $CFG=NOCpulse::Config->new;

  my $queue_name=$ARGV[0];
  my $WARNING_QUEUE_SIZE  = $ARGV[1];
  my $CRITICAL_QUEUE_SIZE = $ARGV[2];

  unless ($queue_name && $WARNING_QUEUE_SIZE && $CRITICAL_QUEUE_SIZE) {
    &help;
    exit(1);
  }

  my $OK_EXIT = 0;
  my $WARNING_EXIT  = 1;
  my $CRITICAL_EXIT = 2;
  my $UNKNOWN_EXIT  = 3;
  my $exit_value=$OK_EXIT;

  my $queue = { ALERTS   => 'alert_queue_dir',
                ACKS     => 'ack_queue_dir',
                REQUESTS => 'request_queue_dir' };

# Locate queue directory
  my $QUEUE_DIR = $CFG->get('notification', $queue->{$queue_name});
  chdir $QUEUE_DIR || &bailout("unable to find queue directory\n");
  my @files=glob("*");

  my $queue_size=scalar(@files);

  # output data for time series and setup exit status
  print "<perldata>\n<hash>\n";
  print '<item key="data">';
  print $queue_size;
  print "</item>\n</hash>\n</perldata>";

  if ($queue_size >= $CRITICAL_QUEUE_SIZE) {
    $exit_value=$CRITICAL_EXIT;
  } elsif ($queue_size >= $WARNING_QUEUE_SIZE) {
    $exit_value=$WARNING_EXIT;
  }
  exit $exit_value;

sub bailout {
  print STDERR "@_\n";
  exit($UNKNOWN_EXIT);
}

sub help {
  print "$0 [queue name (ALERTS|ACKS|REQUESTS)] [warning level] [critical level]\n";
  print "This script is meant for use with the Command Center remote program check with data.  It tracks the number of unprocessed requests current on disk for the specified queue.\n"
}
