checkquota.in 5.68 KB
Newer Older
1
#!/usr/bin/perl -w
2
#
3
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35 36
#
use English;
use Getopt::Std;
    
#
# Parse an ns file. Since the parser runs arbitrary NS file for the user,
# this cannot be safely done on boss without jumping through huge hoops
# to secure tcl and the DB. Yuck! So, instead of running the parser on boss,
# we run it over on ops. This first version operates like this:
#
# NB: This script is setuid.
#
sub usage()
{
37
    print STDOUT "Usage: checkquota [-d] [-p pid | -g gid] [-m mebi] <user>\n";
38 39
    exit(-1);
}
40
my $optlist  = "dp:m:g:";
41
my $debug    = 0;
42 43 44
my $minmebi;
my $project;
my $group;
45 46 47 48 49 50

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
51
my $FS       = "@FSNODE@";
52
my $TESTMODE = @TESTMODE@;
53 54
my $WITHZFS  = @WITHZFS@;
my $ZFS_ROOT = "@ZFS_ROOT@";
55
my $FSLIST   = "@FS_WITH_QUOTAS@";
56 57 58

my $QUOTACMD = "/usr/bin/quota";
my $SSHTB    = "/usr/testbed/bin/sshtb";
59
my $ZFS      = "/sbin/zfs";
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

# Locals
my $user;
my $dbuid;
my $overquota = 0;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

exit(0)
78
    if ($FSLIST eq "" && !$WITHZFS);
79 80 81 82 83 84 85 86 87 88 89 90 91

if ($EUID != 0) {
    # We don't want to run this script unless its the real version.
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
92
use Group;
93
use Project;
94 95
# For mebi conversion.
use Blockstore;
96 97 98 99 100 101 102 103 104 105 106 107

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
108
if (defined($options{"p"})) {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    $project = Project->Lookup($options{"p"});
    if (!defined($project)) {
	die("No such project");
    }
}
elsif (defined($options{"g"})) {
    $group = Group->Lookup($options{"g"});
    if (!defined($group)) {
	die("No such group");
    }
    # Watch for main group.
    if ($group->IsProjectGroup()) {
	$project = $group->GetProject();
	$group   = undef;
    }
}
if (defined($options{"m"})) {
    $minmebi = Blockstore::ConvertToMebi($options{"m"});
    if ($minmebi < 0) {
	die("Invalid format for -m option");
    }
130
}
131 132 133 134 135 136 137 138 139 140 141 142 143 144
if (@ARGV != 1) {
    usage();
}
$user = $ARGV[0];

#
# Must taint check!
#
if ($user =~ /^([-\w]+)$/) {
    $user = $1;
}
else {
    die("Bad data in argument: $user");
}
145

146 147 148 149 150
#
# Convert to a uid since fs node may not have our user names
#
my $userid = getpwnam($user);

151 152 153 154 155
#
# Must flip to real root for the ssh.
# 
$UID = 0;

156 157
if ($WITHZFS) {
    my $fsarg = "$ZFS_ROOT/users/$user";
158 159 160 161 162 163 164 165
    if (defined($group)) {
	my $pid = $group->pid();
	$fsarg .= " $ZFS_ROOT/groups/$pid ";
	$fsarg .= " $ZFS_ROOT/proj/$pid ";
    }
    elsif (defined($project)) {
	my $pid = $project->pid();
	$fsarg .= " $ZFS_ROOT/proj/$pid ";
166 167 168 169 170 171 172 173 174 175 176 177
    }
    my $cmd = "$SSHTB -host $FS $ZFS get -H -o name,value available $fsarg";
    if ($debug) {
	print "$cmd\n";
    }
    open(QUOTA, " $cmd |") or
	 die("*** $0:\n".
	     "    Could not invoke $QUOTACMD on $FS!\n");
    while (<QUOTA>) {
	if ($debug) {
	    print $_;
	}
Leigh Stoller's avatar
Leigh Stoller committed
178
	if ($_ =~ /^([-\w\/]*)\s+([\d\.]*)(\w)/) {
179 180
	    my $path = $1;
	    
181 182 183 184 185 186
	    if (! ($3 eq "M" || $3 eq "G" || $3 eq "T" ||
		   ($3 eq "K" && $2 >= 1024) ||
		   ($3 eq "B" && $2 < 1024*1024))) {
		print STDOUT "*** Disk Quota exceeded on $1\n";
		$overquota++;
	    }
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	    elsif (defined($minmebi)) {
		my $current = Blockstore::ConvertToMebi("${2}${3}B");
		if ($current < 0) {
		    print STDERR $_;
		    print STDERR "Could not convert current to mebi\n";
		    $overquota++;
		    next;
		}
		my $test = 0;
		
		#
		# Makes no sense to require the same amount of space
		# across all filesystems when doing a per-project check.
		#
		if (defined($group)) {
		    my $pid = $group->pid();
		    next
			if ($path !~ /^$ZFS_ROOT\/groups\//);
		    $test = 1;
		}
		elsif (defined($project)) {
		    my $pid = $project->pid();
		    next
			if ($path !~ /^$ZFS_ROOT\/proj\//);
		    $test = 1;
		}
		else {
		    next
			if ($path !~ /^$ZFS_ROOT\/users\//);
		    $test = 1;
		}
		if ($test && $debug) {
		    print STDOUT "Testing $path for min space: $current\n";
		}
		if ($test && $current < $minmebi) {
		    print STDOUT "*** Not enough space on $1\n";
		    $overquota++;
		}
	    }
226 227 228 229 230 231 232 233 234 235 236 237 238 239
	}
	else {
	    print STDERR "*** Disk Quota not recognized: $_";
	    $overquota++;
	}
    }
}
else {   
    #
    # We invoke the quota command and look at the results.
    # 
    open(QUOTA, "$SSHTB -host $FS $QUOTACMD -q -l $userid |") or
	die("*** $0:\n".
	    "    Could not invoke $QUOTACMD on $FS!\n");
240

241 242 243 244 245 246 247 248
    while (<QUOTA>) {
	if (($_ =~ /limit reached on ([-\w\/]*)$/) ||
	    ($_ =~ /grace period on ([-\w\/]*)$/) ||
	    ($_ =~ /Over file quota on ([-\w\/]*)$/) ||
	    ($_ =~ /Over block quota on ([-\w\/]*)$/)) {
	    print STDOUT "*** Disk Quota exceeded on $1\n";
	    $overquota++;
	}
249 250 251 252
    }
}
close(QUOTA);
exit($overquota);