Skip to content

Instantly share code, notes, and snippets.

@jakemcgraw
Created March 5, 2010 20:58
Show Gist options
  • Save jakemcgraw/323140 to your computer and use it in GitHub Desktop.
Save jakemcgraw/323140 to your computer and use it in GitHub Desktop.
Nagios check for PHP configuration
#!/usr/bin/perl
use strict;
# default php executable, version
my $PHPBIN= '/usr/bin/php';
my $PHPVER= '5.2.10';
my $PEARBIN= '/usr/bin/pear';
my $MESSAGE= 'OK ';
use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS $TIMEOUT);
#my $TIMEOUT = 20;
#my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
use Getopt::Long qw(:config no_ignore_case);
my $o_phpbin= undef; # php executable
my $o_phpver= undef; # required php version
my $o_pearbin= undef; # php pear executable
my $o_modules= undef; # required php modules
my $o_packages= undef; # required php pear packages
my @o_modulesL= undef;
my @o_packagesL=undef;
my $o_help= undef; # help option
my $o_timeout= undef;
sub print_usage { print "Usage: $0 [-b <PHP binary>] [-V <PHP version>] [-m <variables modules>] [-P <PEAR binary>] [-p <variable packages>]"}
sub help {
print "\nPHP Configuration Monitor for Nagios\n\n";
print " by Jake McGraw - jmcgraw(at)targetspot.com\n\n";
print "This monitoring script tests the local PHP configuration for version number, ";
print "installed modules and installed PEAR packages.\n\n";
print_usage();
print <<EOT;
-b, --binary=Path
PHP binary to execute (default: $PHPBIN)
-V, --version=Version
required PHP version (default: $PHPVER)
-m, --modules=Module[,Module[,Module...]]
required PHP modules
-P, --pear=Path
PHP PEAR binary to execute (default: $PEARBIN)
-p, --packages=Package[-Version][,Package[-Version][,Pakage[-Version]...]]
specify required PEAR packages, and, optionally, version
EOT
print "\nThere are no required arguments.\n\n";
}
sub check_options {
Getopt::Long::Configure("bundling");
GetOptions(
'b:s' => \$o_phpbin, 'binary:s' => \$o_phpbin,
'V:s' => \$o_phpver, 'version:s' => \$o_phpver,
'm:s' => \$o_modules, 'modules:s' => \$o_modules,
'P:s' => \$o_pearbin, 'pear:s' => \$o_pearbin,
'p:s' => \$o_packages, 'packages:s' => \$o_packages,
);
if (defined($o_help)) { help(); exit $ERRORS{"UNKNOWN"} };
@o_modulesL=split(/,/, $o_modules) if $o_modules;
@o_packagesL=split(/,/, $o_packages) if $o_packages;
$PHPBIN = $o_phpbin if defined($o_phpbin);
$PHPVER = $o_phpver if defined($o_phpver);
$PEARBIN = $o_pearbin if defined($o_pearbin);
}
#### MAIN ####
check_options();
if (defined($TIMEOUT)) {
alarm($TIMEOUT);
}
else {
alarm($o_timeout+10);
}
# Check PHP version
my $result = `$PHPBIN --version`;
if (!$result) {
print "CRITICAL ERROR - $PHPBIN not available\n";
exit $ERRORS{"CRITICAL"};
}
if ($result=~m/^PHP\s+([0-9.]+)/) {
if ($1 ne $PHPVER) {
print "WARNING - Running PHP version $1, expected $PHPVER\n";
exit $ERRORS{"WARNING"};
}
}
else {
print "CRITICAL ERROR - Unable to determine PHP version\n";
exit $ERRORS{"CRITICAL"};
}
$MESSAGE = $MESSAGE."$PHPBIN $PHPVER ";
# Check PHP modules
if ($o_modules and scalar(@o_modulesL) > 0) {
my @missing = ();
my %search = ();
my $module = undef;
my $result = `$PHPBIN -m`;
foreach ($result =~ m/^(\w+)$/mg) {$search{lc $_} = 1}
foreach $module (@o_modulesL) {
if (!$search{lc $module}) {
push(@missing, $module);
}
}
if (scalar(@missing) > 0) {
print "WARNING - Missing ",scalar @missing," PHP module(s) (@missing)\n";
exit $ERRORS{'WARNING'};
}
$MESSAGE.= "PHP modules (".(scalar @o_modulesL).",".(scalar keys %search).") ";
}
# Check PEAR packages
if ($o_packages and scalar(@o_packagesL) > 0) {
my @missing = my @wrongver = ();
my %srch_install = my %srch_version = ();
my $package = undef;
my $result = `$PEARBIN list -a`;
if (!$result) {
print "CRITICAL ERROR - $PEARBIN not available\n";
exit $ERRORS{'CRITICAL'};
}
foreach ($result =~ m/^(?!INSTALLED|PACKAGE)(\w+\s+[0-9.]+)/mg) {
(my $p, my $v) = split(/\s+/, $_);
$srch_install{lc $p} = 1;
$srch_version{lc $p} = $v;
}
foreach $package (@o_packagesL) {
if ($package =~ m/(\w+)-([0-9.]+)/) {
if (!$srch_install{lc $1}) {
push(@missing, $1);
}
elsif ($srch_version{lc $1} ne $2) {
push(@wrongver, $package.'['.$srch_version{lc $1}.']');
}
}
else {
if (!$srch_version{lc $package}) {
push(@missing, $package);
}
}
}
if (scalar(@missing) > 0) {
print "WARNING - PEAR missing (@missing)";
if (scalar(@wrongver) > 0) {
print " wrong version (@wrongver)";
}
print "\n";
exit $ERRORS{'WARNING'};
}
if (scalar(@wrongver) > 0) {
print "WARNING - PEAR wrong version (@wrongver)\n";
exit $ERRORS{'WARNING'};
}
$MESSAGE.= "PEAR packages (".(scalar @o_packagesL).",".(scalar keys %srch_install).") ";
}
print $MESSAGE."\n";
exit $ERRORS{'OK'};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment