#!/usr/bin/perl # # Copyright (c) 2013, Michael Hanselmann # All rights reserved. # # This Nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. # It may be used, redistributed and/or modified under the terms of the GNU # General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt). use warnings; use strict; use constant VERSION => '0.1.0'; use Nagios::Plugin; my $used; my $used_percent; my $old; my $old_percent; my $deleted; my $deleted_percent; my $directories; my $directories_percent; my $soft_limit; my $soft_limit_percent; my $hard_limit; my $hard_limit_percent; my @fields = ( ['used', qr/Used/i, \$used, \$used_percent], ['old-files', qr/Old\s+files/, \$old, \$old_percent], ['deleted-files', qr/Deleted\s+files/, \$deleted, \$deleted_percent], ['directories', qr/Directories/, \$directories, \$directories_percent], ['soft-limit', qr/Soft\s+limit/, \$soft_limit, \$soft_limit_percent], ['hard-limit', qr/Hard\s+limit/, \$hard_limit, \$hard_limit_percent], ); my $np = Nagios::Plugin->new( usage => ("Usage: %s [-v|--verbose] [--cmd=]" . " -a ACCOUNT [opts...]"), version => VERSION, timeout => 30, ); $np->add_arg( spec => 'account|a=s', help => '-a, --account=s', required => 1, ); $np->add_arg( spec => 'cmd=s', help => '--cmd=PATH (path to bbstoreaccounts)', required => 1, default => 'bbstoreaccounts', ); foreach (@fields) { my ($name, undef, undef, undef) = @$_; foreach my $mode ('warning', 'critical') { $np->add_arg(spec => "$name-$mode=s", help => "--$name-$mode=THRESHOLD"); $np->add_arg(spec => "$name-percent-$mode=s", help => "--$name-percent-$mode=THRESHOLD"); } } # Parse options $np->getopts; my $account = $np->opts->account; my $cmd = $np->opts->cmd; open(my $fh, '-|', $cmd, 'info', '-m', $account) or die "Starting '$cmd' failed: $!\n"; while (<$fh>) { chomp (my $line = $_); foreach (@fields) { my ($name, $re, $value, $value_percent) = @$_; if ($line =~ m/^$re\s*:\s*(\d+)\s*kB,\s*(\d+)\s*%\s*$/i) { # Always use bytes internally $$value = int($1) * 1024; $$value_percent = int($2); if ($np->opts->verbose) { print "found $name, $$value, $$value_percent%\n"; } last; } } } close $fh; if ($? == -1) { $np->nagios_exit(CRITICAL, "'$cmd' failed to execute: $!"); } elsif ($? & 127) { $np->nagios_exit(CRITICAL, sprintf("'$cmd' died with signal %d with%s coredump", ($? & 127), ($? & 128) ? '' : 'out')); } elsif ($? != 0) { $np->nagios_exit(CRITICAL, sprintf("'$cmd' exited with non-zero status code %d", $? >> 8)); } else { my $value = $1; foreach (@fields) { my ($name, undef, $value, $value_percent) = @$_; unless (defined($$value) and defined($$value_percent)) { die "Incomplete data for '$name'"; } my $warning = $np->opts->{"$name-warning"}; my $warning_percent = $np->opts->{"$name-percent-warning"}; my $critical = $np->opts->{"$name-critical"}; my $critical_percent = $np->opts->{"$name-percent-critical"}; $np->add_perfdata(label => $name, value => $$value, warning => $warning, critical => $critical); $np->add_perfdata(label => "$name-percent", value => $$value_percent, warning => $warning_percent, critical => $critical_percent); $np->add_message($np->check_threshold(check => $$value, warning => $warning, critical => $critical), "$name=$$value"); $np->add_message($np->check_threshold(check => $$value_percent, warning => $warning_percent, critical => $critical_percent), "$name-percent=$$value_percent"); } my ($code, undef) = $np->check_messages; $np->nagios_exit($code, sprintf('%0.1f GB used (%d %%)', $used / 1024.0 / 1024 / 1024, $used_percent)); } # vim: set sw=2 sts=2 et :