#!/usr/bin/perl

# 
# check_quota - Nagios plugin to check use quota on BSD systems.
#

# -----------------------------------------------------------------------------
# Copyright (C) 2008 John Sennesael
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------------

use strict;

my @quotas ;
my @crit_users;
my @warn_users;
my $total_files;
my $total_blocks;

@quotas = `repquota /usr | grep -v "Block..limits\\|used....soft"` ;
$total_blocks = 0;
$total_files = 0;
foreach my $quota (@quotas)
{
  my @fields = split(/\s+/,$quota);
  my $user = @fields[0];
  my $blocks_used = @fields[2];
  my $blocks_soft = @fields[3];
  my $blocks_hard = @fields[4];
  my $file_used = @fields[6];
  my $file_soft = @fields[7];
  my $file_hard = @fields[8];
  if (($blocks_soft != 0) && ($blocks_used >= $blocks_soft))
  {
    push(@warn_users,$user);
  }
  if (($blocks_hard != 0) && ($blocks_used >= $blocks_hard))
  {
    push(@crit_users,$user);
  }
  if (($file_soft != 0) && ($file_used >= $file_soft))
  {
    push(@warn_users,$user);
  }
  if (($file_hard != 0) && ($file_used >= $file_hard))
  {
    push(@crit_users,$user);
  }
  $total_blocks += $blocks_used;
  $total_files += $file_used;
}

my $status = "UNKNOWN";
if ($#crit_users > 0)
{
  $status = "CRITICAL";
}
elsif ($#warn_users > 0)
{
  $status = "WARNING";
}
else
{
  $status = "OK";
}

if ($status eq "OK")
{
  print "$status - Total usage: $total_blocks blocks - $total_files files.";
}
else
{
  print "$status";
}

if ($#warn_users > 0)
{
  print " - WARNING FOR USERS: @warn_users";
}
if ($#warn_users > 0)
{
  print " - CRITICAL FOR USERS: @crit_users";
}

print "\n";


