#!/usr/bin/perl -w

#######################################################################
##
## Copyright (c) 2011 Mike bofh@norgie.net
##
## License Information:
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
## Home Site: http://www.norgie.net/tools/check_kadmin/
## #####################################################################

use strict;
use Authen::Krb5::Admin;
use Getopt::Long qw(:config no_ignore_case);
use lib "/usr/lib/nagios/plugins/";
use Nagios::Plugin qw(%ERRORS);
use Time::HiRes qw(gettimeofday ualarm);

my $PROGNAME = "check_kadmin";
my $VERSION = '$Revision: 1.0 $';

# c - critical
# h - help
# H - Host
# p - port
# r - realm
# v - verbose
# w - warning

my $opt_c = '';
my $opt_h = '';
my $opt_k = '';
my $opt_p = '';
my $opt_r = '';
my $opt_u = '';
my $opt_v = '';
my $opt_w = '';

local $SIG{ALRM} = sub {
  print "CRITICAL: Timeout while connecting to Kadmin daemon\n";
  exit $ERRORS{"CRITICAL"};
};

GetOptions
  ("c=i" => \$opt_c, "critical=i" => \$opt_c,
   "h"  =>  \$opt_h, "help" => \$opt_h,
   "k=s" => \$opt_k, "keytab=s" => \$opt_k,
   "p=i" => \$opt_p, "port=i" => \$opt_p,
   "r=s" => \$opt_r, "realm=s" => \$opt_r,
   "u=s" => \$opt_u, "user=s" => \$opt_u,
   "v" => \$opt_v, "verbose" => \$opt_v,
   "w=i" => \$opt_w, "warning=i" => \$opt_w
  ) or exit $ERRORS{'UNKNOWN'};

if ($opt_c eq '') {
  $opt_c = 15;
}

$opt_c = $opt_c * 1000000;

if ($opt_h eq "1") {
  &print_help();
}

if ($opt_u ne "" && $opt_k ne "" && $opt_r ne "") {
  my $kconfig = Authen::Krb5::Admin::Config->new;
  $kconfig->realm($opt_r);
  my $service = 'kadmin/admin';
  ualarm($opt_c);
  my $starttime = gettimeofday();
  my $kadm5 = Authen::Krb5::Admin->init_with_skey($opt_u, $opt_k, $service, $kconfig);
  my $kadm5_error = Authen::Krb5::Admin::error;
  my $endtime = gettimeofday();
  my $takentime = $endtime - $starttime;
  my $warn = 0;
  if ($opt_w ne '') {
    if ($takentime >= $opt_w) {
      $warn = 1;
    }
  }
  ualarm(0);
  if (defined $kadm5 && $warn == 0) {
    print "OK: Connected to kadmin daemon in $takentime seconds\n";
    exit $ERRORS{"OK"};
  } elsif (defined $kadm5 && $warn == 1) {
    print "WARNING: Connection to kadmin daemon took $takentime seconds\n";
    exit $ERRORS{"WARNING"};
  } else {
    print "CRITICAL: Cannot connect to kadmin daemon.  Error: $kadm5_error\n";
    exit $ERRORS{"CRITICAL"};
  }
}

sub print_help() {
  print "check_kadmin: Usage: check_kadmin [-h] | [-p port] [-w warn time] [-c critical time] -u admin_princ -k keytab -r realm\n";
  print "Written by BOfH\n";
  print "Options:\n";
  print "-h --help: Print this help message\n";
  print "-p --port: Specify a port to use (default is kadmin)\n";
  print "-w --warning: Return a warning if plugin takes greater than -w seconds\n";
  print "-c --critical: Exit critical if plugin reaches -c seconds\n";
  print "-u --user: Specifies the kadmin admin account to login with\n";
  print "-k --keytab: Specifies the keytab file containing the key\n";
  print "-r --realm: Specifies the realm\n";
  print "You should create an admi prinicple for this plugin and use an ACL to give it minimal privilages.  It only has to be abel to connect to the kadmin daemon.\n";
  print "You will need to configure your krb5.conf file for each realm you wish to test.\n";
  exit $ERRORS{"OK"};
}
