1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | #! /usr/local/bin/perl eval '(exit $?0)' && eval 'exec /usr/local/bin/perl $0 ${1+"$@"}' && eval 'exec /usr/local/bin/perl $0 $argv:q' if 0; # # Copyright (c) 2006 Rajeev K Karamchedu <rajeev<AT>tigr<DOT>org> # All rights reserved. # $Id$ # This program is free software; you may redistribute it and/or modify it # under the same terms as Perl itself. # ============================================================================ #=========================================================# # Perl Modules # #=========================================================# use Net::SNMP v5.1.0 qw(:snmp DEBUG_ALL); use Getopt::Long; use File::Find; use POSIX qw(getuid); #=========================================================# # General Stuff # #=========================================================# use strict; use vars qw($SCRIPT $VERSION %OPTS); #=========================================================# # var et al # #=========================================================# $ENV{"MIBS"} = "NETWORK-APPLIANCE-MIB"; use constant MAX => 100_000; $SCRIPT = 'getquota.pl'; $VERSION = '0.1.0'; my $community = "public"; my $s = ""; my $stime = time; my $uid=0; my $username; my $qtree_quota_oid = ".1.3.6.1.4.1.789.1.4.6.1"; my $qtree_name_oid = ".1.3.6.1.4.1.789.1.4.6.1.12"; my $user_quota_oid = ".1.3.6.1.4.1.789.1.4.6.1"; my $user_name_oid = ".1.3.6.1.4.1.789.1.4.6.1.3"; my $df_oid = ".1.3.6.1.4.1.789.1.5.4.1"; my $df_name_oid = ".1.3.6.1.4.1.789.1.5.4.1.2"; #=========================================================# # # # Process arguments # # Only reason for sticking it in a subroutine is to # # invoke the Pod::Usage module on demand # # # #=========================================================# my %opts = (); _options (); #=========================================================# # Home Area quota report # #=========================================================# if ( exists $opts{home} ) { if ( $opts{home} eq "" ) { # # no username was passed. assume current user # # get the current uid $uid=getuid; # get the username $username = getpwuid($uid); } else { # # username was passed - get the uid # $username = $opts{home}; $uid = getpwnam($username); } # # print out the user quota information # _homequota($username, $uid, $opts{filer}); } #=========================================================# # Qtree Area quota report # #=========================================================# else { # process a qtree quota info request _qtreequota($opts{filer}, $opts{volume}, $opts{qtree}); } exit 0; # [private] ------------------------------------------------------------------ sub _qtreequota { my ($ntap, $vol, $qt, $path ) = @_; my $resource =""; my $name_oid, my $quota_oid; my $used_kb, my $limit_kb, my $files_used; # # we need the filer name and the volume name - at the least # if ( !$ntap ) { _exit ("Cannot determine filer name\n"); } if (!$vol ) { _exit ("Cannot determine volume name\n"); } if ( $qt) { # # Netapp SNMP reports qtrees as /vol/<VOLNAME>/<QTREE> # $resource = "/vol/$vol/$qt"; # # set the base oid for qtree names # $name_oid = $qtree_name_oid; # # set the base oid for quota entries # $quota_oid = $qtree_quota_oid; # # set the OIDs for the requested variables # $used_kb = $quota_oid . ".5"; $limit_kb = $quota_oid . ".8"; $files_used = $quota_oid . ".9"; } else { $resource = "/vol/$vol/"; # # also -- just get the volume details # # set the base oid for volume names # $name_oid = $df_name_oid; # # set the base oid for volume dfentry $quota_oid = $df_oid; # # set the OIDs for the requested variables # $used_kb = $quota_oid . ".4"; $limit_kb = $quota_oid . ".3"; $files_used = $quota_oid . ".7"; } # # create an snmp session # $s = _create_session($ntap); # # for the given resource, get the entry OID # my %oids = _get_entry_oid($name_oid, $resource); # # OID for the passed entry # my @t = split(/$name_oid/, $oids{$resource}); # # OID specific variable entries # $used_kb = $used_kb . $t[1]; $limit_kb = $limit_kb . $t[1]; $files_used = $files_used . $t[1]; # # Fill the array with requested OIDs for snmpget # my @args = ( -varbindlist => [$used_kb, $limit_kb, $files_used] ); # # perform the SNMP GET request for the above variables # if (!defined($s->get_request(@args))) { _exit($s->error()); } printf ("\nQuota for: %s\n\nCurrent Usage: %s\nCurrent Limit: %s\n# of Files Used: %s\n\n", $resource, _byte_size($s->var_bind_list()->{$used_kb}), _byte_size($s->var_bind_list()->{$limit_kb}), $s->var_bind_list()->{$files_used} ); # Close the session $s->close(); } sub _get_mount { # # Use the OS mount command to print the mount information for the resource and parse # the filer and path information. # If the given resource is not mounted, then send a ERROR string back. # # NOTES: In automounted configurations, it is possible to actually mount the resource and then # figure the filer/nfspath stuff but some sites can find this too costly. Issuing a mount command just to # get the quota information may not bode well with all. # my ($mp) = @_; my $os = `uname -s`; chomp $os; my $f = ""; my $p = ""; for ( $os ) { if (/SunOS/) { my $nfsinfo = `/usr/sbin/mount | grep $mp`; # # grab only the info we need, $a and $opts are just throw away vars # my ($mountpt, $a, $nfspath, $opts) = split(/ /, $nfsinfo); ($f, $p) = split(/:/, $nfspath); } elsif (/Linux/) { my $nfsinfo = `/bin/mount | grep $mp`; my ($nfspath) = split(/ /, $nfsinfo); ($f, $p) = split(/:/, $nfspath); } else { $f= "ERROR"; $p= "ERROR"; } } return ($f, $p); } use constant K => 1024; use constant M => K * K; use constant G => M * K; use constant P => G * K; sub _byte_size { # # Load the Math module when necessary # # routine to convert KBs to MB and GB require Math::Round; import Math::Round qw( nearest ); my ($KBytes) = @_; return "${KBytes}KB" if $KBytes < K; return nearest(1, $KBytes / K ) . " MB" if $KBytes < M; return nearest(0.1, $KBytes / M ) . " GB" if $KBytes < G; return nearest(0.01, $KBytes / G ) . " PB" if $KBytes < P; } sub _create_session { my ($filer) = @_; # # Create the SNMP session # # NetApp only support version 1 my $version = 1; my ($sess, $err) = Net::SNMP->session( -hostname => $filer, -version => $version, -community => $community); # Was the session created? if (!defined($sess)) { _exit($err); } return $sess; } sub _exit { printf join('', sprintf("%s: ", $SCRIPT), shift(@_), "\n"), @_; exit 1; } sub _elapsed { # # include this function anywhere in the main to see how long the program has run # One will find that the snmpwalks are the slowest. Unfortunately, till NetApp supports # v2c, we'll have to do that # # Optionally, this routine will also take a message to prefix the timer # my ($msg) = @_; if ( $msg eq "" ) { $msg = "Query ran in" } my $elapsed = time - $stime; print "$msg $elapsed secs\n"; } sub _get_entry_oid { my $baseoid = shift; my $resource = shift; if ( (! $baseoid) || (! $resource ) ) { _exit("ERR"); } my %oidtable; # # set the baseoid to perform a snmpwalk # my @args = ( -varbindlist => [$baseoid] ); if ( $s->version == SNMP_VERSION_1 ) { my $oid; # Walk the MIB from the baseoid while ( defined ($s->get_next_request(@args)) ) { $oid = ($s->var_bind_names())[0]; # # if the baseoid for the retrieved OID is not in the baseoid, then go the last oid and # go the next baseoid # if ( !oid_base_match($baseoid, $oid) ) { last; } my $result = $s->var_bind_list->{$oid}; # # once the resource is matched, fill the array and return # if ( $resource eq $result ) { $oidtable{$result} = $oid; last; } @args = ( -varbindlist => [$oid]); } } else { # # this routine is only here to support v2c, although NetApp does not support it yet, might save # some coding work later on :) -- UNTESTED # push(@args, -maxrepetitions => 25); outer: while (defined($s->get_bulk_request(@args))) { my @oids = oid_lex_sort(keys(%{$s->var_bind_list()})); foreach (@oids) { if (!oid_base_match($baseoid, $_)) { last outer; } if ( $resource eq $s->var_bind_list()->{$_} ) { $oidtable{$s->var_bind_list()->{$_}} = $_; last; } if ($s->var_bind_list()->{$_} eq 'endOfMibView') { last outer; } } # # Get the last OBJECT IDENTIFIER in the returned list # @args = (-maxrepetitions => 25, -varbindlist => [pop(@oids)]); } } return %oidtable; } sub _homequota { my $def_filer = "netmama.tigr.org"; my $nfspath; my ($u_name, $u_uid, $u_filer) = @_; if ($u_uid == 0) { print "\nERROR - Not a regular user (or) Cannot determine userid\n"; exit 1;} if ( (!defined $u_filer) || ( $u_filer eq "" ) ) { # # user did not pass a filer name -- try to find it # ($u_filer, $nfspath ) = _get_mount("/home/$username"); } if ( $u_filer eq "" ) { _exit ("\nCannot determine the serving filer\nPass the filer name as the argument"); } # # create an snmp session $s = _create_session($u_filer); # # get the user entry OID # my %useroids = _get_entry_oid($user_name_oid, $u_uid); my @t = split(/$user_name_oid/, $useroids{$u_uid}); # # Construct the OIDs for the requested information # my $userkb_used = $user_quota_oid . ".5" . $t[1]; my $userkb_limit = $user_quota_oid . ".8" . $t[1]; my @args = ( -varbindlist => [$userkb_used, $userkb_limit] ); # # Perform an snmpget request for the requested oids # if (!defined($s->get_request(@args))) { _exit($s->error()); } printf ("\nHome Area quota for: %s\n\nCurrent Usage: %s\nCurrent Limit: %s\n\n", $u_name, _byte_size($s->var_bind_list()->{$userkb_used}), _byte_size($s->var_bind_list()->{$userkb_limit}) ); # Close the session $s->close(); } sub _options () { my $man = 0; my $help = 0; if ( @ARGV > 0 ) { GetOptions( \%opts, "filer|f=s", "home:s", "volume|v=s", "qtree|q=s", "help", "man") or pod2usage(2); } if ( $opts{man} or $opts{help} ) { # # Load Pod::Usage only if needed; # require Pod::Usage; import Pod::Usage; pod2usage( { -exitval=> 0, -verbose=> 1, -output=> \*STDOUT } ) if $opts{help}; pod2usage( { -exitval=> 0, -verbose=> 2, -output=> \*STDOUT } ) if $opts{man}; } } # ============================================================================ __END__ =head1 NAME getquota.pl - Obtain qtree and user quota information from a NetApp Filer getquota.pl [options] -home [username] print user quota report -filer filer host name -volume volume name where qtree is created -qtree qtree name -help|h brief help message -man full documentation =head1 OPTIONS =over 8 =item B<-help> Print usage info and exit. =item B<-man> Print the manual page and exist. =item B<-home> Query and print the user quota information. If no username is passed, then the quota information for the running user is provided. Optionally, a username and the hosting filer name can be passed to query the usage information of that user. See B<EXAMPLES> below. =item B<-filer> Filer name where the resource is exported from =item B<-volume> Volume name. Requires B<-f>. =item B<-qtree> Qtree name. Requires B<-f> and B<-v>. =back =head1 DESCRIPTION B<This program> will obtain qtree and user quota information from a NetApp Filer =head1 CONTACT Rajeev Karamchedu L<rajeev<AT>tigr<DOT>org> L<http://kreaper.blogsome.com> =head1 EXAMPLES =over 4 =item 1 B<Print user quotas for the running user > #getquota.pl -home Home Area quota for: rajeev Current Usage: 1.3 GB Current Limit: 2 G =item 2 B<Print user quotas for 'jdoe'> #getquota.pl -home jdoe Home Area quota for: jdoe Current Usage: 1.5 GB Current Limit: 2 GB =item 4 B<Print user quotas for user 'janet' on filer 'toaster2'> #getquota.pl -home janet -f toaster2 Home Area quota for: janet Current Usage: 838 MB Current Limit: 1 GB =item 5 B<Print a qtree quota information> #getquota.pl -f toaster -v projects -q TTY Quota for: /vol/projects/TTY Current Usage: 1 GB Current Limit: 1.6 GB # of Files Used: 64105 =item 6 B<Print a volume usage information> #getquota.pl -f toaster -v projects Quota for: /vol/projects/ Current Usage: 589.6 GB Current Limit: 657.2 GB # of Files Used: 5589806 =back =cut |
Recent Comments