Annotation of nagios/check_radius/check_radius.pl, Revision 1.1
1.1 ! andrew 1: #!/usr/bin/perl
! 2: #
! 3: # check_radius.pl - nagios plugin
! 4: #
! 5: #
! 6: # Copyright (C) 2003 andrew fresh
! 7: #
! 8: # This program is free software; you can redistribute it and/or
! 9: # modify it under the terms of the GNU General Public License
! 10: # as published by the Free Software Foundation; either version 2
! 11: # of the License, or (at your option) any later version.
! 12: #
! 13: # This program is distributed in the hope that it will be useful,
! 14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
! 15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 16: # GNU General Public License for more details.
! 17: #
! 18: # You should have received a copy of the GNU General Public License
! 19: # along with this program; if not, write to the Free Software
! 20: # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
! 21: #
! 22: #
! 23: # Report bugs to: andrew@mad-techies.org
! 24: #
! 25: # 11.26.2000 Version 1.0
! 26: #
! 27: # $Id: Win32_check_services.pl,v 1.0 2003/11/26 13:02:54 andrew Exp $
! 28:
! 29: use strict;
! 30: use warnings;
! 31: use diagnostics;
! 32:
! 33: use POSIX;
! 34: use lib "C:/nrpe_nt/lib" ;
! 35: use utils qw($TIMEOUT %ERRORS &print_revision &support);
! 36:
! 37: use Getopt::Long;
! 38: Getopt::Long::Configure('bundling');
! 39:
! 40: my $PROGNAME = "check_radius";
! 41:
! 42: my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK
! 43: my $answer = ''; # stores the test of the errors
! 44: my $hostname = '';
! 45: my $port = '';
! 46: my $secret = '';
! 47: my $user = '';
! 48: my $pwd = '';
! 49: my $timeout = 5;
! 50: my %states; # This stores the count of states;
! 51: my $file;
! 52: my $opt_h ;
! 53: my $opt_V ;
! 54:
! 55:
! 56: # Just in case of problems, let's not hang Nagios
! 57: $SIG{'ALRM'} = sub {
! 58: print ("ERROR: No radius response from $hostname (alarm timeout)\n");
! 59: exit $ERRORS{"UNKNOWN"};
! 60: };
! 61: alarm($TIMEOUT);
! 62:
! 63:
! 64:
! 65: #Option checking
! 66: my $status = GetOptions(
! 67: "V" => \$opt_V, "version" => \$opt_V,
! 68: "h" => \$opt_h, "help" => \$opt_h,
! 69: "H=s" => \$hostname, "hostname=s" => \$hostname,
! 70: "s=s" => \$secret, "secret=s" => \$secret,
! 71: "P=i" => \$port, "port=i" => \$port,
! 72: "u=s" => \$user, "username=s" => \$user,
! 73: "p=s" => \$pwd, "password=s" => \$pwd,
! 74: "t=i" => \$timeout, "timeout=i"=> \$timeout,
! 75: );
! 76:
! 77: if ($status == 0)
! 78: {
! 79: print_help() ;
! 80: exit $ERRORS{'OK'};
! 81: }
! 82:
! 83:
! 84: if ($opt_V) {
! 85: print_revision($PROGNAME,'$Revision: 1.3 $ ');
! 86: exit $ERRORS{'OK'};
! 87: }
! 88:
! 89: if ($opt_h) {
! 90: print_help();
! 91: exit $ERRORS{'OK'};
! 92: }
! 93:
! 94: unless ($hostname && $secret && $user && $pwd) {
! 95: print_help();
! 96: exit $ERRORS{'OK'};
! 97: }
! 98:
! 99: if ($port) {
! 100: $hostname .= ":" . $port;
! 101: }
! 102:
! 103: use Authen::Radius;
! 104:
! 105: #print "Creating Client . . . ";
! 106: my $r = new Authen::Radius(Host => $hostname, Secret => $secret, Timeout => $timeout);
! 107: #print defined $r ? "" : "not ", "ok\n";
! 108:
! 109: unless (defined $r) {
! 110: done('UNKNOWN', "Couldn't create socket!");
! 111: }
! 112:
! 113:
! 114: $r->clear_attributes;
! 115:
! 116: $r->add_attributes (
! 117: { Name => 1, Value => $user, Type => 'string' }, # Username
! 118: { Name => 2, Value => $pwd, Type => 'string' }, # Password
! 119: { Name => 5, Value => '1', Type => 'integer' }, # NASPort
! 120: );
! 121:
! 122: #print "Authenticating . . .";
! 123: my $snt = $r->send_packet(ACCESS_REQUEST);
! 124: unless (defined $snt) {
! 125: done('CRITICAL', "Couldn't sent authentication packet: " . $r->strerror($r->get_error));
! 126: }
! 127:
! 128: $r->clear_attributes;
! 129:
! 130: my $rcv = $r->recv_packet();
! 131:
! 132: #print "" . (defined($rcv) and $rcv == ACCESS_ACCEPT) ? "" : "not ", "ok\n";
! 133:
! 134: unless (defined $rcv) {
! 135: done ('CRITICAL', "Didn't recieve valid response: " . $r->strerror($r->get_error));
! 136: }
! 137:
! 138: unless ($rcv == ACCESS_ACCEPT) {
! 139: done ('WARNING', "Access was denied for $user");
! 140: }
! 141:
! 142:
! 143: #my @a = $r->get_attributes;
! 144: #print "Attributes . . . ";
! 145: #print $#a != -1 ? "" : "not ", "ok\n";
! 146: #for $a (@a) {
! 147: # print "attr: name=$a->{'Name'} value=$a->{'Value'}\n";
! 148: #}
! 149:
! 150:
! 151: if ($state eq 'UNKNOWN') {
! 152: $state = 'OK';
! 153: $answer = "User $user authenticated correctly!";
! 154: }
! 155:
! 156: done($state, $answer);
! 157:
! 158: sub done
! 159: {
! 160: my $state = shift;
! 161: my $answer = shift;
! 162:
! 163: print "$state: ";
! 164: print $answer;
! 165: exit $ERRORS{$state};
! 166: }
! 167:
! 168: sub print_help {
! 169: printf "$PROGNAME plugin for Nagios monitors radius authentication\n";
! 170: printf " $PROGNAME -H <HOSTNAME> -u <USERNAME> -p <PASSWORD>\n";
! 171: printf "\nUsage:\n";
! 172: printf " -H (--hostname) Hostname to query (required)\n";
! 173: printf " -s (--secret) Radius Secret (required)\n";
! 174: printf " -P (--port) Radius auth port\n";
! 175: printf " -u (--username) Username to try authenticating (required)\n";
! 176: printf " -p (--password) Password to authenticate with (required)\n";
! 177: printf " -t (--timeout) Time to wait for response (defaults to 5 secs)\n";
! 178: printf " -h (--help) usage help \n\n";
! 179: print_revision($PROGNAME, '$Revision: 1.0 $');
! 180: }
! 181:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>