Platon Technologies
neprihlásený Prihlásiť Registrácia
SlovakEnglish
open source software development oslavujeme 10 rokov vývoja otvoreného softvéru! Pondelok, 22. júl 2024

Súbor: [Platon] / scripts / perl / filesystem / disk-usage.pl (stiahnutie)

Revízia 1.4, Thu Aug 31 06:48:24 2006 UTC (17 years, 10 months ago) by rajo


Zmeny od 1.3: +6 -4 [lines]

Fix: don't divide by zero.

#!/usr/bin/perl

#
# disk-usage.pl - print summary info about disk usage
#                 by all users
#
# Developed by Lubomir Host 'rajo' <rajo AT platon.sk>
# Copyright (c) 2005 Platon SDG, http://platon.sk/
# Licensed under terms of GNU General Public License.
# All rights reserved.
#
# Changelog:
# 2005-09-22 - created
#
# Usage:
#         perl disk-usage.pl [/dev/hda1] [/dev/md3]
#

# $Platon: scripts/perl/filesystem/disk-usage.pl,v 1.3 2006-03-15 08:34:32 rajo Exp $

use strict;
use Data::Dumper;

$| = 1;

my $user_stat = { };
my $glob_stat = { };

open(PASSWD, "</etc/passwd") or die "Can't open /etc/passwd";
while (my $line = <PASSWD>) {
    next if $line !~ m#/home/#; # ignore system users;
    my ($user, $passwd, $uid, $gid, $info, $homedir, $shell) = split(/:/, $line);
    open(QUOTA, "quota -v $user|") or warn "Can't get quota for user '$user'";
    my    $qline = <QUOTA>; # skip 2 lines
        $qline = <QUOTA>; # skip 2 lines
    while ($qline = <QUOTA>) {
        # TODO: bad info if grace 
        
        chomp $qline;
        # only a one word on the line. It is a long device name (LVM partition or something)
        if (scalar(grep { $_ ne '' } split(/\s+/, $qline)) == 1) {
            # grab 1 line
            $qline .= <QUOTA>;
            chomp $qline;
        }

        my ($filesystem, $blocks, $b_quota, $b_limit, $files, $f_quota) = grep { $_ ne '' } split(/\s+/, $qline);
        $user_stat->{$user}->{$filesystem}->{blocks} = $blocks;
        $user_stat->{$user}->{$filesystem}->{files} = $files;
        $glob_stat->{$filesystem}->{blocks} += $blocks;
        $glob_stat->{$filesystem}->{files} += $files;
    }
    close(QUOTA)
}
close(PASSWD);

#print Dumper($user_stat, $glob_stat);

my @fs_list;
if (scalar(@ARGV) > 0) {
    @fs_list = @ARGV;
}
else {
    @fs_list = sort keys %$glob_stat;
}
print "user\t", join("\t\t", @fs_list), "\n";
print "-" x 60;
foreach my $user (sort keys %$user_stat) {
    print "\n$user\t| ";
    foreach my $filesystem (@fs_list) {
        printf("%04.2f\t",
            ($glob_stat->{$filesystem}->{blocks} == 0
                ? 00.00
                : 100 * $user_stat->{$user}->{$filesystem}->{blocks} / $glob_stat->{$filesystem}->{blocks})
        );
    }
}

print "\n";

# vim: ts=4
# vim600: fdm=marker fdl=0 fdc=3


Platon Group <platon@platon.sk> http://platon.sk/
Copyright © 2002-2006 Platon Group
Stránka používa redakčný systém Metafox
Na začiatok