#!/usr/bin/perl -w use strict; use warnings FATAL => 'all'; use Number::Bytes::Human qw(format_bytes); use Term::ANSIColor qw(:constants); use Data::Dumper; my ($fs, $only_user, $nocolor) = @ARGV; unless ($fs) { printf STDERR "usage: %s [filesystem] [user] ['nocolor']\n", $0; exit; } unless ($fs =~ m|^[\w/-]+$|) { printf STDERR "%s: invalid filesystem\n", $fs; exit; } $nocolor = defined $nocolor && $nocolor eq 'nocolor' ? 1 : 0; my %color_list = ( b => { def => GREEN, med => CYAN, high => YELLOW, crit => RED, }, i => { def => GREEN, med => CYAN, high => YELLOW, crit => RED, }, ); my %usage_perc = ( b => { med => 55, high => 95, crit => 100, }, i => { med => 55, high => 95, crit => 100, }, ); sub calc_usage { my ($used, $val, $type) = @_; my %color = %{$color_list{$type}}; my %usage = %{$usage_perc{$type}}; my $over = $val != 0 && $used >= $val ? 1 : 0; my $perc = $val != 0 ? $used / $val * 100 : 0; if ($perc =~ /\./) { $perc = sprintf "%.1f", $perc; $perc =~ s/\.0//; } my $color = $color{def}; if ($perc >= $usage{crit}) { $color = $color{crit}; if ($over) { $color .= BOLD; $perc .= '+'; } } elsif ($perc >= $usage{high}) { $color = $color{high}; } elsif ($perc >= $usage{med}) { $color = $color{med}; } return ($color, $perc) } printf "\n%14s %-5s%8s%16s%16s%13s%17s%17s\n%s\n\n", 'user', 'uid', 'b used', 'b soft', 'b hard', 'i used', 'i soft', 'i hard', '-' x 107; foreach (`repquota -u $fs`) { next unless /^(\S+)\s+\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+\S+$/; my $user = $1; my $bused = $2 * 1024; my $bsoft = $3 * 1024; my $bhard = $4 * 1024; my $iused = $5; my $isoft = $6; my $ihard = $7; next if defined $only_user && length $only_user && $user !~ /$only_user/i; my ($color_bsoft, $perc_bsoft) = calc_usage $bused, $bsoft, 'b'; my ($color_bhard, $perc_bhard) = calc_usage $bused, $bhard, 'b'; my ($color_isoft, $perc_isoft) = calc_usage $iused, $isoft, 'i'; my ($color_ihard, $perc_ihard) = calc_usage $iused, $ihard, 'i'; my $uid; if ($user eq 'root') { $uid = 0; } elsif (!($uid = (getpwnam $user)[2])) { $uid = ''; } printf "%s%14s %-5s%s%8s%9s%s%6s%%%s%9s%s%6s%%%s%13s%10s%s%6s%%%s%10s%s%6s%%%s\n", $nocolor ? '' : MAGENTA, $user, $uid, $nocolor ? '' : CLEAR, format_bytes($bused), format_bytes($bsoft), $nocolor ? '' : $color_bsoft, $perc_bsoft, $nocolor ? '' : CLEAR, format_bytes($bhard), $nocolor ? '' : $color_bhard, $perc_bhard, $nocolor ? '' : CLEAR, $iused, $isoft, $nocolor ? '' : $color_isoft, $perc_isoft, $nocolor ? '' : CLEAR, $ihard, $nocolor ? '' : $color_ihard, $perc_ihard, $nocolor ? '' : CLEAR; } print "\n";