#!/usr/bin/perl -w # check_p4_license Nagios plugin # Copyright (C) 2009 Jonathan Delgado, delgado@molbio.mgh.harvard.edu # # 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 2 # 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # # Nagios plugin to check the license expiration date of a Perforce server # as well as report the number of users the license is for. # # The only requirement besides a standard Perl install is p4 # # $Author: delgado $ # $Revision: #4 $ $Date: 2009/04/08 $ use strict; use Getopt::Std; use Time::Local; use lib "/usr/local/libexec/nagios"; use utils qw(%ERRORS); our($opt_h, $opt_H, $opt_p, $opt_c, $opt_w); getopts('hH:p:c:w:'); # The path to your p4 binary my $p4 = '/usr/local/bin/p4'; if ( $opt_h ) { print "Usage: $0 -H [-p ] [-w ] [-c ]\n"; print " -H, Hostname or IP address of Perforce server\n"; print " -p, port number of Perforce service on host\n"; print " -w, days difference to return a warning status\n"; print " -c, days difference to return a critical status\n"; exit; } unless ( (defined $opt_p) && ($opt_p =~ m/^\d+$/) ) { $opt_p = 1666; } unless ( defined $opt_H ) { $opt_H = 'perforce'; } unless (defined $opt_c ) { $opt_c = 7; } unless (defined $opt_w ) { $opt_w = 14; } $ENV{'P4PORT'} = "$opt_H:$opt_p"; unless ( open (P4INFO, "$p4 info|") ) { print STDOUT "UNKNOWN: Could not execute p4 info\n"; exit $ERRORS{'UNKNOWN'}; } my ($users, $year, $month, $day); while () { if ( m/^Server license: .+ (\d+) users \(expires (\d+)\/(\d+)\/(\d+)\)/ ) { ($users, $year, $month, $day) = ($1, $2, $3, $4); last; } } close P4INFO; if ( $users ) { $month =~ s/0(\d)/$1/; $month--; $day =~ s/0(\d)/$1/; my $expire = timelocal(0,0,0,$day,$month,$year) - time(); my $daysleft = int($expire / 86400); $month++; if ($expire < ($opt_c * 86400)) { print STDOUT "CRITICAL: $users user license expires in $daysleft day(s)\n"; exit $ERRORS{'CRITICAL'}; } elsif ($expire < ($opt_w * 86400)) { print STDOUT "WARNING: $users user license expires in $daysleft day(s)\n"; exit $ERRORS{'WARNING'}; } else { print STDOUT "OK: $users user license, expires $month/$day/$year\n"; exit $ERRORS{'OK'}; } } else { print STDOUT "UNKNOWN: Can not determine license expiration date\n"; exit $ERRORS{'UNKNOWN'}; }