#!/usr/bin/perl -w # # check_pkgchk.pl - nagios plugin which checks the solaris package database # # Copyright (C) 2007 Gerhard Lausser, gerhard.lausser@consol.de # # 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # # Report bugs to: gerhard.lausser@consol.de # # 2007-01-09 Version 1.0 # # # # If you execute the command pkgchk -q -p /tmp # and see something like this, # ERROR: garbled entry # pathname: /usr# # problem: unknown ftype # then your solaris installation is in danger. # You might no longer be able to install any software packages or patches, # forcing you to reinstall the entire operating system. # The alert from this plugin might point you to this problem, so you can # restore a backup of your /var/sadm/install/contents immediately after you # screwed it up. # use strict; use Getopt::Std; use vars qw($PROGNAME $REVISION $TIMEOUT $opt_V $opt_h $opt_t); my %ERRORS=( OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 ); my %ERRORCODES=( 0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', 3 => 'UNKNOWN' ); $PROGNAME = "check_pkgchk"; $REVISION = '$Revision: 1.0 $'; $TIMEOUT = 10; sub print_usage () { print "Usage:\n"; print " $PROGNAME [-t ]\n"; print " $PROGNAME [-h | --help]\n"; print " $PROGNAME [-V | --version]\n"; print "\n\nOptions:\n"; print " -t, --timeout\n"; print " The number of seconds after which the plugin will abort\n"; print " -h, --help\n"; print " Print detailed help screen\n"; print " -V, --version\n"; print " Print version information\n\n"; } sub print_help () { print "Copyright (c) 2007 Gerhard Lausser\n\n"; print_usage(); print "\n"; print " Check the solaris package database for corruption\n"; print "\n"; support(); } sub print_revision ($$) { my $commandName = shift; my $pluginRevision = shift; $pluginRevision =~ s/^\$Revision: //; $pluginRevision =~ s/ \$\s*$//; print "$commandName $pluginRevision\n"; print "This nagios plugin comes with ABSOLUTELY NO WARRANTY. You may redistribute\ncopies of this plugin under the terms of the GNU General Public License.\n"; } sub support () { my $support='Send email to gerhard.lausser@consol.de if you have questions\nregarding use of this software. \nPlease include version information with all correspondence (when possible,\nuse output from the --version option of the plugin itself).\n'; $support =~ s/@/\@/g; $support =~ s/\\n/\n/g; print $support; } getopts('Vht:'); if ($opt_t) { $TIMEOUT = $opt_t; } $SIG{'ALRM'} = sub { printf "UNKNOWN - %s timed out after %d seconds\n", $PROGNAME, $TIMEOUT; exit $ERRORS{UNKNOWN}; }; alarm($TIMEOUT); if ($opt_V) { print_revision($PROGNAME, $REVISION); exit $ERRORS{OK}; } if ($opt_h) { print_help(); exit $ERRORS{OK}; } my $exitcode = $ERRORS{UNKNOWN}; my $exitmessage = "you should never see this message"; if (-f "/usr/sbin/pkgchk") { my @pkgchk = `/usr/sbin/pkgchk -q -p /tmp 2>&1`; if ($? == 0) { $exitcode = $ERRORS{OK}; $exitmessage = "package database clean"; } else { if (grep /ERROR: garbled entry/, @pkgchk) { $exitcode = $ERRORS{CRITICAL}; $exitmessage = "package database corrupt"; } else { $exitcode = $ERRORS{WARNING}; $exitmessage = sprintf "package database error %s", join(", ", grep /ERROR:/, @pkgchk); } } } else { $exitcode = 3; $exitmessage = "cannot find /usr/sbin/pkgchk"; } printf "%s - %s\n", $ERRORCODES{$exitcode}, $exitmessage; exit $exitcode;